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
3 changes: 2 additions & 1 deletion examples/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ async function main() {
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
'hex'
)
}
},
expectCwtTag: true
});
const base64encoded = await generator.generate(
{
Expand Down
13 changes: 10 additions & 3 deletions src/cat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
await cat.mac(key, 'HS256', { addCwtTag: true });
expect(cat.raw).toBeDefined();
const macHex = cat.raw?.toString('hex');
const token = Buffer.from(macHex!, 'hex');

Check warning on line 38 in src/cat.test.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
const newCat = new CommonAccessToken({});
await newCat.parse(token, key, { expectCwtTag: true });
expect(newCat.claims).toEqual(claims);
Expand All @@ -61,7 +61,7 @@
};
await cat.sign(signKey, 'ES256');
const signedHex = cat.raw?.toString('hex');
const token = Buffer.from(signedHex!, 'hex');

Check warning on line 64 in src/cat.test.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
const verifyKey = {
x: Buffer.from(
'143329cce7868e416927599cf65a34f3ce2ffda55a7eca69ed8919a394d42f0f',
Expand All @@ -74,7 +74,8 @@
kid: 'AsymmetricECDSA256'
};
await cat.verify(token, verifyKey);
expect(cat.claims).toEqual(claims);
const claimsWithCatv = { ...claims, catv: 1 };
expect(cat.claims).toEqual(claimsWithCatv);
});

test('can create a CAT object from a signed base64 encoded token', async () => {
Expand Down Expand Up @@ -157,7 +158,10 @@
cti: '0b71',
catr: {
type: 'header',
'header-name': 'cta-common-access-token'
'header-name': 'cta-common-access-token',
'header-params': ['value1'],
'cookie-name': 'myname',
'cookie-params': ['cookievalue']
}
});
expect(cat.claims).toEqual({
Expand All @@ -170,7 +174,10 @@
cti: '0b71',
catr: {
type: 'header',
'header-name': 'cta-common-access-token'
'header-name': 'cta-common-access-token',
'header-params': ['value1'],
'cookie-name': 'myname',
'cookie-params': ['cookievalue']
},
catv: 1
});
Expand Down
39 changes: 32 additions & 7 deletions src/cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,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 @@ -207,10 +207,35 @@
}
for (const param in dict) {
const key = claimsToLabels[param] ? claimsToLabels[param] : parseInt(param);
const value = claimTransform[param]
? claimTransform[param](dict[param] as string)
: dict[param];
map.set(key, value);
if (key === claimsToLabels['catu'] && !(dict[param] instanceof Map)) {
map.set(key, CommonAccessTokenUri.fromDict(dict[param] as any).payload);

Check warning on line 211 in src/cat.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
} else if (
key === claimsToLabels['catr'] &&
!(dict[param] instanceof Map)
) {
map.set(
key,
CommonAccessTokenRenewal.fromDict(dict[param] as any).payload

Check warning on line 218 in src/cat.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
);
} else if (
key === claimsToLabels['cath'] &&
!(dict[param] instanceof Map)
) {
map.set(
key,
CommonAccessTokenHeader.fromDict(dict[param] as any).payload

Check warning on line 226 in src/cat.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
);
} else if (
key === claimsToLabels['catif'] &&
!(dict[param] instanceof Map)
) {
map.set(key, CommonAccessTokenIf.fromDict(dict[param] as any).payload);
} else {
const value = claimTransform[param]
? claimTransform[param](dict[param] as string)
: dict[param];
map.set(key, value);
}
}
return map;
}
Expand Down Expand Up @@ -262,10 +287,10 @@
private kid?: string;

constructor(claims: CommonAccessTokenClaims) {
if (!claims['catv']) {
claims['catv'] = 1;
}
this.payload = updateMapFromClaims(claims);
if (!this.payload.has(claimsToLabels['catv'])) {
this.payload.set(claimsToLabels['catv'], 1);
}
this.validateTypes();
}

Expand Down
6 changes: 3 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,11 @@ describe('CAT', () => {
)
}
});
const exp = Math.floor(Date.now() / 1000) + 60;
const base64encoded = await generator.generateFromJson(
{
iss: 'eyevinn',
exp: 1742984408,
exp,
iat: 1742980808,
cti: '66400ca63ab2c267cc0d874cc5f9a378',
catv: 1
Expand All @@ -206,7 +207,6 @@ describe('CAT', () => {
kid: 'Symmetric256'
}
);
console.log(base64encoded);
expect(base64encoded).not.toContain('=');
const validator = new CAT({
keys: {
Expand All @@ -223,7 +223,7 @@ describe('CAT', () => {
expect(result.cat).toBeDefined();
expect(result.cat!.claims).toEqual({
iss: 'eyevinn',
exp: 1742984408,
exp,
iat: 1742980808,
cti: '66400ca63ab2c267cc0d874cc5f9a378',
catv: 1
Expand Down
Loading