Passed
Push — master ( 31034c...d0aa87 )
by Aaron
01:57
created

main/src/verify.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 43
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 36
mnd 1
bc 1
fnc 0
dl 0
loc 43
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import {
2
    decode as jwtDecode,
3
    GetPublicKeyOrSecret,
4
    verify as jwtVerify,
5
    VerifyOptions
6
} from 'jsonwebtoken'
7
import depromisify, { SecretOrPublicKeyCb } from './depromisifi'
8
9
export { VerifyOptions }
10
11
export type SecretOrPublicKey = string | Buffer | GetPublicKeyOrSecret
12
13
export interface Jwt {
14
  header: {
15
    alg?: string,
16
    typ?: string,
17
    [key: string]: any
18
  },
19
  payload: { [key: string]: any },
20
  signature: string
21
}
22
23
const decode = (token: string): Jwt => jwtDecode(token, { complete: true }) as Jwt
24
25
export type VerifyCallback = (error: any, jwt?: Jwt) => void
26
27
export const verify = (
28
    token: string,
29
    secretOrPublicKey: SecretOrPublicKey,
30
    options: VerifyOptions,
31
    callback: VerifyCallback
32
) => {
33
  const secretOrPublicKeyCb: SecretOrPublicKeyCb = depromisify(secretOrPublicKey)
34
  jwtVerify(token, secretOrPublicKeyCb, { ...options }, (error) => {
35
    if (error) {
36
      callback(error)
37
    }
38
    callback(null, decode(token))
39
  })
40
}
41
42
export default verify
43