Skip to content

Commit b2837cd

Browse files
Merge pull request #634 from gingters/bugfix/codeFlow-hashLocationStrategy
Support hash location strategy with code flow
2 parents 817fab0 + e448314 commit b2837cd

File tree

6 files changed

+49
-24
lines changed

6 files changed

+49
-24
lines changed

projects/lib/src/oauth-service.ts

+18-17
Original file line numberDiff line numberDiff line change
@@ -1404,24 +1404,8 @@ export class OAuthService extends AuthConfig implements OnDestroy {
14041404
}
14051405
}
14061406

1407-
1408-
private parseQueryString(queryString: string): object {
1409-
if (!queryString || queryString.length === 0) {
1410-
return {};
1411-
}
1412-
1413-
if (queryString.charAt(0) === '?') {
1414-
queryString = queryString.substr(1);
1415-
}
1416-
1417-
return this.urlHelper.parseQueryString(queryString);
1418-
1419-
1420-
}
1421-
14221407
public tryLoginCodeFlow(): Promise<void> {
1423-
1424-
const parts = this.parseQueryString(window.location.search)
1408+
const parts = this.getCodePartsFromUrl(window.location.search);
14251409

14261410
const code = parts['code'];
14271411
const state = parts['state'];
@@ -1469,6 +1453,23 @@ export class OAuthService extends AuthConfig implements OnDestroy {
14691453
}
14701454
}
14711455

1456+
/**
1457+
* Retrieve the returned auth code from the redirect uri that has been called.
1458+
* If required also check hash, as we could use hash location strategy.
1459+
*/
1460+
private getCodePartsFromUrl(queryString: string): object {
1461+
if (!queryString || queryString.length === 0) {
1462+
return this.urlHelper.getHashFragmentParams();
1463+
}
1464+
1465+
// normalize query string
1466+
if (queryString.charAt(0) === '?') {
1467+
queryString = queryString.substr(1);
1468+
}
1469+
1470+
return this.urlHelper.parseQueryString(queryString);
1471+
}
1472+
14721473
/**
14731474
* Get token using an intermediate code. Works for the Authorization Code flow.
14741475
*/

projects/sample/src/app/app.routes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export let APP_ROUTES: Routes = [
3434
];
3535

3636
export let AppRouterModule = RouterModule.forRoot(APP_ROUTES, {
37-
preloadingStrategy: CustomPreloadingStrategy
38-
// useHash: true,
37+
preloadingStrategy: CustomPreloadingStrategy,
38+
useHash: localStorage.getItem('useHashLocationStrategy') === 'true',
3939
// initialNavigation: false
4040
});

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ export const authCodeFlowConfig: AuthConfig = {
55
issuer: 'https://demo.identityserver.io',
66

77
// URL of the SPA to redirect the user to after login
8-
redirectUri: window.location.origin + '/index.html',
8+
redirectUri: window.location.origin
9+
+ ((localStorage.getItem('useHashLocationStrategy') === 'true')
10+
? '/#/index.html'
11+
: '/index.html'),
912

1013
// The SPA's id. The SPA is registerd with this id at the auth-server
1114
// clientId: 'server.code',
@@ -20,7 +23,7 @@ export const authCodeFlowConfig: AuthConfig = {
2023
responseType: 'code',
2124

2225
// set the scope for the permissions the client should request
23-
// The first four are defined by OIDC.
26+
// The first four are defined by OIDC.
2427
// Important: Request offline_access to get a refresh token
2528
// The api scope is a usecase specific one
2629
scope: 'openid profile email offline_access api',

projects/sample/src/app/auth.config.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ export const authConfig: AuthConfig = {
77
issuer: 'https://steyer-identity-server.azurewebsites.net/identity',
88

99
// URL of the SPA to redirect the user to after login
10-
redirectUri: window.location.origin + '/index.html',
10+
redirectUri: window.location.origin
11+
+ ((localStorage.getItem('useHashLocationStrategy') === 'true')
12+
? '/#/index.html'
13+
: '/index.html'),
1114

1215
// URL of the SPA to redirect the user after silent refresh
1316
silentRefreshRedirectUri: window.location.origin + '/silent-refresh.html',

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ <h1 *ngIf="givenName">Welcome, {{givenName}} {{familyName}}!</h1>
99
<label><input type="checkbox" [(ngModel)]="requestAccessToken"> Request AccessToken</label>
1010
</div>
1111
</div>
12+
<div class="panel-body">
13+
<p>Test settings</p>
14+
<div class="checkbox">
15+
<label><input type="checkbox" [(ngModel)]="useHashLocationStrategy"> Use hash location strategy</label>
16+
</div>
17+
</div>
1218
</div>
1319

1420
<div class="panel panel-default">
@@ -67,4 +73,4 @@ <h2>Further Actions</h2>
6773

6874
<button class="btn btn-default" (click)="loadUserProfile()">Load User Profile</button>
6975
</div>
70-
</div>
76+
</div>

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class HomeComponent implements OnInit {
3939
this.oauthService.configure(authCodeFlowConfig);
4040
await this.oauthService.loadDiscoveryDocument();
4141
sessionStorage.setItem('flow', 'code');
42-
42+
4343
this.oauthService.initLoginFlow('/some-state;p1=1;p2=2');
4444
// the parameter here is optional. It's passed around and can be used after logging in
4545
}
@@ -90,6 +90,18 @@ export class HomeComponent implements OnInit {
9090
return this.oauthService.requestAccessToken;
9191
}
9292

93+
set useHashLocationStrategy(value: boolean) {
94+
const oldValue = localStorage.getItem('useHashLocationStrategy') === 'true';
95+
if (value !== oldValue) {
96+
localStorage.setItem('useHashLocationStrategy', value ? 'true' : 'false');
97+
window.location.reload();
98+
}
99+
}
100+
101+
get useHashLocationStrategy() {
102+
return localStorage.getItem('useHashLocationStrategy') === 'true';
103+
}
104+
93105
get id_token() {
94106
return this.oauthService.getIdToken();
95107
}

0 commit comments

Comments
 (0)