|
| 1 | +/* eslint-disable @typescript-eslint/naming-convention */ |
| 2 | +import {inject, Provider} from '@loopback/core'; |
| 3 | +import {HttpErrors, Request} from '@loopback/rest'; |
| 4 | +import {HttpsProxyAgent} from 'https-proxy-agent'; |
| 5 | +import { |
| 6 | + Profile, |
| 7 | + AuthenticateOptions, |
| 8 | + AuthenticateOptionsWithRequest, |
| 9 | + VerifyCallback, |
| 10 | + DecodedIdToken, |
| 11 | +} from 'passport-apple'; |
| 12 | + |
| 13 | +import {AuthErrorKeys} from '../../../error-keys'; |
| 14 | +import {Strategies} from '../../keys'; |
| 15 | +import {VerifyFunction} from '../../types'; |
| 16 | + |
| 17 | +import Strategy from 'passport-apple'; |
| 18 | + |
| 19 | +//import * as AppleStrategy from 'passport-apple'; |
| 20 | +export interface AppleAuthStrategyFactory { |
| 21 | + ( |
| 22 | + options: AuthenticateOptions | AuthenticateOptionsWithRequest, |
| 23 | + verifierPassed?: VerifyFunction.AppleAuthFn, |
| 24 | + ): Strategy; |
| 25 | +} |
| 26 | + |
| 27 | +export class AppleAuthStrategyFactoryProvider |
| 28 | + implements Provider<AppleAuthStrategyFactory> { |
| 29 | + constructor( |
| 30 | + @inject(Strategies.Passport.APPLE_OAUTH2_VERIFIER) |
| 31 | + private readonly verifierAppleAuth: VerifyFunction.AppleAuthFn, |
| 32 | + ) {} |
| 33 | + |
| 34 | + value(): AppleAuthStrategyFactory { |
| 35 | + return (options, verifier) => |
| 36 | + this.getAppleAuthStrategyVerifier(options, verifier); |
| 37 | + } |
| 38 | + |
| 39 | + getAppleAuthStrategyVerifier( |
| 40 | + options: AuthenticateOptions | AuthenticateOptionsWithRequest, |
| 41 | + verifierPassed?: VerifyFunction.AppleAuthFn, |
| 42 | + ): Strategy { |
| 43 | + const verifyFn = verifierPassed ?? this.verifierAppleAuth; |
| 44 | + let strategy; |
| 45 | + if (options && options.passReqToCallback === true) { |
| 46 | + strategy = new Strategy( |
| 47 | + options, |
| 48 | + |
| 49 | + // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 50 | + async ( |
| 51 | + req: Request, |
| 52 | + accessToken: string, |
| 53 | + refreshToken: string, |
| 54 | + decodedIdToken: DecodedIdToken, |
| 55 | + profile: Profile, |
| 56 | + cb: VerifyCallback, |
| 57 | + ) => { |
| 58 | + try { |
| 59 | + const user = await verifyFn( |
| 60 | + accessToken, |
| 61 | + refreshToken, |
| 62 | + decodedIdToken, |
| 63 | + profile, |
| 64 | + cb, |
| 65 | + req, |
| 66 | + ); |
| 67 | + if (!user) { |
| 68 | + throw new HttpErrors.Unauthorized( |
| 69 | + AuthErrorKeys.InvalidCredentials, |
| 70 | + ); |
| 71 | + } |
| 72 | + cb(undefined, user); |
| 73 | + } catch (err) { |
| 74 | + cb(err); |
| 75 | + } |
| 76 | + }, |
| 77 | + ); |
| 78 | + } else { |
| 79 | + strategy = new Strategy( |
| 80 | + options, |
| 81 | + // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 82 | + async ( |
| 83 | + accessToken: string, |
| 84 | + refreshToken: string, |
| 85 | + decodedIdToken: DecodedIdToken, |
| 86 | + profile: Profile, |
| 87 | + cb: VerifyCallback, |
| 88 | + ) => { |
| 89 | + try { |
| 90 | + const user = await verifyFn( |
| 91 | + accessToken, |
| 92 | + refreshToken, |
| 93 | + decodedIdToken, |
| 94 | + profile, |
| 95 | + cb, |
| 96 | + ); |
| 97 | + if (!user) { |
| 98 | + throw new HttpErrors.Unauthorized( |
| 99 | + AuthErrorKeys.InvalidCredentials, |
| 100 | + ); |
| 101 | + } |
| 102 | + cb(undefined, user); |
| 103 | + } catch (err) { |
| 104 | + cb(err); |
| 105 | + } |
| 106 | + }, |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + this._setupProxy(strategy); |
| 111 | + return strategy; |
| 112 | + } |
| 113 | + |
| 114 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 115 | + private _setupProxy(strategy: any) { |
| 116 | + // Setup proxy if any |
| 117 | + let httpsProxyAgent; |
| 118 | + if (process.env['https_proxy']) { |
| 119 | + httpsProxyAgent = new HttpsProxyAgent(process.env['https_proxy']); |
| 120 | + strategy._oauth2.setAgent(httpsProxyAgent); |
| 121 | + } else if (process.env['HTTPS_PROXY']) { |
| 122 | + httpsProxyAgent = new HttpsProxyAgent(process.env['HTTPS_PROXY']); |
| 123 | + strategy._oauth2.setAgent(httpsProxyAgent); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments