feat: feedback applied
This commit is contained in:
@@ -2,6 +2,7 @@ import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
|||||||
import { AuthGuard } from '@nestjs/passport';
|
import { AuthGuard } from '@nestjs/passport';
|
||||||
import { AuthProvider, authProviderCheck, throwHTTPErr } from '../helper';
|
import { AuthProvider, authProviderCheck, throwHTTPErr } from '../helper';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
import { AUTH_PROVIDER_NOT_SPECIFIED } from 'src/errors';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GithubSSOGuard extends AuthGuard('github') implements CanActivate {
|
export class GithubSSOGuard extends AuthGuard('github') implements CanActivate {
|
||||||
@@ -9,7 +10,7 @@ export class GithubSSOGuard extends AuthGuard('github') implements CanActivate {
|
|||||||
context: ExecutionContext,
|
context: ExecutionContext,
|
||||||
): boolean | Promise<boolean> | Observable<boolean> {
|
): boolean | Promise<boolean> | Observable<boolean> {
|
||||||
if (!authProviderCheck(AuthProvider.GITHUB))
|
if (!authProviderCheck(AuthProvider.GITHUB))
|
||||||
throwHTTPErr({ message: 'GitHub auth is not enabled', statusCode: 404 });
|
throwHTTPErr({ message: AUTH_PROVIDER_NOT_SPECIFIED, statusCode: 404 });
|
||||||
|
|
||||||
return super.canActivate(context);
|
return super.canActivate(context);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
|||||||
import { AuthGuard } from '@nestjs/passport';
|
import { AuthGuard } from '@nestjs/passport';
|
||||||
import { AuthProvider, authProviderCheck, throwHTTPErr } from '../helper';
|
import { AuthProvider, authProviderCheck, throwHTTPErr } from '../helper';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
import { AUTH_PROVIDER_NOT_SPECIFIED } from 'src/errors';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GoogleSSOGuard extends AuthGuard('google') implements CanActivate {
|
export class GoogleSSOGuard extends AuthGuard('google') implements CanActivate {
|
||||||
@@ -9,7 +10,7 @@ export class GoogleSSOGuard extends AuthGuard('google') implements CanActivate {
|
|||||||
context: ExecutionContext,
|
context: ExecutionContext,
|
||||||
): boolean | Promise<boolean> | Observable<boolean> {
|
): boolean | Promise<boolean> | Observable<boolean> {
|
||||||
if (!authProviderCheck(AuthProvider.GOOGLE))
|
if (!authProviderCheck(AuthProvider.GOOGLE))
|
||||||
throwHTTPErr({ message: 'Google auth is not enabled', statusCode: 404 });
|
throwHTTPErr({ message: AUTH_PROVIDER_NOT_SPECIFIED, statusCode: 404 });
|
||||||
|
|
||||||
return super.canActivate(context);
|
return super.canActivate(context);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
|||||||
import { AuthGuard } from '@nestjs/passport';
|
import { AuthGuard } from '@nestjs/passport';
|
||||||
import { AuthProvider, authProviderCheck, throwHTTPErr } from '../helper';
|
import { AuthProvider, authProviderCheck, throwHTTPErr } from '../helper';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
import { AUTH_PROVIDER_NOT_SPECIFIED } from 'src/errors';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class MicrosoftSSOGuard
|
export class MicrosoftSSOGuard
|
||||||
@@ -13,7 +14,7 @@ export class MicrosoftSSOGuard
|
|||||||
): boolean | Promise<boolean> | Observable<boolean> {
|
): boolean | Promise<boolean> | Observable<boolean> {
|
||||||
if (!authProviderCheck(AuthProvider.MICROSOFT))
|
if (!authProviderCheck(AuthProvider.MICROSOFT))
|
||||||
throwHTTPErr({
|
throwHTTPErr({
|
||||||
message: 'Microsoft auth is not enabled',
|
message: AUTH_PROVIDER_NOT_SPECIFIED,
|
||||||
statusCode: 404,
|
statusCode: 404,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -117,9 +117,11 @@ export function authProviderCheck(provider: string) {
|
|||||||
throwErr(AUTH_PROVIDER_NOT_SPECIFIED);
|
throwErr(AUTH_PROVIDER_NOT_SPECIFIED);
|
||||||
}
|
}
|
||||||
|
|
||||||
const envVariables = process.env.ALLOWED_AUTH_PROVIDERS.split(',').map(
|
const envVariables = process.env.ALLOWED_AUTH_PROVIDERS
|
||||||
(provider) => provider.trim().toUpperCase(),
|
? process.env.ALLOWED_AUTH_PROVIDERS.split(',').map((provider) =>
|
||||||
);
|
provider.trim().toUpperCase(),
|
||||||
|
)
|
||||||
|
: [];
|
||||||
|
|
||||||
if (!envVariables.includes(provider.toUpperCase())) return false;
|
if (!envVariables.includes(provider.toUpperCase())) return false;
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,32 @@ import * as cookieParser from 'cookie-parser';
|
|||||||
import { VersioningType } from '@nestjs/common';
|
import { VersioningType } from '@nestjs/common';
|
||||||
import * as session from 'express-session';
|
import * as session from 'express-session';
|
||||||
import { emitGQLSchemaFile } from './gql-schema';
|
import { emitGQLSchemaFile } from './gql-schema';
|
||||||
|
import { AuthProvider } from './auth/helper';
|
||||||
|
|
||||||
function checkRequiredEnvVars(requiredEnvVariables: string[]) {
|
function checkEnvironmentAuthProvider() {
|
||||||
for (const envVar of requiredEnvVariables) {
|
if (!process.env.hasOwnProperty('ALLOWED_AUTH_PROVIDERS')) {
|
||||||
if (!process.env[envVar]) {
|
console.log(`"ALLOWED_AUTH_PROVIDERS" is not present in .env file`);
|
||||||
console.error(`Environment variable "${envVar}" is missing or not set.`);
|
process.exit(1);
|
||||||
process.exit(1); // Exit the application with a non-zero status code to indicate an error
|
}
|
||||||
|
|
||||||
|
if (process.env.ALLOWED_AUTH_PROVIDERS === '') {
|
||||||
|
console.log(`"ALLOWED_AUTH_PROVIDERS" is empty in .env file`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const givenAuthProviders = process.env.ALLOWED_AUTH_PROVIDERS.split(',').map(
|
||||||
|
(provider) => provider.toLocaleUpperCase(),
|
||||||
|
);
|
||||||
|
const supportedAuthProviders = Object.values(AuthProvider).map(
|
||||||
|
(provider: string) => provider.toLocaleUpperCase(),
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const givenAuthProvider of givenAuthProviders) {
|
||||||
|
if (!supportedAuthProviders.includes(givenAuthProvider)) {
|
||||||
|
console.error(
|
||||||
|
`Environment variable "ALLOWED_AUTH_PROVIDERS" contains an unsupported auth provider "${givenAuthProvider}".`,
|
||||||
|
);
|
||||||
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,7 +39,7 @@ async function bootstrap() {
|
|||||||
console.log(`Running in production: ${process.env.PRODUCTION}`);
|
console.log(`Running in production: ${process.env.PRODUCTION}`);
|
||||||
console.log(`Port: ${process.env.PORT}`);
|
console.log(`Port: ${process.env.PORT}`);
|
||||||
|
|
||||||
checkRequiredEnvVars(['ALLOWED_AUTH_PROVIDERS']);
|
checkEnvironmentAuthProvider();
|
||||||
|
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user