Skip to content

Commit 8d9d850

Browse files
committed
Remove the now redundant tokenType in OptionalTokenRequest
1 parent 84ae5c7 commit 8d9d850

4 files changed

Lines changed: 30 additions & 28 deletions

File tree

src/auth_scheme/private_token.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ import { joinAll } from '../util.js';
3232
const MAX_UINT16 = (1 << 16) - 1;
3333
export const AUTH_SCHEME_NAME = 'PrivateToken';
3434

35+
export type TokenTypeValue = 0x0001 | 0x0002 | 0xda7a;
36+
3537
// https://datatracker.ietf.org/doc/html/draft-ietf-privacypass-auth-scheme-14#name-token-type-registry
3638
export interface TokenTypeEntry {
37-
value: number;
39+
value: TokenTypeValue;
3840
name: string;
3941
publicVerifiable: boolean;
4042
publicMetadata: boolean;
@@ -170,7 +172,7 @@ export class AuthenticatorInput {
170172

171173
constructor(
172174
tokenTypeEntry: TokenTypeEntry,
173-
public readonly tokenType: number,
175+
public readonly tokenType: TokenTypeValue,
174176
public readonly nonce: Uint8Array,
175177
public readonly challengeDigest: Uint8Array,
176178
public readonly tokenKeyId: Uint8Array,
@@ -203,6 +205,9 @@ export class AuthenticatorInput {
203205
const input = new DataView(bytes.buffer);
204206

205207
const type = input.getUint16(offset);
208+
if (type !== 0x0001 && type !== 0x0002 && type !== 0xda7a) {
209+
throw new Error(`invalid token type: ${type}`);
210+
}
206211
offset += 2;
207212

208213
let len = AuthenticatorInput.NONCE_LENGTH;

src/generic_batched_token.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@
33

44
import * as varint from 'quicvarint';
55

6-
import type { privateVerif, publicVerif } from './index.js';
6+
import { publicVerif, privateVerif, type TokenTypeValue } from './index.js';
77
import {
88
type Token,
99
TOKEN_TYPES,
1010
tokenEntryToSerializedLength,
1111
tokenRequestToTokenTypeEntry,
1212
} from './index.js';
13-
import {
14-
Issuer as Type1Issuer,
15-
TokenRequest as Type1TokenRequest,
16-
TokenResponse as Type1TokenResponse,
17-
} from './priv_verif_token.js';
18-
import {
19-
TokenResponse as Type2TokenResponse,
20-
Issuer as Type2Issuer,
21-
TokenRequest as Type2TokenRequest,
22-
} from './pub_verif_token.js';
13+
import { Issuer as Type1Issuer, TokenRequest as Type1TokenRequest } from './priv_verif_token.js';
14+
import { Issuer as Type2Issuer, TokenRequest as Type2TokenRequest } from './pub_verif_token.js';
2315
import { joinAll } from './util.js';
2416

2517
const TokenStatus = {
@@ -60,7 +52,7 @@ export class TokenRequest {
6052
return this.tokenRequest.serialize();
6153
}
6254

63-
get tokenType(): number {
55+
get tokenType(): TokenTypeValue {
6456
return this.tokenRequest.tokenType;
6557
}
6658

@@ -151,7 +143,6 @@ export class OptionalTokenResponse {
151143
// }
152144
// } OptionalTokenResponse;
153145
constructor(
154-
public readonly tokenType: 1 | 2,
155146
public readonly tokenResponse:
156147
| null
157148
| publicVerif.TokenResponse
@@ -166,7 +157,7 @@ export class OptionalTokenResponse {
166157
case TokenStatus.ABSENT:
167158
// For absent responses, we still need a token type but it doesn't matter
168159
// We use 1 as a placeholder since the response is null
169-
return new OptionalTokenResponse(1, null);
160+
return new OptionalTokenResponse(null);
170161
case TokenStatus.PRESENT: {
171162
if (bytes.length < 3) {
172163
throw new Error('OptionalTokenResponse PRESENT requires at least 3 bytes');
@@ -182,9 +173,9 @@ export class OptionalTokenResponse {
182173
const responseBytes = bytes.slice(3);
183174
const response =
184175
tokenType === TOKEN_TYPES.VOPRF.value
185-
? Type1TokenResponse.deserialize(responseBytes)
186-
: Type2TokenResponse.deserialize(responseBytes);
187-
return new OptionalTokenResponse(tokenType as 1 | 2, response);
176+
? privateVerif.TokenResponse.deserialize(responseBytes)
177+
: publicVerif.TokenResponse.deserialize(responseBytes);
178+
return new OptionalTokenResponse(response);
188179
}
189180
default:
190181
throw new Error('OptionalTokenResponse MUST start with either 0x00 or 0x01');
@@ -199,8 +190,8 @@ export class OptionalTokenResponse {
199190
// Format: [present:1][token_type:2 big-endian][response_data]
200191
const result = new Uint8Array(1 + 2 + serialized.length);
201192
result[0] = TokenStatus.PRESENT;
202-
result[1] = (this.tokenType >> 8) & 0xff;
203-
result[2] = this.tokenType & 0xff;
193+
result[1] = (this.tokenResponse.tokenType >> 8) & 0xff;
194+
result[2] = this.tokenResponse.tokenType & 0xff;
204195
result.set(serialized, 3);
205196
return result;
206197
}
@@ -294,7 +285,7 @@ export class Issuer {
294285
}
295286

296287
private async issuer(
297-
tokenType: number,
288+
tokenType: TokenTypeValue,
298289
truncatedTokenKeyId: number,
299290
): Promise<Type1Issuer | Type2Issuer> {
300291
if (![TOKEN_TYPES.VOPRF.value, TOKEN_TYPES.BLIND_RSA.value].includes(tokenType)) {
@@ -321,11 +312,11 @@ export class Issuer {
321312
try {
322313
const issuer = await this.issuer(tokenType, tokenRequest.truncatedTokenKeyId);
323314
const response = await issuer.issue(tokenRequest.tokenRequest);
324-
tokenResponses.push(new OptionalTokenResponse(tokenType, response));
315+
tokenResponses.push(new OptionalTokenResponse(response));
325316
// eslint-disable-next-line @typescript-eslint/no-unused-vars
326317
} catch (e) {
327318
console.log(e);
328-
tokenResponses.push(new OptionalTokenResponse(tokenType, null));
319+
tokenResponses.push(new OptionalTokenResponse(null));
329320
}
330321
}
331322

src/priv_verif_token.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import {
2020
Token,
2121
TokenChallenge,
2222
type TokenTypeEntry,
23+
type TokenTypeValue,
2324
} from './auth_scheme/private_token.js';
2425
import { joinAll } from './util.js';
26+
import { TOKEN_TYPES } from './index.js';
2527

2628
export interface VOPRFExtraParams {
2729
suite: SuiteID;
@@ -79,16 +81,14 @@ export class TokenRequest {
7981
// uint8_t blinded_msg[Ne];
8082
// } TokenRequest;
8183

82-
tokenType: number;
84+
public readonly tokenType: TokenTypeValue = VOPRF.value;
8385
constructor(
8486
public readonly truncatedTokenKeyId: number,
8587
public readonly blindedMsg: Uint8Array,
8688
) {
8789
if (blindedMsg.length !== VOPRF.Ne) {
8890
throw new Error('blinded message has invalid size');
8991
}
90-
91-
this.tokenType = VOPRF.value;
9292
}
9393

9494
static deserialize(bytes: Uint8Array): TokenRequest {
@@ -139,6 +139,8 @@ export class TokenResponse {
139139
// uint8_t evaluate_proof[Ns+Ns];
140140
// } TokenResponse;
141141

142+
public readonly tokenType: number = TOKEN_TYPES.VOPRF.value;
143+
142144
constructor(
143145
public readonly evaluateMsg: Uint8Array,
144146
public readonly evaluateProof: Uint8Array,

src/pub_verif_token.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Token,
1717
TokenChallenge,
1818
type TokenTypeEntry,
19+
type TokenTypeValue,
1920
} from './auth_scheme/private_token.js';
2021

2122
export enum BlindRSAMode {
@@ -24,6 +25,7 @@ export enum BlindRSAMode {
2425
}
2526

2627
export import PartiallyBlindRSAMode = BlindRSAMode;
28+
import { TOKEN_TYPES } from './index.js';
2729

2830
export interface BlindRSAExtraParams {
2931
suite: Record<BlindRSAMode, (params?: BlindRSAPlatformParams) => BlindRSA>;
@@ -111,7 +113,7 @@ export class TokenRequest {
111113
// uint8_t blinded_msg[Nk];
112114
// } TokenRequest;
113115

114-
tokenType: number;
116+
public readonly tokenType: TokenTypeValue;
115117
constructor(
116118
public readonly truncatedTokenKeyId: number,
117119
public readonly blindedMsg: Uint8Array,
@@ -211,6 +213,8 @@ export class TokenResponse {
211213
// uint8_t blind_sig[Nk];
212214
// } TokenResponse;
213215

216+
public readonly tokenType: number = TOKEN_TYPES.BLIND_RSA.value;
217+
214218
constructor(public readonly blindSig: Uint8Array) {
215219
if (blindSig.length !== BLIND_RSA.Nk) {
216220
throw new Error(`blind signature has invalid size: ${blindSig.length}`);

0 commit comments

Comments
 (0)