Skip to content

Commit

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

GH-142
  • Loading branch information
arpit1503khanna committed Aug 4, 2023
1 parent 34971e9 commit 1a454a7
Show file tree
Hide file tree
Showing 21 changed files with 401 additions and 431 deletions.
7 changes: 4 additions & 3 deletions src/strategies/SAML/saml-strategy-factory-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SamlStrategyFactory>
Expand Down
4 changes: 0 additions & 4 deletions src/strategies/SAML/saml-verify.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import {VerifyFunction} from '../../types';
* It will just throw an error saying Not Implemented
*/
export class SamlVerifyProvider implements Provider<VerifyFunction.SamlFn> {
constructor() {
//This is intentional
}

value(): VerifyFunction.SamlFn {
return async (
profile: SamlStrategy.Profile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
(
Expand Down Expand Up @@ -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,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {VerifyFunction} from '../../types';
export class AppleAuthVerifyProvider
implements Provider<VerifyFunction.AppleAuthFn>
{
constructor() {}

value(): VerifyFunction.AppleAuthFn {
return async (
accessToken: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AzureADAuthStrategyFactory>
Expand All @@ -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,
Expand All @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<VerifyFunction.BearerFn>;
}
export type BearerStrategyFactory = (
options?: PassportBearer.IStrategyOptions,
verifierPassed?: VerifyFunction.BearerFn,
) => PassportBearer.Strategy<VerifyFunction.BearerFn>;

export class BearerStrategyFactoryProvider
implements Provider<BearerStrategyFactory>
Expand All @@ -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<VerifyFunction.BearerFn> {
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,
Expand All @@ -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);
}
}
}
Loading

0 comments on commit 1a454a7

Please sign in to comment.