UNPKG

@snowtop/ent-passport

Version:

snowtop ent passport integration

63 lines (62 loc) 3.14 kB
/// <reference types="node" /> /// <reference types="node" /> import passport, { AuthenticateOptions } from "passport"; import { AuthHandler, AuthViewer } from "@snowtop/ent/auth"; import { IncomingMessage } from "http"; import { Strategy } from "passport-strategy"; import { Ent, ID, LoadEntOptions, Viewer, RequestContext } from "@snowtop/ent"; import { JwtFromRequestFunction, StrategyOptions, VerifyCallback } from "passport-jwt"; import jwt from "jsonwebtoken"; import { Express } from "express"; interface UserToViewerFunc { (context: RequestContext, user: any): Viewer | Promise<Viewer>; } export interface PassportAuthOptions { serializeViewer?(viewer: Viewer): unknown; deserializeViewer?(id: unknown): Promise<Viewer> | Viewer; userToViewer?: UserToViewerFunc; loadOptions?: LoadEntOptions<Ent, any>; } export declare class PassportAuthHandler implements AuthHandler { private options; constructor(options?: PassportAuthOptions); authViewer(context: RequestContext): Promise<Viewer<Ent<any> | null, ID | null> | null>; static testInitSessionBasedFunction(secret: string, options?: PassportAuthOptions): (app: Express) => void; } export declare class PassportStrategyHandler implements AuthHandler { private strategy; private options?; private reqToViewer?; constructor(strategy: passport.Strategy, options?: passport.AuthenticateOptions | undefined, reqToViewer?: UserToViewerFunc | undefined); authViewer(context: RequestContext): Promise<Viewer<Ent<any> | null, ID | null> | null>; static jwtHandler(opts: JwtHandlerOptions): PassportStrategyHandler; static testInitJWTFunction(opts: JwtHandlerOptions): (app: Express) => void; } interface JwtHandlerOptions { loaderOptions?: LoadEntOptions<Ent, any>; authOptions?: AuthenticateOptions; verifyFn?: VerifyCallback; strategyOpts?: StrategyOptions; secretOrKey?: string | Buffer; jwtFromRequest?: JwtFromRequestFunction; reqToViewer?: UserToViewerFunc; } interface LocalStrategyOptions { verifyFn: (context?: RequestContext) => AuthViewer | Promise<AuthViewer>; } export declare class LocalStrategy extends Strategy { private options; name: string; constructor(options: LocalStrategyOptions); authenticate(_req: IncomingMessage): Promise<AuthViewer>; } export declare function useAndAuth(context: RequestContext, strategy: passport.Strategy, options?: AuthenticateOptions): Promise<AuthViewer>; export declare function useAndVerifyAuth(context: RequestContext, verifyFn: () => Promise<Viewer | ID | null>, loadOptions?: LoadEntOptions<Ent, any>, options?: AuthenticateOptions): Promise<AuthViewer>; interface JWTOptions { viewerToPayload?: (v: Viewer) => string | object | Buffer; secretOrKey: jwt.Secret; signInOptions?: jwt.SignOptions; } export declare function defaultViewerToPayload(viewer: Viewer): {}; export declare function useAndVerifyAuthJWT(context: RequestContext, verifyFn: () => Promise<ID | Viewer | null>, jwtOptions: JWTOptions, loadOptions?: LoadEntOptions<Ent, any>, options?: AuthenticateOptions): Promise<[AuthViewer, string]>; export {};