Skip to content

Commit 42e6905

Browse files
committed
More work on metrics.
1 parent cef116e commit 42e6905

File tree

2 files changed

+120
-5
lines changed

2 files changed

+120
-5
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
export default [
2+
'iss',
3+
'sub',
4+
'aud',
5+
'exp',
6+
'nbf',
7+
'iat',
8+
'jti',
9+
'name',
10+
'given_name',
11+
'family_name',
12+
'middle_name',
13+
'nickname',
14+
'preferred_username',
15+
'profile',
16+
'picture',
17+
'website',
18+
'email',
19+
'email_verified',
20+
'gender',
21+
'birthdate',
22+
'zoneinfo',
23+
'locale',
24+
'phone_number',
25+
'phone_number_verified',
26+
'address',
27+
'updated_at',
28+
'azp',
29+
'nonce',
30+
'auth_time',
31+
'at_hash',
32+
'c_hash',
33+
'acr',
34+
'amr',
35+
'sub_jwk',
36+
'cnf',
37+
'sip_from_tag',
38+
'sip_date',
39+
'sip_callid',
40+
'sip_cseq_num',
41+
'sip_via_branch',
42+
'orig',
43+
'dest',
44+
'mky',
45+
'events',
46+
'toe',
47+
'txn',
48+
'rph',
49+
'sid',
50+
'vot',
51+
'vtm'
52+
];

src/editor/utils.js

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isWideScreen } from '../utils.js';
22
import * as metrics from '../metrics.js';
33
import * as jwt from './jwt.js';
4+
import registeredClaims from './jwt-iana-registered-claims.js';
45
import forge from 'node-forge';
56
import {
67
algorithmSelect,
@@ -49,27 +50,89 @@ export function disableUnsupportedAlgorithms() {
4950
}
5051
}
5152

52-
export function getSafeTokenInfo(jwt) {
53+
export function isString(value) {
54+
return typeof value === 'string' || value instanceof String;
55+
}
56+
57+
function getBase64Format(token) {
58+
if(jwt.isValidBase64String(token, true)) {
59+
return 'base64url';
60+
} else if(jwt.isValidBase64String(token, false)) {
61+
return 'base64';
62+
} else {
63+
return 'invalid';
64+
}
65+
}
66+
67+
function getRegisteredClaims(payload) {
68+
const result = [];
69+
70+
registeredClaims.forEach(claim => {
71+
if(claim in payload) {
72+
result.push(claim);
73+
}
74+
});
75+
76+
return result;
77+
}
78+
79+
function getScopes(payload) {
80+
if(!isString(payload.scope)) {
81+
return [];
82+
}
83+
84+
const scopes = payload.scope.split(/\s+/).filter(scope => {
85+
return scope.length > 0 && /\S+/.test(scope);
86+
});
87+
88+
return scopes;
89+
}
90+
91+
function getNumberOfScopes(payload) {
92+
return getScopes(payload).length;
93+
}
94+
95+
function getOIDCScopes(payload) {
96+
const oidcScopes = ['openid', 'profile', 'email',
97+
'address', 'phone', 'offline_access'];
98+
const scopes = getScopes(payload);
99+
100+
return scopes.filter(scope => oidcScopes.indexOf(scope) !== -1);
101+
}
102+
103+
export function getSafeTokenInfo(token) {
53104
try {
54105
sha256.start();
55-
sha256.update(jwt);
106+
sha256.update(token);
56107

57108
const result = {
58109
hash: sha256.digest().toHex()
59110
};
60111

61112
try {
62-
const decoded = jwt.decode(jwt);
113+
const decoded = jwt.decode(token);
63114

64-
return Object.assign(result, {
115+
const result = Object.assign(result, {
65116
decodedWithErrors: decoded.errors,
117+
encodedSize: token.length,
118+
base64Format: getBase64Format(token),
66119
header: {
67120
alg: decoded.header.alg,
68121
},
69122
payload: {
70-
// TODO
123+
registeredClaimsPresent: getRegisteredClaims(decoded.payload),
124+
oidcScopesPresent: getOIDCScopes(decoded.payload),
125+
numberOfScopes: getNumberOfScopes(decoded.payload),
126+
numberOfClaims: Object.keys(decoded.payload).length,
127+
issuer: decoded.payload.iss ? decoded.payload.iss : null
71128
}
72129
});
130+
131+
if(decoded.payload.amr) {
132+
result.payload.amr = decoded.payload.amr;
133+
}
134+
135+
return result;
73136
} catch(e) {
74137
return Object.assign(result, {
75138
error: 'error decoding token',

0 commit comments

Comments
 (0)