File tree 2 files changed +23
-2
lines changed
2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ var JsonWebTokenError = module.exports.JsonWebTokenError = require('./lib/JsonWe
4
4
var TokenExpiredError = module . exports . TokenExpiredError = require ( './lib/TokenExpiredError' ) ;
5
5
6
6
module . exports . decode = function ( jwt , options ) {
7
+ options = options || { } ;
7
8
var decoded = jws . decode ( jwt , options ) ;
8
9
var payload = decoded && decoded . payload ;
9
10
@@ -12,11 +13,21 @@ module.exports.decode = function (jwt, options) {
12
13
try {
13
14
var obj = JSON . parse ( payload ) ;
14
15
if ( typeof obj === 'object' ) {
15
- return obj ;
16
+ payload = obj ;
16
17
}
17
18
} catch ( e ) { }
18
19
}
19
-
20
+
21
+ //return header if `complete` option is enabled. header includes claims
22
+ //such as `kid` and `alg` used to select the key within a JWKS needed to
23
+ //verify the signature
24
+ if ( options . complete === true ) {
25
+ return {
26
+ header : decoded . header ,
27
+ payload : payload ,
28
+ signature : decoded . signature
29
+ }
30
+ }
20
31
return payload ;
21
32
} ;
22
33
Original file line number Diff line number Diff line change @@ -275,5 +275,15 @@ describe('RS256', function() {
275
275
assert . deepEqual ( payload , obj ) ;
276
276
done ( ) ;
277
277
} ) ;
278
+ it ( 'should return the header and payload and signature if complete option is set' , function ( done ) {
279
+ var obj = { foo : 'bar' } ;
280
+ var token = jwt . sign ( obj , priv , { algorithm : 'RS256' } ) ;
281
+ var decoded = jwt . decode ( token , { complete : true } ) ;
282
+ console . log ( decoded ) ;
283
+ assert . deepEqual ( decoded . payload , obj ) ;
284
+ assert . deepEqual ( decoded . header , { typ : 'JWT' , alg : 'RS256' } ) ;
285
+ assert . ok ( typeof decoded . signature == 'string' ) ;
286
+ done ( ) ;
287
+ } ) ;
278
288
} ) ;
279
289
} ) ;
You can’t perform that action at this time.
0 commit comments