authelia/test/unitary/test_jwt.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-12-10 00:47:58 +00:00
var Jwt = require('../../src/lib/jwt');
2016-12-10 00:47:58 +00:00
var sinon = require('sinon');
describe('test jwt', function() {
it('should sign and verify the token', function() {
var data = {user: 'user'};
var secret = 'secret';
var jwt = new Jwt(secret);
2017-01-19 00:44:24 +00:00
return jwt.sign(data, '1m')
.then(function(token) {
return jwt.verify(token);
});
2016-12-10 00:47:58 +00:00
});
it('should verify and fail on wrong token', function() {
var jwt = new Jwt('secret');
2017-01-19 00:44:24 +00:00
var token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoidXNlciIsImlhdCI6MTQ4NDc4NTExMywiZXhwIjoaNDg0Nzg1MTczfQ.yZOZEaMDyOn0tSDiDSPYl4ZP2oL3FQ-Vrzds7hYcNio';
return jwt.verify(token).catch(function() {
return Promise.resolve();
});
2016-12-10 00:47:58 +00:00
});
2017-01-19 00:44:24 +00:00
it('should fail after expiry', function() {
2016-12-10 00:47:58 +00:00
var clock = sinon.useFakeTimers(0);
2017-01-19 00:44:24 +00:00
var data = { user: 'user' };
2016-12-10 00:47:58 +00:00
var jwt = new Jwt('secret');
2017-01-19 00:44:24 +00:00
return jwt.sign(data, '1m')
.then(function(token) {
clock.tick(1000 * 61); // 61 seconds
return jwt.verify(token);
})
.catch(function() {
clock.restore();
return Promise.resolve();
});
2016-12-10 00:47:58 +00:00
});
});