Should 5 seconds timeout on silent_renew be configurable? => fails fast now if server responds
Hello. I have one issue with 5 seconds silent_renew timeout. I have an angular 6 application with implicit flow, long-living session on Identity Server 4 and short-living access tokens. Silent_renew is enabled. When I come to any of private pages, I want to be instantly logged in to my application if session is up or redirected to my login page otherwise, but I have to wait 5 seconds because my AuthGuard is subscribed to _oidcSecurityService.getIsAuthorized(). I think it would be nice if timeout could be set as parameter. Maybe someone can propose a better solution.
It should be instant. If it's not instant, then your silent_renew isn't configured properly (maybe a CORS issue?) or your auth server is down. The 5 second timeout is a fail safe for when the server fails to respond to the silent renew.
Could you try turning on logging and looking at the console for any errors?
Silent renew works properly if user has already been authenticated in my angular application. But I try to implement SSO. I have one application with AuthorizationCode flow and I login through it and create session in my identity provider. After this I try to visit private page of my angular app.

After timeout it successfully calls authorized callback and everything is fine.

That is logs for silent renew. Every 3 seconds it checks token for expiration. If token is expired, it successfully refreshes token.

There are no errors in network section.

We have same problem in our application as @SolarMaximus . Before redirecting to our unauthorized page, there is 5s timeout(either before login or after logout). We noticed, that this behavior started, when we upgraded angular-auth-oidc-client from version 6.x.x to 8.0.3(in version 6.x.x there is no timeout).
In 8.0.3 there was a change which switched default silent_renew value from true to false. But I have this issue when it is enabled. In 6.0.* version and in 8.0.3.
@SolarMaximus We had it enabled in version 6.0.x too.
@SolarMaximus @damienbod Looking at the provided console output:

It shows that refreshSession() is never being called because the authNonce has been previously set.
@SolarMaximus could you try in an incognito window and see if the timeout is hit? (My guess is that it is not).
Maybe the authNonce needs to be cleared on storage init?
@profet23 Timeout still hits. Tried to clear authNonce from localStorage before calling
openIDImplicitFlowConfiguration.storage = localStorage; this.oidcSecurityService.setupModule(openIDImplicitFlowConfiguration, authWellKnownEndpoints);
but it causes infinite recursion with refreshSession, because authNonce is always null.
timer(5000).pipe(
// backup, if nothing happens after 5 seconds stop waiting and emit
tap(() => this.loggerService.logWarning('IsAuthorizedRace: Timeout reached. Emitting.')),
map(() => true)
)
This piece of code was added there https://github.com/damienbod/AspNet5IdentityServerAngularImplicitFlow/commit/343f310e2fa34b93be63d459623879d6e3fa76be
Can you explain please what consequences I can possibly suffer If I switch to an earlier version?
The timeout hits even when using an incognito window? Hrm... You may have an order of operations issue. What are you doing before subscribing to getIsAuthorized()?
Before this change, getIsAuthorized() would not attempt authorization before returning false. So you would have to wait by some mechanism until the library received an authorization event before checking it.
` @Injectable({ providedIn: 'root' }) export class AuthGuard implements CanActivate {
constructor(private _router: Router, private _authService: AuthService) { }
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable
return this._authService.getIsAuthorized().pipe(
map((isAuthorized: boolean) => {
// console.log('AuthGuardService, canActivate isAuthorized: ' + isAuthorized);
if (isAuthorized) {
return true;
}
localStorage.setItem('backUrlAfterLogin', state.url);
this._router.navigate(['/login']);
return false;
})
);
} }
@Injectable({ providedIn: 'root' }) export class AuthService {
public isAuthorized: boolean; public currentUser: User; public userLoggedIn = new Subject();
constructor(private _oidcSecurityService: OidcSecurityService, private _alertService: AlertService, private _router: Router) { this.isAuthorized = false;
this._oidcSecurityService.getIsAuthorized()
.subscribe(isAuthorized => {
this.isAuthorized = isAuthorized;
if (isAuthorized) {
this.userLoggedIn.next();
this._alertService.success('You are logged in');
this._oidcSecurityService.getUserData().subscribe(userData => {
this.currentUser = new User(
userData.sub,
userData.preferred_username,
userData.given_name,
userData.family_name,
userData.email,
userData.email_verified,
Array.isArray(userData.permission) ? userData.permission : [userData.permission],
userData['employee-identifier']
);
// console.log('authService.currentUser:' + JSON.stringify(this.currentUser));
// console.log('authService.jwtToken:' + JSON.stringify(this.getToken()));
});
}
});
}
public getIsAuthorized() { return this._oidcSecurityService.getIsAuthorized(); } ` P.S. Sorry for bad formatting. Probably some symbols in angular code affect it.
@SolarMaximus
I just want to double check... if you open a brand new incognito session. And attempt this, the timeout is still hit? I'm not sure how that would be possible.
Keep in mind that if you open a brand new incognito... and then try hitting the page a second time... you WILL hit the timeout.
I'm curious about that first page load... is the timeout hit in that first page load?
This is what should be happening on the first load:

As you can see, it takes less then a tenth of a second.
Basically, if you never process a successful login/validation the authNonce is never cleared. If the authNonce is not cleared, the process assumes validation/login is already in process and doesn't attempt another one. Since nothing is kicked off, the time out is eventually hit.
I'm just not sure how you are hitting this consistently. I can get it to happen if I load my app and never login. At that point if I keep going to the app page, it'll keep hitting the timeout. But I'm not sure if that's a bug. Since the user is doing some strange thing and never actually authenticating.
What do you mean by processing a successful login/validation? I see the only place in code where authNonce can be cleared - handleSuccessfulValidation method. But I can't call it manually, because it is private.
` StateValidationService.prototype.handleSuccessfulValidation = function () {
this.oidcSecurityCommon.authNonce = '';
if (this.authConfiguration.auto_clean_state_after_authentication) {
this.oidcSecurityCommon.authStateControl = '';
}
this.loggerService.logDebug('AuthorizedCallback token(s) validated, continue');
}; `

This thing happens when I manually in console kill authNonce after accessing private page by anonymous user. It goes to the refresh_session method where nonce is set and timeout happens again.
Tried to update 6.0.12 to 9.0.0 - it remained the same. Can it be something wrong with this._oidcSecurityService.getIsAuthorized()? We use it to determine whether a user authenticated or not. Maybe we can use another options?
I have the same issue, when I am trying to enter some private page, auth guard return getIsAuthorized() observable and it's waiting 5 seconds before it redirects me to the unauthorized page. Do you have any suggestions?
I can confirm the same behavior as well. My default page is the home page which is a private page. If the auth nonce exist and I don't delete it using console, it goes to the home page and takes 5 seconds to redirect to the public page. It used to be instantaneous. If I set silent renew = false the issue goes away.
@damienbod @profet23 seems like number of folks are hitting this issue. Any ideas?
Hack to get us through for now. This code removes the authNonce set if a user goes to a private page and doesn't authenticate. In my case, it is routing to the public landing page.
if (!window.location.hash && localStorage.getItem("_isAuthorized_xxxx) !== "true") {
localStorage.removeItem("authNonce_xxxx");
}