Skip to content

Commit eae9cda

Browse files
authored
fix: issue with catr and other claims of type map (#27)
* chore: check that catr params can be arrays * fix: catv was incorrectly created * fix: can handle claims of type map
1 parent 00c9d3f commit eae9cda

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

examples/generate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ async function main() {
77
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
88
'hex'
99
)
10-
}
10+
},
11+
expectCwtTag: true
1112
});
1213
const base64encoded = await generator.generate(
1314
{

src/cat.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ describe('CAT', () => {
7474
kid: 'AsymmetricECDSA256'
7575
};
7676
await cat.verify(token, verifyKey);
77-
expect(cat.claims).toEqual(claims);
77+
const claimsWithCatv = { ...claims, catv: 1 };
78+
expect(cat.claims).toEqual(claimsWithCatv);
7879
});
7980

8081
test('can create a CAT object from a signed base64 encoded token', async () => {
@@ -157,7 +158,10 @@ describe('CAT', () => {
157158
cti: '0b71',
158159
catr: {
159160
type: 'header',
160-
'header-name': 'cta-common-access-token'
161+
'header-name': 'cta-common-access-token',
162+
'header-params': ['value1'],
163+
'cookie-name': 'myname',
164+
'cookie-params': ['cookievalue']
161165
}
162166
});
163167
expect(cat.claims).toEqual({
@@ -170,7 +174,10 @@ describe('CAT', () => {
170174
cti: '0b71',
171175
catr: {
172176
type: 'header',
173-
'header-name': 'cta-common-access-token'
177+
'header-name': 'cta-common-access-token',
178+
'header-params': ['value1'],
179+
'cookie-name': 'myname',
180+
'cookie-params': ['cookievalue']
174181
},
175182
catv: 1
176183
});

src/cat.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,35 @@ function updateMapFromClaims(
207207
}
208208
for (const param in dict) {
209209
const key = claimsToLabels[param] ? claimsToLabels[param] : parseInt(param);
210-
const value = claimTransform[param]
211-
? claimTransform[param](dict[param] as string)
212-
: dict[param];
213-
map.set(key, value);
210+
if (key === claimsToLabels['catu'] && !(dict[param] instanceof Map)) {
211+
map.set(key, CommonAccessTokenUri.fromDict(dict[param] as any).payload);
212+
} else if (
213+
key === claimsToLabels['catr'] &&
214+
!(dict[param] instanceof Map)
215+
) {
216+
map.set(
217+
key,
218+
CommonAccessTokenRenewal.fromDict(dict[param] as any).payload
219+
);
220+
} else if (
221+
key === claimsToLabels['cath'] &&
222+
!(dict[param] instanceof Map)
223+
) {
224+
map.set(
225+
key,
226+
CommonAccessTokenHeader.fromDict(dict[param] as any).payload
227+
);
228+
} else if (
229+
key === claimsToLabels['catif'] &&
230+
!(dict[param] instanceof Map)
231+
) {
232+
map.set(key, CommonAccessTokenIf.fromDict(dict[param] as any).payload);
233+
} else {
234+
const value = claimTransform[param]
235+
? claimTransform[param](dict[param] as string)
236+
: dict[param];
237+
map.set(key, value);
238+
}
214239
}
215240
return map;
216241
}
@@ -262,10 +287,10 @@ export class CommonAccessToken {
262287
private kid?: string;
263288

264289
constructor(claims: CommonAccessTokenClaims) {
265-
if (!claims['catv']) {
266-
claims['catv'] = 1;
267-
}
268290
this.payload = updateMapFromClaims(claims);
291+
if (!this.payload.has(claimsToLabels['catv'])) {
292+
this.payload.set(claimsToLabels['catv'], 1);
293+
}
269294
this.validateTypes();
270295
}
271296

src/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,11 @@ describe('CAT', () => {
192192
)
193193
}
194194
});
195+
const exp = Math.floor(Date.now() / 1000) + 60;
195196
const base64encoded = await generator.generateFromJson(
196197
{
197198
iss: 'eyevinn',
198-
exp: 1742984408,
199+
exp,
199200
iat: 1742980808,
200201
cti: '66400ca63ab2c267cc0d874cc5f9a378',
201202
catv: 1
@@ -206,7 +207,6 @@ describe('CAT', () => {
206207
kid: 'Symmetric256'
207208
}
208209
);
209-
console.log(base64encoded);
210210
expect(base64encoded).not.toContain('=');
211211
const validator = new CAT({
212212
keys: {
@@ -223,7 +223,7 @@ describe('CAT', () => {
223223
expect(result.cat).toBeDefined();
224224
expect(result.cat!.claims).toEqual({
225225
iss: 'eyevinn',
226-
exp: 1742984408,
226+
exp,
227227
iat: 1742980808,
228228
cti: '66400ca63ab2c267cc0d874cc5f9a378',
229229
catv: 1

0 commit comments

Comments
 (0)