Skip to content

Commit 865259b

Browse files
committed
test header_to_token output
1 parent 69ae637 commit 865259b

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) 2023 Cloudflare, Inc.
22
// Licensed under the Apache-2.0 license found in the LICENSE file or at https://opensource.org/licenses/Apache-2.0
33

4-
import { base64url } from 'rfc4648';
54
import { WWWAuthenticateHeader, type TokenTypeEntry } from './auth_scheme/private_token.js';
65
import {
76
Client as PublicVerifClient,

test/pub_verif_token.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
TOKEN_TYPES,
99
Token,
1010
AuthorizationHeader,
11+
WWWAuthenticateHeader,
12+
header_to_token,
1113
publicVerif,
1214
tokenRequestToTokenTypeEntry,
1315
} from '../src/index.js';
@@ -85,6 +87,62 @@ describe.each(vectors)('PublicVerifiable-Vector-%#', (v: Vectors) => {
8587
});
8688
});
8789

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 = input.toString();
109+
if (url === `https://${tokChl.issuerName}/.well-known/private-token-issuer-directory`) {
110+
return new Response(
111+
JSON.stringify({
112+
'issuer-request-uri': `https://${tokChl.issuerName}/issuer`,
113+
'token-keys': [],
114+
}),
115+
{ status: 200 },
116+
);
117+
}
118+
119+
if (
120+
url !== `https://${tokChl.issuerName}/issuer` ||
121+
!(init?.body instanceof Uint8Array)
122+
) {
123+
return new Response(null, { status: 404 });
124+
}
125+
126+
const tokenRequest = TokenRequest.deserialize(TOKEN_TYPES.BLIND_RSA, init.body);
127+
const tokenResponse = await issuer.issue(tokenRequest);
128+
return new Response(tokenResponse.serialize(), {
129+
status: 200,
130+
headers: { 'Content-Type': 'application/private-token-response' },
131+
});
132+
}),
133+
);
134+
135+
const challengeHeader = new WWWAuthenticateHeader(tokChl, publicKeyEnc).toString();
136+
const authHeader = await header_to_token(challengeHeader);
137+
138+
expect(authHeader).not.toBeNull();
139+
if (authHeader === null) {
140+
return;
141+
}
142+
expect(authHeader.startsWith('PrivateToken token=')).toBe(true);
143+
expect(AuthorizationHeader.parse(TOKEN_TYPES.BLIND_RSA, authHeader)).toHaveLength(1);
144+
});
145+
88146
describe('getPublicKeyBytes', () => {
89147
const modes = [BlindRSAMode.PSS, BlindRSAMode.PSSZero];
90148

0 commit comments

Comments
 (0)