11'use strict'
22
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' )
99
1010//
1111// supported algorithms
1212//
1313
1414const 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' }
1919}
2020
2121//
@@ -24,30 +24,30 @@ const algorithms = {
2424
2525const jwt = module . exports
2626
27- jwt . JWTError = JWTError
27+ jwt . JWTError = JWTError
2828jwt . getAlgorithms = getAlgorithms
29- jwt . encode = encode
30- jwt . decode = decode
29+ jwt . encode = encode
30+ jwt . decode = decode
3131
3232function getAlgorithms ( ) {
3333 return Object . keys ( algorithms )
3434}
3535
3636function encode ( key , data , algorithm , cb ) {
37- if ( paramIsValid ( algorithm , 'function' ) ) {
37+ if ( isFunction ( algorithm , 'function' ) ) {
3838 cb = algorithm
3939 algorithm = 'HS256'
4040 }
4141
42- var defaultHeader = { typ : 'JWT' , alg : algorithm }
42+ var defaultHeader = { typ : 'JWT' , alg : algorithm }
4343
44- var payload = isObject ( data ) && data . payload ?
45- data . payload :
46- data
44+ var payload = isObject ( data ) && data . payload
45+ ? data . payload
46+ : data
4747
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
5151
5252 const validationError = encodeValidations ( key , payload , algorithm )
5353
@@ -101,15 +101,15 @@ function decode (key, token, cb) {
101101 parts [ 2 ]
102102 )
103103
104- return prcResult ( ! res && 'Invalid key!' || null , payload , header , cb )
104+ return prcResult ( ( ! res && 'Invalid key!' ) || null , payload , header , cb )
105105}
106106
107107function 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!'
113113}
114114
115115//
@@ -130,41 +130,41 @@ inherits(JWTError, Error)
130130//
131131
132132function 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 )
135135 . update ( input )
136- . digest ( 'base64' ) ) :
137- b64url . escape ( crypto . createSign ( alg . hash )
136+ . digest ( 'base64' ) )
137+ : b64url . escape ( crypto . createSign ( alg . hash )
138138 . update ( input )
139139 . sign ( key , 'base64' ) )
140140}
141141
142142function 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 )
146146 . update ( input )
147147 . verify ( key , b64url . unescape ( signVar ) , 'base64' )
148148}
149149
150150function prcResult ( err , payload , header , cb ) {
151- if ( paramIsValid ( header , 'function' ) ) {
151+ if ( isFunction ( header , 'function' ) ) {
152152 cb = header
153153 header = undefined
154154 }
155155
156156 err = err && new JWTError ( err )
157157
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+ )
164164}
165165
166- function paramIsValid ( param , type ) {
167- return ! param || typeof param === type
166+ function isFunction ( param ) {
167+ return ! param || typeof param === 'function'
168168}
169169
170170function paramsAreFalsy ( param1 , param2 ) {
@@ -174,5 +174,5 @@ function paramsAreFalsy (param1, param2) {
174174function JSONParse ( str ) {
175175 const res = parse ( str )
176176
177- return res . error && '' || res . value
177+ return res . error ? '' : res . value
178178}
0 commit comments