1 |
|
2 |
|
3 | import type { createPrivateKey, createPublicKey, KeyObject } from "crypto";
|
4 |
|
5 | export class JsonWebTokenError extends Error {
|
6 | inner: Error;
|
7 |
|
8 | constructor(message: string, error?: Error);
|
9 | }
|
10 |
|
11 | export class TokenExpiredError extends JsonWebTokenError {
|
12 | expiredAt: Date;
|
13 |
|
14 | constructor(message: string, expiredAt: Date);
|
15 | }
|
16 |
|
17 | /**
|
18 | * Thrown if current time is before the nbf claim.
|
19 | */
|
20 | export class NotBeforeError extends JsonWebTokenError {
|
21 | date: Date;
|
22 |
|
23 | constructor(message: string, date: Date);
|
24 | }
|
25 |
|
26 | export interface SignOptions {
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | algorithm?: Algorithm | undefined;
|
41 | keyid?: string | undefined;
|
42 |
|
43 | expiresIn?: string | number;
|
44 |
|
45 | notBefore?: string | number | undefined;
|
46 | audience?: string | string[] | undefined;
|
47 | subject?: string | undefined;
|
48 | issuer?: string | undefined;
|
49 | jwtid?: string | undefined;
|
50 | mutatePayload?: boolean | undefined;
|
51 | noTimestamp?: boolean | undefined;
|
52 | header?: JwtHeader | undefined;
|
53 | encoding?: string | undefined;
|
54 | allowInsecureKeySizes?: boolean | undefined;
|
55 | allowInvalidAsymmetricKeyTypes?: boolean | undefined;
|
56 | }
|
57 |
|
58 | export interface VerifyOptions {
|
59 | algorithms?: Algorithm[] | undefined;
|
60 | audience?: string | RegExp | Array<string | RegExp> | undefined;
|
61 | clockTimestamp?: number | undefined;
|
62 | clockTolerance?: number | undefined;
|
63 |
|
64 | complete?: boolean | undefined;
|
65 | issuer?: string | string[] | undefined;
|
66 | ignoreExpiration?: boolean | undefined;
|
67 | ignoreNotBefore?: boolean | undefined;
|
68 | jwtid?: string | undefined;
|
69 | |
70 |
|
71 |
|
72 |
|
73 | nonce?: string | undefined;
|
74 | subject?: string | undefined;
|
75 | maxAge?: string | number | undefined;
|
76 | allowInvalidAsymmetricKeyTypes?: boolean | undefined;
|
77 | }
|
78 |
|
79 | export interface DecodeOptions {
|
80 | complete?: boolean | undefined;
|
81 | json?: boolean | undefined;
|
82 | }
|
83 | export type VerifyErrors =
|
84 | | JsonWebTokenError
|
85 | | NotBeforeError
|
86 | | TokenExpiredError;
|
87 | export type VerifyCallback<T = Jwt | JwtPayload | string> = (
|
88 | error: VerifyErrors | null,
|
89 | decoded: T | undefined,
|
90 | ) => void;
|
91 |
|
92 | export type SignCallback = (
|
93 | error: Error | null,
|
94 | encoded: string | undefined,
|
95 | ) => void;
|
96 |
|
97 |
|
98 | export interface JwtHeader {
|
99 | alg: string | Algorithm;
|
100 | typ?: string | undefined;
|
101 | cty?: string | undefined;
|
102 | crit?: Array<string | Exclude<keyof JwtHeader, "crit">> | undefined;
|
103 | kid?: string | undefined;
|
104 | jku?: string | undefined;
|
105 | x5u?: string | string[] | undefined;
|
106 | "x5t#S256"?: string | undefined;
|
107 | x5t?: string | undefined;
|
108 | x5c?: string | string[] | undefined;
|
109 | }
|
110 |
|
111 |
|
112 | export interface JwtPayload {
|
113 | [key: string]: any;
|
114 | iss?: string | undefined;
|
115 | sub?: string | undefined;
|
116 | aud?: string | string[] | undefined;
|
117 | exp?: number | undefined;
|
118 | nbf?: number | undefined;
|
119 | iat?: number | undefined;
|
120 | jti?: string | undefined;
|
121 | }
|
122 |
|
123 | export interface Jwt {
|
124 | header: JwtHeader;
|
125 | payload: JwtPayload | string;
|
126 | signature: string;
|
127 | }
|
128 |
|
129 |
|
130 | export type Algorithm =
|
131 | | "HS256"
|
132 | | "HS384"
|
133 | | "HS512"
|
134 | | "RS256"
|
135 | | "RS384"
|
136 | | "RS512"
|
137 | | "ES256"
|
138 | | "ES384"
|
139 | | "ES512"
|
140 | | "PS256"
|
141 | | "PS384"
|
142 | | "PS512"
|
143 | | "none";
|
144 |
|
145 | export type SigningKeyCallback = (
|
146 | error: Error | null,
|
147 | signingKey?: Secret | PublicKey,
|
148 | ) => void;
|
149 |
|
150 | export type GetPublicKeyOrSecret = (
|
151 | header: JwtHeader,
|
152 | callback: SigningKeyCallback,
|
153 | ) => void;
|
154 |
|
155 | export type PublicKey = Parameters<typeof createPublicKey>[0];
|
156 |
|
157 | export type PrivateKey = Parameters<typeof createPrivateKey>[0];
|
158 |
|
159 | export type Secret =
|
160 | | string
|
161 | | Buffer
|
162 | | KeyObject
|
163 | | { key: string | Buffer; passphrase: string };
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | export function sign(
|
173 | payload: string | Buffer | object,
|
174 | secretOrPrivateKey: Secret | PrivateKey,
|
175 | options?: SignOptions,
|
176 | ): string;
|
177 | export function sign(
|
178 | payload: string | Buffer | object,
|
179 | secretOrPrivateKey: null,
|
180 | options?: SignOptions & { algorithm: "none" },
|
181 | ): string;
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | export function sign(
|
191 | payload: string | Buffer | object,
|
192 | secretOrPrivateKey: Secret | PrivateKey,
|
193 | callback: SignCallback,
|
194 | ): void;
|
195 | export function sign(
|
196 | payload: string | Buffer | object,
|
197 | secretOrPrivateKey: Secret | PrivateKey,
|
198 | options: SignOptions,
|
199 | callback: SignCallback,
|
200 | ): void;
|
201 | export function sign(
|
202 | payload: string | Buffer | object,
|
203 | secretOrPrivateKey: null,
|
204 | options: SignOptions & { algorithm: "none" },
|
205 | callback: SignCallback,
|
206 | ): void;
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | export function verify(
|
216 | token: string,
|
217 | secretOrPublicKey: Secret | PublicKey,
|
218 | options: VerifyOptions & { complete: true },
|
219 | ): Jwt;
|
220 | export function verify(
|
221 | token: string,
|
222 | secretOrPublicKey: Secret | PublicKey,
|
223 | options?: VerifyOptions & { complete?: false },
|
224 | ): JwtPayload | string;
|
225 | export function verify(
|
226 | token: string,
|
227 | secretOrPublicKey: Secret | PublicKey,
|
228 | options?: VerifyOptions,
|
229 | ): Jwt | JwtPayload | string;
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | export function verify(
|
241 | token: string,
|
242 | secretOrPublicKey: Secret | PublicKey | GetPublicKeyOrSecret,
|
243 | callback?: VerifyCallback<JwtPayload | string>,
|
244 | ): void;
|
245 | export function verify(
|
246 | token: string,
|
247 | secretOrPublicKey: Secret | PublicKey | GetPublicKeyOrSecret,
|
248 | options: VerifyOptions & { complete: true },
|
249 | callback?: VerifyCallback<Jwt>,
|
250 | ): void;
|
251 | export function verify(
|
252 | token: string,
|
253 | secretOrPublicKey: Secret | PublicKey | GetPublicKeyOrSecret,
|
254 | options?: VerifyOptions & { complete?: false },
|
255 | callback?: VerifyCallback<JwtPayload | string>,
|
256 | ): void;
|
257 | export function verify(
|
258 | token: string,
|
259 | secretOrPublicKey: Secret | PublicKey | GetPublicKeyOrSecret,
|
260 | options?: VerifyOptions,
|
261 | callback?: VerifyCallback,
|
262 | ): void;
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | export function decode(token: string, options: DecodeOptions & { complete: true }): null | Jwt;
|
271 | export function decode(token: string, options: DecodeOptions & { json: true }): null | JwtPayload;
|
272 | export function decode(token: string, options?: DecodeOptions): null | JwtPayload | string;
|