forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-jose-tests.ts
388 lines (330 loc) · 9.68 KB
/
node-jose-tests.ts
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* Test file for node-jose. At this time these tests were taken verbatim
* from the documentation on the NPM page at
* https://www.npmjs.com/package/node-jose
* This file definitely needs to be cleaned up and more tests added to
* test the entirety of the definition file.
*/
import * as jose from 'node-jose';
let keystore = jose.JWK.createKeyStore();
let buf = new Buffer('a');
jose.JWK.asKeyStore("{key: somekey}").
then(function (result) {
// {result} is a jose.JWK.KeyStore
keystore = result;
});
let output = keystore.toJSON();
output = keystore.toJSON(true);
// by 'kid'
let kid: string = "someID";
let key = keystore.get(kid);
// ... and by 'kty'
key = keystore.get(kid, { kty: 'RSA' });
// ... and by 'use'
key = keystore.get(kid, { use: 'enc' });
// ... and by 'alg'
key = keystore.get(kid, { use: 'RSA-OAEP' });
// ... and by 'kty' and 'use'
key = keystore.get(kid, { kty: 'RSA', use: 'enc' });
// same as above, but with a single {props} argument
key = keystore.get({ kid: kid, kty: 'RSA', use: 'enc' });
// searching for keys
let everything = keystore.all();
// filter by 'kid'
everything = keystore.all({ kid: kid });
// filter by 'kty'
everything = keystore.all({ kty: 'RSA' });
// filter by 'use'
everything = keystore.all({ use: 'enc' });
// filter by 'alg'
everything = keystore.all({ alg: 'RSA-OAEP' });
// filter by 'kid' + 'kty' + 'alg'
everything = keystore.all({ kid: kid, kty: 'RSA', alg: 'RSA-OAEP' });
// input is either a:
// * jose.JWK.Key to copy from; or
// * JSON object representing a JWK; or
keystore.add({}).
then(function (result) {
// {result} is a jose.JWK.Key
key = result;
});
// input is either a:
// * String serialization of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER
// * Buffer of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER
// form is either a:
// * "json" for a JSON stringified JWK
// * "private" for a DER encoded 'raw' private key
// * "pkcs8" for a DER encoded (unencrypted!) PKCS8 private key
// * "public" for a DER encoded SPKI public key (alternate to 'spki')
// * "spki" for a DER encoded SPKI public key
// * "pkix" for a DER encoded PKIX X.509 certificate
// * "x509" for a DER encoded PKIX X.509 certificate
// * "pem" for a PEM encoded of PKCS8 / SPKI / PKIX
keystore.add(buf, "json").
then(function (result) {
// {result} is a jose.JWK.Key
key = result;
});
// first argument is the key type (kty)
// second is the key size (in bits) or named curve ('crv') for "EC"
keystore.generate("oct", 256).
then(function (result) {
// {result} is a jose.JWK.Key
key = result;
});
// ... with properties
var props = {
kid: 'gBdaS-G8RLax2qgObTD94w',
alg: 'A256GCM',
use: 'enc'
};
keystore.generate("oct", 256, props).
then(function (result) {
// {result} is a jose.JWK.Key
key = result;
});
keystore.remove(key);
// NOTE: key.keystore does not change!!
// where input is either a:
// * jose.JWK.Key instance
// * JSON Object representation of a JWK
jose.JWK.asKey({}).
then(function (result) {
// {result} is a jose.JWK.Key
// {result.keystore} is a unique jose.JWK.KeyStore
key = result;
});
// where input is either a:
// * String serialization of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER
// * Buffer of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER
// form is either a:
// * "json" for a JSON stringified JWK
// * "pkcs8" for a DER encoded (unencrypted!) PKCS8 private key
// * "spki" for a DER encoded SPKI public key
// * "pkix" for a DER encoded PKIX X.509 certificate
// * "x509" for a DER encoded PKIX X.509 certificate
// * "pem" for a PEM encoded of PKCS8 / SPKI / PKIX
jose.JWK.asKey({}, 'json').
then(function (result) {
// {result} is a jose.JWK.Key
// {result.keystore} is a unique jose.JWK.KeyStore
key = result;
});
output = key.toJSON();
output = key.toJSON(true);
// where hash is a supported algorithm, currently one of:
// * SHA-1
// * SHA-256
// * SHA-384
// * SHA-512
key.thumbprint('SHA-1').
then(function (print) {
// {print} is a Buffer containing the thumbprint binary value
});
// {input} is a Buffer
jose.JWS.createSign(key).
update(buf).
final().
then(function (result) {
// {result} is a JSON object -- JWS using the JSON General Serialization
});
jose.JWS.createSign({ format: 'flattened' }, key).
update(buf).
final().
then(function (result) {
// {result} is a JSON object -- JWS using the JSON Flattened Serialization
});
jose.JWS.createSign({ format: 'compact' }, key).
update(buf).
final().
then(function (result) {
// {result} is a String -- JWS using the Compact Serialization
});
jose.JWS.createSign({ alg: 'PS256' }, key).
update(buf).
final().
then(function (result) {
// ....
});
jose.JWS.createSign({ fields: { cty: 'jwk+json' } }, key).
update(buf).
final().
then(function (result) {
// ....
});
jose.JWS.createSign(key).
update(buf, "utf8").
final().
then(function (result) {
// ....
});
// {keys} is an Array of jose.JWK.Key instances
jose.JWS.createSign([key, key]).
update(buf).
final().
then(function (result) {
// ....
});
jose.JWS.createVerify(keystore).
verify("some.jws.object").
then(function (result) {
// {result} is a Object with:
// * header: the combined 'protected' and 'unprotected' header members
// * payload: Buffer of the signed content
// * signature: Buffer of the verified signature
// * key: The key used to verify the signature
});
// {key} can be:
// * jose.JWK.Key
// * JSON object representing a JWK
jose.JWS.createVerify(key).
verify('some.jws.object').
then(function (result) {
// ...
});
jose.JWS.createVerify().
verify('some.jws.key').
then(function (result) {
// ...
});
var opts = {
handlers: {
"exp": true
}
};
jose.JWS.createVerify(key, opts).
verify('some.jws.key').
then(function (result) {
// ...
});
var opts2 = {
handlers: {
"exp": function (jws: any) {
// {jws} is the JWS verify output, pre-verification
jws.header.exp = new Date(jws.header.exp);
}
}
};
jose.JWS.createVerify(key, opts2).
verify('some.jws.key').
then(function (result) {
// ...
});
var opts3 = {
handlers: {
"exp": {
complete: function (jws: any) {
// {jws} is the JWS verify output, post-verification
jws.header.exp = new Date(jws.header.exp);
}
}
}
};
jose.JWS.createVerify(key, opts3).
verify('some.jws.key').
then(function (result) {
// ...
});
//Encryption
// {input} is a Buffer
jose.JWE.createEncrypt(key).
update(buf).
final().
then(function (result) {
// {result} is a JSON Object -- JWE using the JSON General Serialization
});
jose.JWE.createEncrypt({ format: 'compact' }, key).
update(buf).
final().
then(function (result) {
// {result} is a String -- JWE using the Compact Serialization
});
jose.JWE.createEncrypt({ format: 'flattened' }, key).
update(buf).
final().
then(function (result) {
// {result} is a JSON Object -- JWE using the JSON Flattened Serialization
});
jose.JWE.createEncrypt({ zip: true }, key).
update(buf).
final().
then(function (result) {
// ....
});
jose.JWE.createEncrypt({ fields: { cty: 'jwk+json' } }, key).
update(buf).
final().
then(function (result) {
// ....
});
// {keys} is an Array of jose.JWK.Key instances
jose.JWE.createEncrypt([key, key]).
update(buf).
final().
then(function (result) {
// ....
});
jose.JWE.createDecrypt(keystore).
decrypt('some.jwe').
then(function (result) {
// {result} is a Object with:
// * header: the combined 'protected' and 'unprotected' header members
// * protected: an array of the member names from the "protected" member
// * key: Key used to decrypt
// * payload: Buffer of the decrypted content
// * plaintext: Buffer of the decrypted content (alternate)
});
jose.JWE.createDecrypt(key).
decrypt('some.jwe').
then(function (result) {
// ....
});
var opts4 = {
handlers: {
"exp": true
}
};
jose.JWE.createDecrypt(key, opts4).
decrypt('some.jwe').
then(function (result) {
// ...
});
//To perform additional (pre-decrypt) processing on a crit header member:
var opts5 = {
handlers: {
"exp": function (jwe: any) {
// {jwe} is the JWE decrypt output, pre-decryption
jwe.header.exp = new Date(jwe.header.exp);
}
}
};
jose.JWE.createDecrypt(key, opts5).
decrypt('some.jwe').
then(function (result) {
// ...
});
//To perform additional (post-decrypt) processing on a crit header member:
var opts6 = {
handlers: {
"exp": {
complete: function (jwe: any) {
// {jwe} is the JWE decrypt output, post-decryption
jwe.header.exp = new Date(jwe.header.exp);
}
}
}
};
jose.JWE.createDecrypt(key, opts6).
decrypt('some.jwe').
then(function (result) {
// ...
});
var buff = jose.util.asBuffer('input');
var output2 = jose.util.base64url.encode(buf);
// explicit encoding
output2 = jose.util.base64url.encode(buf, "utf8");
// implied "utf8" encoding
output2 = jose.util.base64url.encode(buf);
var output3 = jose.util.base64url.decode('input');
// argument is size (in bytes)
var rnd = jose.util.randomBytes(32);