File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ This is a Node library for generating and validating Common Access Tokens (CTA-5
3232| Audience (` aud ` ) | Yes |
3333| Expiration (` exp ` ) | Yes |
3434| Not Before (` nbf ` ) | Yes |
35- | CWT ID (` cti ` ) | No |
35+ | CWT ID (` cti ` ) | Yes |
3636| Common Access Token Replay (` catreplay ` ) | No |
3737| Common Access Token Probability of Rejection (` catpor ` ) | No |
3838| Common Access Token Version (` catv ` ) | No |
@@ -191,13 +191,13 @@ const base64encoded = await generator.generate(
191191 aud: ' coap://light.example.com' ,
192192 exp: 1444064944 ,
193193 nbf: 1443944944 ,
194- iat: 1443944944 ,
195- cti: ' 0b71'
194+ iat: 1443944944
196195 },
197196 {
198197 type: ' mac' ,
199198 alg: ' HS256' ,
200- kid: ' Symmetric256'
199+ kid: ' Symmetric256' ,
200+ generateCwtId: true // automatically generate a random CWT Id (cti) claim (default: false)
201201 }
202202);
203203```
Original file line number Diff line number Diff line change @@ -305,4 +305,42 @@ describe('CAT claims', () => {
305305 } )
306306 ) . rejects . toThrow ( UriNotAllowedError ) ;
307307 } ) ;
308+
309+ test ( 'can provide CWT Id claim' , async ( ) => {
310+ const base64encoded = await generator . generate (
311+ {
312+ iss : 'eyevinn' ,
313+ cti : 'a47019af6305d3652a918ae356cc2ca2'
314+ } ,
315+ {
316+ type : 'mac' ,
317+ alg : 'HS256' ,
318+ kid : 'Symmetric256'
319+ }
320+ ) ;
321+ const cat = await validator . validate ( base64encoded ! , 'mac' , {
322+ issuer : 'eyevinn'
323+ } ) ;
324+ expect ( cat ) . toBeDefined ( ) ;
325+ expect ( cat ! . claims . cti ) . toEqual ( 'a47019af6305d3652a918ae356cc2ca2' ) ;
326+ } ) ;
327+
328+ test ( 'can auto generate a CWT Id claim' , async ( ) => {
329+ const base64encoded = await generator . generate (
330+ {
331+ iss : 'eyevinn'
332+ } ,
333+ {
334+ type : 'mac' ,
335+ alg : 'HS256' ,
336+ kid : 'Symmetric256' ,
337+ generateCwtId : true
338+ }
339+ ) ;
340+ const cat = await validator . validate ( base64encoded ! , 'mac' , {
341+ issuer : 'eyevinn'
342+ } ) ;
343+ expect ( cat ) . toBeDefined ( ) ;
344+ expect ( cat ! . claims . cti ) . toBeDefined ( ) ;
345+ } ) ;
308346} ) ;
Original file line number Diff line number Diff line change 1+ import crypto from 'crypto' ;
12import { CommonAccessToken , CommonAccessTokenFactory } from './cat' ;
23import { KeyNotFoundError } from './errors' ;
34
@@ -17,6 +18,7 @@ export interface CatGenerateOptions {
1718 type : CatValidationTypes ;
1819 alg : string ;
1920 kid : string ;
21+ generateCwtId ?: boolean ;
2022}
2123
2224/**
@@ -123,6 +125,9 @@ export class CAT {
123125 claims : { [ key : string ] : string | number | Map < number , any > } ,
124126 opts ?: CatGenerateOptions
125127 ) {
128+ if ( opts ?. generateCwtId ) {
129+ claims [ 'cti' ] = crypto . randomBytes ( 16 ) . toString ( 'hex' ) ;
130+ }
126131 const cat = new CommonAccessToken ( claims ) ;
127132 if ( opts && opts . type == 'mac' ) {
128133 const key = this . keys [ opts . kid ] ;
You can’t perform that action at this time.
0 commit comments