@@ -2,14 +2,15 @@ var jwt = require('../index');
2
2
var jws = require ( 'jws' ) ;
3
3
var fs = require ( 'fs' ) ;
4
4
var path = require ( 'path' ) ;
5
+ var sinon = require ( 'sinon' ) ;
5
6
6
7
var assert = require ( 'chai' ) . assert ;
7
8
8
9
describe ( 'verify' , function ( ) {
9
10
var pub = fs . readFileSync ( path . join ( __dirname , 'pub.pem' ) ) ;
10
11
var priv = fs . readFileSync ( path . join ( __dirname , 'priv.pem' ) ) ;
11
12
12
- it ( 'should first assume JSON claim set' , function ( ) {
13
+ it ( 'should first assume JSON claim set' , function ( done ) {
13
14
var header = { alg : 'RS256' } ;
14
15
var payload = { iat : Math . floor ( Date . now ( ) / 1000 ) } ;
15
16
@@ -23,6 +24,43 @@ describe('verify', function() {
23
24
jwt . verify ( signed , pub , { typ : 'JWT' } , function ( err , p ) {
24
25
assert . isNull ( err ) ;
25
26
assert . deepEqual ( p , payload ) ;
27
+ done ( ) ;
26
28
} ) ;
27
29
} ) ;
30
+
31
+ describe ( 'expiration' , function ( ) {
32
+ var clock ;
33
+ beforeEach ( function ( ) {
34
+ // clock = sinon.useFakeTimers(1437018650768);
35
+ } ) ;
36
+ afterEach ( function ( ) {
37
+ try { clock . restore ( ) ; } catch ( e ) { }
38
+ } ) ;
39
+
40
+ it ( 'should error on expired token' , function ( done ) {
41
+ clock = sinon . useFakeTimers ( 1437018650768 ) ;
42
+ var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTQzNzAxODU4M30.NmMv7sXjM1dW0eALNXud8LoXknZ0mH14GtnFclwJv0s' ;
43
+ var key = 'key' ;
44
+ jwt . verify ( token , key , { algorithms : [ 'HS256' ] } , function ( err , p ) {
45
+ assert . equal ( err . name , 'TokenExpiredError' ) ;
46
+ assert . equal ( err . message , 'jwt expired' ) ;
47
+ assert . equal ( err . expiredAt . constructor . name , 'Date' ) ;
48
+ assert . equal ( Number ( err . expiredAt ) , 1437018583000 ) ;
49
+ assert . isUndefined ( p ) ;
50
+ done ( ) ;
51
+ } ) ;
52
+ } ) ;
53
+
54
+ it ( 'should not error on unexpired token' , function ( done ) {
55
+ clock = sinon . useFakeTimers ( 1437018582000 ) ;
56
+ var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTQzNzAxODU4M30.NmMv7sXjM1dW0eALNXud8LoXknZ0mH14GtnFclwJv0s' ;
57
+ var key = 'key' ;
58
+ jwt . verify ( token , key , { algorithms : [ 'HS256' ] } , function ( err , p ) {
59
+ assert . isNull ( err ) ;
60
+ assert . equal ( p . foo , 'bar' ) ;
61
+ done ( ) ;
62
+ } ) ;
63
+ } ) ;
64
+ } ) ;
65
+
28
66
} ) ;
0 commit comments