Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x 4x 4x 3x 3x 3x 3x 3x 3x 3x 5x 5x 3x 1x | 'use strict'; import * as express from 'express'; import * as _ from 'lodash'; import * as passport from 'passport'; import { ServiceAuthenticator } from './server-types'; export interface PassportAuthenticatorOptions { authOptions?: passport.AuthenticateOptions; rolesKey?: string; strategyName?: string; serializeUser?: (user: any) => string | Promise<string>; deserializeUser?: (user: string) => any; } export class PassportAuthenticator implements ServiceAuthenticator { private authenticator: express.Handler; private options: PassportAuthenticatorOptions; constructor(strategy: passport.Strategy, Ioptions: PassportAuthenticatorOptions = {}) { this.options = options; const authStrategy = options.strategyName || strategy.name || 'default_strategy'; passport.use(authStrategy, strategy); this.authenticator = passport.authenticate(authStrategy, options.authOptions || {}); } public getMiddleware(): express.RequestHandler { return this.authenticator; } public getRoles(req: express.Request): Array<string> { const roleKey = this.options.rolesKey || 'roles'; return _.castArray(_.get(req.user, roleKey, [])); } public initialize(router: express.Router): void { router.use(passport.initialize()); const useSession = _.get(this.options, 'authOptions.session', true); Eif (useSession) { router.use(passport.session()); Eif (this.options.serializeUser && this.options.deserializeUser) { passport.serializeUser((user: any, done: (a: any, b: string) => void) => { Promise.resolve(this.options.serializeUser(user)) .then((result: string) => { done(null, result); }).catch((err: Error) => { done(err, null); }); }); passport.deserializeUser((user: string, done: (a: any, b: any) => void) => { Promise.resolve(this.options.deserializeUser(user)) .then((result: any) => { done(null, result); }).catch((err: Error) => { done(err, null); }); }); } } } } |