SpoonX / aurelia-authentication

Authentication plugin for aurelia.
http://aurelia-authentication.spoonx.org
MIT License
90 stars 60 forks source link

Automatic logout if accesstoken expires in more than 24.85 days #375

Closed sondr closed 6 years ago

sondr commented 6 years ago

setTimout gets called right away if timeout exceeds int32 max value(2147483647).

In authService.js:

setTimeout(ttl: number) {
    this.clearTimeout();
    const expiredTokenHandler = () => {
      if (this.config.autoUpdateToken
        && this.authentication.getAccessToken()
        && this.authentication.getRefreshToken()) {
        this.updateToken().catch(error => logger.warn(error.message));

        return;
      }
      this.setResponseObject(null);

      if (this.config.expiredRedirect) {
        PLATFORM.location.assign(this.config.expiredRedirect);
      }
    };

    this.timeoutID = PLATFORM.global.setTimeout(expiredTokenHandler, ttl);
    PLATFORM.addEventListener('focus', () => {
      if (this.isTokenExpired()) {
        expiredTokenHandler();
      }
    });
  }

Change suggestion: this.timeoutID = PLATFORM.global.setTimeout(expiredTokenHandler, ttl); To this.timeoutID = PLATFORM.global.setTimeout(expiredTokenHandler, Math.min(ttl, Math.pow(2, 31) - 1));

Or throw an error if access token lifetime is too long.

RWOverdijk commented 6 years ago

I agree. Something like that could be a good idea. Let the handler take a look at the time remaining and then pick it up again.

Regardless, such a long timeout is a strange thing to do combined with refresh tokens and has no priority for me personally. But a PR would be more than welcome!