Skip to content

Commit 43ecad5

Browse files
author
Fabian Wiles
committed
fixup! feat: remove jsrsasign dependancy
1 parent 290c801 commit 43ecad5

File tree

7 files changed

+68
-158
lines changed

7 files changed

+68
-158
lines changed

package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@
2929
"@angular/platform-browser": "6.0.0",
3030
"@angular/platform-browser-dynamic": "6.0.0",
3131
"@angular/router": "6.0.0",
32-
"@types/jsonwebtoken": "^7.2.7",
3332
"@webcomponents/custom-elements": "^1.1.0",
3433
"angular-oauth2-oidc": "^2.1.8",
3534
"bootstrap": "^3.3.7",
3635
"core-js": "^2.5.1",
37-
"jsonwebtoken": "^8.3.0",
38-
"jwk-to-pem": "^2.0.0",
3936
"rxjs": "6.1.0",
4037
"rxjs-compat": "^6.0.0-rc.0",
4138
"zone.js": "^0.8.26"
@@ -64,7 +61,7 @@
6461
"protractor": "~5.2.0",
6562
"ts-node": "~3.3.0",
6663
"tslint": "~5.8.0",
67-
"typescript": "^2.7.2",
64+
"typescript": "2.7.2",
6865
"webpack": "^4.4.1"
6966
}
7067
}

projects/lib/ng-package.json

+2-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
"dest": "../../dist/lib",
44
"deleteDestPath": false,
55
"lib": {
6+
"languageLevel": ["dom", "es2017"],
67
"entryFile": "src/public_api.ts"
7-
},
8-
"whitelistedNonPeerDependencies": [
9-
"jsonwebtoken",
10-
"jwk-to-pem",
11-
"@types/jsonwebtoken"
12-
]
13-
8+
}
149
}

projects/lib/ng-package.prod.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,5 @@
33
"dest": "../../dist/lib",
44
"lib": {
55
"entryFile": "src/public_api.ts"
6-
},
7-
"whitelistedNonPeerDependencies": [
8-
"jsonwebtoken",
9-
"jwk-to-pem",
10-
"@types/jsonwebtoken"
11-
]
6+
}
127
}

projects/lib/package.json

