UNPKG

alinea

Version:
132 lines (131 loc) 3.78 kB
declare const algorithms: { ES256: { name: string; namedCurve: string; hash: { name: string; }; }; ES384: { name: string; namedCurve: string; hash: { name: string; }; }; ES512: { name: string; namedCurve: string; hash: { name: string; }; }; HS256: { name: string; hash: { name: string; }; }; HS384: { name: string; hash: { name: string; }; }; HS512: { name: string; hash: { name: string; }; }; RS256: { name: string; hash: { name: string; }; }; RS384: { name: string; hash: { name: string; }; }; RS512: { name: string; hash: { name: string; }; }; }; type Algorithm = keyof typeof algorithms; type JWTHeader = { alg: Algorithm; kid?: string; typ?: string; cty?: string; crit?: string[]; [key: string]: any; }; type JWTPayload = Record<string, any>; type JWT = { header: JWTHeader; payload: JWTPayload; signature: Uint8Array; }; export declare function importKey(secret: string | JsonWebKey, algorithm: (typeof algorithms)[Algorithm], use: 'sign' | 'verify'): Promise<CryptoKey>; /** * Options for signing a JWT. * @property algorithm - The algorithm to use for signing (e.g., 'HS256'). * @property header - Optional custom header fields to include in the JWT header. */ export type SignOptions = { /** The algorithm to use for signing (e.g., 'HS256'). */ algorithm: Algorithm; /** Optional custom header fields to include in the JWT header. */ header?: Record<string, any>; }; /** * Signs a JWT payload and returns the token string. * @param payload - The payload to include in the JWT. * @param secret - The secret or key to sign the JWT with. * @param options - Options for signing, including algorithm and header. * @returns The signed JWT as a string. */ export declare function sign(payload: JWTPayload, secret: string | JsonWebKey, options?: SignOptions): Promise<string>; /** * Options for verifying a JWT. * @property algorithms - Allowed algorithms for verification. * @property clockTolerance - Allowed clock skew in seconds. * @property clockTimestamp - Override the current time for verification. */ export type VerifyOptions = { /** Allowed algorithms for verification. */ algorithms?: Array<Algorithm>; /** Allowed clock skew in seconds. */ clockTolerance?: number; /** Override the current time for verification. */ clockTimestamp?: number; }; /** * Verifies a JWT token using a secret or public key. * @param token - The JWT token string to verify. * @param secret - The secret to verify the JWT with. * @param options - Verification options. * @returns The decoded JWT payload if verification succeeds. */ export declare function verify<T = JWTPayload>(token: string, secret: string, options?: VerifyOptions): Promise<T>; /** * Verifies a JWT token using a public key. * @param token - The JWT token string to verify. * @param publicKey - The public key to verify the JWT with. * @param options - Verification options. * @returns The decoded JWT payload if verification succeeds. */ export declare function verify<T = JWTPayload>(token: string, publicKey: JsonWebKey, options?: VerifyOptions): Promise<T>; /** * Decodes a JWT token into its header, payload, and signature components. * @param token - The JWT token string to decode. * @returns The decoded JWT object. */ export declare function decode(token: string): JWT; export {};