From 3766e71ecbd3e4bff95f90e34e9320e69cae9b66 Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Sun, 30 Jun 2024 20:42:19 +0800 Subject: [PATCH] deps (#29) --- api/Note.fs | 29 +++++------------------------ api/Notepad.fsproj | 5 +++-- api/Program.fs | 16 ++++------------ api/Util.fs | 6 ++++++ 4 files changed, 18 insertions(+), 38 deletions(-) create mode 100644 api/Util.fs diff --git a/api/Note.fs b/api/Note.fs index 1215bdd..0f6862c 100644 --- a/api/Note.fs +++ b/api/Note.fs @@ -129,17 +129,8 @@ let createNewUser conn email password = //let jwtSecret = JwtSecrets.GetJwtSecret conn "logbook" - // Get the JWT key from the environment variable - let jwtKey = - match System.Environment.GetEnvironmentVariable("JWT_SECRET") with - | null -> failwith "JWT_SECRET environment variable not found" - | key -> key - - // Get the audience from the environment variable - let audience = - match System.Environment.GetEnvironmentVariable("JWT_AUDIENCE") with - | null -> failwith "JWT_AUDIENCE environment variable not found" - | aud -> aud + let jwtKey = Util.getEnvVar "JWT_SECRET" + let audience = Util.getEnvVar "JWT_AUDIENCE" let issuer = "logbook-swuecho.github.com" @@ -149,6 +140,7 @@ let createNewUser conn email password = jwt + let login: HttpHandler = fun ctx -> Request.mapJson @@ -165,19 +157,8 @@ let login: HttpHandler = // check if user is admin let role = if user.IsSuperuser then "admin" else "user" - // let jwtSecret = JwtSecrets.GetJwtSecret conn "logbook" - - let jwtKey = - match System.Environment.GetEnvironmentVariable("JWT_SECRET") with - | null -> failwith "JWT_SECRET environment variable not found" - | key -> key - - // Get the audience from the environment variable - let audience = - match System.Environment.GetEnvironmentVariable("JWT_AUDIENCE") with - | null -> failwith "JWT_AUDIENCE environment variable not found" - | aud -> aud - + let jwtKey = Util.getEnvVar "JWT_SECRET" + let audience = Util.getEnvVar "JWT_AUDIENCE" let issuer = "logbook-swuecho.github.com" if passwordMatches then diff --git a/api/Notepad.fsproj b/api/Notepad.fsproj index a589a6d..6f2c0fd 100644 --- a/api/Notepad.fsproj +++ b/api/Notepad.fsproj @@ -4,6 +4,7 @@ net7.0 + @@ -23,8 +24,8 @@ - + - + \ No newline at end of file diff --git a/api/Program.fs b/api/Program.fs index 6bcee93..18585fd 100644 --- a/api/Program.fs +++ b/api/Program.fs @@ -55,19 +55,11 @@ let authUserMiddleware (app: IApplicationBuilder) = app.Use(middleware) + + let authService (services: IServiceCollection) = - - // Get the JWT key from the environment variable - let jwtKey = - match System.Environment.GetEnvironmentVariable("JWT_SECRET") with - | null -> failwith "JWT_SECRET environment variable not found" - | key -> key - - // Get the audience from the environment variable - let audience = - match System.Environment.GetEnvironmentVariable("JWT_AUDIENCE") with - | null -> failwith "JWT_AUDIENCE environment variable not found" - | aud -> aud + let jwtKey = Util.getEnvVar "JWT_SECRET" + let audience = Util.getEnvVar "JWT_AUDIENCE" let _ = diff --git a/api/Util.fs b/api/Util.fs new file mode 100644 index 0000000..a8bd595 --- /dev/null +++ b/api/Util.fs @@ -0,0 +1,6 @@ +module Util + +let getEnvVar varName = + match System.Environment.GetEnvironmentVariable(varName) with + | null -> failwith (sprintf "%s environment variable not found" varName) + | value -> value