UNPKG

9.68 kBTypeScriptView Raw
1// Type definitions for jsonwebtoken 9.0
2// Project: https://github.com/auth0/node-jsonwebtoken
3// Definitions by: Maxime LUCE <https://github.com/SomaticIT>,
4// Daniel Heim <https://github.com/danielheim>,
5// Brice BERNARD <https://github.com/brikou>,
6// Veli-Pekka Kestilä <https://github.com/vpk>,
7// Daniel Parker <https://github.com/GeneralistDev>,
8// Kjell Dießel <https://github.com/kettil>,
9// Robert Gajda <https://github.com/RunAge>,
10// Nico Flaig <https://github.com/nflaig>,
11// Linus Unnebäck <https://github.com/LinusU>
12// Ivan Sieder <https://github.com/ivansieder>
13// Piotr Błażejewicz <https://github.com/peterblazejewicz>
14// Nandor Kraszlan <https://github.com/nandi95>
15// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
16
17/// <reference types="node" />
18
19import { KeyObject } from 'crypto';
20
21export class JsonWebTokenError extends Error {
22 inner: Error;
23
24 constructor(message: string, error?: Error);
25}
26
27export class TokenExpiredError extends JsonWebTokenError {
28 expiredAt: Date;
29
30 constructor(message: string, expiredAt: Date);
31}
32
33/**
34 * Thrown if current time is before the nbf claim.
35 */
36export class NotBeforeError extends JsonWebTokenError {
37 date: Date;
38
39 constructor(message: string, date: Date);
40}
41
42export interface SignOptions {
43 /**
44 * Signature algorithm. Could be one of these values :
45 * - HS256: HMAC using SHA-256 hash algorithm (default)
46 * - HS384: HMAC using SHA-384 hash algorithm
47 * - HS512: HMAC using SHA-512 hash algorithm
48 * - RS256: RSASSA using SHA-256 hash algorithm
49 * - RS384: RSASSA using SHA-384 hash algorithm
50 * - RS512: RSASSA using SHA-512 hash algorithm
51 * - ES256: ECDSA using P-256 curve and SHA-256 hash algorithm
52 * - ES384: ECDSA using P-384 curve and SHA-384 hash algorithm
53 * - ES512: ECDSA using P-521 curve and SHA-512 hash algorithm
54 * - none: No digital signature or MAC value included
55 */
56 algorithm?: Algorithm | undefined;
57 keyid?: string | undefined;
58 /** expressed in seconds or a string describing a time span [zeit/ms](https://github.com/zeit/ms.js). Eg: 60, "2 days", "10h", "7d" */
59 expiresIn?: string | number | undefined;
60 /** expressed in seconds or a string describing a time span [zeit/ms](https://github.com/zeit/ms.js). Eg: 60, "2 days", "10h", "7d" */
61 notBefore?: string | number | undefined;
62 audience?: string | string[] | undefined;
63 subject?: string | undefined;
64 issuer?: string | undefined;
65 jwtid?: string | undefined;
66 mutatePayload?: boolean | undefined;
67 noTimestamp?: boolean | undefined;
68 header?: JwtHeader | undefined;
69 encoding?: string | undefined;
70 allowInsecureKeySizes?: boolean | undefined;
71 allowInvalidAsymmetricKeyTypes?: boolean | undefined;
72}
73
74export interface VerifyOptions {
75 algorithms?: Algorithm[] | undefined;
76 audience?: string | RegExp | Array<string | RegExp> | undefined;
77 clockTimestamp?: number | undefined;
78 clockTolerance?: number | undefined;
79 /** return an object with the decoded `{ payload, header, signature }` instead of only the usual content of the payload. */
80 complete?: boolean | undefined;
81 issuer?: string | string[] | undefined;
82 ignoreExpiration?: boolean | undefined;
83 ignoreNotBefore?: boolean | undefined;
84 jwtid?: string | undefined;
85 /**
86 * If you want to check `nonce` claim, provide a string value here.
87 * It is used on Open ID for the ID Tokens. ([Open ID implementation notes](https://openid.net/specs/openid-connect-core-1_0.html#NonceNotes))
88 */
89 nonce?: string | undefined;
90 subject?: string | undefined;
91 maxAge?: string | number | undefined;
92 allowInvalidAsymmetricKeyTypes?: boolean | undefined;
93}
94
95export interface DecodeOptions {
96 complete?: boolean | undefined;
97 json?: boolean | undefined;
98}
99export type VerifyErrors =
100 | JsonWebTokenError
101 | NotBeforeError
102 | TokenExpiredError;
103export type VerifyCallback<T = Jwt | JwtPayload | string> = (
104 error: VerifyErrors | null,
105 decoded: T | undefined,
106) => void;
107
108export type SignCallback = (
109 error: Error | null,
110 encoded: string | undefined,
111) => void;
112
113// standard names https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1
114export interface JwtHeader {
115 alg: string | Algorithm;
116 typ?: string | undefined;
117 cty?: string | undefined;
118 crit?: Array<string | Exclude<keyof JwtHeader, 'crit'>> | undefined;
119 kid?: string | undefined;
120 jku?: string | undefined;
121 x5u?: string | string[] | undefined;
122 'x5t#S256'?: string | undefined;
123 x5t?: string | undefined;
124 x5c?: string | string[] | undefined;
125}
126
127// standard claims https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
128export interface JwtPayload {
129 [key: string]: any;
130 iss?: string | undefined;
131 sub?: string | undefined;
132 aud?: string | string[] | undefined;
133 exp?: number | undefined;
134 nbf?: number | undefined;
135 iat?: number | undefined;
136 jti?: string | undefined;
137}
138
139export interface Jwt {
140 header: JwtHeader;
141 payload: JwtPayload | string;
142 signature: string;
143}
144
145// https://github.com/auth0/node-jsonwebtoken#algorithms-supported
146export type Algorithm =
147 "HS256" | "HS384" | "HS512" |
148 "RS256" | "RS384" | "RS512" |
149 "ES256" | "ES384" | "ES512" |
150 "PS256" | "PS384" | "PS512" |
151 "none";
152
153export type SigningKeyCallback = (
154 error: Error | null,
155 signingKey?: Secret
156) => void;
157
158export type GetPublicKeyOrSecret = (
159 header: JwtHeader,
160 callback: SigningKeyCallback
161) => void;
162
163export type Secret =
164 | string
165 | Buffer
166 | KeyObject
167 | { key: string | Buffer; passphrase: string };
168
169/**
170 * Synchronously sign the given payload into a JSON Web Token string
171 * payload - Payload to sign, could be an literal, buffer or string
172 * secretOrPrivateKey - Either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA.
173 * [options] - Options for the signature
174 * returns - The JSON Web Token string
175 */
176export function sign(
177 payload: string | Buffer | object,
178 secretOrPrivateKey: Secret,
179 options?: SignOptions,
180): string;
181export function sign(
182 payload: string | Buffer | object,
183 secretOrPrivateKey: null,
184 options?: SignOptions & { algorithm: "none" },
185): string;
186
187/**
188 * Sign the given payload into a JSON Web Token string
189 * payload - Payload to sign, could be an literal, buffer or string
190 * secretOrPrivateKey - Either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA.
191 * [options] - Options for the signature
192 * callback - Callback to get the encoded token on
193 */
194export function sign(
195 payload: string | Buffer | object,
196 secretOrPrivateKey: Secret,
197 callback: SignCallback,
198): void;
199export function sign(
200 payload: string | Buffer | object,
201 secretOrPrivateKey: Secret,
202 options: SignOptions,
203 callback: SignCallback,
204): void;
205export function sign(
206 payload: string | Buffer | object,
207 secretOrPrivateKey: null,
208 options: SignOptions & { algorithm: "none" },
209 callback: SignCallback,
210): void;
211
212/**
213 * Synchronously verify given token using a secret or a public key to get a decoded token
214 * token - JWT string to verify
215 * secretOrPublicKey - Either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA.
216 * [options] - Options for the verification
217 * returns - The decoded token.
218 */
219export function verify(token: string, secretOrPublicKey: Secret, options: VerifyOptions & { complete: true }): Jwt;
220export function verify(token: string, secretOrPublicKey: Secret, options?: VerifyOptions & { complete?: false }): JwtPayload | string;
221export function verify(token: string, secretOrPublicKey: Secret, options?: VerifyOptions): Jwt | JwtPayload | string;
222
223/**
224 * Asynchronously verify given token using a secret or a public key to get a decoded token
225 * token - JWT string to verify
226 * secretOrPublicKey - A string or buffer containing either the secret for HMAC algorithms,
227 * or the PEM encoded public key for RSA and ECDSA. If jwt.verify is called asynchronous,
228 * secretOrPublicKey can be a function that should fetch the secret or public key
229 * [options] - Options for the verification
230 * callback - Callback to get the decoded token on
231 */
232export function verify(
233 token: string,
234 secretOrPublicKey: Secret | GetPublicKeyOrSecret,
235 callback?: VerifyCallback<JwtPayload | string>,
236): void;
237export function verify(
238 token: string,
239 secretOrPublicKey: Secret | GetPublicKeyOrSecret,
240 options: VerifyOptions & { complete: true },
241 callback?: VerifyCallback<Jwt>,
242): void;
243export function verify(
244 token: string,
245 secretOrPublicKey: Secret | GetPublicKeyOrSecret,
246 options?: VerifyOptions & { complete?: false },
247 callback?: VerifyCallback<JwtPayload | string>,
248): void;
249export function verify(
250 token: string,
251 secretOrPublicKey: Secret | GetPublicKeyOrSecret,
252 options?: VerifyOptions,
253 callback?: VerifyCallback,
254): void;
255
256/**
257 * Returns the decoded payload without verifying if the signature is valid.
258 * token - JWT string to decode
259 * [options] - Options for decoding
260 * returns - The decoded Token
261 */
262export function decode(token: string, options: DecodeOptions & { complete: true }): null | Jwt;
263export function decode(token: string, options: DecodeOptions & { json: true }): null | JwtPayload;
264export function decode(token: string, options?: DecodeOptions): null | JwtPayload | string;