feat: social auth providers can now be conditionally provisioned

This commit is contained in:
Balu Babu
2023-07-21 15:57:53 +05:30
committed by Andrew Bastin
parent 88f6a4ae26
commit 2d0ebedbbb
4 changed files with 28 additions and 5 deletions

View File

@@ -10,7 +10,7 @@ services:
build: build:
dockerfile: packages/hoppscotch-backend/Dockerfile dockerfile: packages/hoppscotch-backend/Dockerfile
context: . context: .
target: prod target: dev
env_file: env_file:
- ./.env - ./.env
restart: always restart: always

View File

@@ -11,6 +11,7 @@ import { RTJwtStrategy } from './strategies/rt-jwt.strategy';
import { GoogleStrategy } from './strategies/google.strategy'; import { GoogleStrategy } from './strategies/google.strategy';
import { GithubStrategy } from './strategies/github.strategy'; import { GithubStrategy } from './strategies/github.strategy';
import { MicrosoftStrategy } from './strategies/microsoft.strategy'; import { MicrosoftStrategy } from './strategies/microsoft.strategy';
import { EmptyClassProvider, authProviderCheck } from './helper';
@Module({ @Module({
imports: [ imports: [
@@ -26,9 +27,9 @@ import { MicrosoftStrategy } from './strategies/microsoft.strategy';
AuthService, AuthService,
JwtStrategy, JwtStrategy,
RTJwtStrategy, RTJwtStrategy,
GoogleStrategy, authProviderCheck('GOOGLE') ? GoogleStrategy : EmptyClassProvider,
GithubStrategy, authProviderCheck('GITHUB') ? GithubStrategy : EmptyClassProvider,
MicrosoftStrategy, authProviderCheck('MICROSOFT') ? MicrosoftStrategy : EmptyClassProvider,
], ],
controllers: [AuthController], controllers: [AuthController],
}) })

View File

@@ -4,7 +4,7 @@ import { AuthError } from 'src/types/AuthError';
import { AuthTokens } from 'src/types/AuthTokens'; import { AuthTokens } from 'src/types/AuthTokens';
import { Response } from 'express'; import { Response } from 'express';
import * as cookie from 'cookie'; 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 { enum AuthTokenType {
ACCESS_TOKEN = 'access_token', ACCESS_TOKEN = 'access_token',
@@ -97,3 +97,19 @@ export const subscriptionContextCookieParser = (rawCookies: string) => {
refresh_token: cookies[AuthTokenType.REFRESH_TOKEN], 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;
}

View File

@@ -22,6 +22,12 @@ export const AUTH_FAIL = 'auth/fail';
*/ */
export const JSON_INVALID = 'json_invalid'; 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. * Tried to delete a user data document from fb firestore but failed.
* (FirebaseService) * (FirebaseService)