jwt-fp
Version:
A wrapper on top of jsonwebtoken package that uses data structures from fp-ts
54 lines (53 loc) • 2.43 kB
TypeScript
/// <reference types="node" />
import { Either } from 'fp-ts/lib/Either';
import * as t from 'io-ts';
import * as jwt from 'jsonwebtoken';
export declare type Algorithm = 'HS256' | 'HS384' | 'HS512' | 'RS256' | 'RS384' | 'RS512' | 'ES256' | 'ES384' | 'ES512' | 'none';
export interface ISignOptions extends jwt.SignOptions {
algorithm: Algorithm;
}
declare const SignPayload: t.IntersectionC<[t.PartialC<{
exp: t.NumberC;
iat: t.NumberC;
nbf: t.NumberC;
}>, t.TypeC<{
data: t.ObjectC;
}>]>;
export interface ISignPayload extends t.TypeOf<typeof SignPayload> {
}
export interface IVerifyOptions extends jwt.VerifyOptions {
algorithm: Algorithm;
}
declare const VerifyPayload: t.IntersectionC<[t.PartialC<{
exp: t.NumberC;
nbf: t.NumberC;
}>, t.TypeC<{
data: t.ObjectC;
iat: t.NumberC;
}>]>;
export interface IVerifyPayload extends t.TypeOf<typeof VerifyPayload> {
}
export { TokenExpiredError, JsonWebTokenError, NotBeforeError } from 'jsonwebtoken';
export declare type VerifyError = jwt.TokenExpiredError | jwt.JsonWebTokenError | jwt.NotBeforeError;
/**
* Sign the given payload into a JSON Web Token string
* @param {SignPayload} payload - Payload to sign, could be an literal, buffer or string
* @param {String|Buffer} secretOrPrivateKey - Either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA.
* @param {ISignOptions} [options] - Options for the signature
* @returns {String} The JSON Web Token string
*/
export declare const sign: (payload: ISignPayload, secretOrPrivateKey: string | Buffer, options?: ISignOptions | undefined) => string;
/**
* Verify given token using a secret or a public key to get a decoded token
* @param {String} token - JWT string to verify
* @param {String|Buffer} secretOrPublicKey - Either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA.
* @param {VerifyOptions} [options] - Options for the verification
* @returns {Either<VerifyError, string>} Either an error or the decoded string.
*/
export declare const verify: (token: string, secretOrPublicKey: string | Buffer, options?: IVerifyOptions | undefined) => Either<jwt.VerifyErrors, IVerifyPayload>;
/**
* Returns `true` if err is a `VerifyError`, `false` otherwise
* @param {Error} err
* @returns {Boolean} Whether err is a `VerifyError` or not
*/
export declare const isVerifyError: (err: Error) => err is jwt.VerifyErrors;