Skip to content

Commit 2b36a6f

Browse files
authored
fix: use mapsAsObjects=false on all encode/decode (#44)
1 parent b4d83ac commit 2b36a6f

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/cat.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ export class CommonAccessToken {
331331
const recipient = {
332332
key: key.k
333333
};
334+
const encoder = new cbor.Encoder({ mapsAsObjects: false });
334335
if (!opts?.noCwtTag) {
335-
const encoder = new cbor.Encoder({ mapsAsObjects: false });
336336
const plaintext = encoder.encode(this.payload);
337337
const coseMessage = await cose.mac.create(
338338
headers,
@@ -346,7 +346,7 @@ export class CommonAccessToken {
346346
Log(cwtTag, { depth: null });
347347
this.data = encoder.encode(cwtTag);
348348
} else {
349-
const plaintext = cbor.encode(this.payload).toString('hex');
349+
const plaintext = encoder.encode(this.payload).toString('hex');
350350
this.data = await cose.mac.create(headers, plaintext, recipient);
351351
}
352352
this.kid = key.kid;
@@ -369,7 +369,8 @@ export class CommonAccessToken {
369369
throw new Error('Expected CWT tag');
370370
}
371371
if (coseMessage.tag === CWT_TAG) {
372-
const cborCoseMessage = cbor.encode(coseMessage.value);
372+
const encoder = new cbor.Encoder({ mapsAsObjects: false });
373+
const cborCoseMessage = encoder.encode(coseMessage.value);
373374
Log({
374375
kid: key.kid,
375376
key: key.k.toString('hex')
@@ -387,7 +388,8 @@ export class CommonAccessToken {
387388
}
388389

389390
public async sign(key: CWTSigningKey, alg: string): Promise<void> {
390-
const plaintext = cbor.encode(this.payload).toString('hex');
391+
const encoder = new cbor.Encoder({ mapsAsObjects: false });
392+
const plaintext = encoder.encode(this.payload).toString('hex');
391393
const headers = {
392394
p: { alg: alg },
393395
u: { kid: key.kid }
@@ -403,7 +405,10 @@ export class CommonAccessToken {
403405
key: CWTVerifierKey
404406
): Promise<CommonAccessToken> {
405407
const buf = await cose.sign.verify(token, { key: key });
406-
this.payload = await cbor.decode(Buffer.from(buf.toString('hex'), 'hex'));
408+
const decoder = new cbor.Decoder({ mapsAsObjects: false });
409+
this.payload = await decoder.decode(
410+
Buffer.from(buf.toString('hex'), 'hex')
411+
);
407412
return this;
408413
}
409414

0 commit comments

Comments
 (0)