Skip to content

Commit 0f03d39

Browse files
committed
fix(code flow): Fixed code flow for IE 11
1 parent 5297585 commit 0f03d39

File tree

9 files changed

+57
-40
lines changed

9 files changed

+57
-40
lines changed

package-lock.json

Lines changed: 16 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"angular-oauth2-oidc-jwks": "^9.0.0",
3535
"base64-js": "^1.3.0",
3636
"bootstrap": "^3.3.7",
37+
"js-sha256": "^0.9.0",
3738
"jsrsasign": "^8.0.12",
3839
"rxjs": "6.5.4",
3940
"rxjs-compat": "^6.5.2",

projects/lib/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
},
77
"version": "9.0.1",
88
"repository": "manfredsteyer/angular-oauth2-oidc",
9+
"dependencies": {
10+
"js-sha256": "^0.9.0"
11+
},
912
"peerDependencies": {
1013
"@angular/common": ">=8.0.0",
1114
"@angular/core": ">=8.0.0"

projects/lib/src/oauth-service.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,8 +1436,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
14361436
public tryLogin(options: LoginOptions = null): Promise<boolean> {
14371437
if (this.config.responseType === 'code') {
14381438
return this.tryLoginCodeFlow(options).then(_ => true);
1439-
}
1440-
else {
1439+
} else {
14411440
return this.tryLoginImplicitFlow(options);
14421441
}
14431442
}
@@ -2243,6 +2242,12 @@ export class OAuthService extends AuthConfig implements OnDestroy {
22432242
if (crypto) {
22442243
let bytes = new Uint8Array(size);
22452244
crypto.getRandomValues(bytes);
2245+
2246+
// Needed for IE
2247+
if (!bytes.map) {
2248+
(bytes as any).map = Array.prototype.map;
2249+
}
2250+
22462251
bytes = bytes.map(x => unreserved.charCodeAt(x % unreserved.length));
22472252
id = String.fromCharCode.apply(null, bytes);
22482253
} else {

projects/lib/src/token-validation/hash-handler.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Injectable } from '@angular/core';
22

3+
import { sha256 } from 'js-sha256';
4+
5+
36
/**
47
* Abstraction for crypto algorithms
58
*/
@@ -11,10 +14,23 @@ export abstract class HashHandler {
1114
export class DefaultHashHandler implements HashHandler {
1215

1316
async calcHash(valueToHash: string, algorithm: string): Promise<string> {
14-
const encoder = new TextEncoder();
15-
const data = encoder.encode(valueToHash);
16-
const hashArray = await window.crypto.subtle.digest(algorithm, data);
17-
return this.toHashString(hashArray);
17+
// const encoder = new TextEncoder();
18+
// const hashArray = await window.crypto.subtle.digest(algorithm, data);
19+
// const data = encoder.encode(valueToHash);
20+
21+
const hashArray = sha256.array(valueToHash);
22+
// const hashString = this.toHashString(hashArray);
23+
const hashString = this.toHashString2(hashArray);
24+
25+
return hashString;
26+
}
27+
28+
toHashString2(byteArray: number[]) {
29+
let result = '';
30+
for (let e of byteArray) {
31+
result += String.fromCharCode(e);
32+
}
33+
return result;
1834
}
1935

2036
toHashString(buffer: ArrayBuffer) {

projects/quickstart-demo/src/app/app.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class AppComponent {
1515
constructor(private oauthService: OAuthService) {
1616
this.oauthService.configure(authCodeFlowConfig);
1717
this.oauthService.loadDiscoveryDocumentAndLogin();
18+
this.oauthService.setupAutomaticSilentRefresh();
1819

1920
// Automatically load user profile
2021
this.oauthService

projects/quickstart-demo/src/app/app.module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { BrowserModule } from '@angular/platform-browser';
22
import { NgModule } from '@angular/core';
33

44
import { AppComponent } from './app.component';
5-
import { OAuthModule } from 'angular-oauth2-oidc';
5+
import { OAuthModule, OAuthStorage } from 'angular-oauth2-oidc';
66
import { HttpClientModule } from '@angular/common/http';
77

88
@NgModule({
@@ -14,7 +14,9 @@ import { HttpClientModule } from '@angular/common/http';
1414
declarations: [
1515
AppComponent
1616
],
17-
providers: [],
17+
providers: [
18+
{ provide: OAuthStorage, useValue: localStorage }
19+
],
1820
bootstrap: [AppComponent]
1921
})
2022
export class AppModule { }

projects/quickstart-demo/src/app/auth.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export const authCodeFlowConfig: AuthConfig = {
77
responseType: 'code',
88
scope: 'openid profile email offline_access api',
99
showDebugInformation: true,
10+
timeoutFactor: 0.01,
1011
};

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { JwksValidationHandler } from 'angular-oauth2-oidc';
1414
})
1515
export class AppComponent {
1616
constructor(private router: Router, private oauthService: OAuthService) {
17-
17+
1818
// Remember the selected configuration
1919
if (sessionStorage.getItem('flow') === 'code') {
2020
this.configureCodeFlow();
@@ -43,15 +43,17 @@ export class AppComponent {
4343

4444

4545
private configureImplicitFlow() {
46+
4647
this.oauthService.configure(authConfig);
4748
// this.oauthService.setStorage(localStorage);
4849
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
49-
this.oauthService.loadDiscoveryDocumentAndTryLogin();
5050

51+
this.oauthService.loadDiscoveryDocumentAndTryLogin();
5152

5253
// Optional
5354
this.oauthService.setupAutomaticSilentRefresh();
5455

56+
5557
// Display all events
5658
this.oauthService.events.subscribe(e => {
5759
// tslint:disable-next-line:no-console

0 commit comments

Comments
 (0)