manfredsteyer / angular-oauth2-oidc

Support for OAuth 2 and OpenId Connect (OIDC) in Angular.
MIT License
1.9k stars 687 forks source link

Retry Token refreshes #1038

Open teebee78 opened 3 years ago

teebee78 commented 3 years ago

We're using version 10.0.3 of your library with the code flow. We established the automatic token refresh. Our tokens have a lifespan of 30 minutes, the refresh happens after 20.

Now we're experiencing that the call to our token provider application fails due to very short outages. Since the token refresh request happens only once, the token is not renewed and expires. We would like to make our application more robust and be able to handle such short outages of the token provider.

Thanks in advance!

jeroenheijmans commented 3 years ago

There's no such feature AFAIK currently in the library. Hope you don't mind, I'll slightly tweak your title and OP to make it into a feature request.

A decent workaround would probably be to write a HttpInterceptor that does this, then it'll be transparent for the library. The code might even be a useful/potential way to do it in this library.

teebee78 commented 3 years ago

Thank, yes that's perfect.

We found another workaround that seems to work pretty well:

/**

   * When the silent automatic refresh of the token fails once, no more attempts are issued.
   * We wait one minute, and then trigger the token refresh again, which in turn also restarts the silent automatic token refresh.
   * If the triggered retry fails, anothre event of type token_refresh_error gets triggered
   */
  private installRetryOnTokenRefreshErrorHandler(): void {
    this._oauthService.events
      .pipe(
        filter(event => event.type === 'token_refresh_error'),
        tap(event => this._logger.info('Token refresh failed, a retry will be triggered in 1 minute', event)),
        delay(6000),
        takeUntil(this._destroy$),
      )
      .subscribe(() => this._oauthService.refreshToken().catch(() => noop()));
  }