File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change 1
1
var jws = require ( 'jws' ) ;
2
2
3
3
module . exports . decode = function ( jwt ) {
4
- return jws . decode ( jwt ) . payload ;
4
+ var decoded = jws . decode ( jwt ) ;
5
+ return decoded && decoded . payload ;
5
6
} ;
6
7
7
8
module . exports . sign = function ( payload , secretOrPrivateKey , options ) {
Original file line number Diff line number Diff line change @@ -135,5 +135,21 @@ describe('RS256', function() {
135
135
} ) ;
136
136
} ) ;
137
137
138
+ describe ( 'when decoding a invalid jwt token' , function ( ) {
139
+ it ( 'should return null' , function ( done ) {
140
+ var payload = jwt . decode ( 'whatever.token' ) ;
141
+ assert . isNull ( payload ) ;
142
+ done ( ) ;
143
+ } ) ;
144
+ } ) ;
138
145
146
+ describe ( 'when decoding a valid jwt token' , function ( ) {
147
+ it ( 'should return the payload' , function ( done ) {
148
+ var obj = { foo : 'bar' } ;
149
+ var token = jwt . sign ( obj , priv , { algorithm : 'RS256' } ) ;
150
+ var payload = jwt . decode ( token ) ;
151
+ assert . deepEqual ( payload , obj ) ;
152
+ done ( ) ;
153
+ } ) ;
154
+ } ) ;
139
155
} ) ;
You can’t perform that action at this time.
0 commit comments