From 2d0ebedbbbb656a66bb9f460157d7f6575c4bedc Mon Sep 17 00:00:00 2001 From: Balu Babu Date: Fri, 21 Jul 2023 15:57:53 +0530 Subject: [PATCH] feat: social auth providers can now be conditionally provisioned --- docker-compose.yml | 2 +- .../hoppscotch-backend/src/auth/auth.module.ts | 7 ++++--- packages/hoppscotch-backend/src/auth/helper.ts | 18 +++++++++++++++++- packages/hoppscotch-backend/src/errors.ts | 6 ++++++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 417b15002..a1afe694f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,7 +10,7 @@ services: build: dockerfile: packages/hoppscotch-backend/Dockerfile context: . - target: prod + target: dev env_file: - ./.env restart: always diff --git a/packages/hoppscotch-backend/src/auth/auth.module.ts b/packages/hoppscotch-backend/src/auth/auth.module.ts index 4793c2b85..8585635e8 100644 --- a/packages/hoppscotch-backend/src/auth/auth.module.ts +++ b/packages/hoppscotch-backend/src/auth/auth.module.ts @@ -11,6 +11,7 @@ import { RTJwtStrategy } from './strategies/rt-jwt.strategy'; import { GoogleStrategy } from './strategies/google.strategy'; import { GithubStrategy } from './strategies/github.strategy'; import { MicrosoftStrategy } from './strategies/microsoft.strategy'; +import { EmptyClassProvider, authProviderCheck } from './helper'; @Module({ imports: [ @@ -26,9 +27,9 @@ import { MicrosoftStrategy } from './strategies/microsoft.strategy'; AuthService, JwtStrategy, RTJwtStrategy, - GoogleStrategy, - GithubStrategy, - MicrosoftStrategy, + authProviderCheck('GOOGLE') ? GoogleStrategy : EmptyClassProvider, + authProviderCheck('GITHUB') ? GithubStrategy : EmptyClassProvider, + authProviderCheck('MICROSOFT') ? MicrosoftStrategy : EmptyClassProvider, ], controllers: [AuthController], }) diff --git a/packages/hoppscotch-backend/src/auth/helper.ts b/packages/hoppscotch-backend/src/auth/helper.ts index e10cea0d8..4e9968504 100644 --- a/packages/hoppscotch-backend/src/auth/helper.ts +++ b/packages/hoppscotch-backend/src/auth/helper.ts @@ -4,7 +4,7 @@ import { AuthError } from 'src/types/AuthError'; import { AuthTokens } from 'src/types/AuthTokens'; import { Response } from 'express'; import * as cookie from 'cookie'; -import { COOKIES_NOT_FOUND } from 'src/errors'; +import { AUTH_PROVIDER_NOT_SPECIFIED, COOKIES_NOT_FOUND } from 'src/errors'; enum AuthTokenType { ACCESS_TOKEN = 'access_token', @@ -97,3 +97,19 @@ export const subscriptionContextCookieParser = (rawCookies: string) => { refresh_token: cookies[AuthTokenType.REFRESH_TOKEN], }; }; + +export class EmptyClassProvider {} + +export function authProviderCheck(provider: string) { + if (!provider) { + throw new Error(AUTH_PROVIDER_NOT_SPECIFIED); + } + + const envVariables = process.env.ALLOWED_AUTH_PROVIDERS.split(',').map( + (provider) => provider.trim().toUpperCase(), + ); + + if (!envVariables.includes(provider.toUpperCase())) return false; + + return true; +} diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 2e45d5033..b9599dd4f 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -22,6 +22,12 @@ export const AUTH_FAIL = 'auth/fail'; */ export const JSON_INVALID = 'json_invalid'; +/** + * Auth Provider not specified + * (Utils) + */ +export const AUTH_PROVIDER_NOT_SPECIFIED = 'auth/provider_not_specified'; + /** * Tried to delete a user data document from fb firestore but failed. * (FirebaseService)