Skip to content

Commit c2162d0

Browse files
committed
downgrade to jsrsasign
1 parent 1eec310 commit c2162d0

File tree

8 files changed

+70
-32
lines changed

8 files changed

+70
-32
lines changed

package-lock.json

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

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@
3030
"@angular/platform-browser-dynamic": "7.0.0",
3131
"@angular/router": "7.0.0",
3232
"@webcomponents/custom-elements": "^1.1.0",
33+
"base64-js": "^1.3.0",
3334
"bootstrap": "^3.3.7",
3435
"core-js": "^2.5.1",
36+
"jsrsasign": "^8.0.12",
3537
"rxjs": "6.3.3",
3638
"rxjs-compat": "^6.0.0-rc.0",
39+
"text-encoder-lite": "^1.0.1",
3740
"tsickle": "^0.33.0",
3841
"zone.js": "^0.8.26"
3942
},
@@ -47,7 +50,7 @@
4750
"@types/jasmine": "~2.6.3",
4851
"@types/jasminewd2": "~2.0.3",
4952
"@types/node": "~8.0.51",
50-
"angular-oauth2-oidc": "^5.0.0",
53+
"angular-oauth2-oidc": "^5.0.2",
5154
"codelyzer": "~4.2.1",
5255
"cpr": "^3.0.1",
5356
"jasmine-core": "~2.8.0",

projects/lib/ng-package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
"lib": {
66
"languageLevel": ["dom", "es2017"],
77
"entryFile": "src/public_api.ts"
8-
}
8+
},
9+
"whitelistedNonPeerDependencies": [
10+
"jsrsasign"
11+
]
912
}

projects/lib/ng-package.prod.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"dest": "../../dist/lib",
44
"lib": {
55
"entryFile": "src/public_api.ts"
6-
}
6+
},
7+
"whitelistedNonPeerDependencies": [
8+
"jsrsasign"
9+
]
710
}

