Skip to content

Commit eb308d0

Browse files
Enable Code Flow silent refresh
Fixes #34
1 parent e977739 commit eb308d0

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/app/core/auth-config.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ export const authConfig: AuthConfig = {
44
issuer: 'https://demo.identityserver.io',
55
clientId: 'spa', // The "Auth Code + PKCE" client
66
responseType: 'code',
7-
useSilentRefresh: true, // Only needed for Code Flow
87
redirectUri: window.location.origin + '/index.html',
98
silentRefreshRedirectUri: window.location.origin + '/silent-refresh.html',
10-
scope: 'openid profile email api',
9+
scope: 'openid profile email api', // Ask offline_access to support refresh token refreshes
1110
silentRefreshTimeout: 5000, // For faster testing
1211
timeoutFactor: 0.25, // For faster testing
1312
sessionChecksEnabled: true,

src/app/core/auth.service.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class AuthService {
106106
// 2. SILENT LOGIN:
107107
// Try to log in via a refresh because then we can prevent
108108
// needing to redirect the user:
109-
return this.startWithRefresh()
109+
return this.tryNoPromptRefresh()
110110
.then(() => Promise.resolve())
111111
.catch(result => {
112112
// Subset of situations from https://openid.net/specs/openid-connect-core-1_0.html#AuthError
@@ -160,7 +160,7 @@ export class AuthService {
160160
.catch(() => this.isDoneLoadingSubject$.next(true));
161161
}
162162

163-
private startWithRefresh(): Promise<TokenResponse | OAuthEvent> {
163+
private tryNoPromptRefresh(): Promise<TokenResponse | OAuthEvent> {
164164
if (this.oauthService.getRefreshToken()) {
165165
console.log('Found a refresh token, trying to use it.');
166166
return this.oauthService.refreshToken();
@@ -177,7 +177,7 @@ export class AuthService {
177177
}
178178

179179
public logout() { this.oauthService.logOut(); }
180-
public refresh() { this.oauthService.silentRefresh(); }
180+
public refresh() { this.tryNoPromptRefresh(); }
181181
public hasValidToken() { return this.oauthService.hasValidAccessToken(); }
182182

183183
// These normally won't be exposed from a service like this, but

src/silent-refresh.html

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@
22
<html>
33
<body>
44
<script>
5-
parent.postMessage(location.hash, location.origin);
5+
console.log("The silent-refresh.html file was loaded and now posting to the parent.");
6+
7+
// For code flow with IdentityServer4 the redirect will contain the new code in
8+
// the location.search. However, the oauth library expects it in the hash fragment
9+
// so we need to "fake" that.
10+
//
11+
// We can't just set `silentRefreshMessagePrefix` on AuthConfig, because the normal
12+
// redirect after interactive login *does* use the hash fragment, so we'd break that.
13+
//
14+
// See also: https://github.com/manfredsteyer/angular-oauth2-oidc/issues/777
15+
const fakeHashFragment = location.search.replace(/^\?/, "#");
16+
17+
parent.postMessage(fakeHashFragment, location.origin);
618
</script>
719
</body>
820
</html>

0 commit comments

Comments
 (0)