Skip to content

Commit 8e72c09

Browse files
committed
test header_to_token output
1 parent 7dc915c commit 8e72c09

2 files changed

Lines changed: 59 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 } from './auth_scheme/private_token.js';
65
import {
76
Client as PublicVerifClient,

test/pub_verif_token.test.ts

Lines changed: 59 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,63 @@ 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 =
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+
88147
describe('getPublicKeyBytes', () => {
89148
const modes = [BlindRSAMode.PSS, BlindRSAMode.PSSZero];
90149

0 commit comments

Comments
 (0)