UNPKG

passport-unique-token

Version:

Unique Token authentication strategy for Passport.

84 lines (83 loc) 3.16 kB
import { Strategy } from 'passport-strategy'; import express from 'express'; declare type DoneCallback = (err: Error | null, user?: unknown, info?: unknown) => void; export declare type VerifyFunctionWithRequest = (req: express.Request, token: string, done: DoneCallback) => void; export declare type VerifyFunction = (token: string, done: DoneCallback) => void; export interface UniqueTokenOptions { [key: string]: any; tokenField?: string; tokenQuery?: string; tokenParams?: string; tokenHeader?: string; passReqToCallback?: false; caseSensitive?: boolean; failOnMissing?: boolean; } export interface UniqueTokenOptionsWithRequest { [key: string]: any; tokenField?: string; tokenQuery?: string; tokenParams?: string; tokenHeader?: string; passReqToCallback: true; caseSensitive?: boolean; failOnMissing?: boolean; } export interface UniqueTokenAuthenticateOptions { badRequestMessage?: string; } /** * `Strategy` class. * * The token authentication strategy authenticates requests based on the * credentials submitted through standard request headers, body, querystring or params. * * Applications must supply a `verify` callback which accepts * unique `token` credentials, and then calls the `done` callback supplying a * `user`, which should be set to `false` if the credentials are not valid. * If an exception occured, `err` should be set. * * Optionally, `options` can be used to change the fields in which the * credentials are found. * * Options: * * - `tokenField` field name where the token is found, defaults to 'token' * - `tokenQuery` query string name where the token is found, defaults to 'token' * - `tokenParams` params name where the token is found, defaults to 'token' * - `tokenHeader` header name where the token is found, defaults to 'token' * - `passReqToCallback` when `true`, `req` is the first argument to the verify callback (default: `false`) * - `failOnMissing` when `false`, if the token is not found it will not fail (default: `true`) * - `caseSensitive` when `true` the token validation is case Sensitive (default: `false`) * * Examples: * * passport.use(new UniqueTokenStrategy( * function(token, done) { * User.findOne({ token: token }, function (err, user) { * done(err, user); * }); * } * )); * * @param {UniqueTokenOptions | UniqueTokenOptionsWithRequest} options * @param {VerifyFunction | VerifyFunctionWithRequest} verify * @api public */ export declare class UniqueTokenStrategy extends Strategy { name: string; private defaultToken; private tokenField; private tokenQuery; private tokenParams; private tokenHeader; private failOnMissing; private passReqToCallback; private verify; constructor(options: UniqueTokenOptionsWithRequest, verify: VerifyFunctionWithRequest); constructor(options: UniqueTokenOptions, verify: VerifyFunction); constructor(verify: VerifyFunction); authenticate(req: express.Request, options?: UniqueTokenAuthenticateOptions): void; private sanitizeToken; } export {};