Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { CAT } from '../src';

async function main() {
const parser = new CAT({
keys: {
Symmetric256: Buffer.from(
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
'hex'
)
}
});
const result = await parser.validate(process.argv[2], 'mac', {
issuer: 'eyevinn'
});
console.log(result);
}

main();
7 changes: 4 additions & 3 deletions src/cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import { CommonAccessTokenRenewal } from './catr';
import { CommonAccessTokenHeader } from './cath';
import { CommonAccessTokenIf } from './catif';
import { toBase64, toHex } from './util';

export const claimsToLabels: { [key: string]: number } = {
iss: 1, // 3
Expand Down Expand Up @@ -76,8 +77,8 @@
};

const claimTransformReverse: { [key: string]: (value: Buffer) => string } = {
cti: (value: Buffer) => value.toString('hex'),
cattpk: (value: Buffer) => value.toString('hex')
cti: (value: Buffer) => toHex(value),
cattpk: (value: Buffer) => toHex(value)
};

const claimTypeValidators: {
Expand Down Expand Up @@ -146,16 +147,16 @@
* Common Access Token Claims
*/
export type CommonAccessTokenClaims = {
[key: string]: string | number | Map<number | string, any>;

Check warning on line 150 in src/cat.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
};
export type CommonAccessTokenDict = {
[key: string]: string | number | { [key: string]: any };

Check warning on line 153 in src/cat.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
};
export type CommonAccessTokenValue =
| string
| number
| Buffer
| Map<number | string, any>;

Check warning on line 159 in src/cat.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

/**
* CWT Encryption Key
Expand Down Expand Up @@ -228,15 +229,15 @@
const key = claimsToLabels[param];
if (param === 'catu') {
claims[key] = CommonAccessTokenUri.fromDict(
dict[param] as { [key: string]: any }

Check warning on line 232 in src/cat.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
).payload;
} else if (param === 'catr') {
claims[key] = CommonAccessTokenRenewal.fromDict(
dict[param] as { [key: string]: any }

Check warning on line 236 in src/cat.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
).payload;
} else if (param == 'cath') {
claims[key] = CommonAccessTokenHeader.fromDict(
dict[param] as { [key: string]: any }

Check warning on line 240 in src/cat.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
).payload;
} else if (param == 'catif') {
claims[key] = CommonAccessTokenIf.fromDict(
Expand Down Expand Up @@ -492,7 +493,7 @@
}

get base64() {
return this.data?.toString('base64');
return this.data ? toBase64(this.data) : undefined;
}

get keyId() {
Expand Down
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CommonAccessTokenFactory
} from './cat';
import { KeyNotFoundError } from './errors';
import { generateRandomHex, toBase64 } from './util';

export { CommonAccessToken } from './cat';
export { CommonAccessTokenRenewal } from './catr';
Expand Down Expand Up @@ -225,7 +226,7 @@ export class CAT {
opts?: CatGenerateOptions
) {
if (opts?.generateCwtId) {
claims['cti'] = crypto.randomBytes(16).toString('hex');
claims['cti'] = generateRandomHex(16);
}
const cat = new CommonAccessToken(claims);
if (opts && opts.type == 'mac') {
Expand All @@ -239,7 +240,7 @@ export class CAT {
if (!cat.raw) {
throw new Error('Failed to MAC token');
}
return cat.raw.toString('base64');
return toBase64(cat.raw);
}
}

Expand Down Expand Up @@ -281,7 +282,7 @@ export class CAT {
opts?: CatGenerateOptions
) {
if (opts?.generateCwtId) {
dict['cti'] = crypto.randomBytes(16).toString('hex');
dict['cti'] = generateRandomHex(16);
}
const cat = CommonAccessTokenFactory.fromDict(dict);
if (opts && opts.type == 'mac') {
Expand All @@ -295,7 +296,7 @@ export class CAT {
if (!cat.raw) {
throw new Error('Failed to MAC token');
}
return cat.raw.toString('base64');
return toBase64(cat.raw);
}
}

Expand All @@ -313,7 +314,7 @@ export class CAT {
opts: CatRenewOptions
): Promise<string> {
const newClaims = cat.claims;
newClaims['cti'] = crypto.randomBytes(16).toString('hex');
newClaims['cti'] = generateRandomHex(16);
newClaims['iat'] = Math.floor(Date.now() / 1000);
newClaims['iss'] = opts.issuer;
newClaims['exp'] = newClaims['iat'] + (newClaims['catr'] as any)['expadd'];
Expand All @@ -329,6 +330,6 @@ export class CAT {
if (!newCat.raw) {
throw new Error('Failed to MAC token');
}
return newCat.raw.toString('base64');
return toBase64(newCat.raw);
}
}
26 changes: 26 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import crypto from 'crypto';

/**
* Generate a random hex string of specified length
* @param bytes Number of random bytes to generate
* @returns Hex string
*/
export function generateRandomHex(bytes: number): string {
const randomBytes = new Uint8Array(bytes);
crypto.getRandomValues(randomBytes);
return Array.from(randomBytes)
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('');
}

export function toBase64(input: Buffer): string {
const bytes = new Uint8Array(input);
return btoa(String.fromCharCode(...bytes));
}

export function toHex(input: Buffer): string {
const bytes = new Uint8Array(input);
return Array.from(bytes)
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('');
}
Loading