|
1 | 1 | import { Challenge, Credential } from 'mppx' |
| 2 | +import { Base64 } from 'ox' |
2 | 3 | import { describe, expect, test } from 'vp/test' |
3 | 4 |
|
4 | 5 | const challenge = Challenge.from({ |
@@ -88,6 +89,28 @@ describe('serialize', () => { |
88 | 89 | const deserialized = Credential.deserialize(header) |
89 | 90 | expect(deserialized.challenge.request).toEqual({ amount: '1000' }) |
90 | 91 | }) |
| 92 | + |
| 93 | + test('behavior: serializes opaque as a base64url string', () => { |
| 94 | + const credential = Credential.from({ |
| 95 | + challenge: Challenge.from({ |
| 96 | + id: 'opaque123', |
| 97 | + intent: 'charge', |
| 98 | + meta: { pi: 'pi_3abc123XYZ' }, |
| 99 | + method: 'tempo', |
| 100 | + realm: 'api.example.com', |
| 101 | + request: { amount: '1000' }, |
| 102 | + }), |
| 103 | + payload: { signature: '0x1234' }, |
| 104 | + }) |
| 105 | + |
| 106 | + const header = Credential.serialize(credential) |
| 107 | + const encoded = header.replace(/^Payment\s+/i, '') |
| 108 | + const parsed = JSON.parse(Base64.toString(encoded)) as { |
| 109 | + challenge: { opaque?: unknown } |
| 110 | + } |
| 111 | + |
| 112 | + expect(parsed.challenge.opaque).toBe('eyJwaSI6InBpXzNhYmMxMjNYWVoifQ') |
| 113 | + }) |
91 | 114 | }) |
92 | 115 |
|
93 | 116 | describe('deserialize', () => { |
@@ -134,6 +157,49 @@ describe('deserialize', () => { |
134 | 157 | expect(deserialized.source).toBe(original.source) |
135 | 158 | }) |
136 | 159 |
|
| 160 | + test('behavior: deserializes spec-compliant opaque string credentials', () => { |
| 161 | + const encoded = Base64.fromString( |
| 162 | + JSON.stringify({ |
| 163 | + challenge: { |
| 164 | + id: 'opaque123', |
| 165 | + intent: 'charge', |
| 166 | + method: 'tempo', |
| 167 | + opaque: 'eyJwaSI6InBpXzNhYmMxMjNYWVoifQ', |
| 168 | + realm: 'api.example.com', |
| 169 | + request: 'eyJhbW91bnQiOiIxMDAwIn0', |
| 170 | + }, |
| 171 | + payload: { signature: '0x1234' }, |
| 172 | + }), |
| 173 | + { pad: false, url: true }, |
| 174 | + ) |
| 175 | + |
| 176 | + const credential = Credential.deserialize(`Payment ${encoded}`) |
| 177 | + |
| 178 | + expect(credential.challenge.opaque).toEqual({ pi: 'pi_3abc123XYZ' }) |
| 179 | + expect(credential.challenge.request).toEqual({ amount: '1000' }) |
| 180 | + }) |
| 181 | + |
| 182 | + test('behavior: preserves legacy object-shaped opaque credentials', () => { |
| 183 | + const encoded = Base64.fromString( |
| 184 | + JSON.stringify({ |
| 185 | + challenge: { |
| 186 | + id: 'opaque123', |
| 187 | + intent: 'charge', |
| 188 | + method: 'tempo', |
| 189 | + opaque: { pi: 'pi_3abc123XYZ' }, |
| 190 | + realm: 'api.example.com', |
| 191 | + request: 'eyJhbW91bnQiOiIxMDAwIn0', |
| 192 | + }, |
| 193 | + payload: { signature: '0x1234' }, |
| 194 | + }), |
| 195 | + { pad: false, url: true }, |
| 196 | + ) |
| 197 | + |
| 198 | + const credential = Credential.deserialize(`Payment ${encoded}`) |
| 199 | + |
| 200 | + expect(credential.challenge.opaque).toEqual({ pi: 'pi_3abc123XYZ' }) |
| 201 | + }) |
| 202 | + |
137 | 203 | test('error: throws for missing Payment scheme', () => { |
138 | 204 | expect(() => Credential.deserialize('Bearer abc123')).toThrow('Missing Payment scheme.') |
139 | 205 | }) |
|
0 commit comments