Total Complexity | 1 |
Complexity/F | 0 |
Lines of Code | 43 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 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 |