|
| 1 | +import type { Jwk } from '@openid4vc/oauth2' |
| 2 | +import { describe, expect, test } from 'vitest' |
| 3 | +import { verifyCredentialRequestDiVpProof } from '../di-vp-proof-type' |
| 4 | + |
| 5 | +const credentialIssuer = 'https://issuer.com' |
| 6 | +const signerJwk = { kty: 'OKP', crv: 'Ed25519', x: 'some-x' } satisfies Jwk |
| 7 | + |
| 8 | +function validVp(overrides: Record<string, unknown> = {}) { |
| 9 | + return { |
| 10 | + '@context': ['https://www.w3.org/ns/credentials/v2'], |
| 11 | + type: ['VerifiablePresentation'], |
| 12 | + proof: { |
| 13 | + type: 'DataIntegrityProof', |
| 14 | + cryptosuite: 'eddsa-2022', |
| 15 | + proofPurpose: 'authentication', |
| 16 | + verificationMethod: 'did:key:z6Mk...#z6Mk...', |
| 17 | + domain: credentialIssuer, |
| 18 | + challenge: 'some-nonce', |
| 19 | + proofValue: 'z5hrbHzZ...', |
| 20 | + ...overrides, |
| 21 | + }, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe('verifyCredentialRequestDiVpProof', () => { |
| 26 | + test('verifies a valid di_vp proof and returns the signer jwk', async () => { |
| 27 | + const result = await verifyCredentialRequestDiVpProof({ |
| 28 | + vp: validVp(), |
| 29 | + expectedNonce: 'some-nonce', |
| 30 | + credentialIssuer, |
| 31 | + callbacks: { |
| 32 | + verifyDataIntegrityProof: () => ({ verified: true, signerJwk }), |
| 33 | + }, |
| 34 | + }) |
| 35 | + |
| 36 | + expect(result.signerJwk).toStrictEqual(signerJwk) |
| 37 | + }) |
| 38 | + |
| 39 | + test('throws when the nonce has expired', async () => { |
| 40 | + await expect( |
| 41 | + verifyCredentialRequestDiVpProof({ |
| 42 | + vp: validVp(), |
| 43 | + expectedNonce: 'some-nonce', |
| 44 | + nonceExpiresAt: new Date(Date.now() - 1000), |
| 45 | + credentialIssuer, |
| 46 | + callbacks: { |
| 47 | + verifyDataIntegrityProof: () => ({ verified: true, signerJwk }), |
| 48 | + }, |
| 49 | + }) |
| 50 | + ).rejects.toThrow('Nonce used for credential request proof expired') |
| 51 | + }) |
| 52 | + |
| 53 | + test('throws when proofPurpose is not authentication', async () => { |
| 54 | + await expect( |
| 55 | + verifyCredentialRequestDiVpProof({ |
| 56 | + vp: validVp({ proofPurpose: 'assertionMethod' }), |
| 57 | + expectedNonce: 'some-nonce', |
| 58 | + credentialIssuer, |
| 59 | + callbacks: { |
| 60 | + verifyDataIntegrityProof: () => ({ verified: true, signerJwk }), |
| 61 | + }, |
| 62 | + }) |
| 63 | + ).rejects.toThrow(`proof.proofPurpose' must be 'authentication'`) |
| 64 | + }) |
| 65 | + |
| 66 | + test('throws when domain does not match the credential issuer', async () => { |
| 67 | + await expect( |
| 68 | + verifyCredentialRequestDiVpProof({ |
| 69 | + vp: validVp({ domain: 'https://other-issuer.com' }), |
| 70 | + expectedNonce: 'some-nonce', |
| 71 | + credentialIssuer, |
| 72 | + callbacks: { |
| 73 | + verifyDataIntegrityProof: () => ({ verified: true, signerJwk }), |
| 74 | + }, |
| 75 | + }) |
| 76 | + ).rejects.toThrow(`proof.domain' does not match the credential issuer identifier`) |
| 77 | + }) |
| 78 | + |
| 79 | + test('throws when challenge does not match the expected nonce', async () => { |
| 80 | + await expect( |
| 81 | + verifyCredentialRequestDiVpProof({ |
| 82 | + vp: validVp({ challenge: 'wrong-nonce' }), |
| 83 | + expectedNonce: 'some-nonce', |
| 84 | + credentialIssuer, |
| 85 | + callbacks: { |
| 86 | + verifyDataIntegrityProof: () => ({ verified: true, signerJwk }), |
| 87 | + }, |
| 88 | + }) |
| 89 | + ).rejects.toThrow(`proof.challenge' does not match the expected nonce`) |
| 90 | + }) |
| 91 | + |
| 92 | + test('throws when challenge is present but no nonce was expected', async () => { |
| 93 | + await expect( |
| 94 | + verifyCredentialRequestDiVpProof({ |
| 95 | + vp: validVp(), |
| 96 | + credentialIssuer, |
| 97 | + callbacks: { |
| 98 | + verifyDataIntegrityProof: () => ({ verified: true, signerJwk }), |
| 99 | + }, |
| 100 | + }) |
| 101 | + ).rejects.toThrow(`proof.challenge' must not be present when no nonce was issued`) |
| 102 | + }) |
| 103 | + |
| 104 | + test('throws when cryptosuite is missing', async () => { |
| 105 | + await expect( |
| 106 | + verifyCredentialRequestDiVpProof({ |
| 107 | + vp: validVp({ cryptosuite: undefined }), |
| 108 | + expectedNonce: 'some-nonce', |
| 109 | + credentialIssuer, |
| 110 | + callbacks: { |
| 111 | + verifyDataIntegrityProof: () => ({ verified: true, signerJwk }), |
| 112 | + }, |
| 113 | + }) |
| 114 | + ).rejects.toThrow(`missing required 'proof.cryptosuite'`) |
| 115 | + }) |
| 116 | + |
| 117 | + test('throws when verificationMethod is missing', async () => { |
| 118 | + await expect( |
| 119 | + verifyCredentialRequestDiVpProof({ |
| 120 | + vp: validVp({ verificationMethod: undefined }), |
| 121 | + expectedNonce: 'some-nonce', |
| 122 | + credentialIssuer, |
| 123 | + callbacks: { |
| 124 | + verifyDataIntegrityProof: () => ({ verified: true, signerJwk }), |
| 125 | + }, |
| 126 | + }) |
| 127 | + ).rejects.toThrow(`missing required 'proof.verificationMethod'`) |
| 128 | + }) |
| 129 | + |
| 130 | + test('throws when the proof entry is missing', async () => { |
| 131 | + await expect( |
| 132 | + verifyCredentialRequestDiVpProof({ |
| 133 | + vp: { '@context': ['https://www.w3.org/ns/credentials/v2'], type: ['VerifiablePresentation'] }, |
| 134 | + expectedNonce: 'some-nonce', |
| 135 | + credentialIssuer, |
| 136 | + callbacks: { |
| 137 | + verifyDataIntegrityProof: () => ({ verified: true, signerJwk }), |
| 138 | + }, |
| 139 | + }) |
| 140 | + ).rejects.toThrow(`di_vp proof is missing a 'proof' entry`) |
| 141 | + }) |
| 142 | + |
| 143 | + test('throws when no verifyDataIntegrityProof callback is configured', async () => { |
| 144 | + await expect( |
| 145 | + verifyCredentialRequestDiVpProof({ |
| 146 | + vp: validVp(), |
| 147 | + expectedNonce: 'some-nonce', |
| 148 | + credentialIssuer, |
| 149 | + callbacks: {}, |
| 150 | + }) |
| 151 | + ).rejects.toThrow('no verifyDataIntegrityProof callback configured') |
| 152 | + }) |
| 153 | + |
| 154 | + test('throws when the callback reports the proof as not verified', async () => { |
| 155 | + await expect( |
| 156 | + verifyCredentialRequestDiVpProof({ |
| 157 | + vp: validVp(), |
| 158 | + expectedNonce: 'some-nonce', |
| 159 | + credentialIssuer, |
| 160 | + callbacks: { |
| 161 | + verifyDataIntegrityProof: () => ({ verified: false }), |
| 162 | + }, |
| 163 | + }) |
| 164 | + ).rejects.toThrow('Error verifying credential request di_vp proof') |
| 165 | + }) |
| 166 | +}) |
0 commit comments