Store and Use Refresh Token on Expiry

Created on 15 December 2022, over 1 year ago
Updated 19 April 2024, 2 months ago

I'm looking for some guidance as to how we can utilize the refresh token returned by authentication for refreshing the access token when it expires. Our team needs this because we use the access token to access external resources.

πŸ’¬ Support request
Status

Active

Version

3.0

Component

Miscellaneous

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¦πŸ‡ΉAustria guedressel

    We've the same requirement. Our solution is to store the tokens on the user session for later use (potential token refreshing).

    Hooking to the "openid_connect_userinfo_save" did the job for us:

    
    /**
     * Implements hook_openid_connect_userinfo_save().
     */
    function helbling_user_openid_connect_userinfo_save(UserInterface $account, array $context): void {
    	// Store OpenID Connect tokens for later use.
    	$request = \Drupal::request();
    	if( ! $request->hasSession() ) {
    		// This is a strange situation: login via openid connect seem to have completed though no
    		// PHP session is available required to keep Drupal's login state.
    		// Β―\_(ツ)_/Β―
    		return;
    	}
    	$tokens = $context['tokens'];
    	$request->getSession()->set('oidc_tokens', $tokens);
    }
    
    
Production build 0.69.0 2024