-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
},
77
"version": "4.0.2",
88
"repository": "manfredsteyer/angular-oauth2-oidc",
9-
"dependencies": {
10-
"jsonwebtoken": "^8.3.0",
11-
"jwk-to-pem": "^2.0.0",
12-
"@types/jsonwebtoken": "^7.2.7"
13-
},
149
"peerDependencies": {
1510
"@angular/common": "^6.0.0",
1611
"@angular/core": "^6.0.0"

projects/lib/src/oauth-service.ts

+28-25
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,14 @@ export class OAuthService extends AuthConfig {
331331

332332
/**
333333
* DEPRECATED. Use a provider for OAuthStorage instead:
334-
*
334+
*
335335
* { provide: OAuthStorage, useValue: localStorage }
336-
*
336+
*
337337
* Sets a custom storage used to store the received
338338
* tokens on client side. By default, the browser's
339339
* sessionStorage is used.
340340
* @ignore
341-
*
341+
*
342342
* @param storage
343343
*/
344344
public setStorage(storage: OAuthStorage): void {
@@ -845,7 +845,7 @@ export class OAuthService extends AuthConfig {
845845
const redirectUri = this.silentRefreshRedirectUri || this.redirectUri;
846846
this.createLoginUrl(null, null, redirectUri, noPrompt, params).then(url => {
847847
iframe.setAttribute('src', url);
848-
848+
849849
if (!this.silentRefreshShowIFrame) {
850850
iframe.style['display'] = 'none';
851851
}
@@ -1529,26 +1529,29 @@ export class OAuthService extends AuthConfig {
15291529
loadKeys: () => this.loadJwks()
15301530
};
15311531

1532-
if (
1533-
!this.disableAtHashCheck &&
1534-
this.requestAccessToken &&
1535-
!this.checkAtHash(validationParams)
1536-
) {
1537-
const err = 'Wrong at_hash';
1538-
console.warn(err);
1539-
return Promise.reject(err);
1540-
}
1541-
1542-
return this.checkSignature(validationParams).then(_ => {
1543-
const result: ParsedIdToken = {
1544-
idToken: idToken,
1545-
idTokenClaims: claims,
1546-
idTokenClaimsJson: claimsJson,
1547-
idTokenHeader: header,
1548-
idTokenHeaderJson: headerJson,
1549-
idTokenExpiresAt: expiresAtMSec
1550-
};
1551-
return result;
1532+
return this.checkAtHash(validationParams)
1533+
.then(atHashValid => {
1534+
if (
1535+
!this.disableAtHashCheck &&
1536+
this.requestAccessToken &&
1537+
!atHashValid
1538+
) {
1539+
const err = 'Wrong at_hash';
1540+
console.warn(err);
1541+
return Promise.reject(err);
1542+
}
1543+
1544+
return this.checkSignature(validationParams).then(_ => {
1545+
const result: ParsedIdToken = {
1546+
idToken: idToken,
1547+
idTokenClaims: claims,
1548+
idTokenClaimsJson: claimsJson,
1549+
idTokenHeader: header,
1550+
idTokenHeaderJson: headerJson,
1551+
idTokenExpiresAt: expiresAtMSec
1552+
};
1553+
return result;
1554+
});
15521555
});
15531556
}
15541557

@@ -1774,7 +1777,7 @@ export class OAuthService extends AuthConfig {
17741777
});
17751778
}
17761779

1777-
private checkAtHash(params: ValidationParams): boolean {
1780+
private async checkAtHash(params: ValidationParams): Promise<boolean> {
17781781
if (!this.tokenValidationHandler) {
17791782
console.warn(
17801783
'No tokenValidationHandler configured. Cannot check at_hash.'

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

+28-18
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import {
22
AbstractValidationHandler,
33
ValidationParams
44
} from './validation-handler';
5-
import * as jwkToPem from 'jwk-to-pem';
6-
import * as jwt from 'jsonwebtoken';
7-
85

96
/**
107
* Validates the signature of an id_token against one
@@ -36,7 +33,10 @@ export class JwksValidationHandler extends AbstractValidationHandler {
3633
*/
3734
gracePeriodInSec = 600;
3835

39-
validateSignature(params: ValidationParams, retry = false): Promise<any> {
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> {
4040
if (!params.idToken) throw new Error('Parameter idToken expected!');
4141
if (!params.idTokenHeader)
4242
throw new Error('Parameter idTokenHandler expected.');
@@ -53,8 +53,8 @@ export class JwksValidationHandler extends AbstractValidationHandler {
5353
// console.debug('validateSignature: retry', retry);
5454

5555
let kid: string = params.idTokenHeader['kid'];
56-
let keys: object[] = params.jwks['keys'];
57-
let key: object;
56+
let keys: JsonWebKey[] = params.jwks['keys'];
57+
let key: JsonWebKey;
5858

5959
let alg = params.idTokenHeader['alg'];
6060

@@ -107,17 +107,28 @@ export class JwksValidationHandler extends AbstractValidationHandler {
107107
return Promise.reject(error);
108108
}
109109

110-
const pem = jwkToPem(key);
111-
try {
112-
jwt.verify(
113-
params.idToken,
114-
pem,
115-
{algorithms: this.allowedAlgorithms, clockTolerance: this.gracePeriodInSec}
116-
);
117-
} catch (err) {
110+
const [header, body, sig] = params.idToken.split(',');
111+
112+
const cyptokey = await this.cyptoObj.subtle.importKey('jwk', key as any, alg, true, ['verify']);
113+
const isValid = await this.cyptoObj.subtle.verify(alg, cyptokey, this.textEncoder.encode(sig), this.textEncoder.encode(body));
114+
115+
if(isValid) {
116+
return Promise.resolve();
117+
}else {
118118
return Promise.reject('Signature not valid');
119119
}
120-
return Promise.resolve();
120+
121+
// const pem = jwkToPemAsAny(key);
122+
// try {
123+
// jwt.verify(
124+
// params.idToken,
125+
// pem,
126+
// {algorithms: this.allowedAlgorithms, clockTolerance: this.gracePeriodInSec}
127+
// );
128+
// } catch (err) {
129+
// return Promise.reject('Signature not valid');
130+
// }
131+
// return Promise.resolve();
121132
}
122133

123134
private alg2kty(alg: string) {
@@ -132,9 +143,8 @@ export class JwksValidationHandler extends AbstractValidationHandler {
132143
}
133144

134145
async calcHash(valueToHash: string, algorithm: string): Promise<string> {
135-
const encoder = new TextEncoder();
136-
const valueAsBytes = encoder.encode(valueToHash);
137-
const resultBytes = await window.crypto.subtle.digest(algorithm, valueAsBytes);
146+
const valueAsBytes = this.textEncoder.encode(valueToHash);
147+
const resultBytes = await this.cyptoObj.subtle.digest(algorithm, valueAsBytes);
138148
// the returned bytes are encoded as UTF-16
139149
return String.fromCharCode.apply(null, new Uint16Array(resultBytes));
140150
}

0 commit comments

Comments
 (0)