From eb308d095c122fd90a9e5cd80a6c1f2edf9eaf0a Mon Sep 17 00:00:00 2001 From: Jeroen Heijmans Date: Sun, 29 Mar 2020 14:46:05 +0200 Subject: [PATCH] Enable Code Flow silent refresh Fixes #34 --- src/app/core/auth-config.ts | 3 +-- src/app/core/auth.service.ts | 6 +++--- src/silent-refresh.html | 14 +++++++++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/app/core/auth-config.ts b/src/app/core/auth-config.ts index 3ae5f11..e83c8e9 100644 --- a/src/app/core/auth-config.ts +++ b/src/app/core/auth-config.ts @@ -4,10 +4,9 @@ export const authConfig: AuthConfig = { issuer: 'https://demo.identityserver.io', clientId: 'spa', // The "Auth Code + PKCE" client responseType: 'code', - useSilentRefresh: true, // Only needed for Code Flow redirectUri: window.location.origin + '/index.html', silentRefreshRedirectUri: window.location.origin + '/silent-refresh.html', - scope: 'openid profile email api', + scope: 'openid profile email api', // Ask offline_access to support refresh token refreshes silentRefreshTimeout: 5000, // For faster testing timeoutFactor: 0.25, // For faster testing sessionChecksEnabled: true, diff --git a/src/app/core/auth.service.ts b/src/app/core/auth.service.ts index fa1b98e..e055dfb 100644 --- a/src/app/core/auth.service.ts +++ b/src/app/core/auth.service.ts @@ -106,7 +106,7 @@ export class AuthService { // 2. SILENT LOGIN: // Try to log in via a refresh because then we can prevent // needing to redirect the user: - return this.startWithRefresh() + return this.tryNoPromptRefresh() .then(() => Promise.resolve()) .catch(result => { // Subset of situations from https://openid.net/specs/openid-connect-core-1_0.html#AuthError @@ -160,7 +160,7 @@ export class AuthService { .catch(() => this.isDoneLoadingSubject$.next(true)); } - private startWithRefresh(): Promise { + private tryNoPromptRefresh(): Promise { if (this.oauthService.getRefreshToken()) { console.log('Found a refresh token, trying to use it.'); return this.oauthService.refreshToken(); @@ -177,7 +177,7 @@ export class AuthService { } public logout() { this.oauthService.logOut(); } - public refresh() { this.oauthService.silentRefresh(); } + public refresh() { this.tryNoPromptRefresh(); } public hasValidToken() { return this.oauthService.hasValidAccessToken(); } // These normally won't be exposed from a service like this, but diff --git a/src/silent-refresh.html b/src/silent-refresh.html index bd90a4d..cf53b0e 100644 --- a/src/silent-refresh.html +++ b/src/silent-refresh.html @@ -2,7 +2,19 @@