laravel / socialite

Laravel wrapper around OAuth 1 & OAuth 2 libraries.

Home Page:https://laravel.com/docs/socialite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Refreshing access tokens does not work for Google

martinbean opened this issue · comments

Socialite Version

5.11.0

Laravel Version

10.43.0

PHP Version

8.3.2

Database Driver & Version

No response

Description

Reading Google’s docs it seems Google does not return a new, fresh refresh token when refreshing access tokens, with the user instead being expected to keep the original refresh token in storage and use that in requests to refresh access tokens. So, as long as the access token has not expired, the original refresh token should be used to request new access tokens.

From Google’s docs:

As long as the user has not revoked the access granted to the application, the token server returns a JSON object that contains a new access token.

The code sample in this section does not have a refresh_token member:

{
  "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
  "expires_in": 3920,
  "scope": "https://www.googleapis.com/auth/drive.metadata.readonly",
  "token_type": "Bearer"
}

I’ve tested by changing the AbstractProvider class to instead pass the provided refresh token as a “default”. When I do so, I can continually obtain new access tokens. Basically it’s the following line change within the refreshToken method:

- Arr::get($response, 'refresh_token'),
+ Arr::get($response, 'refresh_token', $refreshToken),

But I think we’ll need to do this for the Google provider only, so as not to inadvertently affect other providers who may not follow this logic.

Steps To Reproduce

Call Socialite::driver('google')->refreshToken($refreshToken). Because the response does not contain refresh_token, constructing the new Token instance fails with the following error:

Laravel\Socialite\Two\Token::__construct(): Argument #2 ($refreshToken) must be of type string, null given, called in vendor/laravel/socialite/src/Two/AbstractProvider.php on line 353.

Thanks @martinbean. Would accept a PR for that!

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

Thanks @martinbean. Would accept a PR for that!

Cool. Will work on that later today.