'use strict' ; import * as express from 'express' ; import * as _ from 'lodash' ; import * as passport from 'passport' ; import { ServiceAuthenticator } from '../server/model/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 , options = { } ) { this . options = options ; const authStrategy = options . strategyName || strategy . name || 'default_strategy' ; passport . use ( authStrategy , strategy ) ; this . authenticator = passport . authenticate ( authStrategy , options . authOptions || { } ) ; } public getMiddleware ( ) { return this . authenticator ; } public getRoles ( req ) { const roleKey = this . options . rolesKey || 'roles' ; return _ . castArray ( _ . get ( req . user , roleKey , [ ] ) ) ; } public initialize ( router ) { router . use ( passport . initialize ( ) ) ; const useSession = _ . get ( this . options , 'authOptions.session' , true ) ; if ( useSession ) { router . use ( passport . session ( ) ) ; if ( this . options . serializeUser && this . options . deserializeUser ) { passport . serializeUser ( ( user , done ) => { Promise . resolve ( this . options . serializeUser ( user ) ) . then ( ( result ) => { done ( null , result ) ; } ) . catch ( ( err ) => { done ( err , null ) ; } ) ; } ) ; passport . deserializeUser ( ( user , done ) => { Promise . resolve ( this . options . deserializeUser ( user ) ) . then ( ( result ) => { done ( null , result ) ; } ) . catch ( ( err ) => { done ( err , null ) ; } ) ; } ) ; } } } }