forked from auth0/node-jsonwebtoken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt.asymmetric_signing.tests.js
177 lines (151 loc) · 5.84 KB
/
jwt.asymmetric_signing.tests.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
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
var jwt = require('../index');
var PS_SUPPORTED = require('../lib/psSupported');
var fs = require('fs');
var path = require('path');
var expect = require('chai').expect;
var assert = require('chai').assert;
var ms = require('ms');
function loadKey(filename) {
return fs.readFileSync(path.join(__dirname, filename));
}
var algorithms = {
RS256: {
pub_key: loadKey('pub.pem'),
priv_key: loadKey('priv.pem'),
invalid_pub_key: loadKey('invalid_pub.pem')
},
ES256: {
// openssl ecparam -name secp256r1 -genkey -param_enc explicit -out ecdsa-private.pem
priv_key: loadKey('ecdsa-private.pem'),
// openssl ec -in ecdsa-private.pem -pubout -out ecdsa-public.pem
pub_key: loadKey('ecdsa-public.pem'),
invalid_pub_key: loadKey('ecdsa-public-invalid.pem')
}
};
if (PS_SUPPORTED) {
algorithms.PS256 = {
pub_key: loadKey('pub.pem'),
priv_key: loadKey('priv.pem'),
invalid_pub_key: loadKey('invalid_pub.pem')
};
}
describe('Asymmetric Algorithms', function(){
Object.keys(algorithms).forEach(function (algorithm) {
describe(algorithm, function () {
var pub = algorithms[algorithm].pub_key;
var priv = algorithms[algorithm].priv_key;
// "invalid" means it is not the public key for the loaded "priv" key
var invalid_pub = algorithms[algorithm].invalid_pub_key;
describe('when signing a token', function () {
var token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm });
it('should be syntactically valid', function () {
expect(token).to.be.a('string');
expect(token.split('.')).to.have.length(3);
});
context('asynchronous', function () {
it('should validate with public key', function (done) {
jwt.verify(token, pub, function (err, decoded) {
assert.ok(decoded.foo);
assert.equal('bar', decoded.foo);
done();
});
});
it('should throw with invalid public key', function (done) {
jwt.verify(token, invalid_pub, function (err, decoded) {
assert.isUndefined(decoded);
assert.isNotNull(err);
done();
});
});
});
context('synchronous', function () {
it('should validate with public key', function () {
var decoded = jwt.verify(token, pub);
assert.ok(decoded.foo);
assert.equal('bar', decoded.foo);
});
it('should throw with invalid public key', function () {
var jwtVerify = jwt.verify.bind(null, token, invalid_pub)
assert.throw(jwtVerify, 'invalid signature');
});
});
});
describe('when signing a token with expiration', function () {
var token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm, expiresIn: '10m' });
it('should be valid expiration', function (done) {
jwt.verify(token, pub, function (err, decoded) {
assert.isNotNull(decoded);
assert.isNull(err);
done();
});
});
it('should be invalid', function (done) {
// expired token
token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm, expiresIn: -1 * ms('10m') });
jwt.verify(token, pub, function (err, decoded) {
assert.isUndefined(decoded);
assert.isNotNull(err);
assert.equal(err.name, 'TokenExpiredError');
assert.instanceOf(err.expiredAt, Date);
assert.instanceOf(err, jwt.TokenExpiredError);
done();
});
});
it('should NOT be invalid', function (done) {
// expired token
token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm, expiresIn: -1 * ms('10m') });
jwt.verify(token, pub, { ignoreExpiration: true }, function (err, decoded) {
assert.ok(decoded.foo);
assert.equal('bar', decoded.foo);
done();
});
});
});
describe('when verifying a malformed token', function () {
it('should throw', function (done) {
jwt.verify('fruit.fruit.fruit', pub, function (err, decoded) {
assert.isUndefined(decoded);
assert.isNotNull(err);
assert.equal(err.name, 'JsonWebTokenError');
done();
});
});
});
describe('when decoding a jwt token with additional parts', function () {
var token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm });
it('should throw', function (done) {
jwt.verify(token + '.foo', pub, function (err, decoded) {
assert.isUndefined(decoded);
assert.isNotNull(err);
done();
});
});
});
describe('when decoding a invalid jwt token', function () {
it('should return null', function (done) {
var payload = jwt.unsafeDecode('whatever.token');
assert.isNull(payload);
done();
});
});
describe('when decoding a valid jwt token', function () {
it('should return the payload', function (done) {
var obj = { foo: 'bar' };
var token = jwt.sign(obj, priv, { algorithm: algorithm });
var payload = jwt.unsafeDecode(token);
assert.equal(payload.foo, obj.foo);
done();
});
it('should return the header and payload and signature if complete option is set', function (done) {
var obj = { foo: 'bar' };
var token = jwt.sign(obj, priv, { algorithm: algorithm });
var decoded = jwt.unsafeDecode(token, { complete: true });
assert.equal(decoded.payload.foo, obj.foo);
assert.deepEqual(decoded.header, { typ: 'JWT', alg: algorithm });
assert.ok(typeof decoded.signature == 'string');
done();
});
});
});
});
});