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
2 changes: 1 addition & 1 deletion examples/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function main() {
const result = await parser.validate(process.argv[2], 'mac', {
issuer: 'eyevinn'
});
console.log(result);
console.dir(result, { depth: null });
}

main();
32 changes: 32 additions & 0 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');
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 Down Expand Up @@ -183,6 +183,38 @@
});
});

test('can create a CAT object from a dict with tags', async () => {
const cat = new CommonAccessToken({
1: 'eyevinn',
2: 'jonas',
3: 'coap://light.example.com',
4: 1444064944,
5: 1443944944,
6: 1443944944,
323: {
0: 1,
1: 60,
2: 10,
5: ['Secure', 'HttpOnly', 'Domain=.streaming.a2d.tv']
} as any

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

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
});
expect(cat.claims).toEqual({
iss: 'eyevinn',
sub: 'jonas',
aud: 'coap://light.example.com',
exp: 1444064944,
nbf: 1443944944,
iat: 1443944944,
catr: {
type: 'cookie',
expadd: 60,
deadline: 10,
'cookie-params': ['Secure', 'HttpOnly', 'Domain=.streaming.a2d.tv']
},
catv: 1
});
});

test('can determine whether the token should be renewed', async () => {
const now = Math.floor(Date.now() / 1000);
const cat = new CommonAccessToken({
Expand Down
14 changes: 10 additions & 4 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 @@ -208,28 +208,34 @@
for (const param in dict) {
const key = claimsToLabels[param] ? claimsToLabels[param] : parseInt(param);
if (key === claimsToLabels['catu'] && !(dict[param] instanceof Map)) {
map.set(key, CommonAccessTokenUri.fromDict(dict[param] as any).payload);
map.set(
key,
CommonAccessTokenUri.fromDictTags(dict[param] as any).payload

Check warning on line 213 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
CommonAccessTokenRenewal.fromDictTags(dict[param] as any).payload

Check warning on line 221 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
CommonAccessTokenHeader.fromDictTags(dict[param] as any).payload
);
} else if (
key === claimsToLabels['catif'] &&
!(dict[param] instanceof Map)
) {
map.set(key, CommonAccessTokenIf.fromDict(dict[param] as any).payload);
map.set(
key,
CommonAccessTokenIf.fromDictTags(dict[param] as any).payload
);
} else {
const value = claimTransform[param]
? claimTransform[param](dict[param] as string)
Expand Down
13 changes: 13 additions & 0 deletions src/cath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ export type CommonAccessTokenHeaderMap = Map<string, MatchMap>;
export class CommonAccessTokenHeader {
private cathMap: CommonAccessTokenHeaderMap = new Map();

public static fromDictTags(dict: { [key: number]: any }) {
const newDict: { [key: string]: any } = {};
for (const headerTag in dict) {
const matchDict: { [key: string]: any } = {};
for (const matchTag in dict[headerTag]) {
const tag = parseInt(matchTag);
matchDict[labelsToMatch[tag]] = dict[headerTag][matchTag];
}
newDict[labelsToMatch[parseInt(headerTag)]] = matchDict;
}
return CommonAccessTokenHeader.fromDict(newDict);
}

public static fromDict(dict: { [key: string]: any }) {
const cath = new CommonAccessTokenHeader();
for (const header in dict) {
Expand Down
9 changes: 9 additions & 0 deletions src/catif.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ export type CatIfDictValue = {
export class CommonAccessTokenIf {
private catIfMap: CommonAccessTokenIfMap = new Map();

public static fromDictTags(dict: { [key: number]: any }) {
const newDict: { [key: string]: any } = {};
for (const key in dict) {
const tag = parseInt(key);
newDict[labelsToClaim[tag]] = dict[key];
}
return CommonAccessTokenIf.fromDict(newDict);
}

public static fromDict(dict: { [key: string]: any }) {
const catif = new CommonAccessTokenIf();
for (const catIfClaim in dict) {
Expand Down
13 changes: 13 additions & 0 deletions src/catr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ export type CommonAccessTokenRenewalMap = Map<number, CatrPartValue>;
export class CommonAccessTokenRenewal {
private catrMap: CommonAccessTokenRenewalMap = new Map();

public static fromDictTags(dict: { [key: number]: any }) {
const newDict: { [key: string]: any } = {};
for (const key in dict) {
const tag = parseInt(key);
if (labelsToCatrPart[tag] === 'type') {
newDict[labelsToCatrPart[tag]] = labelsToCatrRenewalType[dict[key]];
} else {
newDict[labelsToCatrPart[tag]] = dict[key];
}
}
return CommonAccessTokenRenewal.fromDict(newDict);
}

public static fromDict(dict: { [key: string]: any }) {
const catr = new CommonAccessTokenRenewal();
for (const catrPart in dict) {
Expand Down
13 changes: 13 additions & 0 deletions src/catu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ export type CommonAccessTokenUriMap = Map<number, UriPartMap>;
export class CommonAccessTokenUri {
private catuMap: CommonAccessTokenUriMap = new Map();

public static fromDictTags(dict: { [key: number]: any }) {
const newDict: { [key: string]: any } = {};
for (const uriPartTag in dict) {
const matchDict: { [key: string]: any } = {};
for (const matchTag in dict[uriPartTag]) {
const tag = parseInt(matchTag);
matchDict[labelsToMatch[tag]] = dict[uriPartTag][matchTag];
}
newDict[labelsToUriPart[parseInt(uriPartTag)]] = matchDict;
}
return CommonAccessTokenUri.fromDict(newDict);
}

public static fromDict(dict: { [key: string]: any }) {
const catu = new CommonAccessTokenUri();
for (const uriPart in dict) {
Expand Down
Loading