|
1 | 1 | import { isWideScreen } from '../utils.js';
|
2 | 2 | import * as metrics from '../metrics.js';
|
3 | 3 | import * as jwt from './jwt.js';
|
| 4 | +import registeredClaims from './jwt-iana-registered-claims.js'; |
4 | 5 | import forge from 'node-forge';
|
5 | 6 | import {
|
6 | 7 | algorithmSelect,
|
@@ -49,27 +50,89 @@ export function disableUnsupportedAlgorithms() {
|
49 | 50 | }
|
50 | 51 | }
|
51 | 52 |
|
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) { |
53 | 104 | try {
|
54 | 105 | sha256.start();
|
55 |
| - sha256.update(jwt); |
| 106 | + sha256.update(token); |
56 | 107 |
|
57 | 108 | const result = {
|
58 | 109 | hash: sha256.digest().toHex()
|
59 | 110 | };
|
60 | 111 |
|
61 | 112 | try {
|
62 |
| - const decoded = jwt.decode(jwt); |
| 113 | + const decoded = jwt.decode(token); |
63 | 114 |
|
64 |
| - return Object.assign(result, { |
| 115 | + const result = Object.assign(result, { |
65 | 116 | decodedWithErrors: decoded.errors,
|
| 117 | + encodedSize: token.length, |
| 118 | + base64Format: getBase64Format(token), |
66 | 119 | header: {
|
67 | 120 | alg: decoded.header.alg,
|
68 | 121 | },
|
69 | 122 | 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 |
71 | 128 | }
|
72 | 129 | });
|
| 130 | + |
| 131 | + if(decoded.payload.amr) { |
| 132 | + result.payload.amr = decoded.payload.amr; |
| 133 | + } |
| 134 | + |
| 135 | + return result; |
73 | 136 | } catch(e) {
|
74 | 137 | return Object.assign(result, {
|
75 | 138 | error: 'error decoding token',
|
|
0 commit comments