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

Refreshing access tokens does not work for Google #683

Closed
martinbean opened this issue Jan 31, 2024 · 3 comments · Fixed by #686
Closed

Refreshing access tokens does not work for Google #683

martinbean opened this issue Jan 31, 2024 · 3 comments · Fixed by #686

Comments

@martinbean
Copy link

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.

@driesvints
Copy link
Member

Thanks @martinbean. Would accept a PR for that!

Copy link

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!

@martinbean
Copy link
Author

Thanks @martinbean. Would accept a PR for that!

Cool. Will work on that later today.

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

Successfully merging a pull request may close this issue.

2 participants