Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new config strategy to backend
- Loading branch information
1 parent
13f98cc
commit 1b5e53f
Showing
19 changed files
with
304 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
const configVariables = [ | ||
{ | ||
key: "setupFinished", | ||
type: "boolean", | ||
default: "false", | ||
secret: false, | ||
locked: true | ||
}, | ||
{ | ||
key: "appUrl", | ||
type: "string", | ||
default: "http://localhost:3000", | ||
secret: false, | ||
}, | ||
{ | ||
key: "showHomePage", | ||
type: "boolean", | ||
default: "true", | ||
secret: false, | ||
}, | ||
{ | ||
key: "allowRegistration", | ||
type: "boolean", | ||
default: "true", | ||
secret: false, | ||
}, | ||
{ | ||
key: "allowUnauthenticatedShares", | ||
type: "boolean", | ||
default: "false", | ||
secret: false, | ||
}, | ||
{ | ||
key: "maxFileSize", | ||
type: "number", | ||
default: "1000000000", | ||
secret: false, | ||
}, | ||
{ | ||
key: "jwtSecret", | ||
type: "string", | ||
default: "long-random-string", | ||
locked: true | ||
}, | ||
{ | ||
key: "emailRecipientsEnabled", | ||
type: "boolean", | ||
default: "false", | ||
secret: false, | ||
}, | ||
{ | ||
key: "smtpHost", | ||
type: "string", | ||
default: "", | ||
}, | ||
{ | ||
key: "smtpPort", | ||
type: "number", | ||
default: "", | ||
}, | ||
{ | ||
key: "smtpEmail", | ||
type: "string", | ||
default: "", | ||
}, | ||
{ | ||
key: "smtpPassword", | ||
type: "string", | ||
default: "", | ||
}, | ||
]; | ||
|
||
async function main() { | ||
for (const variable of configVariables) { | ||
const existingConfigVariable = await prisma.config.findUnique({ | ||
where: { key: variable.key }, | ||
}); | ||
|
||
// Create a new config variable if it doesn't exist | ||
if (!existingConfigVariable) { | ||
await prisma.config.create({ | ||
data: variable, | ||
}); | ||
} else { | ||
// Update the config variable if the default value has changed | ||
if (existingConfigVariable.default != variable.default) { | ||
await prisma.config.update({ | ||
where: { key: variable.key }, | ||
data: { default: variable.default }, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
// Delete the config variable if it doesn't exist anymore | ||
const configVariablesFromDatabase = await prisma.config.findMany(); | ||
|
||
for (const configVariableFromDatabase of configVariablesFromDatabase) { | ||
if (!configVariables.find((v) => v.key == configVariableFromDatabase.key)) { | ||
await prisma.config.delete({ | ||
where: { key: configVariableFromDatabase.key }, | ||
}); | ||
} | ||
} | ||
} | ||
main() | ||
.then(async () => { | ||
await prisma.$disconnect(); | ||
}) | ||
.catch(async (e) => { | ||
console.error(e); | ||
await prisma.$disconnect(); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { ExecutionContext } from "@nestjs/common"; | ||
import { ExecutionContext, Injectable } from "@nestjs/common"; | ||
import { AuthGuard } from "@nestjs/passport"; | ||
import { ConfigService } from "src/config/config.service"; | ||
|
||
@Injectable() | ||
export class JwtGuard extends AuthGuard("jwt") { | ||
constructor() { | ||
constructor(private config: ConfigService) { | ||
super(); | ||
} | ||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
try { | ||
return (await super.canActivate(context)) as boolean; | ||
} catch { | ||
return process.env.ALLOW_UNAUTHENTICATED_SHARES == "true"; | ||
return this.config.get("allowUnauthenticatedShares"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { ConfigService } from "@nestjs/config"; | ||
import { PassportStrategy } from "@nestjs/passport"; | ||
import { User } from "@prisma/client"; | ||
import { ExtractJwt, Strategy } from "passport-jwt"; | ||
import { ConfigService } from "src/config/config.service"; | ||
import { PrismaService } from "src/prisma/prisma.service"; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor(config: ConfigService, private prisma: PrismaService) { | ||
console.log(config.get("jwtSecret")); | ||
config.get("jwtSecret"); | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: config.get("JWT_SECRET"), | ||
secretOrKey: config.get("jwtSecret"), | ||
}); | ||
} | ||
|
||
async validate(payload: { sub: string }) { | ||
console.log("vali"); | ||
const user: User = await this.prisma.user.findUnique({ | ||
where: { id: payload.sub }, | ||
}); | ||
|
||
console.log({ user }); | ||
return user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Controller, Get } from "@nestjs/common"; | ||
import { ConfigService } from "./config.service"; | ||
import { ConfigDTO } from "./dto/config.dto"; | ||
|
||
@Controller("configs") | ||
export class ConfigController { | ||
constructor(private configService: ConfigService) {} | ||
|
||
@Get() | ||
async list() { | ||
return new ConfigDTO().fromList(await this.configService.list()) | ||
} | ||
|
||
@Get("admin") | ||
async listForAdmin() { | ||
return await this.configService.listForAdmin(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Global, Module } from "@nestjs/common"; | ||
import { PrismaService } from "src/prisma/prisma.service"; | ||
import { ConfigController } from "./config.controller"; | ||
import { ConfigService } from "./config.service"; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [ | ||
{ | ||
provide: "CONFIG_VARIABLES", | ||
useFactory: async (prisma: PrismaService) => { | ||
return await prisma.config.findMany(); | ||
}, | ||
inject: [PrismaService], | ||
}, | ||
ConfigService, | ||
], | ||
controllers: [ConfigController], | ||
exports: [ConfigService], | ||
}) | ||
export class ConfigModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Inject, Injectable } from "@nestjs/common"; | ||
import { Config } from "@prisma/client"; | ||
import { PrismaService } from "src/prisma/prisma.service"; | ||
|
||
@Injectable() | ||
export class ConfigService { | ||
constructor( | ||
@Inject("CONFIG_VARIABLES") private configVariables: Config[], | ||
private prisma: PrismaService | ||
) {} | ||
|
||
get(key: string): any { | ||
const configVariable = this.configVariables.filter( | ||
(variable) => variable.key == key | ||
)[0]; | ||
|
||
if (!configVariable) throw new Error(`Config variable ${key} not found`); | ||
|
||
const value = configVariable.value ?? configVariable.default; | ||
|
||
if (configVariable.type == "number") return parseInt(value); | ||
if (configVariable.type == "boolean") return value == "true"; | ||
if (configVariable.type == "string") return value; | ||
} | ||
|
||
async listForAdmin() { | ||
return await this.prisma.config.findMany(); | ||
} | ||
|
||
async list() { | ||
const configVariables = await this.prisma.config.findMany({ | ||
where: { secret: { equals: false } }, | ||
}); | ||
|
||
return configVariables.map((configVariable) => { | ||
if (!configVariable.value) configVariable.value = configVariable.default; | ||
|
||
return configVariable; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Expose, plainToClass } from "class-transformer"; | ||
|
||
export class ConfigDTO { | ||
@Expose() | ||
key: string; | ||
|
||
@Expose() | ||
value: string; | ||
|
||
@Expose() | ||
type: string; | ||
|
||
fromList(partial: Partial<ConfigDTO>[]) { | ||
return partial.map((part) => | ||
plainToClass(ConfigDTO, part, { excludeExtraneousValues: true }) | ||
); | ||
} | ||
} |
Oops, something went wrong.