1
1
'use strict'
2
2
3
- const crypto = require ( 'crypto' )
4
- const b64url = require ( 'base64-url' )
5
- const inherits = require ( 'util' ) . inherits
6
- const parse = require ( 'json-parse-safe' )
7
- const extend = require ( 'xtend' )
8
- const isObject = require ( 'is.object' )
3
+ const crypto = require ( 'crypto' )
4
+ const b64url = require ( 'base64-url' )
5
+ const inherits = require ( 'util' ) . inherits
6
+ const parse = require ( 'json-parse-safe' )
7
+ const extend = require ( 'xtend' )
8
+ const isObject = require ( 'is.object' )
9
9
10
10
//
11
11
// supported algorithms
12
12
//
13
13
14
14
const algorithms = {
15
- HS256 : { hash : 'sha256' , type : 'hmac' } ,
16
- HS384 : { hash : 'sha384' , type : 'hmac' } ,
17
- HS512 : { hash : 'sha512' , type : 'hmac' } ,
18
- RS256 : { hash : 'RSA-SHA256' , type : 'sign' }
15
+ HS256 : { hash : 'sha256' , type : 'hmac' } ,
16
+ HS384 : { hash : 'sha384' , type : 'hmac' } ,
17
+ HS512 : { hash : 'sha512' , type : 'hmac' } ,
18
+ RS256 : { hash : 'RSA-SHA256' , type : 'sign' }
19
19
}
20
20
21
21
//
@@ -24,30 +24,30 @@ const algorithms = {
24
24
25
25
const jwt = module . exports
26
26
27
- jwt . JWTError = JWTError
27
+ jwt . JWTError = JWTError
28
28
jwt . getAlgorithms = getAlgorithms
29
- jwt . encode = encode
30
- jwt . decode = decode
29
+ jwt . encode = encode
30
+ jwt . decode = decode
31
31
32
32
function getAlgorithms ( ) {
33
33
return Object . keys ( algorithms )
34
34
}
35
35
36
36
function encode ( key , data , algorithm , cb ) {
37
- if ( paramIsValid ( algorithm , 'function' ) ) {
37
+ if ( isFunction ( algorithm , 'function' ) ) {
38
38
cb = algorithm
39
39
algorithm = 'HS256'
40
40
}
41
41
42
- var defaultHeader = { typ : 'JWT' , alg : algorithm }
42
+ var defaultHeader = { typ : 'JWT' , alg : algorithm }
43
43
44
- var payload = isObject ( data ) && data . payload ?
45
- data . payload :
46
- data
44
+ var payload = isObject ( data ) && data . payload
45
+ ? data . payload
46
+ : data
47
47
48
- var header = isObject ( data ) && data . header ?
49
- extend ( data . header , defaultHeader ) :
50
- defaultHeader
48
+ var header = isObject ( data ) && data . header
49
+ ? extend ( data . header , defaultHeader )
50
+ : defaultHeader
51
51
52
52
const validationError = encodeValidations ( key , payload , algorithm )
53
53
@@ -101,15 +101,15 @@ function decode (key, token, cb) {
101
101
parts [ 2 ]
102
102
)
103
103
104
- return prcResult ( ! res && 'Invalid key!' || null , payload , header , cb )
104
+ return prcResult ( ( ! res && 'Invalid key!' ) || null , payload , header , cb )
105
105
}
106
106
107
107
function encodeValidations ( key , payload , algorithm ) {
108
- return paramsAreFalsy ( key , payload ) ?
109
- 'The key and payload are mandatory!' :
110
- ! Object . keys ( payload ) . length ?
111
- 'The payload is an empty object!' :
112
- ! algorithms [ algorithm ] && 'The algorithm is not supported!'
108
+ return paramsAreFalsy ( key , payload )
109
+ ? 'The key and payload are mandatory!'
110
+ : ! Object . keys ( payload ) . length
111
+ ? 'The payload is an empty object!'
112
+ : ! algorithms [ algorithm ] && 'The algorithm is not supported!'
113
113
}
114
114
115
115
//
@@ -130,41 +130,41 @@ inherits(JWTError, Error)
130
130
//
131
131
132
132
function sign ( alg , key , input ) {
133
- return 'hmac' === alg . type ?
134
- b64url . escape ( crypto . createHmac ( alg . hash , key )
133
+ return alg . type === 'hmac'
134
+ ? b64url . escape ( crypto . createHmac ( alg . hash , key )
135
135
. update ( input )
136
- . digest ( 'base64' ) ) :
137
- b64url . escape ( crypto . createSign ( alg . hash )
136
+ . digest ( 'base64' ) )
137
+ : b64url . escape ( crypto . createSign ( alg . hash )
138
138
. update ( input )
139
139
. sign ( key , 'base64' ) )
140
140
}
141
141
142
142
function verify ( alg , key , input , signVar ) {
143
- return 'hmac' === alg . type ?
144
- signVar === sign ( alg , key , input ) :
145
- crypto . createVerify ( alg . hash )
143
+ return alg . type === 'hmac'
144
+ ? signVar === sign ( alg , key , input )
145
+ : crypto . createVerify ( alg . hash )
146
146
. update ( input )
147
147
. verify ( key , b64url . unescape ( signVar ) , 'base64' )
148
148
}
149
149
150
150
function prcResult ( err , payload , header , cb ) {
151
- if ( paramIsValid ( header , 'function' ) ) {
151
+ if ( isFunction ( header , 'function' ) ) {
152
152
cb = header
153
153
header = undefined
154
154
}
155
155
156
156
err = err && new JWTError ( err )
157
157
158
- return cb ?
159
- cb ( err , payload , header ) :
160
- ( header ?
161
- { error : err , value : payload , header : header } :
162
- { error : err , value : payload }
163
- )
158
+ return cb
159
+ ? cb ( err , payload , header )
160
+ : ( header
161
+ ? { error : err , value : payload , header : header }
162
+ : { error : err , value : payload }
163
+ )
164
164
}
165
165
166
- function paramIsValid ( param , type ) {
167
- return ! param || typeof param === type
166
+ function isFunction ( param ) {
167
+ return ! param || typeof param === 'function'
168
168
}
169
169
170
170
function paramsAreFalsy ( param1 , param2 ) {
@@ -174,5 +174,5 @@ function paramsAreFalsy (param1, param2) {
174
174
function JSONParse ( str ) {
175
175
const res = parse ( str )
176
176
177
- return res . error && '' || res . value
177
+ return res . error ? '' : res . value
178
178
}
0 commit comments