Skip to content

Commit 93902a5

Browse files
committed
feat(code-flow): allow using implicit flow by setting useSilentRefresh to true
1 parent d54deac commit 93902a5

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

projects/lib/src/auth.config.ts

+10
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ export class AuthConfig {
231231
*/
232232
public waitForTokenInMsec? = 0;
233233

234+
/**
235+
* Set this to true if you want to use silent refresh together with
236+
* code flow. As silent refresh is the only option for refreshing
237+
* with implicit flow, you don't need to explicitly turn it on in
238+
* this case.
239+
*/
240+
public useSilentRefresh?;
241+
234242
/**
235243
* Code Flow is by defauld used together with PKCI which is also higly recommented.
236244
* You can disbale it here by setting this flag to true.
@@ -252,4 +260,6 @@ export class AuthConfig {
252260
public openUri?: ((uri: string) => void) = uri => {
253261
location.href = uri;
254262
}
263+
264+
255265
}

projects/lib/src/oauth-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
200200

201201
protected refreshInternal(params, noPrompt): Promise<TokenResponse | OAuthEvent> {
202202

203-
if (!this.silentRefreshRedirectUri && this.responseType === 'code') {
203+
if (!this.useSilentRefresh && this.responseType === 'code') {
204204
return this.refreshToken();
205205
} else {
206206
return this.silentRefresh(params, noPrompt);

projects/sample/src/app/auth-code-flow.config.ts

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { AuthConfig } from 'angular-oauth2-oidc';
22

3+
// Set this to true, to use silent refresh; otherwise the example
4+
// uses the refresh_token via an AJAX coll to get new tokens.
5+
const useSilentRefresh = false;
6+
37
export const authCodeFlowConfig: AuthConfig = {
48
issuer: 'https://idsvr4.azurewebsites.net',
59

@@ -9,8 +13,6 @@ export const authCodeFlowConfig: AuthConfig = {
913
? '/#/index.html'
1014
: '/index.html'),
1115

12-
silentRefreshRedirectUri: `${window.location.origin}/silent-refresh.html`,
13-
1416
// The SPA's id. The SPA is registerd with this id at the auth-server
1517
// clientId: 'server.code',
1618
clientId: 'spa',
@@ -27,16 +29,25 @@ export const authCodeFlowConfig: AuthConfig = {
2729
// The first four are defined by OIDC.
2830
// Important: Request offline_access to get a refresh token
2931
// The api scope is a usecase specific one
30-
scope: 'openid profile email offline_access api',
32+
scope: (useSilentRefresh) ?
33+
'openid profile email api' :
34+
'openid profile email offline_access api',
3135

32-
showDebugInformation: true,
36+
// ^^ Please note that offline_access is not needed for silent refresh
37+
// At least when using idsvr, this even prevents silent refresh
38+
// as idsvr ALWAYS prompts the user for consent when this scope is
39+
// requested
3340

34-
// If you specify this property, the lib tries to refresh the
35-
// token via a silet refresh; otherwise it sends over a refresh_token
36-
// via an AJAX call to get new tokens.
37-
silentRefreshRedirectUri: window.location.origin + '/silent-refresh.html',
41+
// This is needed for silent refresh (refreshing tokens w/o a refresh_token)
42+
// **AND** for logging in with a popup
43+
silentRefreshRedirectUri:
44+
`${window.location.origin}/silent-refresh.html`,
3845

46+
useSilentRefresh: useSilentRefresh,
3947

40-
timeoutFactor: 0.01
48+
showDebugInformation: true,
49+
50+
timeoutFactor: 0.01,
4151
// disablePKCI: true,
52+
4253
};

projects/sample/src/app/home/home.component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { authCodeFlowConfig } from '../auth-code-flow.config';
99
export class HomeComponent implements OnInit {
1010
loginFailed: boolean = false;
1111
userProfile: object;
12+
usePopup: boolean;
1213

1314
constructor(private oauthService: OAuthService) {
1415
}
@@ -92,7 +93,7 @@ export class HomeComponent implements OnInit {
9293

9394
this.oauthService.oidc = true;
9495

95-
if (!this.oauthService.silentRefreshRedirectUri && this.oauthService.responseType === 'code') {
96+
if (!this.oauthService.useSilentRefresh && this.oauthService.responseType === 'code') {
9697
this.oauthService
9798
.refreshToken()
9899
.then(info => console.debug('refresh ok', info))

0 commit comments

Comments
 (0)