Skip to content

Commit d5682d4

Browse files
authored
feat: support to generate a token from a JSON object (#14)
* feat: support to generate a token from a JSON object * chore: use generateFromJson in example
1 parent f673658 commit d5682d4

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,20 @@ const generator = new CAT({
208208
)
209209
}
210210
});
211-
const base64encoded = await generator.generate(
211+
const base64encoded = await generator.generateFromJson(
212212
{
213213
iss: 'coap://as.example.com',
214214
sub: 'jonas',
215215
aud: 'coap://light.example.com',
216216
exp: 1444064944,
217217
nbf: 1443944944,
218218
iat: 1443944944,
219-
catr: CommonAccessTokenRenewal.fromDict({
219+
catr: {
220220
type: 'header',
221221
'header-name': 'cta-common-access-token',
222222
expadd: 120,
223223
deadline: 60
224-
}).payload
224+
}
225225
},
226226
{
227227
type: 'mac',

src/index.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,68 @@ describe('CAT', () => {
4848
});
4949
});
5050

51+
test('can generate a token from a JSON object and verify it', async () => {
52+
const generator = new CAT({
53+
keys: {
54+
Symmetric256: Buffer.from(
55+
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
56+
'hex'
57+
)
58+
}
59+
});
60+
const base64encoded = await generator.generateFromJson(
61+
{
62+
iss: 'coap://as.example.com',
63+
exp: Math.floor(Date.now() / 1000) + 60,
64+
catr: {
65+
type: 'header',
66+
'header-name': 'cta-common-access-token',
67+
expadd: 120,
68+
deadline: 60
69+
},
70+
catu: {
71+
scheme: {
72+
'exact-match': 'https'
73+
}
74+
}
75+
},
76+
{
77+
type: 'mac',
78+
alg: 'HS256',
79+
kid: 'Symmetric256'
80+
}
81+
);
82+
const validator = new CAT({
83+
keys: {
84+
Symmetric256: Buffer.from(
85+
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
86+
'hex'
87+
)
88+
}
89+
});
90+
const result = await validator.validate(base64encoded!, 'mac', {
91+
issuer: 'coap://as.example.com',
92+
url: new URL('https://example.com')
93+
});
94+
expect(result.error).not.toBeDefined();
95+
expect(result.cat).toBeDefined();
96+
expect(result.cat!.claims).toEqual({
97+
iss: 'coap://as.example.com',
98+
catr: {
99+
deadline: 60,
100+
expadd: 120,
101+
'header-name': 'cta-common-access-token',
102+
type: 'header'
103+
},
104+
catu: {
105+
scheme: {
106+
'exact-match': 'https'
107+
}
108+
},
109+
exp: expect.any(Number)
110+
});
111+
});
112+
51113
test('can validate a MAC:ed token with standard claims', async () => {
52114
const base64encoded =
53115
'0YRDoQEFoQRMU3ltbWV0cmljMjU2eDZkOTAxMDNhMTAxNzU2MzZmNjE3MDNhMmYyZjYxNzMyZTY1Nzg2MTZkNzA2YzY1MmU2MzZmNmRYIDL8dIteq8pMXXX9oL4eo2NX1kQUaselV6p/JHSEVXWX';

src/index.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import crypto from 'crypto';
2-
import { CommonAccessToken, CommonAccessTokenFactory } from './cat';
2+
import {
3+
CommonAccessToken,
4+
CommonAccessTokenDict,
5+
CommonAccessTokenFactory
6+
} from './cat';
37
import { KeyNotFoundError } from './errors';
48

59
export { CommonAccessToken } from './cat';
@@ -170,6 +174,29 @@ export class CAT {
170174
}
171175
}
172176

177+
public async generateFromJson(
178+
dict: CommonAccessTokenDict,
179+
opts?: CatGenerateOptions
180+
) {
181+
if (opts?.generateCwtId) {
182+
dict['cti'] = crypto.randomBytes(16).toString('hex');
183+
}
184+
const cat = CommonAccessTokenFactory.fromDict(dict);
185+
if (opts && opts.type == 'mac') {
186+
const key = this.keys[opts.kid];
187+
if (!key) {
188+
throw new Error('Key not found');
189+
}
190+
await cat.mac({ k: key, kid: opts.kid }, opts.alg, {
191+
addCwtTag: this.expectCwtTag
192+
});
193+
if (!cat.raw) {
194+
throw new Error('Failed to MAC token');
195+
}
196+
return cat.raw.toString('base64');
197+
}
198+
}
199+
173200
public async renewToken(
174201
cat: CommonAccessToken,
175202
opts: CatRenewOptions

0 commit comments

Comments
 (0)