Skip to content

Commit

Permalink
fix(chore): fixing sonar smells
Browse files Browse the repository at this point in the history
resolving sonar smells to improve quality gate

GH-142
  • Loading branch information
arpit1503khanna authored and RaghavaroraSF committed Apr 26, 2023
1 parent 008b5c5 commit 067b8ee
Show file tree
Hide file tree
Showing 18 changed files with 385 additions and 363 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import {inject, Provider} from '@loopback/core';
import {HttpErrors, Request} from '@loopback/rest';
import {HttpsProxyAgent} from 'https-proxy-agent';
import {AnyObject} from '@loopback/repository';

import {
Profile,
AuthenticateOptions,
AuthenticateOptionsWithRequest,
VerifyCallback,
DecodedIdToken,
Strategy,
} from 'passport-apple';

import {AuthErrorKeys} from '../../../error-keys';
import {Strategies} from '../../keys';
import {VerifyFunction} from '../../types';

import Strategy from 'passport-apple';
export interface AppleAuthStrategyFactory {
(
options: AuthenticateOptions | AuthenticateOptionsWithRequest,
Expand All @@ -40,38 +41,36 @@ export class AppleAuthStrategyFactoryProvider
): Strategy {
const verifyFn = verifierPassed ?? this.verifierAppleAuth;
let strategy;
const func = async (
req: Request,
accessToken: string,
refreshToken: string,
decodedIdToken: string,
profile: Profile,
cb: VerifyCallback,
) => {
try {
const user = await verifyFn(
accessToken,
refreshToken,
decodedIdToken,
profile,
cb,
req,
);
if (!user) {
throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials);
}
cb(undefined, user);
} catch (err) {
cb(err);
}
};
if (options && options.passReqToCallback === true) {
strategy = new Strategy(
options,

// eslint-disable-next-line @typescript-eslint/no-misused-promises
async (
req: Request,
accessToken: string,
refreshToken: string,
decodedIdToken: DecodedIdToken,
profile: Profile,
cb: VerifyCallback,
) => {
try {
const user = await verifyFn(
accessToken,
refreshToken,
decodedIdToken,
profile,
cb,
req,
);
if (!user) {
throw new HttpErrors.Unauthorized(
AuthErrorKeys.InvalidCredentials,
);
}
cb(undefined, user);
} catch (err) {
cb(err);
}
},
func,
);
} else {
strategy = new Strategy(
Expand All @@ -80,7 +79,7 @@ export class AppleAuthStrategyFactoryProvider
async (
accessToken: string,
refreshToken: string,
decodedIdToken: DecodedIdToken,
decodedIdToken: string,
profile: Profile,
cb: VerifyCallback,
) => {
Expand Down Expand Up @@ -109,8 +108,7 @@ export class AppleAuthStrategyFactoryProvider
return strategy;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _setupProxy(strategy: any) {
private _setupProxy(strategy: AnyObject) {
// Setup proxy if any
let httpsProxyAgent;
if (process.env['https_proxy']) {
Expand All @@ -119,6 +117,8 @@ export class AppleAuthStrategyFactoryProvider
} else if (process.env['HTTPS_PROXY']) {
httpsProxyAgent = new HttpsProxyAgent(process.env['HTTPS_PROXY']);
strategy._oauth2.setAgent(httpsProxyAgent);
} else {
//this is intentional
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,61 @@ export class AzureADAuthStrategyFactoryProvider
return (options, verifier) =>
this.getAzureADAuthStrategyVerifier(options, verifier);
}
oidcFunctionVerifier1(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);
}
};
}
oidcFunctionVerifier2(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,
Expand All @@ -40,74 +95,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.oidcFunctionVerifier1(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.oidcFunctionVerifier2(verifyFn),
);
} else {
throw new Error('Invalid value for passReqToCallback');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,40 @@ export class BearerStrategyFactoryProvider
this.getBearerStrategyVerifier(options, verifier);
}

getBearerStrategyVerifier1(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);
}
};
}

getBearerStrategyVerifier2(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);
}
};
}
getBearerStrategyVerifier(
options?: PassportBearer.IStrategyOptions,
verifierPassed?: VerifyFunction.BearerFn,
Expand All @@ -37,41 +71,13 @@ 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.getBearerStrategyVerifier1(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.getBearerStrategyVerifier2(verifyFn),
);
} else {
return new PassportBearer.Strategy(
Expand Down
Loading

0 comments on commit 067b8ee

Please sign in to comment.