/**
 * Builds and configure a Passport strategy to authenticate the proxy clients.
 */ import * as express from "express" ; import { Either } from "fp-ts/lib/Either" ; import { Option } from "fp-ts/lib/Option" ; import * as passport from "passport-http-bearer" ; import { ISessionStorage } from "../services/ISessionStorage" ; import { MyPortalToken } from "../types/token" ; import { User } from "../types/user" ; import { fulfill , StrategyDoneFunction } from "../utils/strategies" ; const bearerMyPortalTokenStrategy = ( sessionStorage ) : passport . Strategy < passport . VerifyFunctionWithRequest > => { const options = { passReqToCallback : true , realm : "Proxy API" , scope : "request" } ; return new passport . Strategy < passport . VerifyFunctionWithRequest > ( options , ( _ , token , done ) => { sessionStorage . getByMyPortalToken ( token as MyPortalToken ) . then ( ( errorOrUser ) => { try { fulfill ( errorOrUser , done ) ; } catch ( e ) { done ( e ) ; } } , ( ) => { try { done ( undefined , false ) ; } catch ( e ) { done ( e ) ; } } ) ; } ) ; } ; export default bearerMyPortalTokenStrategy ;