Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

公众号OAuth授权, 先静默授权,就不能再非静默授权了 #357

Closed
ycqiang opened this issue Jan 12, 2020 · 0 comments
Closed

Comments

@ycqiang
Copy link

ycqiang commented Jan 12, 2020

<?php
/**
 * Class OAuthAuthenticate: 微信公众号, 企业微信的网页应用。
 */
class OAuthAuthenticate
{
    public function handle($request, Closure $next, $account = 'default', $scope = null, $type = 'service')
    {
        $isNewSession = false;
        //保证兼容性
        $class = ('work' !== $type) ? 'wechat' : 'work';
        $prefix = ('work' !== $type) ? 'official_account' : 'work';
        $sessionKey = \sprintf($class.'.oauth_user.%s', $account);
        $config = config(\sprintf('wechat.'.$prefix.'.%s', $account), []);
        $officialAccount = app(\sprintf('wechat.'.$prefix.'.%s', $account));
        $scope = $scope ?: Arr::get($config, 'oauth.scopes', ['snsapi_base']);

        if (is_string($scope)) {
            $scope = array_map('trim', explode(',', $scope));
        }

        $session = session($sessionKey, []);

        if (!$session) {
            if ($request->has('code')) {
                session([$sessionKey => $officialAccount->oauth->user() ?? []]);
                $isNewSession = true;

                event(new WeChatUserAuthorized(session($sessionKey), $isNewSession, $account));

                return redirect()->to($this->getTargetUrl($request));
            }

            session()->forget($sessionKey);

            return $officialAccount->oauth->scopes($scope)->redirect($request->fullUrl());
        }

        event(new WeChatUserAuthorized(session($sessionKey), $isNewSession, $account));

        return $next($request);
    }
}

原因是 session 没有过期且 sessionKey 不变就不会再次授权;account 一样的情况下,scope 从 'snsapi_base' 换成 'snsapi_userinfo' ,sessionKey 不变,则不会再次授权;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants