Skip to content

Commit

Permalink
refactor(chore): fix sonar code smells (#207)
Browse files Browse the repository at this point in the history
fix sonar code smells related to function type,
any typings and empty constructors

GH-204
  • Loading branch information
sf-sahil-jassal authored Jan 4, 2024
1 parent c2c247f commit 0236903
Show file tree
Hide file tree
Showing 21 changed files with 47 additions and 65 deletions.
2 changes: 0 additions & 2 deletions src/__tests__/fixtures/providers/azuread-auth.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {Request} from '@loopback/rest';
export class BearerTokenVerifyProvider
implements Provider<VerifyFunction.AzureADAuthFn>
{
constructor() {}

value(): VerifyFunction.AzureADAuthFn {
return async (
accessToken: string,
Expand Down
4 changes: 3 additions & 1 deletion src/strategies/client-auth-strategy.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export class ClientAuthStrategyProvider
this.clientMetadata.options as StrategyOptionsWithRequestInterface,
);
} else {
return Promise.reject(`The strategy ${name} is not available.`);
return Promise.reject(
new Error(`The strategy ${name} is not available.`),
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import {inject, Provider} from '@loopback/core';
import {AnyObject} from '@loopback/repository';
import {HttpErrors, Request} from '@loopback/rest';
import {HttpsProxyAgent} from 'https-proxy-agent';
import {
Profile,
// eslint-disable-next-line @typescript-eslint/naming-convention
import Strategy, {
AuthenticateOptions,
AuthenticateOptionsWithRequest,
VerifyCallback,
DecodedIdToken,
Profile,
VerifyCallback,
} from 'passport-apple';
import {AuthErrorKeys} from '../../../error-keys';
import {Strategies} from '../../keys';

import {VerifyFunction} from '../../types';
// eslint-disable-next-line @typescript-eslint/naming-convention
import Strategy from 'passport-apple';
export interface AppleAuthStrategyFactory {
(
options: AuthenticateOptions | AuthenticateOptionsWithRequest,
verifierPassed?: VerifyFunction.AppleAuthFn,
): Strategy;
}

export type AppleAuthStrategyFactory = (
options: AuthenticateOptions | AuthenticateOptionsWithRequest,
verifierPassed?: VerifyFunction.AppleAuthFn,
) => Strategy;

export class AppleAuthStrategyFactoryProvider
implements Provider<AppleAuthStrategyFactory>
Expand Down Expand Up @@ -109,8 +107,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 +116,6 @@ export class AppleAuthStrategyFactoryProvider
} else if (process.env['HTTPS_PROXY']) {
httpsProxyAgent = new HttpsProxyAgent(process.env['HTTPS_PROXY']);
strategy._oauth2.setAgent(httpsProxyAgent);
}
} else return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {VerifyFunction} from '../../types';
export class AzureADAuthVerifyProvider
implements Provider<VerifyFunction.AzureADAuthFn>
{
constructor() {}

value(): VerifyFunction.AzureADAuthFn {
return async (
accessToken: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {VerifyFunction} from '../../types';
export class BearerTokenVerifyProvider
implements Provider<VerifyFunction.BearerFn>
{
constructor() {}

value(): VerifyFunction.BearerFn {
return async (token: string) => {
throw new HttpErrors.NotImplemented(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {VerifyFunction} from '../../types';
export class ClientPasswordVerifyProvider
implements Provider<VerifyFunction.OauthClientPasswordFn>
{
constructor() {}

value(): VerifyFunction.OauthClientPasswordFn {
return async (clientId: string, clientSecret: string) => {
throw new HttpErrors.NotImplemented(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {Cognito, VerifyFunction} from '../../types';
export class CognitoAuthVerifyProvider
implements Provider<VerifyFunction.CognitoAuthFn>
{
constructor() {}

value(): VerifyFunction.CognitoAuthFn {
return async (
accessToken: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {inject, Provider} from '@loopback/core';
import {AnyObject} from '@loopback/repository';
import {HttpErrors, Request} from '@loopback/rest';
import {HttpsProxyAgent} from 'https-proxy-agent';
import {
Expand All @@ -7,7 +8,6 @@ import {
StrategyOption,
StrategyOptionWithRequest,
} from 'passport-facebook';

import {AuthErrorKeys} from '../../../error-keys';
import {Strategies} from '../../keys';
import {VerifyCallback, VerifyFunction} from '../../types';
Expand Down Expand Up @@ -99,8 +99,7 @@ export class FacebookAuthStrategyFactoryProvider
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {VerifyCallback, VerifyFunction} from '../../types';
export class FacebookAuthVerifyProvider
implements Provider<VerifyFunction.FacebookAuthFn>
{
constructor() {}

value(): VerifyFunction.FacebookAuthFn {
return async (
accessToken: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
StrategyOptionsWithRequest,
VerifyCallback,
} from 'passport-google-oauth20';

import {AnyObject} from '@loopback/repository';
import {AuthErrorKeys} from '../../../error-keys';
import {Strategies} from '../../keys';
import {VerifyFunction} from '../../types';
Expand Down Expand Up @@ -97,8 +97,7 @@ export class GoogleAuthStrategyFactoryProvider
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {VerifyFunction} from '../../types';
export class GoogleAuthVerifyProvider
implements Provider<VerifyFunction.GoogleAuthFn>
{
constructor() {}

value(): VerifyFunction.GoogleAuthFn {
return async (
accessToken: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {inject, Provider} from '@loopback/core';
import {AnyObject} from '@loopback/repository';
import {HttpErrors, Request} from '@loopback/rest';
import {HttpsProxyAgent} from 'https-proxy-agent';
import {
Expand All @@ -7,7 +8,6 @@ import {
StrategyOption,
StrategyOptionWithRequest,
} from 'passport-instagram';

import {AuthErrorKeys} from '../../../error-keys';
import {Strategies} from '../../keys';
import {VerifyCallback, VerifyFunction} from '../../types';
Expand Down Expand Up @@ -95,8 +95,7 @@ export class InstagramAuthStrategyFactoryProvider
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {VerifyCallback, VerifyFunction} from '../../types';
export class InstagramAuthVerifyProvider
implements Provider<VerifyFunction.InstagramAuthFn>
{
constructor() {}

value(): VerifyFunction.InstagramAuthFn {
return async (
accessToken: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {Keycloak, VerifyFunction} from '../../types';
export class KeycloakVerifyProvider
implements Provider<VerifyFunction.KeycloakAuthFn>
{
constructor() {}

value(): VerifyFunction.KeycloakAuthFn {
return async (
accessToken: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {VerifyFunction} from '../../types';
export class LocalPasswordVerifyProvider
implements Provider<VerifyFunction.LocalPasswordFn>
{
constructor() {}

value(): VerifyFunction.LocalPasswordFn {
return async (username: string, password: string) => {
throw new HttpErrors.NotImplemented(
Expand Down
21 changes: 15 additions & 6 deletions src/strategies/passport/passport-otp/otp-auth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {AnyObject} from '@loopback/repository';
import {Request} from '@loopback/rest';
import * as passport from 'passport';

export namespace Otp {
export type VerifyFunction = (
key: string,
otp: string,
done: (error: any, user?: any, info?: any) => void,
done: (
error?: string | Error | null,
user?: AnyObject,
info?: AnyObject,
) => void,
) => void;

export interface StrategyOptions {
Expand All @@ -15,8 +20,8 @@ export namespace Otp {

export type VerifyCallback = (
err?: string | Error | null,
user?: any,
info?: any,
user?: AnyObject,
info?: AnyObject,
) => void;

export class Strategy extends passport.Strategy {
Expand All @@ -31,7 +36,7 @@ export namespace Otp {
name: string;
private readonly verify: VerifyFunction;

authenticate(req: any, options?: StrategyOptions): void {
authenticate(req: Request, options?: StrategyOptions): void {
const key = req.body.key || options?.key;
const otp = req.body.otp || options?.otp;

Expand All @@ -40,7 +45,11 @@ export namespace Otp {
return;
}

const verified = (err?: any, user?: any, _info?: any) => {
const verified = (
err?: string | Error | null,
user?: AnyObject,
_info?: AnyObject,
) => {
if (err) {
this.error(err);
return;
Expand Down
2 changes: 0 additions & 2 deletions src/strategies/passport/passport-otp/otp-verify.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {HttpErrors} from '@loopback/rest';
import {VerifyFunction} from '../../types';

export class OtpVerifyProvider implements Provider<VerifyFunction.OtpAuthFn> {
constructor() {}

value(): VerifyFunction.OtpAuthFn {
return async (_key: string, _otp: string) => {
throw new HttpErrors.NotImplemented(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import {VerifyFunction} from '../../types';
import {Oauth2ResourceOwnerPassword} from './oauth2-resource-owner-password-grant';
import {isEmpty} from 'lodash';

export interface ResourceOwnerPasswordStrategyFactory {
(
options?: Oauth2ResourceOwnerPassword.StrategyOptionsWithRequestInterface,
verifierPassed?: VerifyFunction.ResourceOwnerPasswordFn,
): Oauth2ResourceOwnerPassword.Strategy;
}
export type ResourceOwnerPasswordStrategyFactory = (
options?: Oauth2ResourceOwnerPassword.StrategyOptionsWithRequestInterface,
verifierPassed?: VerifyFunction.ResourceOwnerPasswordFn,
) => Oauth2ResourceOwnerPassword.Strategy;

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

value(): VerifyFunction.ResourceOwnerPasswordFn {
return async (clientId, clientSecret, username, password) => {
throw new HttpErrors.NotImplemented(
Expand Down
4 changes: 3 additions & 1 deletion src/strategies/user-auth-strategy.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ export class AuthStrategyProvider implements Provider<Strategy | undefined> {
return this.processSamlFactory(verifier);
}
default:
return Promise.reject(`The strategy ${name} is not available.`);
return Promise.reject(
new Error(`The strategy ${name} is not available.`),
);
}
}
}
7 changes: 4 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ export interface AuthenticationMetadata<T = void> {
* interface definition of a function which accepts a request
* and returns an authenticated user
*/
export interface AuthenticateFn<T> {
(request: Request, response?: Response): Promise<T>;
}
export type AuthenticateFn<T> = (
request: Request,
response?: Response,
) => Promise<T>;

export interface ClientAuthCode<T extends IAuthUser, ID = number> {
clientId: string;
Expand Down

0 comments on commit 0236903

Please sign in to comment.