Skip to content

Commit 748fdb8

Browse files
committed
bundle sizes
1 parent 96b563b commit 748fdb8

File tree

22 files changed

+375
-247
lines changed

22 files changed

+375
-247
lines changed

angular.json

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,7 @@
130130
}
131131
}
132132
},
133-
"sample-e2e": {
134-
"root": "projects/sample-e2e/",
135-
"projectType": "application",
136-
"architect": {
137-
"e2e": {
138-
"builder": "@angular-devkit/build-angular:protractor",
139-
"options": {
140-
"protractorConfig": "projects/sample-e2e/protractor.conf.js",
141-
"devServerTarget": "sample:serve"
142-
}
143-
},
144-
"lint": {
145-
"builder": "@angular-devkit/build-angular:tslint",
146-
"options": {
147-
"tsConfig": "projects/sample-e2e/tsconfig.e2e.json",
148-
"exclude": [
149-
"**/node_modules/**"
150-
]
151-
}
152-
}
153-
}
154-
},
133+
155134
"quickstart-demo": {
156135
"projectType": "application",
157136
"schematics": {},
@@ -264,6 +243,42 @@
264243
}
265244
}
266245
}
246+
},
247+
248+
"angular-oauth2-oidc-jwks": {
249+
"projectType": "library",
250+
"root": "projects/angular-oauth2-oidc-jwks",
251+
"sourceRoot": "projects/angular-oauth2-oidc-jwks/src",
252+
"prefix": "lib",
253+
"architect": {
254+
"build": {
255+
"builder": "@angular-devkit/build-ng-packagr:build",
256+
"options": {
257+
"tsConfig": "projects/angular-oauth2-oidc-jwks/tsconfig.lib.json",
258+
"project": "projects/angular-oauth2-oidc-jwks/ng-package.json"
259+
}
260+
},
261+
"test": {
262+
"builder": "@angular-devkit/build-angular:karma",
263+
"options": {
264+
"main": "projects/angular-oauth2-oidc-jwks/src/test.ts",
265+
"tsConfig": "projects/angular-oauth2-oidc-jwks/tsconfig.spec.json",
266+
"karmaConfig": "projects/angular-oauth2-oidc-jwks/karma.conf.js"
267+
}
268+
},
269+
"lint": {
270+
"builder": "@angular-devkit/build-angular:tslint",
271+
"options": {
272+
"tsConfig": [
273+
"projects/angular-oauth2-oidc-jwks/tsconfig.lib.json",
274+
"projects/angular-oauth2-oidc-jwks/tsconfig.spec.json"
275+
],
276+
"exclude": [
277+
"**/node_modules/**"
278+
]
279+
}
280+
}
281+
}
267282
}
268283
},
269284
"schematics": {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"rxjs-compat": "^6.5.2",
3737
"text-encoder-lite": "^1.0.1",
3838
"tsickle": "^0.35.0",
39+
"tslib": "^1.9.0",
3940
"zone.js": "^0.9.1"
4041
},
4142
"devDependencies": {
@@ -63,6 +64,7 @@
6364
"prettier": "1.18.2",
6465
"protractor": "~5.4.2",
6566
"ts-node": "~8.3.0",
67+
"tsickle": "^0.35.0",
6668
"tslib": "^1.9.0",
6769
"tslint": "~5.18.0",
6870
"typescript": "3.4.5"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# AngularOauth2OidcJwks
2+
3+
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 8.0.1.
4+
5+
## Code scaffolding
6+
7+
Run `ng generate component component-name --project angular-oauth2-oidc-jwks` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project angular-oauth2-oidc-jwks`.
8+
> Note: Don't forget to add `--project angular-oauth2-oidc-jwks` or else it will be added to the default project in your `angular.json` file.
9+
10+
## Build
11+
12+
Run `ng build angular-oauth2-oidc-jwks` to build the project. The build artifacts will be stored in the `dist/` directory.
13+
14+
## Publishing
15+
16+
After building your library with `ng build angular-oauth2-oidc-jwks`, go to the dist folder `cd dist/angular-oauth2-oidc-jwks` and run `npm publish`.
17+
18+
## Running unit tests
19+
20+
Run `ng test angular-oauth2-oidc-jwks` to execute the unit tests via [Karma](https://karma-runner.github.io).
21+
22+
## Further help
23+
24+
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Karma configuration file, see link for more information
2+
// https://karma-runner.github.io/1.0/config/configuration-file.html
3+
4+
module.exports = function (config) {
5+
config.set({
6+
basePath: '',
7+
frameworks: ['jasmine', '@angular-devkit/build-angular'],
8+
plugins: [
9+
require('karma-jasmine'),
10+
require('karma-chrome-launcher'),
11+
require('karma-jasmine-html-reporter'),
12+
require('karma-coverage-istanbul-reporter'),
13+
require('@angular-devkit/build-angular/plugins/karma')
14+
],
15+
client: {
16+
clearContext: false // leave Jasmine Spec Runner output visible in browser
17+
},
18+
coverageIstanbulReporter: {
19+
dir: require('path').join(__dirname, '../../coverage/angular-oauth2-oidc-jwks'),
20+
reports: ['html', 'lcovonly'],
21+
fixWebpackSourcePaths: true
22+
},
23+
reporters: ['progress', 'kjhtml'],
24+
port: 9876,
25+
colors: true,
26+
logLevel: config.LOG_INFO,
27+
autoWatch: true,
28+
browsers: ['Chrome'],
29+
singleRun: false,
30+
restartOnFileChange: true
31+
});
32+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
3+
"dest": "../../dist/angular-oauth2-oidc-jwks",
4+
"lib": {
5+
"entryFile": "src/public-api.ts"
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "angular-oauth2-oidc-jwks",
3+
"version": "9.0.0",
4+
"dependencies": {
5+
"jsrsasign": "^8.0.12"
6+
}
7+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import * as rs from 'jsrsasign';
2+
import { AbstractValidationHandler, ValidationParams } from 'angular-oauth2-oidc';
3+
4+
/**
5+
* Validates the signature of an id_token against one
6+
* of the keys of an JSON Web Key Set (jwks).
7+
*
8+
* This jwks can be provided by the discovery document.
9+
*/
10+
export class JwksValidationHandler extends AbstractValidationHandler {
11+
/**
12+
* Allowed algorithms
13+
*/
14+
allowedAlgorithms: string[] = [
15+
'HS256',
16+
'HS384',
17+
'HS512',
18+
'RS256',
19+
'RS384',
20+
'RS512',
21+
'ES256',
22+
'ES384',
23+
'PS256',
24+
'PS384',
25+
'PS512'
26+
];
27+
28+
/**
29+
* Time period in seconds the timestamp in the signature can
30+
* differ from the current time.
31+
*/
32+
gracePeriodInSec = 600;
33+
34+
validateSignature(params: ValidationParams, retry = false): Promise<any> {
35+
if (!params.idToken) throw new Error('Parameter idToken expected!');
36+
if (!params.idTokenHeader)
37+
throw new Error('Parameter idTokenHandler expected.');
38+
if (!params.jwks) throw new Error('Parameter jwks expected!');
39+
40+
if (
41+
!params.jwks['keys'] ||
42+
!Array.isArray(params.jwks['keys']) ||
43+
params.jwks['keys'].length === 0
44+
) {
45+
throw new Error('Array keys in jwks missing!');
46+
}
47+
48+
// console.debug('validateSignature: retry', retry);
49+
50+
let kid: string = params.idTokenHeader['kid'];
51+
let keys: object[] = params.jwks['keys'];
52+
let key: object;
53+
54+
let alg = params.idTokenHeader['alg'];
55+
56+
if (kid) {
57+
key = keys.find(k => k['kid'] === kid /* && k['use'] === 'sig' */);
58+
} else {
59+
let kty = this.alg2kty(alg);
60+
let matchingKeys = keys.filter(
61+
k => k['kty'] === kty && k['use'] === 'sig'
62+
);
63+
64+
/*
65+
if (matchingKeys.length == 0) {
66+
let error = 'No matching key found.';
67+
console.error(error);
68+
return Promise.reject(error);
69+
}*/
70+
if (matchingKeys.length > 1) {
71+
let error =
72+
'More than one matching key found. Please specify a kid in the id_token header.';
73+
console.error(error);
74+
return Promise.reject(error);
75+
} else if (matchingKeys.length === 1) {
76+
key = matchingKeys[0];
77+
}
78+
}
79+
80+
if (!key && !retry && params.loadKeys) {
81+
return params
82+
.loadKeys()
83+
.then(loadedKeys => (params.jwks = loadedKeys))
84+
.then(_ => this.validateSignature(params, true));
85+
}
86+
87+
if (!key && retry && !kid) {
88+
let error = 'No matching key found.';
89+
console.error(error);
90+
return Promise.reject(error);
91+
}
92+
93+
if (!key && retry && kid) {
94+
let error =
95+
'expected key not found in property jwks. ' +
96+
'This property is most likely loaded with the ' +
97+
'discovery document. ' +
98+
'Expected key id (kid): ' +
99+
kid;
100+
101+
console.error(error);
102+
return Promise.reject(error);
103+
}
104+
105+
let keyObj = rs.KEYUTIL.getKey(key);
106+
let validationOptions = {
107+
alg: this.allowedAlgorithms,
108+
gracePeriod: this.gracePeriodInSec
109+
};
110+
let isValid = rs.KJUR.jws.JWS.verifyJWT(
111+
params.idToken,
112+
keyObj,
113+
validationOptions
114+
);
115+
116+
if (isValid) {
117+
return Promise.resolve();
118+
} else {
119+
return Promise.reject('Signature not valid');
120+
}
121+
}
122+
123+
private alg2kty(alg: string) {
124+
switch (alg.charAt(0)) {
125+
case 'R':
126+
return 'RSA';
127+
case 'E':
128+
return 'EC';
129+
default:
130+
throw new Error('Cannot infer kty from alg: ' + alg);
131+
}
132+
}
133+
134+
calcHash(valueToHash: string, algorithm: string): Promise<string> {
135+
let hashAlg = new rs.KJUR.crypto.MessageDigest({ alg: algorithm });
136+
let result = hashAlg.digestString(valueToHash);
137+
let byteArrayAsString = this.toByteArrayAsString(result);
138+
return Promise.resolve(byteArrayAsString);
139+
}
140+
141+
toByteArrayAsString(hexString: string) {
142+
let result = '';
143+
for (let i = 0; i < hexString.length; i += 2) {
144+
let hexDigit = hexString.charAt(i) + hexString.charAt(i + 1);
145+
let num = parseInt(hexDigit, 16);
146+
result += String.fromCharCode(num);
147+
}
148+
return result;
149+
}
150+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './lib/jwks-validation-handler';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
2+
3+
import 'zone.js/dist/zone';
4+
import 'zone.js/dist/zone-testing';
5+
import { getTestBed } from '@angular/core/testing';
6+
import {
7+
BrowserDynamicTestingModule,
8+
platformBrowserDynamicTesting
9+
} from '@angular/platform-browser-dynamic/testing';
10+
11+
declare const require: any;
12+
13+
// First, initialize the Angular testing environment.
14+
getTestBed().initTestEnvironment(
15+
BrowserDynamicTestingModule,
16+
platformBrowserDynamicTesting()
17+
);
18+
// Then we find all the tests.
19+
const context = require.context('./', true, /\.spec\.ts$/);
20+
// And load the modules.
21+
context.keys().map(context);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "../../out-tsc/lib",
5+
"target": "es2015",
6+
"declaration": true,
7+
"inlineSources": true,
8+
"types": [],
9+
"lib": [
10+
"dom",
11+
"es2018"
12+
]
13+
},
14+
"angularCompilerOptions": {
15+
"annotateForClosureCompiler": true,
16+
"skipTemplateCodegen": true,
17+
"strictMetadataEmit": true,
18+
"fullTemplateTypeCheck": true,
19+
"strictInjectionParameters": true,
20+
"enableResourceInlining": true
21+
},
22+
"exclude": [
23+
"src/test.ts",
24+
"**/*.spec.ts"
25+
]
26+
}

0 commit comments

Comments
 (0)