-
Notifications
You must be signed in to change notification settings - Fork 4
/
convert.spec.js
91 lines (84 loc) · 3.84 KB
/
convert.spec.js
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
/* jshint mocha: true */
'use strict';
const convert = require('./convert');
const expect = require('chai').expect;
describe('convert', () => {
it('decode a jwt using HS256', () => {
var options = {
decode: true,
jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ'
};
var decoded = JSON.parse(convert(options));
expect(decoded).to.have.property('sub');
expect(decoded).to.have.property('name');
expect(decoded).to.have.property('admin');
expect(decoded.sub).to.equal('1234567890');
expect(decoded.name).to.equal('John Doe');
expect(decoded.admin).to.equal(true);
});
it('verify a jwt using HS256', () => {
var options = {
algorithms: ['HS256', 'HS384'],
decode: true,
jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ',
secret: 'secret'
};
var decoded = JSON.parse(convert(options));
expect(decoded).to.have.property('sub');
expect(decoded).to.have.property('name');
expect(decoded).to.have.property('admin');
expect(decoded.sub).to.equal('1234567890');
expect(decoded.name).to.equal('John Doe');
expect(decoded.admin).to.equal(true);
});
it('encode a jwt using HS256', () => {
var options = {
algorithm: 'HS256',
encode: true,
timestamp: false,
jwt: '{ "sub": "1234567890", "name": "John Doe", "admin": true }',
secret: 'secret'
};
var encoded = convert(options);
expect(encoded).to.equal('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ');
});
it('decode a jwt using RS256', () => {
var options = {
decode: true,
jwt: 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.EkN-DOsnsuRjRO6BxXemmJDm3HbxrbRzXglbN2S4sOkopdU4IsDxTI8jO19W_A4K8ZPJijNLis4EZsHeY559a4DFOd50_OqgHGuERTqYZyuhtF39yxJPAjUESwxk2J5k_4zM3O-vtd1Ghyo4IbqKKSy6J9mTniYJPenn5-HIirE'
};
var decoded = JSON.parse(convert(options));
expect(decoded).to.have.property('sub');
expect(decoded).to.have.property('name');
expect(decoded).to.have.property('admin');
expect(decoded.sub).to.equal('1234567890');
expect(decoded.name).to.equal('John Doe');
expect(decoded.admin).to.equal(true);
});
it('verify a jwt using RS256', () => {
var options = {
algorithms: ['RS256'],
decode: true,
jwt: 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.EkN-DOsnsuRjRO6BxXemmJDm3HbxrbRzXglbN2S4sOkopdU4IsDxTI8jO19W_A4K8ZPJijNLis4EZsHeY559a4DFOd50_OqgHGuERTqYZyuhtF39yxJPAjUESwxk2J5k_4zM3O-vtd1Ghyo4IbqKKSy6J9mTniYJPenn5-HIirE',
'public-key-file': './public.key'
};
var decoded = JSON.parse(convert(options));
expect(decoded).to.have.property('sub');
expect(decoded).to.have.property('name');
expect(decoded).to.have.property('admin');
expect(decoded.sub).to.equal('1234567890');
expect(decoded.name).to.equal('John Doe');
expect(decoded.admin).to.equal(true);
});
it('encode a jwt using RS256', () => {
var options = {
algorithm: 'RS256',
encode: true,
timestamp: false,
jwt: '{ "sub": "1234567890", "name": "John Doe", "admin": true }',
'private-key-file': './private.key'
};
var encoded = convert(options);
expect(encoded).to.equal('eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.EkN-DOsnsuRjRO6BxXemmJDm3HbxrbRzXglbN2S4sOkopdU4IsDxTI8jO19W_A4K8ZPJijNLis4EZsHeY559a4DFOd50_OqgHGuERTqYZyuhtF39yxJPAjUESwxk2J5k_4zM3O-vtd1Ghyo4IbqKKSy6J9mTniYJPenn5-HIirE');
});
});