From 0499548dd3b2aa949e1ce1f8cb87d8552f259a16 Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Mon, 5 Dec 2022 16:53:52 +0100 Subject: [PATCH] refactor: convert config variables to upper case --- backend/prisma/seed/config.seed.ts | 24 +++++++++---------- backend/src/app.module.ts | 6 ++--- backend/src/auth/auth.controller.ts | 2 +- backend/src/auth/auth.service.ts | 6 ++--- backend/src/auth/guard/jwt.guard.ts | 2 +- backend/src/auth/strategy/jwt.strategy.ts | 4 ++-- backend/src/config/config.service.ts | 2 +- backend/src/email/email.service.ts | 3 +-- backend/src/file/file.service.ts | 6 ++--- backend/src/file/pipe/fileValidation.pipe.ts | 2 +- backend/src/share/share.service.ts | 4 ++-- frontend/src/components/auth/SignInForm.tsx | 3 ++- frontend/src/components/auth/SignUpForm.tsx | 3 ++- frontend/src/components/navBar/NavBar.tsx | 4 ++-- frontend/src/components/upload/Dropzone.tsx | 4 ++-- .../upload/modals/showCreateUploadModal.tsx | 12 +++++----- frontend/src/pages/admin/setup.tsx | 2 +- frontend/src/pages/auth/signUp.tsx | 2 +- frontend/src/pages/index.tsx | 4 ++-- frontend/src/pages/upload.tsx | 10 ++++---- 20 files changed, 54 insertions(+), 51 deletions(-) diff --git a/backend/prisma/seed/config.seed.ts b/backend/prisma/seed/config.seed.ts index d7af208..2a71df8 100644 --- a/backend/prisma/seed/config.seed.ts +++ b/backend/prisma/seed/config.seed.ts @@ -3,7 +3,7 @@ import * as crypto from "crypto"; const configVariables = [ { - key: "setupFinished", + key: "SETUP_FINISHED", description: "Whether the setup has been finished", type: "boolean", value: "false", @@ -11,49 +11,49 @@ const configVariables = [ locked: true, }, { - key: "appUrl", + key: "APP_URL", description: "On which URL Pingvin Share is available", type: "string", value: "http://localhost:3000", secret: false, }, { - key: "showHomePage", + key: "SHOW_HOME_PAGE", description: "Whether to show the home page", type: "boolean", value: "true", secret: false, }, { - key: "allowRegistration", + key: "ALLOW_REGISTRATION", description: "Whether registration is allowed", type: "boolean", value: "true", secret: false, }, { - key: "allowUnauthenticatedShares", + key: "ALLOW_UNAUTHENTICATED_SHARES", description: "Whether unauthorized users can create shares", type: "boolean", value: "false", secret: false, }, { - key: "maxFileSize", + key: "MAX_FILE_SIZE", description: "Maximum file size in bytes", type: "number", value: "1000000000", secret: false, }, { - key: "jwtSecret", + key: "JWT_SECRET", description: "Long random string used to sign JWT tokens", type: "string", value: crypto.randomBytes(256).toString("base64"), locked: true, }, { - key: "emailRecipientsEnabled", + key: "ENABLE_EMAIL_RECIPIENTS", description: "Whether to send emails to recipients. Only set this to true if you entered the host, port, email and password of your SMTP server.", type: "boolean", @@ -61,25 +61,25 @@ const configVariables = [ secret: false, }, { - key: "smtpHost", + key: "SMTP_HOST", description: "Host of the SMTP server", type: "string", value: "", }, { - key: "smtpPort", + key: "SMTP_PORT", description: "Port of the SMTP server", type: "number", value: "", }, { - key: "smtpEmail", + key: "SMTP_EMAIL", description: "Email address of the SMTP server", type: "string", value: "", }, { - key: "smtpPassword", + key: "SMTP_PASSWORD", description: "Password of the SMTP server", type: "string", value: "", diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index fa8108e..aaf45c9 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -26,13 +26,13 @@ import { UserModule } from "./user/user.module"; MulterModule.registerAsync({ useFactory: (config: ConfigService) => ({ fileFilter: (req: Request, file, cb) => { - const maxFileSize = config.get("maxFileSize"); + const MAX_FILE_SIZE = config.get("MAX_FILE_SIZE"); const requestFileSize = parseInt(req.headers["content-length"]); - const isValidFileSize = requestFileSize <= maxFileSize; + const isValidFileSize = requestFileSize <= MAX_FILE_SIZE; cb( !isValidFileSize && new HttpException( - `File must be smaller than ${maxFileSize} bytes`, + `File must be smaller than ${MAX_FILE_SIZE} bytes`, HttpStatus.PAYLOAD_TOO_LARGE ), isValidFileSize diff --git a/backend/src/auth/auth.controller.ts b/backend/src/auth/auth.controller.ts index 6de47a1..f9afde3 100644 --- a/backend/src/auth/auth.controller.ts +++ b/backend/src/auth/auth.controller.ts @@ -28,7 +28,7 @@ export class AuthController { @Throttle(10, 5 * 60) @Post("signUp") async signUp(@Body() dto: AuthRegisterDTO) { - if (!this.config.get("allowRegistration")) + if (!this.config.get("ALLOW_REGISTRATION")) throw new ForbiddenException("Registration is not allowed"); return this.authService.signUp(dto); } diff --git a/backend/src/auth/auth.service.ts b/backend/src/auth/auth.service.ts index e697069..76f8fff 100644 --- a/backend/src/auth/auth.service.ts +++ b/backend/src/auth/auth.service.ts @@ -30,7 +30,7 @@ export class AuthService { email: dto.email, username: dto.username, password: hash, - isAdmin: !this.config.get("setupFinished"), + isAdmin: !this.config.get("SETUP_FINISHED"), }, }); @@ -74,7 +74,7 @@ export class AuthService { throw new ForbiddenException("Invalid password"); const hash = await argon.hash(newPassword); - + this.prisma.user.update({ where: { id: user.id }, data: { password: hash }, @@ -89,7 +89,7 @@ export class AuthService { }, { expiresIn: "15min", - secret: this.config.get("jwtSecret"), + secret: this.config.get("JWT_SECRET"), } ); } diff --git a/backend/src/auth/guard/jwt.guard.ts b/backend/src/auth/guard/jwt.guard.ts index 07854dd..39ecd13 100644 --- a/backend/src/auth/guard/jwt.guard.ts +++ b/backend/src/auth/guard/jwt.guard.ts @@ -11,7 +11,7 @@ export class JwtGuard extends AuthGuard("jwt") { try { return (await super.canActivate(context)) as boolean; } catch { - return this.config.get("allowUnauthenticatedShares"); + return this.config.get("ALLOW_UNAUTHENTICATED_SHARES"); } } } diff --git a/backend/src/auth/strategy/jwt.strategy.ts b/backend/src/auth/strategy/jwt.strategy.ts index d2172ca..d4bed84 100644 --- a/backend/src/auth/strategy/jwt.strategy.ts +++ b/backend/src/auth/strategy/jwt.strategy.ts @@ -8,10 +8,10 @@ import { PrismaService } from "src/prisma/prisma.service"; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor(config: ConfigService, private prisma: PrismaService) { - config.get("jwtSecret"); + config.get("JWT_SECRET"); super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), - secretOrKey: config.get("jwtSecret"), + secretOrKey: config.get("JWT_SECRET"), }); } diff --git a/backend/src/config/config.service.ts b/backend/src/config/config.service.ts index 04b1974..1291fee 100644 --- a/backend/src/config/config.service.ts +++ b/backend/src/config/config.service.ts @@ -63,7 +63,7 @@ export class ConfigService { async finishSetup() { return await this.prisma.config.update({ - where: { key: "setupFinished" }, + where: { key: "SETUP_FINISHED" }, data: { value: "true" }, }); } diff --git a/backend/src/email/email.service.ts b/backend/src/email/email.service.ts index 2d979c1..9231a9c 100644 --- a/backend/src/email/email.service.ts +++ b/backend/src/email/email.service.ts @@ -22,8 +22,7 @@ export class EmailService { if (!this.config.get("emailRecepientsEnabled")) throw new InternalServerErrorException("Email service disabled"); - const shareUrl = `${this.config.get("appUrl")}/share/${shareId}`; - + const shareUrl = `${this.config.get("APP_URL")}/share/${shareId}`; await transporter.sendMail({ from: `"Pingvin Share" <${this.config.get("SMTP_EMAIL")}>`, diff --git a/backend/src/file/file.service.ts b/backend/src/file/file.service.ts index 33efaf3..034ba06 100644 --- a/backend/src/file/file.service.ts +++ b/backend/src/file/file.service.ts @@ -82,7 +82,7 @@ export class FileService { const downloadToken = this.generateFileDownloadToken(shareId, fileId); return `${this.config.get( - "appUrl" + "APP_URL" )}/api/shares/${shareId}/files/${fileId}?token=${downloadToken}`; } @@ -96,7 +96,7 @@ export class FileService { }, { expiresIn: "10min", - secret: this.config.get("jwtSecret"), + secret: this.config.get("JWT_SECRET"), } ); } @@ -104,7 +104,7 @@ export class FileService { verifyFileDownloadToken(shareId: string, token: string) { try { const claims = this.jwtService.verify(token, { - secret: this.config.get("jwtSecret"), + secret: this.config.get("JWT_SECRET"), }); return claims.shareId == shareId; } catch { diff --git a/backend/src/file/pipe/fileValidation.pipe.ts b/backend/src/file/pipe/fileValidation.pipe.ts index 6a0ec86..c53e7f1 100644 --- a/backend/src/file/pipe/fileValidation.pipe.ts +++ b/backend/src/file/pipe/fileValidation.pipe.ts @@ -10,7 +10,7 @@ import { ConfigService } from "src/config/config.service"; export class FileValidationPipe implements PipeTransform { constructor(private config: ConfigService) {} async transform(value: any, metadata: ArgumentMetadata) { - if (value.size > this.config.get("maxFileSize")) + if (value.size > this.config.get("MAX_FILE_SIZE")) throw new BadRequestException("File is "); return value; } diff --git a/backend/src/share/share.service.ts b/backend/src/share/share.service.ts index 777ea73..2d33997 100644 --- a/backend/src/share/share.service.ts +++ b/backend/src/share/share.service.ts @@ -235,7 +235,7 @@ export class ShareService { }, { expiresIn: moment(expiration).diff(new Date(), "seconds") + "s", - secret: this.config.get("jwtSecret"), + secret: this.config.get("JWT_SECRET"), } ); } @@ -247,7 +247,7 @@ export class ShareService { try { const claims = this.jwtService.verify(token, { - secret: this.config.get("jwtSecret"), + secret: this.config.get("JWT_SECRET"), // Ignore expiration if expiration is 0 ignoreExpiration: moment(expiration).isSame(0), }); diff --git a/frontend/src/components/auth/SignInForm.tsx b/frontend/src/components/auth/SignInForm.tsx index 2f48f29..47ed0e5 100644 --- a/frontend/src/components/auth/SignInForm.tsx +++ b/frontend/src/components/auth/SignInForm.tsx @@ -49,7 +49,7 @@ const SignInForm = () => { > Welcome back - {config.get("allowRegistration") && ( + {config.get("ALLOW_REGISTRATION") && ( You don't have an account yet?{" "} @@ -65,6 +65,7 @@ const SignInForm = () => { > diff --git a/frontend/src/components/auth/SignUpForm.tsx b/frontend/src/components/auth/SignUpForm.tsx index a8cc673..5c5ed39 100644 --- a/frontend/src/components/auth/SignUpForm.tsx +++ b/frontend/src/components/auth/SignUpForm.tsx @@ -57,7 +57,7 @@ const SignUpForm = () => { > Sign up - {config.get("allowRegistration") && ( + {config.get("ALLOW_REGISTRATION") && ( You have an account already?{" "} @@ -78,6 +78,7 @@ const SignUpForm = () => { /> { ]); useEffect(() => { - if (config.get("showHomePage")) + if (config.get("SHOW_HOME_PAGE")) setUnauthenticatedLinks((array) => [ { link: "/", @@ -139,7 +139,7 @@ const NavBar = () => { ...array, ]); - if (config.get("allowRegistration")) + if (config.get("ALLOW_REGISTRATION")) setUnauthenticatedLinks((array) => [ ...array, { diff --git a/frontend/src/components/upload/Dropzone.tsx b/frontend/src/components/upload/Dropzone.tsx index 7aba326..234da42 100644 --- a/frontend/src/components/upload/Dropzone.tsx +++ b/frontend/src/components/upload/Dropzone.tsx @@ -45,7 +45,7 @@ const Dropzone = ({ return (
{ toast.error(e[0].errors[0].message); }} @@ -75,7 +75,7 @@ const Dropzone = ({ Drag'n'drop files here to start your share. We can accept only files that are less than{" "} - {byteStringToHumanSizeString(config.get("maxFileSize"))} in size. + {byteStringToHumanSizeString(config.get("MAX_FILE_SIZE"))} in size.
diff --git a/frontend/src/components/upload/modals/showCreateUploadModal.tsx b/frontend/src/components/upload/modals/showCreateUploadModal.tsx index 1cc93d5..85c3b32 100644 --- a/frontend/src/components/upload/modals/showCreateUploadModal.tsx +++ b/frontend/src/components/upload/modals/showCreateUploadModal.tsx @@ -29,8 +29,8 @@ const showCreateUploadModal = ( modals: ModalsContextProps, options: { isUserSignedIn: boolean; - allowUnauthenticatedShares: boolean; - emailRecipientsEnabled: boolean; + ALLOW_UNAUTHENTICATED_SHARES: boolean; + ENABLE_EMAIL_RECIPIENTS: boolean; }, uploadCallback: ( id: string, @@ -62,14 +62,14 @@ const CreateUploadModalBody = ({ ) => void; options: { isUserSignedIn: boolean; - allowUnauthenticatedShares: boolean; - emailRecipientsEnabled: boolean; + ALLOW_UNAUTHENTICATED_SHARES: boolean; + ENABLE_EMAIL_RECIPIENTS: boolean; }; }) => { const modals = useModals(); const [showNotSignedInAlert, setShowNotSignedInAlert] = useState( - options.emailRecipientsEnabled + options.ENABLE_EMAIL_RECIPIENTS ); const validationSchema = yup.object().shape({ @@ -230,7 +230,7 @@ const CreateUploadModalBody = ({ {ExpirationPreview({ form })}
- {options.emailRecipientsEnabled && ( + {options.ENABLE_EMAIL_RECIPIENTS && ( Email recipients diff --git a/frontend/src/pages/admin/setup.tsx b/frontend/src/pages/admin/setup.tsx index 14c4942..7872005 100644 --- a/frontend/src/pages/admin/setup.tsx +++ b/frontend/src/pages/admin/setup.tsx @@ -17,7 +17,7 @@ const Setup = () => { if (!user) { router.push("/auth/signUp"); return; - } else if (config.get("setupFinished")) { + } else if (config.get("SETUP_FINISHED")) { router.push("/"); return; } diff --git a/frontend/src/pages/auth/signUp.tsx b/frontend/src/pages/auth/signUp.tsx index 96d51b9..faa68ec 100644 --- a/frontend/src/pages/auth/signUp.tsx +++ b/frontend/src/pages/auth/signUp.tsx @@ -10,7 +10,7 @@ const SignUp = () => { const router = useRouter(); if (user) { router.replace("/"); - } else if (config.get("allowRegistration") == "false") { + } else if (config.get("ALLOW_REGISTRATION") == "false") { router.replace("/auth/signIn"); } else { return ( diff --git a/frontend/src/pages/index.tsx b/frontend/src/pages/index.tsx index b122ba4..0e80be9 100644 --- a/frontend/src/pages/index.tsx +++ b/frontend/src/pages/index.tsx @@ -74,9 +74,9 @@ export default function Home() { const { classes } = useStyles(); const router = useRouter(); - if (user || config.get("allowUnauthenticatedShares")) { + if (user || config.get("ALLOW_UNAUTHENTICATED_SHARES")) { router.replace("/upload"); - } else if (!config.get("showHomePage")) { + } else if (!config.get("SHOW_HOME_PAGE")) { router.replace("/auth/signIn"); } else { return ( diff --git a/frontend/src/pages/upload.tsx b/frontend/src/pages/upload.tsx index 06aff04..2c86c1c 100644 --- a/frontend/src/pages/upload.tsx +++ b/frontend/src/pages/upload.tsx @@ -95,7 +95,7 @@ const Upload = () => { } } }, [files]); - if (!user && !config.get("allowUnauthenticatedShares")) { + if (!user && !config.get("ALLOW_UNAUTHENTICATED_SHARES")) { router.replace("/"); } else { return ( @@ -110,10 +110,12 @@ const Upload = () => { modals, { isUserSignedIn: user ? true : false, - allowUnauthenticatedShares: config.get( - "allowUnauthenticatedShares" + ALLOW_UNAUTHENTICATED_SHARES: config.get( + "ALLOW_UNAUTHENTICATED_SHARES" + ), + ENABLE_EMAIL_RECIPIENTS: config.get( + "ENABLE_EMAIL_RECIPIENTS" ), - emailRecipientsEnabled: config.get("emailRecipientsEnabled"), }, uploadFiles )