|
| 1 | +import { canonicalizeEx } from 'json-canonicalize'; |
| 2 | +import { SignJWT, jwtVerify } from 'jose'; |
| 3 | +import { AssertionKey } from './../client/AssertionConfig.js'; |
| 4 | + |
| 5 | +export type AssertionKeyAlg = 'RS256' | 'HS256'; |
| 6 | +export type AssertionType = 'handling' | 'other'; |
| 7 | +export type Scope = 'tdo' | 'payload'; |
| 8 | +export type AppliesToState = 'encrypted' | 'unencrypted'; |
| 9 | +export type BindingMethod = 'jws'; |
| 10 | + |
| 11 | +const kAssertionHash = 'assertionHash'; |
| 12 | +const kAssertionSignature = 'assertionSig'; |
| 13 | + |
| 14 | +// Statement type |
| 15 | +export type Statement = { |
| 16 | + format: string; |
| 17 | + schema: string; |
| 18 | + value: string; |
| 19 | +}; |
| 20 | + |
| 21 | +// Binding type |
| 22 | +export type Binding = { |
| 23 | + method: string; |
| 24 | + signature: string; |
| 25 | +}; |
| 26 | + |
| 27 | +// Assertion type |
| 28 | +export type Assertion = { |
| 29 | + id: string; |
| 30 | + type: AssertionType; |
| 31 | + scope: Scope; |
| 32 | + appliesToState?: AppliesToState; |
| 33 | + statement: Statement; |
| 34 | + binding: Binding; |
| 35 | + hash: () => Promise<string>; |
| 36 | + sign: (hash: string, sig: string, key: AssertionKey) => Promise<void>; |
| 37 | + verify: (key: AssertionKey) => Promise<[string, string]>; |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * Computes the SHA-256 hash of the assertion object, excluding the 'binding' and 'hash' properties. |
| 42 | + * |
| 43 | + * @returns {Promise<string>} A promise that resolves to the hexadecimal string representation of the hash. |
| 44 | + */ |
| 45 | +export async function hash(this: Assertion): Promise<string> { |
| 46 | + const result = canonicalizeEx(this, { exclude: ['binding', 'hash', 'sign', 'verify'] }); |
| 47 | + |
| 48 | + const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(result)); |
| 49 | + return Buffer.from(hash).toString('hex'); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Signs the given hash and signature using the provided key and sets the binding method and signature. |
| 54 | + * |
| 55 | + * @param {string} hash - The hash to be signed. |
| 56 | + * @param {string} sig - The signature to be signed. |
| 57 | + * @param {AssertionKey} key - The key used for signing. |
| 58 | + * @returns {Promise<void>} A promise that resolves when the signing is complete. |
| 59 | + */ |
| 60 | +export async function sign( |
| 61 | + this: Assertion, |
| 62 | + assertionHash: string, |
| 63 | + sig: string, |
| 64 | + key: AssertionKey |
| 65 | +): Promise<void> { |
| 66 | + const payload: any = {}; |
| 67 | + payload[kAssertionHash] = assertionHash; |
| 68 | + payload[kAssertionSignature] = sig; |
| 69 | + |
| 70 | + try { |
| 71 | + const token = await new SignJWT(payload).setProtectedHeader({ alg: key.alg }).sign(key.key); |
| 72 | + |
| 73 | + this.binding.method = 'jws'; |
| 74 | + this.binding.signature = token; |
| 75 | + } catch (error) { |
| 76 | + throw new Error(`Signing assertion failed: ${error.message}`); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Verifies the signature of the assertion using the provided key. |
| 82 | + * |
| 83 | + * @param {AssertionKey} key - The key used for verification. |
| 84 | + * @returns {Promise<[string, string]>} A promise that resolves to a tuple containing the assertion hash and signature. |
| 85 | + * @throws {Error} If the verification fails. |
| 86 | + */ |
| 87 | +export async function verify(this: Assertion, key: AssertionKey): Promise<[string, string]> { |
| 88 | + try { |
| 89 | + const { payload } = await jwtVerify(this.binding.signature, key.key, { |
| 90 | + algorithms: [key.alg], |
| 91 | + }); |
| 92 | + |
| 93 | + return [payload[kAssertionHash] as string, payload[kAssertionSignature] as string]; |
| 94 | + } catch (error) { |
| 95 | + throw new Error(`Verifying assertion failed: ${error.message}`); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Creates an Assertion object with the specified properties. |
| 101 | + * |
| 102 | + * @param {string} id - The unique identifier for the assertion. |
| 103 | + * @param {AssertionType} type - The type of the assertion (e.g., 'handling', 'other'). |
| 104 | + * @param {Scope} scope - The scope of the assertion (e.g., 'tdo', 'payload'). |
| 105 | + * @param {Statement} statement - The statement associated with the assertion. |
| 106 | + * @param {Binding} binding - The binding method and signature for the assertion. |
| 107 | + * @param {AppliesToState} [appliesToState] - The state to which the assertion applies (optional). |
| 108 | + * @returns {Assertion} The created Assertion object. |
| 109 | + */ |
| 110 | +export function CreateAssertion( |
| 111 | + id: string, |
| 112 | + type: AssertionType, |
| 113 | + scope: Scope, |
| 114 | + statement: Statement, |
| 115 | + appliesToState?: AppliesToState, |
| 116 | + binding?: Binding |
| 117 | +): Assertion { |
| 118 | + return { |
| 119 | + id, |
| 120 | + type, |
| 121 | + scope, |
| 122 | + appliesToState, |
| 123 | + statement, |
| 124 | + binding: { method: binding?.method ?? '', signature: binding?.signature ?? '' }, |
| 125 | + hash, |
| 126 | + sign, |
| 127 | + verify, |
| 128 | + }; |
| 129 | +} |
0 commit comments