Skip to content

Commit 4cbff63

Browse files
authored
fix: handle tokens where claims of type map is not tagged (#29)
1 parent 40908f2 commit 4cbff63

File tree

7 files changed

+91
-5
lines changed

7 files changed

+91
-5
lines changed

examples/parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async function main() {
1212
const result = await parser.validate(process.argv[2], 'mac', {
1313
issuer: 'eyevinn'
1414
});
15-
console.log(result);
15+
console.dir(result, { depth: null });
1616
}
1717

1818
main();

src/cat.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,38 @@ describe('CAT', () => {
183183
});
184184
});
185185

186+
test('can create a CAT object from a dict with tags', async () => {
187+
const cat = new CommonAccessToken({
188+
1: 'eyevinn',
189+
2: 'jonas',
190+
3: 'coap://light.example.com',
191+
4: 1444064944,
192+
5: 1443944944,
193+
6: 1443944944,
194+
323: {
195+
0: 1,
196+
1: 60,
197+
2: 10,
198+
5: ['Secure', 'HttpOnly', 'Domain=.streaming.a2d.tv']
199+
} as any
200+
});
201+
expect(cat.claims).toEqual({
202+
iss: 'eyevinn',
203+
sub: 'jonas',
204+
aud: 'coap://light.example.com',
205+
exp: 1444064944,
206+
nbf: 1443944944,
207+
iat: 1443944944,
208+
catr: {
209+
type: 'cookie',
210+
expadd: 60,
211+
deadline: 10,
212+
'cookie-params': ['Secure', 'HttpOnly', 'Domain=.streaming.a2d.tv']
213+
},
214+
catv: 1
215+
});
216+
});
217+
186218
test('can determine whether the token should be renewed', async () => {
187219
const now = Math.floor(Date.now() / 1000);
188220
const cat = new CommonAccessToken({

src/cat.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,28 +208,34 @@ function updateMapFromClaims(
208208
for (const param in dict) {
209209
const key = claimsToLabels[param] ? claimsToLabels[param] : parseInt(param);
210210
if (key === claimsToLabels['catu'] && !(dict[param] instanceof Map)) {
211-
map.set(key, CommonAccessTokenUri.fromDict(dict[param] as any).payload);
211+
map.set(
212+
key,
213+
CommonAccessTokenUri.fromDictTags(dict[param] as any).payload
214+
);
212215
} else if (
213216
key === claimsToLabels['catr'] &&
214217
!(dict[param] instanceof Map)
215218
) {
216219
map.set(
217220
key,
218-
CommonAccessTokenRenewal.fromDict(dict[param] as any).payload
221+
CommonAccessTokenRenewal.fromDictTags(dict[param] as any).payload
219222
);
220223
} else if (
221224
key === claimsToLabels['cath'] &&
222225
!(dict[param] instanceof Map)
223226
) {
224227
map.set(
225228
key,
226-
CommonAccessTokenHeader.fromDict(dict[param] as any).payload
229+
CommonAccessTokenHeader.fromDictTags(dict[param] as any).payload
227230
);
228231
} else if (
229232
key === claimsToLabels['catif'] &&
230233
!(dict[param] instanceof Map)
231234
) {
232-
map.set(key, CommonAccessTokenIf.fromDict(dict[param] as any).payload);
235+
map.set(
236+
key,
237+
CommonAccessTokenIf.fromDictTags(dict[param] as any).payload
238+
);
233239
} else {
234240
const value = claimTransform[param]
235241
? claimTransform[param](dict[param] as string)

src/cath.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ export type CommonAccessTokenHeaderMap = Map<string, MatchMap>;
1010
export class CommonAccessTokenHeader {
1111
private cathMap: CommonAccessTokenHeaderMap = new Map();
1212

13+
public static fromDictTags(dict: { [key: number]: any }) {
14+
const newDict: { [key: string]: any } = {};
15+
for (const headerTag in dict) {
16+
const matchDict: { [key: string]: any } = {};
17+
for (const matchTag in dict[headerTag]) {
18+
const tag = parseInt(matchTag);
19+
matchDict[labelsToMatch[tag]] = dict[headerTag][matchTag];
20+
}
21+
newDict[labelsToMatch[parseInt(headerTag)]] = matchDict;
22+
}
23+
return CommonAccessTokenHeader.fromDict(newDict);
24+
}
25+
1326
public static fromDict(dict: { [key: string]: any }) {
1427
const cath = new CommonAccessTokenHeader();
1528
for (const header in dict) {

src/catif.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ export type CatIfDictValue = {
1414
export class CommonAccessTokenIf {
1515
private catIfMap: CommonAccessTokenIfMap = new Map();
1616

17+
public static fromDictTags(dict: { [key: number]: any }) {
18+
const newDict: { [key: string]: any } = {};
19+
for (const key in dict) {
20+
const tag = parseInt(key);
21+
newDict[labelsToClaim[tag]] = dict[key];
22+
}
23+
return CommonAccessTokenIf.fromDict(newDict);
24+
}
25+
1726
public static fromDict(dict: { [key: string]: any }) {
1827
const catif = new CommonAccessTokenIf();
1928
for (const catIfClaim in dict) {

src/catr.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ export type CommonAccessTokenRenewalMap = Map<number, CatrPartValue>;
5151
export class CommonAccessTokenRenewal {
5252
private catrMap: CommonAccessTokenRenewalMap = new Map();
5353

54+
public static fromDictTags(dict: { [key: number]: any }) {
55+
const newDict: { [key: string]: any } = {};
56+
for (const key in dict) {
57+
const tag = parseInt(key);
58+
if (labelsToCatrPart[tag] === 'type') {
59+
newDict[labelsToCatrPart[tag]] = labelsToCatrRenewalType[dict[key]];
60+
} else {
61+
newDict[labelsToCatrPart[tag]] = dict[key];
62+
}
63+
}
64+
return CommonAccessTokenRenewal.fromDict(newDict);
65+
}
66+
5467
public static fromDict(dict: { [key: string]: any }) {
5568
const catr = new CommonAccessTokenRenewal();
5669
for (const catrPart in dict) {

src/catu.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ export type CommonAccessTokenUriMap = Map<number, UriPartMap>;
4848
export class CommonAccessTokenUri {
4949
private catuMap: CommonAccessTokenUriMap = new Map();
5050

51+
public static fromDictTags(dict: { [key: number]: any }) {
52+
const newDict: { [key: string]: any } = {};
53+
for (const uriPartTag in dict) {
54+
const matchDict: { [key: string]: any } = {};
55+
for (const matchTag in dict[uriPartTag]) {
56+
const tag = parseInt(matchTag);
57+
matchDict[labelsToMatch[tag]] = dict[uriPartTag][matchTag];
58+
}
59+
newDict[labelsToUriPart[parseInt(uriPartTag)]] = matchDict;
60+
}
61+
return CommonAccessTokenUri.fromDict(newDict);
62+
}
63+
5164
public static fromDict(dict: { [key: string]: any }) {
5265
const catu = new CommonAccessTokenUri();
5366
for (const uriPart in dict) {

0 commit comments

Comments
 (0)