-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make google auth optional on server side #508
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Injectable, CanActivate, HttpException } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { EnvironmentService } from 'src/integrations/environment/environment.service'; | ||
|
||
@Injectable() | ||
export class GoogleProviderEnabledGuard implements CanActivate { | ||
constructor(private readonly environmentService: EnvironmentService) {} | ||
canActivate(): boolean | Promise<boolean> | Observable<boolean> { | ||
if (!this.environmentService.getAuthGoogleEnabled()) { | ||
throw new HttpException('Google auth is not enabled', 404); | ||
} | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { plainToClass } from 'class-transformer'; | ||
import { plainToClass, Transform } from 'class-transformer'; | ||
import { | ||
IsEnum, | ||
IsOptional, | ||
IsString, | ||
IsUrl, | ||
ValidateIf, | ||
validateSync, | ||
IsBoolean, | ||
} from 'class-validator'; | ||
import { assert } from 'src/utils/assert'; | ||
import { IsDuration } from './decorators/is-duration.decorator'; | ||
|
@@ -38,34 +39,44 @@ export class EnvironmentVariables { | |
@IsUrl({ require_tld: false }) | ||
FRONT_AUTH_CALLBACK_URL: string; | ||
|
||
@IsString() | ||
@Transform(({ value }) => envValueToBoolean(value)) | ||
@IsOptional() | ||
@IsBoolean() | ||
AUTH_GOOGLE_ENABLED?: boolean; | ||
|
||
@IsString() | ||
@ValidateIf((env) => env.AUTH_GOOGLE_ENABLED === true) | ||
AUTH_GOOGLE_CLIENT_ID?: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
@ValidateIf((env) => env.AUTH_GOOGLE_ENABLED === true) | ||
AUTH_GOOGLE_CLIENT_SECRET?: string; | ||
|
||
@IsUrl({ require_tld: false }) | ||
@IsOptional() | ||
@ValidateIf((env) => env.AUTH_GOOGLE_ENABLED === true) | ||
AUTH_GOOGLE_CALLBACK_URL?: string; | ||
|
||
// Storage | ||
@IsEnum(StorageType) | ||
@IsOptional() | ||
STORAGE_TYPE?: StorageType; | ||
|
||
@ValidateIf((_, value) => value === StorageType.S3) | ||
@ValidateIf((env) => env.STORAGE_TYPE === StorageType.S3) | ||
@IsAWSRegion() | ||
STORAGE_REGION?: AwsRegion; | ||
STORAGE_S3_REGION?: AwsRegion; | ||
|
||
@ValidateIf((env) => env.STORAGE_TYPE === StorageType.S3) | ||
@IsString() | ||
STORAGE_S3_NAME?: string; | ||
|
||
@IsString() | ||
STORAGE_LOCATION: string; | ||
@ValidateIf((env) => env.STORAGE_TYPE === StorageType.Local) | ||
STORAGE_LOCAL_PATH?: string; | ||
} | ||
|
||
export function validate(config: Record<string, unknown>) { | ||
const validatedConfig = plainToClass(EnvironmentVariables, config, { | ||
enableImplicitConversion: true, | ||
enableImplicitConversion: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have disabled this because it was casting ENV_VARIABLE=false to TRUE... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my other comment below There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, maybe some ideas here: typestack/class-transformer#550 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice link did not find it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to use |
||
}); | ||
|
||
const errors = validateSync(validatedConfig, { | ||
|
@@ -75,3 +86,16 @@ export function validate(config: Record<string, unknown>) { | |
|
||
return validatedConfig; | ||
} | ||
|
||
const envValueToBoolean = (value: any) => { | ||
if (typeof value === 'boolean') { | ||
return value; | ||
} | ||
if (['true', 'on', 'yes', '1'].includes(value.toLowerCase())) { | ||
return true; | ||
} | ||
if (['false', 'off', 'no', '0'].includes(value.toLowerCase())) { | ||
return false; | ||
} | ||
return undefined; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ import { LocalStorageModule } from './local-storage/local-storage.module'; | |
import { LocalStorageModuleOptions } from './local-storage/interfaces'; | ||
import { EnvironmentModule } from './environment/environment.module'; | ||
import { EnvironmentService } from './environment/environment.service'; | ||
import { assert } from 'src/utils/assert'; | ||
|
||
/** | ||
* S3 Storage Module factory | ||
|
@@ -16,23 +15,16 @@ import { assert } from 'src/utils/assert'; | |
const S3StorageModuleFactory = async ( | ||
environmentService: EnvironmentService, | ||
): Promise<S3StorageModuleOptions> => { | ||
const fileSystem = environmentService.getStorageType(); | ||
const bucketName = environmentService.getStorageLocation(); | ||
const region = environmentService.getStorageRegion(); | ||
|
||
if (fileSystem === 'local') { | ||
return { bucketName }; | ||
} | ||
|
||
assert(region, 'S3 region is not defined'); | ||
const bucketName = environmentService.getStorageS3Name(); | ||
const region = environmentService.getStorageS3Region(); | ||
|
||
return { | ||
bucketName, | ||
bucketName: bucketName ?? '', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm only moving the problem to here. I believe the S3Module should not be instanciated if S3 is not configured in env variables. I would encapsulated LocalDriver and S3Driver behind one common FileStorage Interface. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we track this as a new task ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I'll discuss it with Jeremy tomorrow and create a task based on discussion! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created here: #510 we can discuss it tomorrow |
||
credentials: fromNodeProviderChain({ | ||
clientConfig: { region }, | ||
}), | ||
forcePathStyle: true, | ||
region, | ||
region: region ?? '', | ||
}; | ||
}; | ||
|
||
|
@@ -44,10 +36,10 @@ const S3StorageModuleFactory = async ( | |
const localStorageModuleFactory = async ( | ||
environmentService: EnvironmentService, | ||
): Promise<LocalStorageModuleOptions> => { | ||
const folderName = environmentService.getStorageLocation(); | ||
const storagePath = environmentService.getStorageLocalPath(); | ||
|
||
return { | ||
storagePath: process.cwd() + '/' + folderName, | ||
storagePath: process.cwd() + '/' + storagePath, | ||
}; | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@magrinj @emilienchvt not very proud of these disabled but
super()
has to be called.Another solution would be to not register the strategy using DynamicModules
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not extremely better, but disabledClientId, disabledClientSecret and disabledCallbackUrl might be less generic