From 8035d1d592bb6d474c00477240aa97e0be0ef1ea Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Wed, 6 Dec 2023 11:44:03 +0600 Subject: [PATCH] fix: allowedAuthProviders and enableAndDisableSSO --- .../src/admin/infra.resolver.ts | 28 ++++++----------- .../src/infra-config/infra-config.service.ts | 31 ++++++++++--------- .../src/infra-config/input-args.ts | 15 +++++++++ 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/packages/hoppscotch-backend/src/admin/infra.resolver.ts b/packages/hoppscotch-backend/src/admin/infra.resolver.ts index 098fb6251..5cb2541ea 100644 --- a/packages/hoppscotch-backend/src/admin/infra.resolver.ts +++ b/packages/hoppscotch-backend/src/admin/infra.resolver.ts @@ -25,9 +25,10 @@ import { GqlAdmin } from './decorators/gql-admin.decorator'; import { ShortcodeWithUserEmail } from 'src/shortcode/shortcode.model'; import { InfraConfig } from 'src/infra-config/infra-config.model'; import { InfraConfigService } from 'src/infra-config/infra-config.service'; -import { InfraConfigArgs } from 'src/infra-config/input-args'; -import { AuthProvider } from 'src/auth/helper'; -import { AuthProviderStatus } from 'src/infra-config/helper'; +import { + EnableAndDisableSSOArgs, + InfraConfigArgs, +} from 'src/infra-config/input-args'; import { InfraConfigEnumForClient } from 'src/types/InfraConfig'; @UseGuards(GqlThrottlerGuard) @@ -251,7 +252,6 @@ export class InfraResolver { }) names: InfraConfigEnumForClient[], ) { - console.log(names); const infraConfigs = await this.infraConfigService.getMany(names); if (E.isLeft(infraConfigs)) throwErr(infraConfigs.left); return infraConfigs.right; @@ -260,7 +260,6 @@ export class InfraResolver { @Query(() => [String], { description: 'Allowed Auth Provider list', }) - @UseGuards(GqlAuthGuard, GqlAdminGuard) allowedAuthProviders() { return this.infraConfigService.getAllowedAuthProviders(); } @@ -300,22 +299,13 @@ export class InfraResolver { @UseGuards(GqlAuthGuard, GqlAdminGuard) async enableAndDisableSSO( @Args({ - name: 'provider', - type: () => AuthProvider, - description: 'Auth Provider to enable or disable', + name: 'data', + type: () => [EnableAndDisableSSOArgs], + description: 'SSO provider and status', }) - provider: AuthProvider, - @Args({ - name: 'status', - type: () => AuthProviderStatus, - description: 'Status to enable or disable', - }) - status: AuthProviderStatus, + data: EnableAndDisableSSOArgs[], ) { - const isUpdated = await this.infraConfigService.enableAndDisableSSO( - provider, - status, - ); + const isUpdated = await this.infraConfigService.enableAndDisableSSO(data); if (E.isLeft(isUpdated)) throwErr(isUpdated.left); return true; diff --git a/packages/hoppscotch-backend/src/infra-config/infra-config.service.ts b/packages/hoppscotch-backend/src/infra-config/infra-config.service.ts index a47d34b48..b65e5aefe 100644 --- a/packages/hoppscotch-backend/src/infra-config/infra-config.service.ts +++ b/packages/hoppscotch-backend/src/infra-config/infra-config.service.ts @@ -19,8 +19,7 @@ import { import { throwErr, validateUrl } from 'src/utils'; import { ConfigService } from '@nestjs/config'; import { AuthProviderStatus, stopApp } from './helper'; -import { InfraConfigArgs } from './input-args'; -import { AuthProvider } from 'src/auth/helper'; +import { EnableAndDisableSSOArgs, InfraConfigArgs } from './input-args'; @Injectable() export class InfraConfigService implements OnModuleInit { @@ -197,25 +196,27 @@ export class InfraConfigService implements OnModuleInit { * @param status Status to enable or disable * @returns Either true or an error */ - async enableAndDisableSSO( - provider: AuthProvider, - status: AuthProviderStatus, - ) { + async enableAndDisableSSO(statusList: EnableAndDisableSSOArgs[]) { const enabledAuthProviders = this.configService .get('INFRA.VITE_ALLOWED_AUTH_PROVIDERS') .split(','); - const isProviderEnabled = enabledAuthProviders.includes(provider); let newEnabledAuthProviders = enabledAuthProviders; - if (status === AuthProviderStatus.ENABLE && !isProviderEnabled) { - newEnabledAuthProviders = [...enabledAuthProviders, provider]; - } else if (status === AuthProviderStatus.DISABLE && isProviderEnabled) { - newEnabledAuthProviders = enabledAuthProviders.filter( - (p) => p !== provider, - ); - if (newEnabledAuthProviders.length === 0) { - return E.left(AUTH_PROVIDER_NOT_SPECIFIED); + + statusList.forEach(({ provider, status }) => { + if (status === AuthProviderStatus.ENABLE) { + newEnabledAuthProviders = [...newEnabledAuthProviders, provider]; + } else if (status === AuthProviderStatus.DISABLE) { + newEnabledAuthProviders = newEnabledAuthProviders.filter( + (p) => p !== provider, + ); } + }); + + newEnabledAuthProviders = [...new Set(newEnabledAuthProviders)]; + + if (newEnabledAuthProviders.length === 0) { + return E.left(AUTH_PROVIDER_NOT_SPECIFIED); } const isUpdated = await this.update( diff --git a/packages/hoppscotch-backend/src/infra-config/input-args.ts b/packages/hoppscotch-backend/src/infra-config/input-args.ts index df2c46544..74c80ac59 100644 --- a/packages/hoppscotch-backend/src/infra-config/input-args.ts +++ b/packages/hoppscotch-backend/src/infra-config/input-args.ts @@ -1,5 +1,7 @@ import { Field, InputType } from '@nestjs/graphql'; import { InfraConfigEnumForClient } from 'src/types/InfraConfig'; +import { AuthProviderStatus } from './helper'; +import { AuthProvider } from 'src/auth/helper'; @InputType() export class InfraConfigArgs { @@ -13,3 +15,16 @@ export class InfraConfigArgs { }) value: string; } + +@InputType() +export class EnableAndDisableSSOArgs { + @Field(() => AuthProvider, { + description: 'Auth Provider', + }) + provider: AuthProvider; + + @Field(() => AuthProviderStatus, { + description: 'Auth Provider Status', + }) + status: AuthProviderStatus; +}