|
| 1 | +import * as jose from 'jose' |
| 2 | +import { beforeAll, describe, expect, test } from 'vitest' |
| 3 | +import { callbacks, getSignJwtCallback } from '../../../tests/util.mjs' |
| 4 | +import type { Jwk } from '../../common/jwk/z-jwk' |
| 5 | +import { decodeJwt } from '../../common/jwt/decode-jwt' |
| 6 | +import { createClientAttestationJwt } from '../client-attestation' |
| 7 | +import { createClientAttestationPopJwt, verifyClientAttestationPopJwt } from '../client-attestation-pop' |
| 8 | +import { |
| 9 | + zClientAttestationJwtHeader, |
| 10 | + zClientAttestationJwtPayload, |
| 11 | + zClientAttestationPopJwtHeader, |
| 12 | + zClientAttestationPopJwtPayload, |
| 13 | +} from '../z-client-attestation' |
| 14 | + |
| 15 | +const authorizationServer = 'https://oauth2-auth-server.com' |
| 16 | + |
| 17 | +async function generateEs256Key() { |
| 18 | + const { publicKey, privateKey } = await jose.generateKeyPair('ES256', { extractable: true }) |
| 19 | + return { |
| 20 | + privateJwk: (await jose.exportJWK(privateKey)) as Jwk, |
| 21 | + publicJwk: (await jose.exportJWK(publicKey)) as Jwk, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe('Client (Wallet) Attestation', () => { |
| 26 | + // Attester signs the client attestation; the client instance holds the `cnf` key and signs the PoP. |
| 27 | + let attester: Awaited<ReturnType<typeof generateEs256Key>> |
| 28 | + let instance: Awaited<ReturnType<typeof generateEs256Key>> |
| 29 | + let signJwt: ReturnType<typeof getSignJwtCallback> |
| 30 | + let clientAttestationJwt: string |
| 31 | + let clientAttestation: ReturnType< |
| 32 | + typeof decodeJwt<typeof zClientAttestationJwtHeader, typeof zClientAttestationJwtPayload> |
| 33 | + > |
| 34 | + |
| 35 | + beforeAll(async () => { |
| 36 | + attester = await generateEs256Key() |
| 37 | + instance = await generateEs256Key() |
| 38 | + signJwt = getSignJwtCallback([attester.privateJwk, instance.privateJwk]) |
| 39 | + |
| 40 | + clientAttestationJwt = await createClientAttestationJwt({ |
| 41 | + callbacks: { signJwt }, |
| 42 | + clientId: 'wallet', |
| 43 | + confirmation: { jwk: instance.publicJwk }, |
| 44 | + expiresAt: new Date(Date.now() + 3600 * 1000), |
| 45 | + signer: { method: 'jwk', alg: 'ES256', publicJwk: attester.publicJwk }, |
| 46 | + }) |
| 47 | + |
| 48 | + clientAttestation = decodeJwt({ |
| 49 | + jwt: clientAttestationJwt, |
| 50 | + headerSchema: zClientAttestationJwtHeader, |
| 51 | + payloadSchema: zClientAttestationJwtPayload, |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + test('creates a draft-09 Client Attestation JWT without an `iss` claim', () => { |
| 56 | + expect(clientAttestation.payload.iss).toBeUndefined() |
| 57 | + expect(clientAttestation.payload.sub).toBe('wallet') |
| 58 | + expect(clientAttestation.payload.cnf.jwk).toEqual(instance.publicJwk) |
| 59 | + }) |
| 60 | + |
| 61 | + test('creates a draft-09 PoP JWT (no `iss`, no `exp`, uses `challenge`) that verifies', async () => { |
| 62 | + const clientAttestationPopJwt = await createClientAttestationPopJwt({ |
| 63 | + callbacks: { signJwt, generateRandom: callbacks.generateRandom }, |
| 64 | + authorizationServer, |
| 65 | + clientAttestation: clientAttestationJwt, |
| 66 | + challenge: 'challenge-123', |
| 67 | + }) |
| 68 | + |
| 69 | + const { payload } = decodeJwt({ |
| 70 | + jwt: clientAttestationPopJwt, |
| 71 | + headerSchema: zClientAttestationPopJwtHeader, |
| 72 | + payloadSchema: zClientAttestationPopJwtPayload, |
| 73 | + }) |
| 74 | + expect(payload.iss).toBeUndefined() |
| 75 | + expect(payload.exp).toBeUndefined() |
| 76 | + expect(payload.nonce).toBeUndefined() |
| 77 | + expect(payload.challenge).toBe('challenge-123') |
| 78 | + expect(payload.aud).toBe(authorizationServer) |
| 79 | + expect(payload.jti).toEqual(expect.any(String)) |
| 80 | + |
| 81 | + await expect( |
| 82 | + verifyClientAttestationPopJwt({ |
| 83 | + callbacks: { verifyJwt: callbacks.verifyJwt }, |
| 84 | + authorizationServer, |
| 85 | + clientAttestation, |
| 86 | + clientAttestationPopJwt, |
| 87 | + expectedChallenge: 'challenge-123', |
| 88 | + }) |
| 89 | + ).resolves.toBeDefined() |
| 90 | + }) |
| 91 | + |
| 92 | + test('verifies a legacy (<= draft 07) PoP JWT carrying `iss` and `nonce`', async () => { |
| 93 | + const now = Math.floor(Date.now() / 1000) |
| 94 | + const { jwt: legacyPopJwt } = await signJwt( |
| 95 | + { method: 'jwk', alg: 'ES256', publicJwk: instance.publicJwk }, |
| 96 | + { |
| 97 | + header: { typ: 'oauth-client-attestation-pop+jwt', alg: 'ES256' }, |
| 98 | + payload: { |
| 99 | + iss: 'wallet', |
| 100 | + aud: authorizationServer, |
| 101 | + iat: now, |
| 102 | + exp: now + 300, |
| 103 | + jti: 'legacy-jti', |
| 104 | + nonce: 'legacy-nonce', |
| 105 | + }, |
| 106 | + } |
| 107 | + ) |
| 108 | + |
| 109 | + await expect( |
| 110 | + verifyClientAttestationPopJwt({ |
| 111 | + callbacks: { verifyJwt: callbacks.verifyJwt }, |
| 112 | + authorizationServer, |
| 113 | + clientAttestation, |
| 114 | + clientAttestationPopJwt: legacyPopJwt, |
| 115 | + // `expectedNonce` is the deprecated alias for `expectedChallenge` |
| 116 | + expectedNonce: 'legacy-nonce', |
| 117 | + }) |
| 118 | + ).resolves.toBeDefined() |
| 119 | + }) |
| 120 | + |
| 121 | + test('rejects a legacy PoP JWT whose `iss` does not match the attestation `sub`', async () => { |
| 122 | + const now = Math.floor(Date.now() / 1000) |
| 123 | + const { jwt: legacyPopJwt } = await signJwt( |
| 124 | + { method: 'jwk', alg: 'ES256', publicJwk: instance.publicJwk }, |
| 125 | + { |
| 126 | + header: { typ: 'oauth-client-attestation-pop+jwt', alg: 'ES256' }, |
| 127 | + payload: { |
| 128 | + iss: 'not-wallet', |
| 129 | + aud: authorizationServer, |
| 130 | + iat: now, |
| 131 | + jti: 'legacy-jti', |
| 132 | + }, |
| 133 | + } |
| 134 | + ) |
| 135 | + |
| 136 | + await expect( |
| 137 | + verifyClientAttestationPopJwt({ |
| 138 | + callbacks: { verifyJwt: callbacks.verifyJwt }, |
| 139 | + authorizationServer, |
| 140 | + clientAttestation, |
| 141 | + clientAttestationPopJwt: legacyPopJwt, |
| 142 | + }) |
| 143 | + ).rejects.toThrow("'iss'") |
| 144 | + }) |
| 145 | +}) |
0 commit comments