|
| 1 | +import type { SignedToken, TokenPayload } from '../types.js'; |
| 2 | +import type { NodeIdEncoded } from '../../ids/types.js'; |
| 3 | +import * as tokensUtils from '../utils.js'; |
| 4 | +import * as ids from '../../ids/index.js'; |
| 5 | +import * as validationErrors from '../../validation/errors.js'; |
| 6 | +import * as utils from '../../utils/index.js'; |
| 7 | + |
| 8 | +interface AuthSignedIdentity extends TokenPayload { |
| 9 | + typ: 'AuthSignedIdentity'; |
| 10 | + iss: NodeIdEncoded; |
| 11 | + exp: number; |
| 12 | + jti: string; |
| 13 | +} |
| 14 | + |
| 15 | +function assertAuthSignedIdentity( |
| 16 | + authSignedIdentity: unknown, |
| 17 | +): asserts authSignedIdentity is AuthSignedIdentity { |
| 18 | + if (!utils.isObject(authSignedIdentity)) { |
| 19 | + throw new validationErrors.ErrorParse('must be POJO'); |
| 20 | + } |
| 21 | + if (authSignedIdentity['typ'] !== 'AuthSignedIdentity') { |
| 22 | + throw new validationErrors.ErrorParse( |
| 23 | + '`typ` property must be `AuthSignedToken`', |
| 24 | + ); |
| 25 | + } |
| 26 | + if ( |
| 27 | + authSignedIdentity['iss'] == null || |
| 28 | + ids.decodeNodeId(authSignedIdentity['iss'] == null) |
| 29 | + ) { |
| 30 | + throw new validationErrors.ErrorParse( |
| 31 | + '`iss` property must be an encoded node ID', |
| 32 | + ); |
| 33 | + } |
| 34 | + if (typeof authSignedIdentity['exp'] !== 'number') { |
| 35 | + throw new validationErrors.ErrorParse('`exp` property must be a number'); |
| 36 | + } |
| 37 | + if (typeof authSignedIdentity['jti'] !== 'string') { |
| 38 | + throw new validationErrors.ErrorParse('`jti` property must be a string'); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function parseAuthSignedIdentity( |
| 43 | + authIdentityEncoded: unknown, |
| 44 | +): SignedToken<AuthSignedIdentity> { |
| 45 | + const encodedToken = |
| 46 | + tokensUtils.parseSignedToken<AuthSignedIdentity>(authIdentityEncoded); |
| 47 | + const authIdentity = |
| 48 | + tokensUtils.parseTokenPayload<AuthSignedIdentity>(encodedToken); |
| 49 | + assertAuthSignedIdentity(authIdentity); |
| 50 | + return encodedToken; |
| 51 | +} |
| 52 | + |
| 53 | +export { assertAuthSignedIdentity, parseAuthSignedIdentity }; |
| 54 | + |
| 55 | +export type { AuthSignedIdentity }; |
0 commit comments