@@ -17,16 +17,34 @@ describe('failure tests', function () {
17
17
}
18
18
} ) ;
19
19
20
+ it ( 'should throw if algorithms is not sent' , function ( ) {
21
+ try {
22
+ expressjwt ( { secret : 'shhhh' } ) ;
23
+ } catch ( e ) {
24
+ assert . ok ( e ) ;
25
+ assert . equal ( e . message , 'algorithms should be set' ) ;
26
+ }
27
+ } ) ;
28
+
29
+ it ( 'should throw if algorithms is not an array' , function ( ) {
30
+ try {
31
+ expressjwt ( { secret : 'shhhh' , algorithms : 'foo' } ) ;
32
+ } catch ( e ) {
33
+ assert . ok ( e ) ;
34
+ assert . equal ( e . message , 'algorithms must be an array' ) ;
35
+ }
36
+ } ) ;
37
+
20
38
it ( 'should throw if no authorization header and credentials are required' , function ( ) {
21
- expressjwt ( { secret : 'shhhh' , credentialsRequired : true } ) ( req , res , function ( err ) {
39
+ expressjwt ( { secret : 'shhhh' , credentialsRequired : true , algorithms : [ 'HS256' ] } ) ( req , res , function ( err ) {
22
40
assert . ok ( err ) ;
23
41
assert . equal ( err . code , 'credentials_required' ) ;
24
42
} ) ;
25
43
} ) ;
26
44
27
45
it ( 'support unless skip' , function ( ) {
28
46
req . originalUrl = '/index.html' ;
29
- expressjwt ( { secret : 'shhhh' } ) . unless ( { path : '/index.html' } ) ( req , res , function ( err ) {
47
+ expressjwt ( { secret : 'shhhh' , algorithms : [ 'HS256' ] , algorithms : [ 'HS256' ] } ) . unless ( { path : '/index.html' } ) ( req , res , function ( err ) {
30
48
assert . ok ( ! err ) ;
31
49
} ) ;
32
50
} ) ;
@@ -37,15 +55,15 @@ describe('failure tests', function () {
37
55
corsReq . headers = {
38
56
'access-control-request-headers' : 'sasa, sras, authorization'
39
57
} ;
40
- expressjwt ( { secret : 'shhhh' } ) ( corsReq , res , function ( err ) {
58
+ expressjwt ( { secret : 'shhhh' , algorithms : [ 'HS256' ] } ) ( corsReq , res , function ( err ) {
41
59
assert . ok ( ! err ) ;
42
60
} ) ;
43
61
} ) ;
44
62
45
63
it ( 'should throw if authorization header is malformed' , function ( ) {
46
64
req . headers = { } ;
47
65
req . headers . authorization = 'wrong' ;
48
- expressjwt ( { secret : 'shhhh' } ) ( req , res , function ( err ) {
66
+ expressjwt ( { secret : 'shhhh' , algorithms : [ 'HS256' ] } ) ( req , res , function ( err ) {
49
67
assert . ok ( err ) ;
50
68
assert . equal ( err . code , 'credentials_bad_format' ) ;
51
69
} ) ;
@@ -54,7 +72,7 @@ describe('failure tests', function () {
54
72
it ( 'should throw if authorization header is not Bearer' , function ( ) {
55
73
req . headers = { } ;
56
74
req . headers . authorization = 'Basic foobar' ;
57
- expressjwt ( { secret : 'shhhh' } ) ( req , res , function ( err ) {
75
+ expressjwt ( { secret : 'shhhh' , algorithms : [ 'HS256' ] } ) ( req , res , function ( err ) {
58
76
assert . ok ( err ) ;
59
77
assert . equal ( err . code , 'credentials_bad_scheme' ) ;
60
78
} ) ;
@@ -63,15 +81,15 @@ describe('failure tests', function () {
63
81
it ( 'should next if authorization header is not Bearer and credentialsRequired is false' , function ( ) {
64
82
req . headers = { } ;
65
83
req . headers . authorization = 'Basic foobar' ;
66
- expressjwt ( { secret : 'shhhh' , credentialsRequired : false } ) ( req , res , function ( err ) {
84
+ expressjwt ( { secret : 'shhhh' , algorithms : [ 'HS256' ] , credentialsRequired : false } ) ( req , res , function ( err ) {
67
85
assert . ok ( typeof err === 'undefined' ) ;
68
86
} ) ;
69
87
} ) ;
70
88
71
89
it ( 'should throw if authorization header is not well-formatted jwt' , function ( ) {
72
90
req . headers = { } ;
73
91
req . headers . authorization = 'Bearer wrongjwt' ;
74
- expressjwt ( { secret : 'shhhh' } ) ( req , res , function ( err ) {
92
+ expressjwt ( { secret : 'shhhh' , algorithms : [ 'HS256' ] } ) ( req , res , function ( err ) {
75
93
assert . ok ( err ) ;
76
94
assert . equal ( err . code , 'invalid_token' ) ;
77
95
} ) ;
@@ -80,7 +98,7 @@ describe('failure tests', function () {
80
98
it ( 'should throw if jwt is an invalid json' , function ( ) {
81
99
req . headers = { } ;
82
100
req . headers . authorization = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.yJ1c2VybmFtZSI6InNhZ3VpYXIiLCJpYXQiOjE0NzEwMTg2MzUsImV4cCI6MTQ3MzYxMDYzNX0.foo' ;
83
- expressjwt ( { secret : 'shhhh' } ) ( req , res , function ( err ) {
101
+ expressjwt ( { secret : 'shhhh' , algorithms : [ 'HS256' ] } ) ( req , res , function ( err ) {
84
102
assert . ok ( err ) ;
85
103
assert . equal ( err . code , 'invalid_token' ) ;
86
104
} ) ;
@@ -92,7 +110,7 @@ describe('failure tests', function () {
92
110
93
111
req . headers = { } ;
94
112
req . headers . authorization = 'Bearer ' + token ;
95
- expressjwt ( { secret : 'different-shhhh' } ) ( req , res , function ( err ) {
113
+ expressjwt ( { secret : 'different-shhhh' , algorithms : [ 'HS256' ] } ) ( req , res , function ( err ) {
96
114
assert . ok ( err ) ;
97
115
assert . equal ( err . code , 'invalid_token' ) ;
98
116
assert . equal ( err . message , 'invalid signature' ) ;
@@ -101,11 +119,11 @@ describe('failure tests', function () {
101
119
102
120
it ( 'should throw if audience is not expected' , function ( ) {
103
121
var secret = 'shhhhhh' ;
104
- var token = jwt . sign ( { foo : 'bar' , aud : 'expected-audience' } , secret ) ;
122
+ var token = jwt . sign ( { foo : 'bar' , aud : 'expected-audience' } , secret , { expiresIn : 500 } ) ;
105
123
106
124
req . headers = { } ;
107
125
req . headers . authorization = 'Bearer ' + token ;
108
- expressjwt ( { secret : 'shhhhhh' , audience : 'not-expected-audience' } ) ( req , res , function ( err ) {
126
+ expressjwt ( { secret : 'shhhhhh' , algorithms : [ 'HS256' ] , audience : 'not-expected-audience' } ) ( req , res , function ( err ) {
109
127
assert . ok ( err ) ;
110
128
assert . equal ( err . code , 'invalid_token' ) ;
111
129
assert . equal ( err . message , 'jwt audience invalid. expected: not-expected-audience' ) ;
@@ -118,7 +136,7 @@ describe('failure tests', function () {
118
136
119
137
req . headers = { } ;
120
138
req . headers . authorization = 'Bearer ' + token ;
121
- expressjwt ( { secret : 'shhhhhh' } ) ( req , res , function ( err ) {
139
+ expressjwt ( { secret : 'shhhhhh' , algorithms : [ 'HS256' ] } ) ( req , res , function ( err ) {
122
140
assert . ok ( err ) ;
123
141
assert . equal ( err . code , 'invalid_token' ) ;
124
142
assert . equal ( err . inner . name , 'TokenExpiredError' ) ;
@@ -132,7 +150,7 @@ describe('failure tests', function () {
132
150
133
151
req . headers = { } ;
134
152
req . headers . authorization = 'Bearer ' + token ;
135
- expressjwt ( { secret : 'shhhhhh' , issuer : 'http://wrong' } ) ( req , res , function ( err ) {
153
+ expressjwt ( { secret : 'shhhhhh' , algorithms : [ 'HS256' ] , issuer : 'http://wrong' } ) ( req , res , function ( err ) {
136
154
assert . ok ( err ) ;
137
155
assert . equal ( err . code , 'invalid_token' ) ;
138
156
assert . equal ( err . message , 'jwt issuer invalid. expected: http://wrong' ) ;
@@ -141,14 +159,13 @@ describe('failure tests', function () {
141
159
142
160
it ( 'should use errors thrown from custom getToken function' , function ( ) {
143
161
var secret = 'shhhhhh' ;
144
- var token = jwt . sign ( { foo : 'bar' } , secret ) ;
145
162
146
163
function getTokenThatThrowsError ( ) {
147
164
throw new UnauthorizedError ( 'invalid_token' , { message : 'Invalid token!' } ) ;
148
165
}
149
166
150
167
expressjwt ( {
151
- secret : 'shhhhhh' ,
168
+ secret : 'shhhhhh' , algorithms : [ 'HS256' ] ,
152
169
getToken : getTokenThatThrowsError
153
170
} ) ( req , res , function ( err ) {
154
171
assert . ok ( err ) ;
@@ -157,7 +174,6 @@ describe('failure tests', function () {
157
174
} ) ;
158
175
} ) ;
159
176
160
-
161
177
it ( 'should throw error when signature is wrong' , function ( ) {
162
178
var secret = "shhh" ;
163
179
var token = jwt . sign ( { foo : 'bar' , iss : 'http://www' } , secret ) ;
@@ -170,7 +186,7 @@ describe('failure tests', function () {
170
186
// build request
171
187
req . headers = [ ] ;
172
188
req . headers . authorization = 'Bearer ' + newToken ;
173
- expressjwt ( { secret : secret } ) ( req , res , function ( err ) {
189
+ expressjwt ( { secret : secret , algorithms : [ 'HS256' ] } ) ( req , res , function ( err ) {
174
190
assert . ok ( err ) ;
175
191
assert . equal ( err . code , 'invalid_token' ) ;
176
192
assert . equal ( err . message , 'invalid token' ) ;
@@ -183,7 +199,7 @@ describe('failure tests', function () {
183
199
184
200
req . headers = { } ;
185
201
req . headers . authorization = 'Bearer ' + token ;
186
- expressjwt ( { secret : secret , credentialsRequired : false } ) ( req , res , function ( err ) {
202
+ expressjwt ( { secret : secret , credentialsRequired : false , algorithms : [ 'HS256' ] } ) ( req , res , function ( err ) {
187
203
assert . ok ( err ) ;
188
204
assert . equal ( err . code , 'invalid_token' ) ;
189
205
assert . equal ( err . message , 'jwt expired' ) ;
@@ -196,7 +212,7 @@ describe('failure tests', function () {
196
212
197
213
req . headers = { } ;
198
214
req . headers . authorization = 'Bearer ' + token ;
199
- expressjwt ( { secret : "not the secret" , credentialsRequired : false } ) ( req , res , function ( err ) {
215
+ expressjwt ( { secret : "not the secret" , algorithms : [ 'HS256' ] , credentialsRequired : false } ) ( req , res , function ( err ) {
200
216
assert . ok ( err ) ;
201
217
assert . equal ( err . code , 'invalid_token' ) ;
202
218
assert . equal ( err . message , 'invalid signature' ) ;
@@ -215,7 +231,7 @@ describe('work tests', function () {
215
231
216
232
req . headers = { } ;
217
233
req . headers . authorization = 'Bearer ' + token ;
218
- expressjwt ( { secret : secret } ) ( req , res , function ( ) {
234
+ expressjwt ( { secret : secret , algorithms : [ 'HS256' ] } ) ( req , res , function ( ) {
219
235
assert . equal ( 'bar' , req . user . foo ) ;
220
236
} ) ;
221
237
} ) ;
@@ -226,7 +242,7 @@ describe('work tests', function () {
226
242
227
243
req . headers = { } ;
228
244
req . headers . authorization = 'Bearer ' + token ;
229
- expressjwt ( { secret : secret , requestProperty : 'auth.token' } ) ( req , res , function ( ) {
245
+ expressjwt ( { secret : secret , algorithms : [ 'HS256' ] , requestProperty : 'auth.token' } ) ( req , res , function ( ) {
230
246
assert . equal ( 'bar' , req . auth . token . foo ) ;
231
247
} ) ;
232
248
} ) ;
@@ -237,7 +253,7 @@ describe('work tests', function () {
237
253
238
254
req . headers = { } ;
239
255
req . headers . authorization = 'Bearer ' + token ;
240
- expressjwt ( { secret : secret } ) ( req , res , function ( ) {
256
+ expressjwt ( { secret : secret , algorithms : [ 'HS256' ] } ) ( req , res , function ( ) {
241
257
assert . equal ( 'bar' , req . user . foo ) ;
242
258
} ) ;
243
259
} ) ;
@@ -248,7 +264,7 @@ describe('work tests', function () {
248
264
249
265
req . headers = { } ;
250
266
req . headers . authorization = 'Bearer ' + token ;
251
- expressjwt ( { secret : secret , userProperty : 'auth' } ) ( req , res , function ( ) {
267
+ expressjwt ( { secret : secret , algorithms : [ 'HS256' ] , userProperty : 'auth' } ) ( req , res , function ( ) {
252
268
assert . equal ( 'bar' , req . auth . foo ) ;
253
269
} ) ;
254
270
} ) ;
@@ -261,7 +277,7 @@ describe('work tests', function () {
261
277
res = { } ;
262
278
req . headers = { } ;
263
279
req . headers . authorization = 'Bearer ' + token ;
264
- expressjwt ( { secret : secret , resultProperty : 'locals.user' } ) ( req , res , function ( ) {
280
+ expressjwt ( { secret : secret , algorithms : [ 'HS256' ] , resultProperty : 'locals.user' } ) ( req , res , function ( ) {
265
281
assert . equal ( 'bar' , res . locals . user . foo ) ;
266
282
assert . ok ( typeof req . user === 'undefined' ) ;
267
283
} ) ;
@@ -275,22 +291,22 @@ describe('work tests', function () {
275
291
res = { } ;
276
292
req . headers = { } ;
277
293
req . headers . authorization = 'Bearer ' + token ;
278
- expressjwt ( { secret : secret , userProperty : 'auth' , resultProperty : 'locals.user' } ) ( req , res , function ( ) {
294
+ expressjwt ( { secret : secret , algorithms : [ 'HS256' ] , userProperty : 'auth' , resultProperty : 'locals.user' } ) ( req , res , function ( ) {
279
295
assert . equal ( 'bar' , res . locals . user . foo ) ;
280
296
assert . ok ( typeof req . auth === 'undefined' ) ;
281
297
} ) ;
282
298
} ) ;
283
299
284
300
it ( 'should work if no authorization header and credentials are not required' , function ( ) {
285
301
req = { } ;
286
- expressjwt ( { secret : 'shhhh' , credentialsRequired : false } ) ( req , res , function ( err ) {
302
+ expressjwt ( { secret : 'shhhh' , algorithms : [ 'HS256' ] , credentialsRequired : false } ) ( req , res , function ( err ) {
287
303
assert ( typeof err === 'undefined' ) ;
288
304
} ) ;
289
305
} ) ;
290
306
291
307
it ( 'should not work if no authorization header' , function ( ) {
292
308
req = { } ;
293
- expressjwt ( { secret : 'shhhh' } ) ( req , res , function ( err ) {
309
+ expressjwt ( { secret : 'shhhh' , algorithms : [ 'HS256' ] } ) ( req , res , function ( err ) {
294
310
assert ( typeof err !== 'undefined' ) ;
295
311
} ) ;
296
312
} ) ;
@@ -301,7 +317,7 @@ describe('work tests', function () {
301
317
req . headers = { } ;
302
318
req . headers . authorization = 'Bearer ' + token ;
303
319
304
- expressjwt ( { secret : 'secretB' } ) ( req , res , function ( err ) {
320
+ expressjwt ( { secret : 'secretB' , algorithms : [ 'HS256' ] } ) ( req , res , function ( err ) {
305
321
var index = err . stack . indexOf ( 'UnauthorizedError: invalid signature' )
306
322
assert . equal ( index , 0 , "Stack trace didn't include 'invalid signature' message." )
307
323
} ) ;
@@ -322,6 +338,7 @@ describe('work tests', function () {
322
338
323
339
expressjwt ( {
324
340
secret : secret ,
341
+ algorithms : [ 'HS256' ] ,
325
342
getToken : getTokenFromQuery
326
343
} ) ( req , res , function ( ) {
327
344
assert . equal ( 'bar' , req . user . foo ) ;
@@ -339,7 +356,7 @@ describe('work tests', function () {
339
356
340
357
req . headers = { } ;
341
358
req . headers . authorization = 'Bearer ' + token ;
342
- expressjwt ( { secret : secretCallback } ) ( req , res , function ( ) {
359
+ expressjwt ( { secret : secretCallback , algorithms : [ 'HS256' ] } ) ( req , res , function ( ) {
343
360
assert . equal ( 'bar' , req . user . foo ) ;
344
361
} ) ;
345
362
} ) ;
0 commit comments