|
8 | 8 | TOKEN_TYPES, |
9 | 9 | Token, |
10 | 10 | AuthorizationHeader, |
| 11 | + WWWAuthenticateHeader, |
| 12 | + header_to_token, |
11 | 13 | publicVerif, |
12 | 14 | tokenRequestToTokenTypeEntry, |
13 | 15 | } from '../src/index.js'; |
@@ -85,6 +87,63 @@ describe.each(vectors)('PublicVerifiable-Vector-%#', (v: Vectors) => { |
85 | 87 | }); |
86 | 88 | }); |
87 | 89 |
|
| 90 | +test('header_to_token returns an Authorization header value', async () => { |
| 91 | + const v = vectors[0]; |
| 92 | + const [{ privateKey, publicKey }, publicKeyEnc] = await keysFromVector(v); |
| 93 | + const salt = hexToUint8(v.salt); |
| 94 | + const mode = |
| 95 | + salt.length == (BlindRSAMode.PSS as number) ? BlindRSAMode.PSS : BlindRSAMode.PSSZero; |
| 96 | + const nonce = hexToUint8(v.nonce); |
| 97 | + const blind = hexToUint8(v.blind); |
| 98 | + const tokChl = TokenChallenge.deserialize(hexToUint8(v.token_challenge)); |
| 99 | + const issuer = new Issuer(mode, tokChl.issuerName, privateKey, publicKey); |
| 100 | + |
| 101 | + vi.spyOn(crypto, 'getRandomValues') |
| 102 | + .mockReturnValueOnce(nonce) |
| 103 | + .mockReturnValueOnce(salt) |
| 104 | + .mockReturnValueOnce(blind); |
| 105 | + vi.stubGlobal( |
| 106 | + 'fetch', |
| 107 | + vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { |
| 108 | + const url = |
| 109 | + input instanceof Request ? input.url : input instanceof URL ? input.href : input; |
| 110 | + if (url === `https://${tokChl.issuerName}/.well-known/private-token-issuer-directory`) { |
| 111 | + return new Response( |
| 112 | + JSON.stringify({ |
| 113 | + 'issuer-request-uri': `https://${tokChl.issuerName}/issuer`, |
| 114 | + 'token-keys': [], |
| 115 | + }), |
| 116 | + { status: 200 }, |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + if ( |
| 121 | + url !== `https://${tokChl.issuerName}/issuer` || |
| 122 | + !(init?.body instanceof Uint8Array) |
| 123 | + ) { |
| 124 | + return new Response(null, { status: 404 }); |
| 125 | + } |
| 126 | + |
| 127 | + const tokenRequest = TokenRequest.deserialize(TOKEN_TYPES.BLIND_RSA, init.body); |
| 128 | + const tokenResponse = await issuer.issue(tokenRequest); |
| 129 | + return new Response(tokenResponse.serialize(), { |
| 130 | + status: 200, |
| 131 | + headers: { 'Content-Type': 'application/private-token-response' }, |
| 132 | + }); |
| 133 | + }), |
| 134 | + ); |
| 135 | + |
| 136 | + const challengeHeader = new WWWAuthenticateHeader(tokChl, publicKeyEnc).toString(); |
| 137 | + const authHeader = await header_to_token(challengeHeader); |
| 138 | + |
| 139 | + expect(authHeader).not.toBeNull(); |
| 140 | + if (authHeader === null) { |
| 141 | + return; |
| 142 | + } |
| 143 | + expect(authHeader.startsWith('PrivateToken token=')).toBe(true); |
| 144 | + expect(AuthorizationHeader.parse(TOKEN_TYPES.BLIND_RSA, authHeader)).toHaveLength(1); |
| 145 | +}); |
| 146 | + |
88 | 147 | describe('getPublicKeyBytes', () => { |
89 | 148 | const modes = [BlindRSAMode.PSS, BlindRSAMode.PSSZero]; |
90 | 149 |
|
|
0 commit comments