@@ -29,19 +29,20 @@ describe('verify', function() {
29
29
} ) ;
30
30
31
31
describe ( 'expiration' , function ( ) {
32
+ // { foo: 'bar', iat: 1437018582, exp: 1437018583 }
33
+ var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTQzNzAxODU4M30.NmMv7sXjM1dW0eALNXud8LoXknZ0mH14GtnFclwJv0s' ;
34
+ var key = 'key' ;
35
+
32
36
var clock ;
33
- beforeEach ( function ( ) {
34
- // clock = sinon.useFakeTimers(1437018650768);
35
- } ) ;
36
37
afterEach ( function ( ) {
37
38
try { clock . restore ( ) ; } catch ( e ) { }
38
39
} ) ;
39
40
40
41
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 ) {
42
+ clock = sinon . useFakeTimers ( 1437018650000 ) ;
43
+ var options = { algorithms : [ 'HS256' ] } ;
44
+
45
+ jwt . verify ( token , key , options , function ( err , p ) {
45
46
assert . equal ( err . name , 'TokenExpiredError' ) ;
46
47
assert . equal ( err . message , 'jwt expired' ) ;
47
48
assert . equal ( err . expiredAt . constructor . name , 'Date' ) ;
@@ -53,14 +54,79 @@ describe('verify', function() {
53
54
54
55
it ( 'should not error on unexpired token' , function ( done ) {
55
56
clock = sinon . useFakeTimers ( 1437018582000 ) ;
56
- var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTQzNzAxODU4M30.NmMv7sXjM1dW0eALNXud8LoXknZ0mH14GtnFclwJv0s' ;
57
- var key = 'key' ;
58
- jwt . verify ( token , key , { algorithms : [ 'HS256' ] } , function ( err , p ) {
57
+ var options = { algorithms : [ 'HS256' ] }
58
+
59
+ jwt . verify ( token , key , options , function ( err , p ) {
59
60
assert . isNull ( err ) ;
60
61
assert . equal ( p . foo , 'bar' ) ;
61
62
done ( ) ;
62
63
} ) ;
63
64
} ) ;
65
+
66
+ describe ( 'option: maxAge' , function ( ) {
67
+ it ( 'should error for claims issued before a certain timespan' , function ( done ) {
68
+ clock = sinon . useFakeTimers ( 1437018582500 ) ;
69
+ var options = { algorithms : [ 'HS256' ] , maxAge : '321ms' } ;
70
+
71
+ jwt . verify ( token , key , options , function ( err , p ) {
72
+ assert . equal ( err . name , 'TokenExpiredError' ) ;
73
+ assert . equal ( err . message , 'maxAge exceded' ) ;
74
+ assert . equal ( err . expiredAt . constructor . name , 'Date' ) ;
75
+ assert . equal ( Number ( err . expiredAt ) , 1437018582321 ) ;
76
+ assert . isUndefined ( p ) ;
77
+ done ( ) ;
78
+ } ) ;
79
+ } ) ;
80
+ it ( 'should not error if within maxAge timespan' , function ( done ) {
81
+ clock = sinon . useFakeTimers ( 1437018582500 ) ;
82
+ var options = { algorithms : [ 'HS256' ] , maxAge : '600ms' } ;
83
+
84
+ jwt . verify ( token , key , options , function ( err , p ) {
85
+ assert . isNull ( err ) ;
86
+ assert . equal ( p . foo , 'bar' ) ;
87
+ done ( ) ;
88
+ } ) ;
89
+ } ) ;
90
+ it ( 'can be more restrictive than expiration' , function ( done ) {
91
+ clock = sinon . useFakeTimers ( 1437018582900 ) ;
92
+ var options = { algorithms : [ 'HS256' ] , maxAge : '800ms' } ;
93
+
94
+ jwt . verify ( token , key , options , function ( err , p ) {
95
+ assert . equal ( err . name , 'TokenExpiredError' ) ;
96
+ assert . equal ( err . message , 'maxAge exceded' ) ;
97
+ assert . equal ( err . expiredAt . constructor . name , 'Date' ) ;
98
+ assert . equal ( Number ( err . expiredAt ) , 1437018582800 ) ;
99
+ assert . isUndefined ( p ) ;
100
+ done ( ) ;
101
+ } ) ;
102
+ } ) ;
103
+ it ( 'cannot be more permissive than expiration' , function ( done ) {
104
+ clock = sinon . useFakeTimers ( 1437018583100 ) ;
105
+ var options = { algorithms : [ 'HS256' ] , maxAge : '1200ms' } ;
106
+
107
+ jwt . verify ( token , key , options , function ( err , p ) {
108
+ // maxAge not exceded, but still expired
109
+ assert . equal ( err . name , 'TokenExpiredError' ) ;
110
+ assert . equal ( err . message , 'jwt expired' ) ;
111
+ assert . equal ( err . expiredAt . constructor . name , 'Date' ) ;
112
+ assert . equal ( Number ( err . expiredAt ) , 1437018583000 ) ;
113
+ assert . isUndefined ( p ) ;
114
+ done ( ) ;
115
+ } ) ;
116
+ } ) ;
117
+ it ( 'should error if maxAge is specified but there is no iat claim' , function ( done ) {
118
+ clock = sinon . useFakeTimers ( 1437018582900 ) ;
119
+ var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIifQ.0MBPd4Bru9-fK_HY3xmuDAc6N_embknmNuhdb9bKL_U' ;
120
+ var options = { algorithms : [ 'HS256' ] , maxAge : '1s' } ;
121
+
122
+ jwt . verify ( token , key , options , function ( err , p ) {
123
+ assert . equal ( err . name , 'JsonWebTokenError' ) ;
124
+ assert . equal ( err . message , 'iat required when maxAge is specified' ) ;
125
+ assert . isUndefined ( p ) ;
126
+ done ( ) ;
127
+ } ) ;
128
+ } ) ;
129
+ } ) ;
64
130
} ) ;
65
131
66
132
} ) ;
0 commit comments