projects/lib/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
"author": {
55
"name": "Manfred Steyer"
66
},
7-
"version": "5.0.0",
7+
"version": "5.0.2",
88
"repository": "manfredsteyer/angular-oauth2-oidc",
9+
"dependencies": {
10+
"jsrsasign": "^8.0.12"
11+
},
912
"peerDependencies": {
1013
"@angular/common": ">=6.0.0 < 8.0.0",
1114
"@angular/core": ">=6.0.0 < 8.0.0"

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

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import {
33
ValidationParams
44
} from './validation-handler';
55

6+
// declare var require: any;
7+
// let rs = require('jsrsasign');
8+
9+
import * as rs from 'jsrsasign';
10+
611
/**
712
* Validates the signature of an id_token against one
813
* of the keys of an JSON Web Key Set (jwks).
@@ -33,10 +38,7 @@ export class JwksValidationHandler extends AbstractValidationHandler {
3338
*/
3439
gracePeriodInSec = 600;
3540

36-
private cyptoObj: Crypto = window.crypto || (window as any).msCrypto // for IE11
37-
private textEncoder = new (window as any).TextEncoder();
38-
39-
async validateSignature(params: ValidationParams, retry = false): Promise<any> {
41+
validateSignature(params: ValidationParams, retry = false): Promise<any> {
4042
if (!params.idToken) throw new Error('Parameter idToken expected!');
4143
if (!params.idTokenHeader)
4244
throw new Error('Parameter idTokenHandler expected.');
@@ -50,9 +52,11 @@ export class JwksValidationHandler extends AbstractValidationHandler {
5052
throw new Error('Array keys in jwks missing!');
5153
}
5254

55+
// console.debug('validateSignature: retry', retry);
56+
5357
let kid: string = params.idTokenHeader['kid'];
54-
let keys: JsonWebKey[] = params.jwks['keys'];
55-
let key: JsonWebKey;
58+
let keys: object[] = params.jwks['keys'];
59+
let key: object;
5660

5761
let alg = params.idTokenHeader['alg'];
5862

@@ -64,6 +68,12 @@ export class JwksValidationHandler extends AbstractValidationHandler {
6468
k => k['kty'] === kty && k['use'] === 'sig'
6569
);
6670

71+
/*
72+
if (matchingKeys.length == 0) {
73+
let error = 'No matching key found.';
74+
console.error(error);
75+
return Promise.reject(error);
76+
}*/
6777
if (matchingKeys.length > 1) {
6878
let error =
6979
'More than one matching key found. Please specify a kid in the id_token header.';
@@ -99,14 +109,20 @@ export class JwksValidationHandler extends AbstractValidationHandler {
99109
return Promise.reject(error);
100110
}
101111

102-
const [header, body, sig] = params.idToken.split(',');
103-
104-
const cyptokey = await this.cyptoObj.subtle.importKey('jwk', key as any, alg, true, ['verify']);
105-
const isValid = await this.cyptoObj.subtle.verify(alg, cyptokey, this.textEncoder.encode(sig), this.textEncoder.encode(body));
106-
107-
if(isValid) {
112+
let keyObj = rs.KEYUTIL.getKey(key);
113+
let validationOptions = {
114+
alg: this.allowedAlgorithms,
115+
gracePeriod: this.gracePeriodInSec
116+
};
117+
let isValid = rs.KJUR.jws.JWS.verifyJWT(
118+
params.idToken,
119+
keyObj,
120+
validationOptions
121+
);
122+
123+
if (isValid) {
108124
return Promise.resolve();
109-
}else {
125+
} else {
110126
return Promise.reject('Signature not valid');
111127
}
112128
}
@@ -122,11 +138,11 @@ export class JwksValidationHandler extends AbstractValidationHandler {
122138
}
123139
}
124140

125-
async calcHash(valueToHash: string, algorithm: string): Promise<string> {
126-
const valueAsBytes = this.textEncoder.encode(valueToHash);
127-
const resultBytes = await this.cyptoObj.subtle.digest(algorithm, valueAsBytes);
128-
// the returned bytes are encoded as UTF-16
129-
return String.fromCharCode.apply(null, new Uint16Array(resultBytes));
141+
calcHash(valueToHash: string, algorithm: string): Promise<string> {
142+
let hashAlg = new rs.KJUR.crypto.MessageDigest({ alg: algorithm });
143+
let result = hashAlg.digestString(valueToHash);
144+
let byteArrayAsString = this.toByteArrayAsString(result);
145+
return Promise.resolve(byteArrayAsString);
130146
}
131147

132148
toByteArrayAsString(hexString: string) {
@@ -138,4 +154,4 @@ export class JwksValidationHandler extends AbstractValidationHandler {
138154
}
139155
return result;
140156
}
141-
}
157+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ export abstract class AbstractValidationHandler implements ValidationHandler {
7070
*/
7171
protected inferHashAlgorithm(jwtHeader: object): string {
7272
let alg: string = jwtHeader['alg'];
73-
73+
7474
if (!alg.match(/^.S[0-9]{3}$/)) {
7575
throw new Error('Algorithm not supported: ' + alg);
7676
}
7777

78-
return 'sha' + alg.substr(2);
78+
return 'sha-' + alg.substr(2);
7979
}
8080

8181
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { googleAuthConfig } from './auth.google.config';
33
import { authConfig } from './auth.config';
44
import { FlightHistoryComponent } from './flight-history/flight-history.component';
55
import { Component } from '@angular/core';
6-
import { OAuthService, AuthConfig, NullValidationHandler } from 'angular-oauth2-oidc';
6+
import { OAuthService, AuthConfig, NullValidationHandler, JwksValidationHandler } from 'angular-oauth2-oidc';
77
// import { JwksValidationHandler } from 'angular-oauth2-oidc';
88
import { Router } from '@angular/router';
99
import { filter, delay } from 'rxjs/operators';
@@ -32,7 +32,7 @@ export class AppComponent {
3232
private configureWithNewConfigApi() {
3333
this.oauthService.configure(authConfig);
3434
this.oauthService.setStorage(localStorage);
35-
this.oauthService.tokenValidationHandler = new NullValidationHandler();
35+
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
3636
this.oauthService.loadDiscoveryDocumentAndTryLogin();
3737

3838

0 commit comments

Comments
 (0)