Problem/Motivation

We are using this module to connect on a long term basis to Google Drive. This means we need to periodically refresh access tokens. Google OAuth2 apparently only returns a refresh token when you exchange an authorization code for an access token; subsequent access tokens don't have the refresh token (see https://developers.google.com/identity/protocols/oauth2/web-server#offline). This means a second refresh fails because there's no refresh token on the first refreshed access token.

Steps to reproduce

  1. Authorize the Drupal module (via /admin/config/system/oauth2-client) to access your Google Account. You receive an access token with refresh token.
  2. Use the access token after expiration, triggering a refresh. The new access token does not have the refresh token in its data, and the AccessToken stored (in State in this case) has an empty refresh token.
  3. Attempt to obtain a new access token after expiration. Refresh fails.

Proposed resolution

I can think of at least two possible resolutions:

  1. Change Drupal\oauth2_client\Plugin\Oauth2GrantType\RefreshToken::getAccessToken() for all cases to check incoming AccessToken for a refresh token. If it's missing, set the refresh token on the new AccessToken to the existing one before saving.
            // Compensate for the fact that Google doesn't return a refresh token each time.
            if (empty($newAccessToken->getRefreshToken())) {
              $newAccessToken->setRefreshToken($refreshToken);
            }
    
  2. Allow a hook_alter() on Oauth2GrantTypePluginManager so an implementing site can register its own RefreshToken grant plugin to do the above.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#2 3607719-roll-refresh-token-forward.patch666 byteslslalbai
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

lslalbai created an issue. See original summary.

lslalbai’s picture

StatusFileSize
new666 bytes

fathershawn made their first commit to this issue’s fork.

fathershawn’s picture

Status: Active » Needs review

I had not provided the alter hook path before, as it makes it easier to break the core function of this module. So here it is with a caution in the code comments. Let me know if this solves your need!

fathershawn’s picture

Version: 4.1.3 » 4.1.x-dev
fathershawn’s picture

Category: Bug report » Feature request

Changing this to a Feature request since omitting the alterInfo method was intentional.

lslalbai’s picture

I am confirming this works for me with a custom Refresh Token Grant Type plugin. thank you!

fathershawn’s picture

Status: Needs review » Reviewed & tested by the community
fathershawn’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • fathershawn committed 5199550a on 4.1.x
    feat: #3607719 Allow grant types to be customized in client code.
    
    By:...
fathershawn’s picture

Status: Fixed » Closed (fixed)