-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcat.test.ts
More file actions
217 lines (209 loc) · 6.42 KB
/
cat.test.ts
File metadata and controls
217 lines (209 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { CommonAccessToken, CommonAccessTokenFactory } from './cat';
import { CommonAccessTokenRenewal } from './catr';
describe('CAT', () => {
test('can create a CAT object and return claims as JSON', () => {
const claims = {
iss: 'coap://as.example.com',
sub: 'jonas',
aud: 'coap://light.example.com',
exp: 1444064944,
nbf: 1443944944,
iat: 1443944944,
cti: '0b71',
catv: 1
};
const cwt = new CommonAccessToken(claims);
expect(cwt.claims).toEqual(claims);
});
test('can MAC a CAT object with CWT Tag', async () => {
const claims = {
iss: 'coap://jonas.example.com',
nbf: 1741985961,
iat: 1741985961,
catv: 1
};
const cat = new CommonAccessToken(claims);
const key = {
k: Buffer.from(
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
'hex'
),
kid: 'Symmetric256'
};
await cat.mac(key, 'HS256', { addCwtTag: true });
expect(cat.raw).toBeDefined();
const macHex = cat.raw?.toString('hex');
const token = Buffer.from(macHex!, 'hex');
const newCat = new CommonAccessToken({});
await newCat.parse(token, key, { expectCwtTag: true });
expect(newCat.claims).toEqual(claims);
});
test('can sign a CAT object', async () => {
const claims = {
iss: 'coap://as.example.com',
sub: 'jonas',
aud: 'coap://light.example.com',
exp: 1444064944,
nbf: 1443944944,
iat: 1443944944,
cti: '0b71'
};
const cat = new CommonAccessToken(claims);
const signKey = {
d: Buffer.from(
'6c1382765aec5358f117733d281c1c7bdc39884d04a45a1e6c67c858bc206c19',
'hex'
),
kid: 'AsymmetricECDSA256'
};
await cat.sign(signKey, 'ES256');
const signedHex = cat.raw?.toString('hex');
const token = Buffer.from(signedHex!, 'hex');
const verifyKey = {
x: Buffer.from(
'143329cce7868e416927599cf65a34f3ce2ffda55a7eca69ed8919a394d42f0f',
'hex'
),
y: Buffer.from(
'60f7f1a780d8a783bfb7a2dd6b2796e8128dbbcef9d3d168db9529971a36e7b9',
'hex'
),
kid: 'AsymmetricECDSA256'
};
await cat.verify(token, verifyKey);
const claimsWithCatv = { ...claims, catv: 1 };
expect(cat.claims).toEqual(claimsWithCatv);
});
test('can create a CAT object from a signed base64 encoded token', async () => {
const base64encoded =
'0oRDoQEmoQRSQXN5bW1ldHJpY0VDRFNBMjU2eKZkOTAxMDNhNzAxNzU2MzZmNjE3MDNhMmYyZjYxNzMyZTY1Nzg2MTZkNzA2YzY1MmU2MzZmNmQwMjY1NmE2ZjZlNjE3MzAzNzgxODYzNmY2MTcwM2EyZjJmNmM2OTY3Njg3NDJlNjU3ODYxNmQ3MDZjNjUyZTYzNmY2ZDA0MWE1NjEyYWViMDA1MWE1NjEwZDlmMDA2MWE1NjEwZDlmMDA3NDIwYjcxWEDctMzQNy7mvRZNvwmyJ2b5WG+Q1erTbN4SCFyM05lnBH/fBFJJ2QR20OypFvHd2veW3fzGsRY/ZRM1dxUE1Mfb';
const verifyKey = {
x: Buffer.from(
'143329cce7868e416927599cf65a34f3ce2ffda55a7eca69ed8919a394d42f0f',
'hex'
),
y: Buffer.from(
'60f7f1a780d8a783bfb7a2dd6b2796e8128dbbcef9d3d168db9529971a36e7b9',
'hex'
),
kid: 'AsymmetricECDSA256'
};
const cat = await CommonAccessTokenFactory.fromSignedToken(
base64encoded,
verifyKey
);
expect(cat.claims).toEqual({
iss: 'coap://as.example.com',
sub: 'jonas',
aud: 'coap://light.example.com',
exp: 1444064944,
nbf: 1443944944,
iat: 1443944944,
cti: '0b71'
});
});
test('can create a CAT object from a mac:ed base64 encoded token', async () => {
const base64encoded =
'0YRDoQEEoQRMU3ltbWV0cmljMjU2eKZkOTAxMDNhNzAxNzU2MzZmNjE3MDNhMmYyZjYxNzMyZTY1Nzg2MTZkNzA2YzY1MmU2MzZmNmQwMjY1NmE2ZjZlNjE3MzAzNzgxODYzNmY2MTcwM2EyZjJmNmM2OTY3Njg3NDJlNjU3ODYxNmQ3MDZjNjUyZTYzNmY2ZDA0MWE1NjEyYWViMDA1MWE1NjEwZDlmMDA2MWE1NjEwZDlmMDA3NDIwYjcxSKuCk/+kFmlY';
const key = {
k: Buffer.from(
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
'hex'
),
kid: 'Symmetric256'
};
const cat = await CommonAccessTokenFactory.fromMacedToken(
base64encoded,
key,
false
);
expect(cat.claims).toEqual({
iss: 'coap://as.example.com',
sub: 'jonas',
aud: 'coap://light.example.com',
exp: 1444064944,
nbf: 1443944944,
iat: 1443944944,
cti: '0b71'
});
});
test('can provide information about renewal mechanism', async () => {
const cat = new CommonAccessToken({
iss: 'eyevinn',
catr: CommonAccessTokenRenewal.fromDict({
type: 'header',
'header-name': 'cta-common-access-token'
}).payload
});
expect(cat.claims.catr).toEqual({
type: 'header',
'header-name': 'cta-common-access-token'
});
});
test('can create a CAT object from a dict', async () => {
const cat = CommonAccessTokenFactory.fromDict({
iss: 'eyevinn',
sub: 'jonas',
aud: 'coap://light.example.com',
exp: 1444064944,
nbf: 1443944944,
iat: 1443944944,
cti: '0b71',
catr: {
type: 'header',
'header-name': 'cta-common-access-token',
'header-params': ['value1'],
'cookie-name': 'myname',
'cookie-params': ['cookievalue']
}
});
expect(cat.claims).toEqual({
iss: 'eyevinn',
sub: 'jonas',
aud: 'coap://light.example.com',
exp: 1444064944,
nbf: 1443944944,
iat: 1443944944,
cti: '0b71',
catr: {
type: 'header',
'header-name': 'cta-common-access-token',
'header-params': ['value1'],
'cookie-name': 'myname',
'cookie-params': ['cookievalue']
},
catv: 1
});
});
test('can determine whether the token should be renewed', async () => {
const now = Math.floor(Date.now() / 1000);
const cat = new CommonAccessToken({
iss: 'eyevinn',
exp: now + 30,
catr: CommonAccessTokenRenewal.fromDict({
type: 'automatic',
expadd: 60
}).payload
});
expect(cat.shouldRenew).toBe(true);
const cat2 = new CommonAccessToken({
iss: 'eyevinn',
exp: now + 100,
catr: CommonAccessTokenRenewal.fromDict({
type: 'automatic',
expadd: 60
}).payload
});
expect(cat2.shouldRenew).toBe(false);
const cat3 = new CommonAccessToken({
iss: 'eyevinn',
exp: now + 100,
catr: CommonAccessTokenRenewal.fromDict({
type: 'automatic',
expadd: 60,
deadline: 105
}).payload
});
expect(cat3.shouldRenew).toBe(true);
});
});