yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

GoogleOAuth and refresh_token #7584

Closed erastovpavel closed 9 years ago

erastovpavel commented 9 years ago

I cant find how we can input my params in public function buildAuthUrl in /vendor/yiisoft/yii2-authclient/OAuth2.php line 59.

For example for Google OAuth2 if we wanted to get "request_token" we must write something like: 'access_type' => 'offline' In $params array of function "buildAuthUrl":

public function buildAuthUrl(array $params = [])  {
        $defaultParams = [
            'client_id' => $this->clientId,
            'response_type' => 'code',
            'redirect_uri' => $this->getReturnUrl(),
            'xoauth_displayname' => Yii::$app->name,
        ];
        if (!empty($this->scope)) {
            $defaultParams['scope'] = $this->scope;
        }

        return $this->composeUrl($this->authUrl, array_merge($defaultParams, $params));
}

So I extend GoogleOAuth, added buildAuthParams, which we can wtite in config:

'authClientCollection' => [
            'class' => 'yii\authclient\Collection',
            'clients' => [
                'google' => [
                    'class' => 'app\components\GoogleOAuth',
                    'clientId' => 'my-id',
                    'clientSecret' => 'my-secret',
                    'scope' => 'my-scopes',
                    'returnUrl' => 'my-returnUrl',
                    'buildAuthParams' => [
                        'access_type' => 'offline',
                    ]
                ],
            ],

And extended GoogleOAuth code is:

namespace app\components;
use \Yii;
use yii\authclient\clients\GoogleOAuth as OldGoogleOAuth;

class GoogleOAuth extends OldGoogleOAuth {

    public $buildAuthParams = [];

    public function buildAuthUrl(array $params = [])
    {
        $defaultParams = [
            'client_id' => $this->clientId,
            'response_type' => 'code',
            'redirect_uri' => $this->getReturnUrl(),
            'xoauth_displayname' => Yii::$app->name,
        ];
        if (!empty($this->scope)) {
            $defaultParams['scope'] = $this->scope;
        }

        return $this->composeUrl($this->authUrl, array_merge($defaultParams, $this->buildAuthParams));
    }
}
klimov-paul commented 9 years ago

Such change makes no sense. If you with any custom parameter passed to created auth URL you should invoke buildAuthUrl() manually passing them via $params argument. You may also try to specify extra params setting them directly at authUrl:

'google' => [
    'class' => 'yii\authclient\clients\GoogleOAuth',
    'authUrl' => 'https://accounts.google.com/o/oauth2/auth?access_type=offline',
    ...
],