-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
jsonwebtoken_v8.5.x.js
164 lines (134 loc) · 4.43 KB
/
jsonwebtoken_v8.5.x.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
declare module "jsonwebtoken" {
declare class JsonWebTokenError extends Error {
name: string;
message: string;
inner: Error;
}
declare class TokenExpiredError extends Error {
name: string;
expiredAt: number;
inner: Error;
}
declare class NotBeforeError extends Error {
name: string;
date: Date;
inner: Error;
}
declare type Encodable = String | Buffer | Object;
declare type Key = {
key: string | Buffer,
passphrase: string | Buffer,
...
};
declare type Algorithm =
'RS256'
| 'RS384'
| 'RS512'
| 'ES256'
| 'ES384'
| 'ES512'
| 'HS256'
| 'HS384'
| 'HS512'
| 'none';
declare type SignCallback = (err: Error, token: string) => void;
declare type SigningOptions<Headers> = $Shape<{
algorithm: Algorithm,
expiresIn: number | string,
notBefore: number | string,
audience: string | string[],
issuer: string | string[],
jwtid: string,
subject: string,
noTimestamp: boolean,
header: Headers,
keyid: string,
...
}>;
declare type SigningOptionsWithAlgorithm<H> = SigningOptions<H> & { algorithm: Algorithm, ... };
declare type VerifyCallback = (err: JsonWebTokenError | NotBeforeError | TokenExpiredError | null, decoded: Payload) => void;
declare type VerifyOptionsWithAlgorithm = VerifyOptions & { algorithms: Array<Algorithm>, ... };
declare type VerifyOptions = $Shape<{
algorithms: Array<Algorithm>,
audience: string | string[],
issuer: string | string[],
ignoreExpiration: boolean,
ignoreNotBefore: boolean,
subject: string | string[],
clockTolerance: number,
maxAge: string | number,
clockTimestamp: number,
nonce?: string,
/** return an object with the decoded `{ payload, header, signature }` instead of only the usual content of the payload. */
complete?: boolean,
...
}>;
declare type DecodingOptions = $Shape<{
complete: boolean,
json: boolean,
...
}>;
declare interface Sign {
<P: Encodable>
(payload: P, secretOrPrivateKey: string | Buffer): string;
<P: Encodable>
(payload: P, secretOrPrivateKey: string | Buffer, callback: SignCallback): string;
<P: Encodable, H>
(payload: P, secretOrPrivateKey: Key, options: SigningOptionsWithAlgorithm<H>): string;
<P: Encodable, H>
(payload: P, secretOrPrivateKey: string | Buffer, options: $Shape<SigningOptions<H>>): string;
<P: Encodable, H>
(payload: P, secretOrPrivateKey: string | Buffer, options: $Shape<SigningOptions<H>>, callback: SignCallback): string;
<P: Encodable, H>
(payload: P, secretOrPrivateKey: Key, options: SigningOptionsWithAlgorithm<H>, callback: SignCallback): string;
}
declare type Payload = Object & {
jti?: string,
iss?: string,
sub?: string,
aud?: string | string[],
exp?: number,
iat?: number,
nbf?: number,
...
}
declare type Token = {
header: {
typ: 'JWT',
alg: Algorithm,
...
},
payload: Payload,
signature?: string,
...
}
declare interface Decode {
(jwt: string): Payload;
(jwt: string, options: DecodingOptions): Payload;
(jwt: string, options: DecodingOptions & { complete: true, ... }): Token;
}
declare interface Verify {
(jwt: string, secretOrPrivateKey: string | Buffer): Payload;
(jwt: string, secretOrPrivateKey: string | Buffer, options: VerifyOptions | VerifyCallback): Payload;
(jwt: string, secretOrPrivateKey: string | Buffer, options: VerifyOptions, callback: VerifyCallback): Payload;
(jwt: string, secretOrPrivateKey: Key, options: VerifyOptionsWithAlgorithm): Payload;
(jwt: string, secretOrPrivateKey: Key, options: VerifyOptionsWithAlgorithm, callback: VerifyCallback): Payload;
(jwt: string, getKey: (header: { kid: ?string, ... }, callback: (err: ?Error, key?: string) => any) => any, callback: VerifyCallback): Payload;
(jwt: string, getKey: (header: { kid: ?string, ... }, callback: (err: ?Error, key?: string) => any) => any, options: VerifyOptionsWithAlgorithm, callback: VerifyCallback): Payload;
}
declare class TokenExpiredError extends Error {
}
declare class WebTokenError extends Error {
}
declare class NotBeforeError extends Error {
}
declare module.exports: {
sign: Sign,
decode: Decode,
verify: Verify,
JsonWebTokenError: Class<WebTokenError>,
NotBeforeError: Class<NotBeforeError>,
TokenExpiredError: Class<TokenExpiredError>,
...
}
}