|
| 1 | +// SONAR-IGNORE-ALL |
| 2 | +import {inject, Provider} from '@loopback/core'; |
| 3 | +import {HttpErrors, Request} from '@loopback/rest'; |
| 4 | +import {AnyObject} from '@loopback/repository'; |
| 5 | +import {HttpsProxyAgent} from 'https-proxy-agent'; |
| 6 | +import {Profile, Strategy, VerifiedCallback} from 'passport-saml'; |
| 7 | +import { |
| 8 | + SamlConfig, |
| 9 | + StrategyOptions, |
| 10 | +} from 'passport-saml/lib/passport-saml/types'; |
| 11 | +import {AuthErrorKeys} from '../../error-keys'; |
| 12 | +import {Strategies} from '../../keys'; |
| 13 | +import {VerifyFunction} from '../../types'; |
| 14 | + |
| 15 | +export interface SamlStrategyFactory { |
| 16 | + (options: StrategyOptions, verifierPassed?: VerifyFunction.SamlFn): Strategy; |
| 17 | +} |
| 18 | + |
| 19 | +export class SamlStrategyFactoryProvider |
| 20 | + implements Provider<SamlStrategyFactory> |
| 21 | +{ |
| 22 | + constructor( |
| 23 | + @inject(Strategies.SAML_VERIFIER) |
| 24 | + private readonly verifierSaml: VerifyFunction.SamlFn, |
| 25 | + ) {} |
| 26 | + |
| 27 | + value(): SamlStrategyFactory { |
| 28 | + return (options, verifier) => |
| 29 | + this.getSamlStrategyVerifier(options, verifier); |
| 30 | + } |
| 31 | + |
| 32 | + getSamlStrategyVerifier( |
| 33 | + options: StrategyOptions, |
| 34 | + verifierPassed?: VerifyFunction.SamlFn, |
| 35 | + ): Strategy { |
| 36 | + const verifyFn = verifierPassed ?? this.verifierSaml; |
| 37 | + let strategy; |
| 38 | + const func = async ( |
| 39 | + req: Request, |
| 40 | + profile: Profile | null | undefined, |
| 41 | + cb: VerifiedCallback, |
| 42 | + ) => { |
| 43 | + try { |
| 44 | + const user = await verifyFn(profile, cb, req); |
| 45 | + if (!user) { |
| 46 | + throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); |
| 47 | + } |
| 48 | + cb(null, user as unknown as Record<string, unknown>); |
| 49 | + } catch (err) { |
| 50 | + cb(err); |
| 51 | + } |
| 52 | + }; |
| 53 | + if (options && options.passReqToCallback === true) { |
| 54 | + strategy = new Strategy( |
| 55 | + options as SamlConfig, |
| 56 | + // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 57 | + func, |
| 58 | + ); |
| 59 | + } else { |
| 60 | + strategy = new Strategy( |
| 61 | + options as SamlConfig, |
| 62 | + // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 63 | + async (profile: Profile | null | undefined, cb: VerifiedCallback) => { |
| 64 | + try { |
| 65 | + const user = await verifyFn(profile, cb); |
| 66 | + if (!user) { |
| 67 | + throw new HttpErrors.Unauthorized( |
| 68 | + AuthErrorKeys.InvalidCredentials, |
| 69 | + ); |
| 70 | + } |
| 71 | + cb(null, user as unknown as Record<string, unknown>); |
| 72 | + } catch (err) { |
| 73 | + cb(err); |
| 74 | + } |
| 75 | + }, |
| 76 | + ); |
| 77 | + } |
| 78 | + this._setupProxy(strategy); |
| 79 | + return strategy; |
| 80 | + } |
| 81 | + |
| 82 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 83 | + private _setupProxy(strategy: AnyObject) { |
| 84 | + // Setup proxy if any |
| 85 | + let httpsProxyAgent; |
| 86 | + if (process.env['https_proxy']) { |
| 87 | + httpsProxyAgent = new HttpsProxyAgent(process.env['https_proxy']); |
| 88 | + strategy._oauth2.setAgent(httpsProxyAgent); |
| 89 | + } else if (process.env['HTTPS_PROXY']) { |
| 90 | + httpsProxyAgent = new HttpsProxyAgent(process.env['HTTPS_PROXY']); |
| 91 | + strategy._oauth2.setAgent(httpsProxyAgent); |
| 92 | + } else { |
| 93 | + //this is intentional |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments