Skip to content

Commit 240e079

Browse files
authored
fix: convert to base64 and hex without Buffer methods (#25)
1 parent 363a2f0 commit 240e079

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

examples/parse.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { CAT } from '../src';
2+
3+
async function main() {
4+
const parser = new CAT({
5+
keys: {
6+
Symmetric256: Buffer.from(
7+
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
8+
'hex'
9+
)
10+
}
11+
});
12+
const result = await parser.validate(process.argv[2], 'mac', {
13+
issuer: 'eyevinn'
14+
});
15+
console.log(result);
16+
}
17+
18+
main();

src/cat.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { CommonAccessTokenUri } from './catu';
1515
import { CommonAccessTokenRenewal } from './catr';
1616
import { CommonAccessTokenHeader } from './cath';
1717
import { CommonAccessTokenIf } from './catif';
18+
import { toBase64, toHex } from './util';
1819

1920
export const claimsToLabels: { [key: string]: number } = {
2021
iss: 1, // 3
@@ -76,8 +77,8 @@ const claimTransform: { [key: string]: (value: string) => Buffer } = {
7677
};
7778

7879
const claimTransformReverse: { [key: string]: (value: Buffer) => string } = {
79-
cti: (value: Buffer) => value.toString('hex'),
80-
cattpk: (value: Buffer) => value.toString('hex')
80+
cti: (value: Buffer) => toHex(value),
81+
cattpk: (value: Buffer) => toHex(value)
8182
};
8283

8384
const claimTypeValidators: {
@@ -492,7 +493,7 @@ export class CommonAccessToken {
492493
}
493494

494495
get base64() {
495-
return this.data?.toString('base64');
496+
return this.data ? toBase64(this.data) : undefined;
496497
}
497498

498499
get keyId() {

src/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
CommonAccessTokenFactory
66
} from './cat';
77
import { KeyNotFoundError } from './errors';
8+
import { generateRandomHex, toBase64 } from './util';
89

910
export { CommonAccessToken } from './cat';
1011
export { CommonAccessTokenRenewal } from './catr';
@@ -225,7 +226,7 @@ export class CAT {
225226
opts?: CatGenerateOptions
226227
) {
227228
if (opts?.generateCwtId) {
228-
claims['cti'] = crypto.randomBytes(16).toString('hex');
229+
claims['cti'] = generateRandomHex(16);
229230
}
230231
const cat = new CommonAccessToken(claims);
231232
if (opts && opts.type == 'mac') {
@@ -239,7 +240,7 @@ export class CAT {
239240
if (!cat.raw) {
240241
throw new Error('Failed to MAC token');
241242
}
242-
return cat.raw.toString('base64');
243+
return toBase64(cat.raw);
243244
}
244245
}
245246

@@ -281,7 +282,7 @@ export class CAT {
281282
opts?: CatGenerateOptions
282283
) {
283284
if (opts?.generateCwtId) {
284-
dict['cti'] = crypto.randomBytes(16).toString('hex');
285+
dict['cti'] = generateRandomHex(16);
285286
}
286287
const cat = CommonAccessTokenFactory.fromDict(dict);
287288
if (opts && opts.type == 'mac') {
@@ -295,7 +296,7 @@ export class CAT {
295296
if (!cat.raw) {
296297
throw new Error('Failed to MAC token');
297298
}
298-
return cat.raw.toString('base64');
299+
return toBase64(cat.raw);
299300
}
300301
}
301302

@@ -313,7 +314,7 @@ export class CAT {
313314
opts: CatRenewOptions
314315
): Promise<string> {
315316
const newClaims = cat.claims;
316-
newClaims['cti'] = crypto.randomBytes(16).toString('hex');
317+
newClaims['cti'] = generateRandomHex(16);
317318
newClaims['iat'] = Math.floor(Date.now() / 1000);
318319
newClaims['iss'] = opts.issuer;
319320
newClaims['exp'] = newClaims['iat'] + (newClaims['catr'] as any)['expadd'];
@@ -329,6 +330,6 @@ export class CAT {
329330
if (!newCat.raw) {
330331
throw new Error('Failed to MAC token');
331332
}
332-
return newCat.raw.toString('base64');
333+
return toBase64(newCat.raw);
333334
}
334335
}

src/util.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import crypto from 'crypto';
2+
3+
/**
4+
* Generate a random hex string of specified length
5+
* @param bytes Number of random bytes to generate
6+
* @returns Hex string
7+
*/
8+
export function generateRandomHex(bytes: number): string {
9+
const randomBytes = new Uint8Array(bytes);
10+
crypto.getRandomValues(randomBytes);
11+
return Array.from(randomBytes)
12+
.map((byte) => byte.toString(16).padStart(2, '0'))
13+
.join('');
14+
}
15+
16+
export function toBase64(input: Buffer): string {
17+
const bytes = new Uint8Array(input);
18+
return btoa(String.fromCharCode(...bytes));
19+
}
20+
21+
export function toHex(input: Buffer): string {
22+
const bytes = new Uint8Array(input);
23+
return Array.from(bytes)
24+
.map((byte) => byte.toString(16).padStart(2, '0'))
25+
.join('');
26+
}

0 commit comments

Comments
 (0)