From 1a454a729def07bee6a52b0108921065fd7b251e Mon Sep 17 00:00:00 2001 From: arpit1503khanna Date: Fri, 4 Aug 2023 17:13:41 +0530 Subject: [PATCH] fix(chore): resolving sonar smells to improve quality gate resolving sonar smells to improve quality gate GH-142 --- .../SAML/saml-strategy-factory-provider.ts | 7 +- src/strategies/SAML/saml-verify.provider.ts | 4 - .../apple-auth-strategy-factory-provider.ts | 3 +- .../apple-auth-verify.provider.ts | 2 - .../azuread-auth-strategy-factory-provider.ts | 129 +++++++++--------- .../bearer-strategy-factory-provider.ts | 117 ++++++++-------- ...ient-password-strategy-factory-provider.ts | 46 +++---- .../client-password-strategy.ts | 22 ++- ...ient-password-strategy-factory-provider.ts | 49 +++---- .../cognito-auth-strategy-factory-provider.ts | 14 +- ...facebook-auth-strategy-factory-provider.ts | 14 +- .../google-auth-strategy-factory-provider.ts | 15 +- .../insta-auth-strategy-factory-provider.ts | 14 +- .../keycloak-strategy-factory-provider.ts | 12 +- ...ocal-password-strategy-factory-provider.ts | 125 ++++++++--------- .../passport/passport-otp/otp-auth.ts | 12 +- .../otp-strategy-factory.provider.ts | 34 ++--- .../oauth2-resource-owner-password-grant.ts | 52 ++++--- ...esource-owner-strategy-factory-provider.ts | 118 ++++++++-------- src/strategies/types/types.ts | 39 +++--- src/strategy-adapter.ts | 4 +- 21 files changed, 401 insertions(+), 431 deletions(-) diff --git a/src/strategies/SAML/saml-strategy-factory-provider.ts b/src/strategies/SAML/saml-strategy-factory-provider.ts index 28c111e..98d375d 100644 --- a/src/strategies/SAML/saml-strategy-factory-provider.ts +++ b/src/strategies/SAML/saml-strategy-factory-provider.ts @@ -14,9 +14,10 @@ import { import {AuthErrorKeys} from '../../error-keys'; import {Strategies} from '../../keys'; import {VerifyFunction} from '../../types'; -export interface SamlStrategyFactory { - (options: SamlConfig, verifierPassed?: VerifyFunction.SamlFn): Strategy; -} +export type SamlStrategyFactory = ( + options: SamlConfig, + verifierPassed?: VerifyFunction.SamlFn, +) => Strategy; export class SamlStrategyFactoryProvider implements Provider diff --git a/src/strategies/SAML/saml-verify.provider.ts b/src/strategies/SAML/saml-verify.provider.ts index e6c1394..e198e60 100644 --- a/src/strategies/SAML/saml-verify.provider.ts +++ b/src/strategies/SAML/saml-verify.provider.ts @@ -11,10 +11,6 @@ import {VerifyFunction} from '../../types'; * It will just throw an error saying Not Implemented */ export class SamlVerifyProvider implements Provider { - constructor() { - //This is intentional - } - value(): VerifyFunction.SamlFn { return async ( profile: SamlStrategy.Profile, diff --git a/src/strategies/passport/passport-apple-oauth2/apple-auth-strategy-factory-provider.ts b/src/strategies/passport/passport-apple-oauth2/apple-auth-strategy-factory-provider.ts index 5e20745..5a5e25c 100644 --- a/src/strategies/passport/passport-apple-oauth2/apple-auth-strategy-factory-provider.ts +++ b/src/strategies/passport/passport-apple-oauth2/apple-auth-strategy-factory-provider.ts @@ -12,7 +12,6 @@ import { import {AuthErrorKeys} from '../../../error-keys'; import {Strategies} from '../../keys'; import {VerifyFunction} from '../../types'; - import Strategy from 'passport-apple'; export interface AppleAuthStrategyFactory { ( @@ -40,7 +39,7 @@ export class AppleAuthStrategyFactoryProvider ): Strategy { const verifyFn = verifierPassed ?? this.verifierAppleAuth; let strategy; - if (options && options.passReqToCallback === true) { + if (options?.passReqToCallback === true) { strategy = new Strategy( options, diff --git a/src/strategies/passport/passport-apple-oauth2/apple-auth-verify.provider.ts b/src/strategies/passport/passport-apple-oauth2/apple-auth-verify.provider.ts index a600467..e6e97dc 100644 --- a/src/strategies/passport/passport-apple-oauth2/apple-auth-verify.provider.ts +++ b/src/strategies/passport/passport-apple-oauth2/apple-auth-verify.provider.ts @@ -9,8 +9,6 @@ import {VerifyFunction} from '../../types'; export class AppleAuthVerifyProvider implements Provider { - constructor() {} - value(): VerifyFunction.AppleAuthFn { return async ( accessToken: string, diff --git a/src/strategies/passport/passport-azure-ad/azuread-auth-strategy-factory-provider.ts b/src/strategies/passport/passport-azure-ad/azuread-auth-strategy-factory-provider.ts index d655697..3771ea3 100644 --- a/src/strategies/passport/passport-azure-ad/azuread-auth-strategy-factory-provider.ts +++ b/src/strategies/passport/passport-azure-ad/azuread-auth-strategy-factory-provider.ts @@ -12,12 +12,10 @@ import { IOIDCStrategyOptionWithoutRequest, } from 'passport-azure-ad'; -export interface AzureADAuthStrategyFactory { - ( - options: IOIDCStrategyOptionWithoutRequest | IOIDCStrategyOptionWithRequest, - verifierPassed?: VerifyFunction.AzureADAuthFn, - ): OIDCStrategy; -} +export type AzureADAuthStrategyFactory = ( + options: IOIDCStrategyOptionWithoutRequest | IOIDCStrategyOptionWithRequest, + verifierPassed?: VerifyFunction.AzureADAuthFn, +) => OIDCStrategy; export class AzureADAuthStrategyFactoryProvider implements Provider @@ -31,6 +29,61 @@ export class AzureADAuthStrategyFactoryProvider return (options, verifier) => this.getAzureADAuthStrategyVerifier(options, verifier); } + createCallbackWithReq(verifyFn: VerifyFunction.AzureADAuthFn) { + return async ( + req: Request, + iss: string, + sub: string, + profile: IProfile, + accessToken: string, + refreshToken: string, + done: VerifyCallback, + ) => { + if (!profile.oid) { + return done(new Error('No oid found'), null); + } + + try { + const user = await verifyFn( + accessToken, + refreshToken, + profile, + done, + req, + ); + if (!user) { + throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); + } + done(null, user); + } catch (err) { + done(err); + } + }; + } + createCallbackWithoutReq(verifyFn: VerifyFunction.AzureADAuthFn) { + return async ( + iss: string, + sub: string, + profile: IProfile, + accessToken: string, + refreshToken: string, + done: VerifyCallback, + ) => { + if (!profile.oid) { + return done(new Error('No oid found'), null); + } + + try { + const user = await verifyFn(accessToken, refreshToken, profile, done); + if (!user) { + throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); + } + done(null, user); + } catch (err) { + done(err); + } + }; + } getAzureADAuthStrategyVerifier( options: IOIDCStrategyOptionWithoutRequest | IOIDCStrategyOptionWithRequest, @@ -40,74 +93,14 @@ export class AzureADAuthStrategyFactoryProvider if (options && options.passReqToCallback === true) { return new OIDCStrategy( options, - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( - req: Request, - iss: string, - sub: string, - profile: IProfile, - accessToken: string, - refreshToken: string, - done: VerifyCallback, - ) => { - if (!profile.oid) { - return done(new Error('No oid found'), null); - } - - try { - const user = await verifyFn( - accessToken, - refreshToken, - profile, - done, - req, - ); - if (!user) { - throw new HttpErrors.Unauthorized( - AuthErrorKeys.InvalidCredentials, - ); - } - done(null, user); - } catch (err) { - done(err); - } - }, + this.createCallbackWithReq(verifyFn), ); } else if (options && options.passReqToCallback === false) { return new OIDCStrategy( options, - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( - iss: string, - sub: string, - profile: IProfile, - accessToken: string, - refreshToken: string, - done: VerifyCallback, - ) => { - if (!profile.oid) { - return done(new Error('No oid found'), null); - } - - try { - const user = await verifyFn( - accessToken, - refreshToken, - profile, - done, - ); - if (!user) { - throw new HttpErrors.Unauthorized( - AuthErrorKeys.InvalidCredentials, - ); - } - done(null, user); - } catch (err) { - done(err); - } - }, + this.createCallbackWithoutReq(verifyFn), ); } else { throw new Error('Invalid value for passReqToCallback'); diff --git a/src/strategies/passport/passport-bearer/bearer-strategy-factory-provider.ts b/src/strategies/passport/passport-bearer/bearer-strategy-factory-provider.ts index ccd3d18..1d6ef34 100644 --- a/src/strategies/passport/passport-bearer/bearer-strategy-factory-provider.ts +++ b/src/strategies/passport/passport-bearer/bearer-strategy-factory-provider.ts @@ -8,12 +8,10 @@ import {Strategies} from '../../keys'; import {VerifyFunction} from '../../types'; import {isEmpty} from 'lodash'; -export interface BearerStrategyFactory { - ( - options?: PassportBearer.IStrategyOptions, - verifierPassed?: VerifyFunction.BearerFn, - ): PassportBearer.Strategy; -} +export type BearerStrategyFactory = ( + options?: PassportBearer.IStrategyOptions, + verifierPassed?: VerifyFunction.BearerFn, +) => PassportBearer.Strategy; export class BearerStrategyFactoryProvider implements Provider @@ -28,6 +26,61 @@ export class BearerStrategyFactoryProvider this.getBearerStrategyVerifier(options, verifier); } + getBearerStrategyVerifierWithRequest(verifyFn: VerifyFunction.BearerFn) { + return async ( + req: Request, + token: string, + cb: (err: Error | null, user?: IAuthUser | false) => void, + ) => { + try { + const user = await verifyFn(token, req); + if (!user) { + throw new HttpErrors.Unauthorized(AuthErrorKeys.TokenInvalid); + } + cb(null, user); + } catch (err) { + cb(err); + } + }; + } + + getBearerStrategyVerifierWithoutRequest(verifyFn: VerifyFunction.BearerFn) { + return async ( + token: string, + cb: (err: Error | null, user?: IAuthUser | false) => void, + ) => { + try { + const user = await verifyFn(token); + if (!user) { + throw new HttpErrors.Unauthorized(AuthErrorKeys.TokenInvalid); + } + cb(null, user); + } catch (err) { + cb(err); + } + }; + } + getBearerStrategyVerifierDefault( + verifyFn: VerifyFunction.BearerFn, + ): PassportBearer.Strategy { + return new PassportBearer.Strategy( + // eslint-disable-next-line @typescript-eslint/no-misused-promises + async ( + token: string, + cb: (err: Error | null, user?: IAuthUser | false) => void, + ) => { + try { + const user = await verifyFn(token); + if (!user) { + throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); + } + cb(null, user); + } catch (err) { + cb(err); + } + }, + ); + } getBearerStrategyVerifier( options?: PassportBearer.IStrategyOptions, verifierPassed?: VerifyFunction.BearerFn, @@ -37,62 +90,16 @@ export class BearerStrategyFactoryProvider return new PassportBearer.Strategy( options, // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( - req: Request, - token: string, - cb: (err: Error | null, user?: IAuthUser | false) => void, - ) => { - try { - const user = await verifyFn(token, req); - if (!user) { - throw new HttpErrors.Unauthorized(AuthErrorKeys.TokenInvalid); - } - cb(null, user); - } catch (err) { - cb(err); - } - }, + this.getBearerStrategyVerifierWithRequest(verifyFn), ); } else if (!!options && !isEmpty(options)) { return new PassportBearer.Strategy( options, - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( - token: string, - cb: (err: Error | null, user?: IAuthUser | false) => void, - ) => { - try { - const user = await verifyFn(token); - if (!user) { - throw new HttpErrors.Unauthorized(AuthErrorKeys.TokenInvalid); - } - cb(null, user); - } catch (err) { - cb(err); - } - }, + this.getBearerStrategyVerifierWithoutRequest(verifyFn), ); } else { - return new PassportBearer.Strategy( - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( - token: string, - cb: (err: Error | null, user?: IAuthUser | false) => void, - ) => { - try { - const user = await verifyFn(token); - if (!user) { - throw new HttpErrors.Unauthorized( - AuthErrorKeys.InvalidCredentials, - ); - } - cb(null, user); - } catch (err) { - cb(err); - } - }, - ); + return this.getBearerStrategyVerifierDefault(verifyFn); } } } diff --git a/src/strategies/passport/passport-client-password/client-password-strategy-factory-provider.ts b/src/strategies/passport/passport-client-password/client-password-strategy-factory-provider.ts index 9788431..7a9b942 100644 --- a/src/strategies/passport/passport-client-password/client-password-strategy-factory-provider.ts +++ b/src/strategies/passport/passport-client-password/client-password-strategy-factory-provider.ts @@ -7,12 +7,10 @@ import {IAuthClient} from '../../../types'; import {Strategies} from '../../keys'; import {VerifyFunction} from '../../types'; -export interface ClientPasswordStrategyFactory { - ( - options?: ClientPasswordStrategy.StrategyOptionsWithRequestInterface, - verifierPassed?: VerifyFunction.OauthClientPasswordFn, - ): ClientPasswordStrategy.Strategy; -} +export type ClientPasswordStrategyFactory = ( + options?: ClientPasswordStrategy.StrategyOptionsWithRequestInterface, + verifierPassed?: VerifyFunction.OauthClientPasswordFn, +) => ClientPasswordStrategy.Strategy; export class ClientPasswordStrategyFactoryProvider implements Provider @@ -45,38 +43,38 @@ export class ClientPasswordStrategyFactoryProvider const verifyFn = verifierPassed ?? this.verifier; if (options?.passReqToCallback) { return new ClientPasswordStrategy.Strategy( - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( + ( clientId: string, clientSecret: string | undefined, cb: (err: Error | null, client?: IAuthClient | null) => void, req: Request | undefined, ) => { - try { - const client = await verifyFn(clientId, clientSecret, req); - this.clientPasswordVerifierHelper(client, clientSecret); - cb(null, client); - } catch (err) { - cb(err); - } + verifyFn(clientId, clientSecret, req) + .then((client) => { + this.clientPasswordVerifierHelper(client, clientSecret); + cb(null, client); + }) + .catch((err) => { + cb(err); + }); }, options, ); } else { return new ClientPasswordStrategy.Strategy( - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( + ( clientId: string, clientSecret: string | undefined, cb: (err: Error | null, client?: IAuthClient | null) => void, ) => { - try { - const client = await verifyFn(clientId, clientSecret); - this.clientPasswordVerifierHelper(client, clientSecret); - cb(null, client); - } catch (err) { - cb(err); - } + verifyFn(clientId, clientSecret) + .then((client) => { + this.clientPasswordVerifierHelper(client, clientSecret); + cb(null, client); + }) + .catch((err) => { + cb(err); + }); }, ); } diff --git a/src/strategies/passport/passport-client-password/client-password-strategy.ts b/src/strategies/passport/passport-client-password/client-password-strategy.ts index 76ce956..72c75e9 100644 --- a/src/strategies/passport/passport-client-password/client-password-strategy.ts +++ b/src/strategies/passport/passport-client-password/client-password-strategy.ts @@ -12,18 +12,16 @@ export interface StrategyOptionsWithRequestInterface { passReqToCallback: boolean; } -export interface VerifyFunctionWithRequest { - ( - clientId: string, - clientSecret: string | undefined, - done: ( - error: Error | null, - client?: IAuthSecureClient | IAuthClient | null, - info?: Object | undefined, - ) => void, - req?: express.Request, - ): void; -} +export type VerifyFunctionWithRequest = ( + clientId: string, + clientSecret: string | undefined, + done: ( + error: Error | null, + client?: IAuthSecureClient | IAuthClient | null, + info?: Object | undefined, + ) => void, + req?: express.Request, +) => void; export class Strategy extends passport.Strategy { constructor( diff --git a/src/strategies/passport/passport-client-password/secure-client-password-strategy-factory-provider.ts b/src/strategies/passport/passport-client-password/secure-client-password-strategy-factory-provider.ts index 4c99660..7469f3b 100644 --- a/src/strategies/passport/passport-client-password/secure-client-password-strategy-factory-provider.ts +++ b/src/strategies/passport/passport-client-password/secure-client-password-strategy-factory-provider.ts @@ -7,12 +7,10 @@ import {ClientType, IAuthSecureClient} from '../../../types'; import {Strategies} from '../../keys'; import {VerifyFunction} from '../../types'; -export interface SecureClientPasswordStrategyFactory { - ( - options?: ClientPasswordStrategy.StrategyOptionsWithRequestInterface, - verifierPassed?: VerifyFunction.OauthSecureClientPasswordFn, - ): ClientPasswordStrategy.Strategy; -} +export type SecureClientPasswordStrategyFactory = ( + options?: ClientPasswordStrategy.StrategyOptionsWithRequestInterface, + verifierPassed?: VerifyFunction.OauthSecureClientPasswordFn, +) => ClientPasswordStrategy.Strategy; export class SecureClientPasswordStrategyFactoryProvider implements Provider @@ -49,41 +47,38 @@ export class SecureClientPasswordStrategyFactoryProvider const verifyFn = verifierPassed ?? this.verifier; if (options?.passReqToCallback) { return new ClientPasswordStrategy.Strategy( - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( + ( clientId: string, clientSecret: string | undefined, cb: (err: Error | null, client?: IAuthSecureClient | null) => void, req: Request | undefined, ) => { - try { - const client = await verifyFn(clientId, clientSecret, req); - this.secureClientPasswordVerifierHelper(client, clientSecret); - - cb(null, client); - } catch (err) { - cb(err); - } + verifyFn(clientId, clientSecret, req) + .then((client) => { + this.secureClientPasswordVerifierHelper(client, clientSecret); + cb(null, client); + }) + .catch((err) => { + cb(err); + }); }, options, ); } else { return new ClientPasswordStrategy.Strategy( - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( + ( clientId: string, clientSecret: string | undefined, cb: (err: Error | null, client?: IAuthSecureClient | null) => void, ) => { - try { - const client = await verifyFn(clientId, clientSecret); - - this.secureClientPasswordVerifierHelper(client, clientSecret); - - cb(null, client); - } catch (err) { - cb(err); - } + verifyFn(clientId, clientSecret) + .then((client) => { + this.secureClientPasswordVerifierHelper(client, clientSecret); + cb(null, client); + }) + .catch((err) => { + cb(err); + }); }, ); } diff --git a/src/strategies/passport/passport-cognito-oauth2/cognito-auth-strategy-factory-provider.ts b/src/strategies/passport/passport-cognito-oauth2/cognito-auth-strategy-factory-provider.ts index 7cd4ae7..a076d2b 100644 --- a/src/strategies/passport/passport-cognito-oauth2/cognito-auth-strategy-factory-provider.ts +++ b/src/strategies/passport/passport-cognito-oauth2/cognito-auth-strategy-factory-provider.ts @@ -7,12 +7,10 @@ import {Cognito, VerifyFunction} from '../../types'; const CognitoStrategy = require('passport-cognito-oauth2'); -export interface CognitoAuthStrategyFactory { - ( - options: Cognito.StrategyOptions, - verifierPassed?: VerifyFunction.CognitoAuthFn, - ): typeof CognitoStrategy; -} +export type CognitoAuthStrategyFactory = ( + options: Cognito.StrategyOptions, + verifierPassed?: VerifyFunction.CognitoAuthFn, +) => typeof CognitoStrategy; export class CognitoStrategyFactoryProvider implements Provider @@ -33,7 +31,7 @@ export class CognitoStrategyFactoryProvider ): typeof CognitoStrategy { const verifyFn = verifierPassed ?? this.verifierCognito; let strategy; - if (options && options.passReqToCallback === true) { + if (options?.passReqToCallback === true) { strategy = new CognitoStrategy( options, @@ -100,6 +98,8 @@ export class CognitoStrategyFactoryProvider } else if (process.env['HTTPS_PROXY']) { httpsProxyAgent = new HttpsProxyAgent(process.env['HTTPS_PROXY']); strategy._oauth2.setAgent(httpsProxyAgent); + } else { + //this is intentional } } } diff --git a/src/strategies/passport/passport-facebook-oauth2/facebook-auth-strategy-factory-provider.ts b/src/strategies/passport/passport-facebook-oauth2/facebook-auth-strategy-factory-provider.ts index 1118119..a9917cc 100644 --- a/src/strategies/passport/passport-facebook-oauth2/facebook-auth-strategy-factory-provider.ts +++ b/src/strategies/passport/passport-facebook-oauth2/facebook-auth-strategy-factory-provider.ts @@ -16,12 +16,10 @@ interface ExtendedStrategyOption extends StrategyOption { passReqToCallback?: false; } -export interface FacebookAuthStrategyFactory { - ( - options: ExtendedStrategyOption | StrategyOptionWithRequest, - verifierPassed?: VerifyFunction.FacebookAuthFn, - ): Strategy; -} +export type FacebookAuthStrategyFactory = ( + options: ExtendedStrategyOption | StrategyOptionWithRequest, + verifierPassed?: VerifyFunction.FacebookAuthFn, +) => Strategy; export class FacebookAuthStrategyFactoryProvider implements Provider @@ -42,7 +40,7 @@ export class FacebookAuthStrategyFactoryProvider ): Strategy { const verifyFn = verifierPassed ?? this.verifierFacebookAuth; let strategy; - if (options && options.passReqToCallback === true) { + if (options?.passReqToCallback === true) { strategy = new Strategy( options, // eslint-disable-next-line @typescript-eslint/no-misused-promises @@ -111,6 +109,8 @@ export class FacebookAuthStrategyFactoryProvider } else if (process.env['HTTPS_PROXY']) { httpsProxyAgent = new HttpsProxyAgent(process.env['HTTPS_PROXY']); strategy._oauth2.setAgent(httpsProxyAgent); + } else { + //this is intentional } } } diff --git a/src/strategies/passport/passport-google-oauth2/google-auth-strategy-factory-provider.ts b/src/strategies/passport/passport-google-oauth2/google-auth-strategy-factory-provider.ts index ac2d129..11f65ca 100644 --- a/src/strategies/passport/passport-google-oauth2/google-auth-strategy-factory-provider.ts +++ b/src/strategies/passport/passport-google-oauth2/google-auth-strategy-factory-provider.ts @@ -13,13 +13,10 @@ import {AuthErrorKeys} from '../../../error-keys'; import {Strategies} from '../../keys'; import {VerifyFunction} from '../../types'; -//import * as GoogleStrategy from 'passport-google-oauth20'; -export interface GoogleAuthStrategyFactory { - ( - options: StrategyOptions | StrategyOptionsWithRequest, - verifierPassed?: VerifyFunction.GoogleAuthFn, - ): Strategy; -} +export type GoogleAuthStrategyFactory = ( + options: StrategyOptions | StrategyOptionsWithRequest, + verifierPassed?: VerifyFunction.GoogleAuthFn, +) => Strategy; export class GoogleAuthStrategyFactoryProvider implements Provider @@ -40,7 +37,7 @@ export class GoogleAuthStrategyFactoryProvider ): Strategy { const verifyFn = verifierPassed ?? this.verifierGoogleAuth; let strategy; - if (options && options.passReqToCallback === true) { + if (options?.passReqToCallback === true) { strategy = new Strategy( options, @@ -110,6 +107,8 @@ export class GoogleAuthStrategyFactoryProvider } else if (process.env['HTTPS_PROXY']) { httpsProxyAgent = new HttpsProxyAgent(process.env['HTTPS_PROXY']); strategy._oauth2.setAgent(httpsProxyAgent); + } else { + //this is intentional } } } diff --git a/src/strategies/passport/passport-insta-oauth2/insta-auth-strategy-factory-provider.ts b/src/strategies/passport/passport-insta-oauth2/insta-auth-strategy-factory-provider.ts index 5322f47..804f372 100644 --- a/src/strategies/passport/passport-insta-oauth2/insta-auth-strategy-factory-provider.ts +++ b/src/strategies/passport/passport-insta-oauth2/insta-auth-strategy-factory-provider.ts @@ -12,12 +12,10 @@ import {AuthErrorKeys} from '../../../error-keys'; import {Strategies} from '../../keys'; import {VerifyCallback, VerifyFunction} from '../../types'; -export interface InstagramAuthStrategyFactory { - ( - options: StrategyOption | StrategyOptionWithRequest, - verifierPassed?: VerifyFunction.InstagramAuthFn, - ): Strategy; -} +export type InstagramAuthStrategyFactory = ( + options: StrategyOption | StrategyOptionWithRequest, + verifierPassed?: VerifyFunction.InstagramAuthFn, +) => Strategy; export class InstagramAuthStrategyFactoryProvider implements Provider @@ -38,7 +36,7 @@ export class InstagramAuthStrategyFactoryProvider ): Strategy { const verifyFn = verifierPassed ?? this.verifierInstagramAuth; let strategy; - if (options && options.passReqToCallback === true) { + if (options?.passReqToCallback === true) { strategy = new Strategy( options, // eslint-disable-next-line @typescript-eslint/no-misused-promises @@ -107,6 +105,8 @@ export class InstagramAuthStrategyFactoryProvider } else if (process.env['HTTPS_PROXY']) { httpsProxyAgent = new HttpsProxyAgent(process.env['HTTPS_PROXY']); strategy._oauth2.setAgent(httpsProxyAgent); + } else { + //this is intentional } } } diff --git a/src/strategies/passport/passport-keycloak/keycloak-strategy-factory-provider.ts b/src/strategies/passport/passport-keycloak/keycloak-strategy-factory-provider.ts index 210e37a..ae87b10 100644 --- a/src/strategies/passport/passport-keycloak/keycloak-strategy-factory-provider.ts +++ b/src/strategies/passport/passport-keycloak/keycloak-strategy-factory-provider.ts @@ -8,12 +8,10 @@ import {Keycloak, VerifyFunction} from '../../types'; export const KeycloakStrategy = require('@exlinc/keycloak-passport'); -export interface KeycloakStrategyFactory { - ( - options: Keycloak.StrategyOptions, - verifierPassed?: VerifyFunction.KeycloakAuthFn, - ): typeof KeycloakStrategy; -} +export type KeycloakStrategyFactory = ( + options: Keycloak.StrategyOptions, + verifierPassed?: VerifyFunction.KeycloakAuthFn, +) => typeof KeycloakStrategy; export class KeycloakStrategyFactoryProvider implements Provider @@ -127,6 +125,8 @@ export class KeycloakStrategyFactoryProvider } else if (process.env['HTTPS_PROXY']) { httpsProxyAgent = new HttpsProxyAgent(process.env['HTTPS_PROXY']); strategy._oauth2.setAgent(httpsProxyAgent); + } else { + //this is intentional } } } diff --git a/src/strategies/passport/passport-local/local-password-strategy-factory-provider.ts b/src/strategies/passport/passport-local/local-password-strategy-factory-provider.ts index 88693f8..23f68da 100644 --- a/src/strategies/passport/passport-local/local-password-strategy-factory-provider.ts +++ b/src/strategies/passport/passport-local/local-password-strategy-factory-provider.ts @@ -8,14 +8,12 @@ import {Strategies} from '../../keys'; import {VerifyFunction} from '../../types'; import {isEmpty} from 'lodash'; -export interface LocalPasswordStrategyFactory { - ( - options?: - | PassportLocal.IStrategyOptions - | PassportLocal.IStrategyOptionsWithRequest, - verifierPassed?: VerifyFunction.LocalPasswordFn, - ): PassportLocal.Strategy; -} +export type LocalPasswordStrategyFactory = ( + options?: + | PassportLocal.IStrategyOptions + | PassportLocal.IStrategyOptionsWithRequest, + verifierPassed?: VerifyFunction.LocalPasswordFn, +) => PassportLocal.Strategy; export class LocalPasswordStrategyFactoryProvider implements Provider @@ -29,7 +27,60 @@ export class LocalPasswordStrategyFactoryProvider return (options, verifier) => this.getLocalStrategyVerifier(options, verifier); } - + getLocalStrategyWithRequest(verifyFn: VerifyFunction.LocalPasswordFn) { + return async ( + req: Request, + username: string, + password: string, + cb: (err: Error | null, user?: IAuthUser | false) => void, + ) => { + try { + const user = await verifyFn(username, password, req); + if (!user) { + throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); + } + cb(null, user); + } catch (err) { + cb(err); + } + }; + } + getLocalStrategyWithoutRequest(verifyFn: VerifyFunction.LocalPasswordFn) { + return async ( + username: string, + password: string, + cb: (err: Error | null, user?: IAuthUser | false) => void, + ) => { + try { + const user = await verifyFn(username, password); + if (!user) { + throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); + } + cb(null, user); + } catch (err) { + cb(err); + } + }; + } + getLocalStrategyVerifierUndefinedRequest( + verifyFn: VerifyFunction.LocalPasswordFn, + ) { + return async ( + username: string, + password: string, + cb: (err: Error | null, user?: IAuthUser | false) => void, + ) => { + try { + const user = await verifyFn(username, password, undefined); + if (!user) { + throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); + } + cb(null, user); + } catch (err) { + cb(err); + } + }; + } getLocalStrategyVerifier( options?: | PassportLocal.IStrategyOptions @@ -37,71 +88,23 @@ export class LocalPasswordStrategyFactoryProvider verifierPassed?: VerifyFunction.LocalPasswordFn, ): PassportLocal.Strategy { const verifyFn = verifierPassed ?? this.verifierLocal; + if (options?.passReqToCallback) { return new PassportLocal.Strategy( options, // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( - req: Request, - username: string, - password: string, - cb: (err: Error | null, user?: IAuthUser | false) => void, - ) => { - try { - const user = await verifyFn(username, password, req); - if (!user) { - throw new HttpErrors.Unauthorized( - AuthErrorKeys.InvalidCredentials, - ); - } - cb(null, user); - } catch (err) { - cb(err); - } - }, + this.getLocalStrategyWithRequest(verifyFn), ); } else if (!!options && !isEmpty(options)) { return new PassportLocal.Strategy( options, // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( - username: string, - password: string, - cb: (err: Error | null, user?: IAuthUser | false) => void, - ) => { - try { - const user = await verifyFn(username, password); - if (!user) { - throw new HttpErrors.Unauthorized( - AuthErrorKeys.InvalidCredentials, - ); - } - cb(null, user); - } catch (err) { - cb(err); - } - }, + this.getLocalStrategyWithoutRequest(verifyFn), ); } else { return new PassportLocal.Strategy( // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( - username: string, - password: string, - cb: (err: Error | null, user?: IAuthUser | false) => void, - ) => { - try { - const user = await verifyFn(username, password, undefined); - if (!user) { - throw new HttpErrors.Unauthorized( - AuthErrorKeys.InvalidCredentials, - ); - } - cb(null, user); - } catch (err) { - cb(err); - } - }, + this.getLocalStrategyVerifierUndefinedRequest(verifyFn), ); } } diff --git a/src/strategies/passport/passport-otp/otp-auth.ts b/src/strategies/passport/passport-otp/otp-auth.ts index bf16465..be45726 100644 --- a/src/strategies/passport/passport-otp/otp-auth.ts +++ b/src/strategies/passport/passport-otp/otp-auth.ts @@ -2,13 +2,11 @@ import * as passport from 'passport'; export namespace Otp { - export interface VerifyFunction { - ( - key: string, - otp: string, - done: (error: any, user?: any, info?: any) => void, - ): void; - } + export type VerifyFunction = ( + key: string, + otp: string, + done: (error: any, user?: any, info?: any) => void, + ) => void; export interface StrategyOptions { key?: string; diff --git a/src/strategies/passport/passport-otp/otp-strategy-factory.provider.ts b/src/strategies/passport/passport-otp/otp-strategy-factory.provider.ts index f37b1a4..b43d315 100644 --- a/src/strategies/passport/passport-otp/otp-strategy-factory.provider.ts +++ b/src/strategies/passport/passport-otp/otp-strategy-factory.provider.ts @@ -5,12 +5,10 @@ import {Strategies} from '../../keys'; import {VerifyFunction} from '../../types'; import {Otp} from './otp-auth'; -export interface PassportOtpStrategyFactory { - ( - options: Otp.StrategyOptions, - verifierPassed?: VerifyFunction.OtpAuthFn, - ): Otp.Strategy; -} +export type PassportOtpStrategyFactory = ( + options: Otp.StrategyOptions, + verifierPassed?: VerifyFunction.OtpAuthFn, +) => Otp.Strategy; export class PassportOtpStrategyFactoryProvider implements Provider @@ -32,17 +30,19 @@ export class PassportOtpStrategyFactoryProvider const verifyFn = verifierPassed ?? this.verifierOtp; return new Otp.Strategy( options, - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async (key: string, otp: string, cb: Otp.VerifyCallback) => { - try { - const user = await verifyFn(key, otp); - if (!user) { - throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); - } - cb(null, user); - } catch (err) { - cb(err); - } + (key: string, otp: string, cb: Otp.VerifyCallback) => { + verifyFn(key, otp) + .then((user) => { + if (!user) { + throw new HttpErrors.Unauthorized( + AuthErrorKeys.InvalidCredentials, + ); + } + cb(null, user); + }) + .catch((err) => { + cb(err); + }); }, ); } diff --git a/src/strategies/passport/passport-resource-owner-password/oauth2-resource-owner-password-grant.ts b/src/strategies/passport/passport-resource-owner-password/oauth2-resource-owner-password-grant.ts index e658bfd..9bf7372 100644 --- a/src/strategies/passport/passport-resource-owner-password/oauth2-resource-owner-password-grant.ts +++ b/src/strategies/passport/passport-resource-owner-password/oauth2-resource-owner-password-grant.ts @@ -7,34 +7,30 @@ export namespace Oauth2ResourceOwnerPassword { passReqToCallback: boolean; } - export interface VerifyFunctionWithRequest { - ( - req: Request, - clientId: string, - clientSecret: string, - username: string, - password: string, - done: ( - error: Error | null, - client?: IAuthClient | false, - info?: IAuthUser | false, - ) => void, - ): void; - } + export type VerifyFunctionWithRequest = ( + req: Request, + clientId: string, + clientSecret: string, + username: string, + password: string, + done: ( + error: Error | null, + client?: IAuthClient | false, + info?: IAuthUser | false, + ) => void, + ) => void; - export interface VerifyFunction { - ( - clientId: string, - clientSecret: string, - username: string, - password: string, - done: ( - error: Error | null, - client?: IAuthClient | false, - info?: IAuthUser | false, - ) => void, - ): void; - } + export type VerifyFunction = ( + clientId: string, + clientSecret: string, + username: string, + password: string, + done: ( + error: Error | null, + client?: IAuthClient | false, + info?: IAuthUser | false, + ) => void, + ) => void; export class Strategy extends passport.Strategy { constructor(verify: VerifyFunction); @@ -65,8 +61,6 @@ export namespace Oauth2ResourceOwnerPassword { authenticate(req: Request, options?: {}): void { if ( - /* eslint-disable @typescript-eslint/prefer-optional-chain */ - !req.body || !req.body?.['client_id'] || !req.body?.['username'] || !req.body?.['password'] diff --git a/src/strategies/passport/passport-resource-owner-password/resource-owner-strategy-factory-provider.ts b/src/strategies/passport/passport-resource-owner-password/resource-owner-strategy-factory-provider.ts index eaeae1c..2873027 100644 --- a/src/strategies/passport/passport-resource-owner-password/resource-owner-strategy-factory-provider.ts +++ b/src/strategies/passport/passport-resource-owner-password/resource-owner-strategy-factory-provider.ts @@ -33,73 +33,71 @@ export class ResourceOwnerPasswordStrategyFactoryProvider verifierPassed?: VerifyFunction.ResourceOwnerPasswordFn, ): Oauth2ResourceOwnerPassword.Strategy { const verifyFn = verifierPassed ?? this.verifierResourceOwner; + if (options?.passReqToCallback) { return new Oauth2ResourceOwnerPassword.Strategy( options, - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( - req: Request, - clientId: string, - clientSecret: string, - username: string, - password: string, - cb: ( - err: Error | null, - client?: IAuthClient | false, - user?: IAuthUser | false, - ) => void, - ) => { - try { - const userInfo = await verifyFn( - clientId, - clientSecret, - username, - password, - req, - ); - if (!userInfo || isEmpty(userInfo)) { - throw new HttpErrors.Unauthorized( - AuthErrorKeys.InvalidCredentials, - ); - } - cb(null, userInfo.client, userInfo.user); - } catch (err) { - cb(err); - } - }, + this.getResourceOwnerStrategyWithRequest(verifyFn), ); } else { return new Oauth2ResourceOwnerPassword.Strategy( - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async ( - clientId: string, - clientSecret: string, - username: string, - password: string, - cb: ( - err: Error | null, - client?: IAuthClient | false, - user?: IAuthUser | false, - ) => void, - ) => { - try { - const userInfo = await verifyFn( - clientId, - clientSecret, - username, - password, - ); - if (!userInfo || isEmpty(userInfo)) { - throw new HttpErrors.Unauthorized( - AuthErrorKeys.InvalidCredentials, - ); - } - cb(null, userInfo.client, userInfo.user); - } catch (err) { - cb(err); - } - }, + this.getResourceOwnerStrategyWithoutRequest(verifyFn), ); } } + + getResourceOwnerStrategyWithRequest( + verifyFn: VerifyFunction.ResourceOwnerPasswordFn, + ) { + return ( + req: Request, + clientId: string, + clientSecret: string, + username: string, + password: string, + cb: ( + err: Error | null, + client?: IAuthClient | false, + user?: IAuthUser | false, + ) => void, + ) => { + verifyFn(clientId, clientSecret, username, password, req) + .then((userInfo) => { + if (!userInfo || isEmpty(userInfo)) { + throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); + } + cb(null, userInfo.client, userInfo.user); + }) + .catch((err) => { + cb(err); + }); + }; + } + + getResourceOwnerStrategyWithoutRequest( + verifyFn: VerifyFunction.ResourceOwnerPasswordFn, + ) { + return ( + clientId: string, + clientSecret: string, + username: string, + password: string, + cb: ( + err: Error | null, + client?: IAuthClient | false, + user?: IAuthUser | false, + ) => void, + ) => { + verifyFn(clientId, clientSecret, username, password) + .then((userInfo) => { + if (!userInfo || isEmpty(userInfo)) { + throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); + } + cb(null, userInfo.client, userInfo.user); + }) + .catch((err) => { + cb(err); + }); + }; + } } diff --git a/src/strategies/types/types.ts b/src/strategies/types/types.ts index 7f2a996..4fe38f7 100644 --- a/src/strategies/types/types.ts +++ b/src/strategies/types/types.ts @@ -40,28 +40,24 @@ export namespace VerifyFunction { (token: string, req?: Request): Promise; } - export interface ResourceOwnerPasswordFn { - ( - clientId: string, - clientSecret: string, - username: string, - password: string, - req?: Request, - ): Promise<{client: T; user: S} | null>; - } + export type ResourceOwnerPasswordFn = ( + clientId: string, + clientSecret: string, + username: string, + password: string, + req?: Request, + ) => Promise<{client: T; user: S} | null>; - export interface SecureResourceOwnerPasswordFn< + export type SecureResourceOwnerPasswordFn< T = IAuthSecureClient, S = IAuthUser, - > { - ( - clientId: string, - clientSecret: string, - username: string, - password: string, - req?: Request, - ): Promise<{client: T; user: S} | null>; - } + > = ( + clientId: string, + clientSecret: string, + username: string, + password: string, + req?: Request, + ) => Promise<{client: T; user: S} | null>; export interface GoogleAuthFn extends GenericAuthFn { ( @@ -142,8 +138,5 @@ export namespace VerifyFunction { ): Promise; } // eslint-disable-next-line @typescript-eslint/no-explicit-any - export interface GenericAuthFn { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (...params: any): Promise; - } + export type GenericAuthFn = (...params: any) => Promise; } diff --git a/src/strategy-adapter.ts b/src/strategy-adapter.ts index 2356750..6810a95 100644 --- a/src/strategy-adapter.ts +++ b/src/strategy-adapter.ts @@ -58,10 +58,10 @@ export class StrategyAdapter { strategy.error = (error: string) => { reject(new HttpErrors.Unauthorized(error)); }; - + const REDIRECT_URL = 302; strategy.redirect = (url: string) => { if (response) { - response.redirect(302, url); + response.redirect(REDIRECT_URL, url); } resolve(); };