summaryrefslogtreecommitdiff
path: root/VPNAuth.Server/Pages/Auth.cshtml.cs
diff options
context:
space:
mode:
authorTim <contact@bytim.eu>2025-04-18 12:25:59 +0200
committerTim <contact@bytim.eu>2025-04-18 12:25:59 +0200
commit4b2ad030fa381662f4b0c2464e97b0d2c5f6a716 (patch)
treedcc6af3136764322bd779110dcedd35e293d583c /VPNAuth.Server/Pages/Auth.cshtml.cs
downloadVPNAuth-4b2ad030fa381662f4b0c2464e97b0d2c5f6a716.tar.xz
VPNAuth-4b2ad030fa381662f4b0c2464e97b0d2c5f6a716.zip
Initial commit
Diffstat (limited to 'VPNAuth.Server/Pages/Auth.cshtml.cs')
-rw-r--r--VPNAuth.Server/Pages/Auth.cshtml.cs52
1 files changed, 52 insertions, 0 deletions
diff --git a/VPNAuth.Server/Pages/Auth.cshtml.cs b/VPNAuth.Server/Pages/Auth.cshtml.cs
new file mode 100644
index 0000000..bdcbc59
--- /dev/null
+++ b/VPNAuth.Server/Pages/Auth.cshtml.cs
@@ -0,0 +1,52 @@
+using Microsoft.AspNetCore.Mvc.RazorPages;
+using Microsoft.EntityFrameworkCore.ChangeTracking;
+using VPNAuth.Server.Database;
+
+namespace VPNAuth.Server.Pages;
+
+public class Auth : PageModel
+{
+ public Config Config;
+ public ConfigUser? User;
+ public bool ValidRequest;
+ public EntityEntry<AuthRequest>? RequestEntry;
+
+ public readonly List<string> RequiredQueryParams =
+ [
+ "response_type",
+ "client_id",
+ "scope",
+ "code_challenge_method",
+ "code_challenge"
+ ];
+
+ public void OnGet()
+ {
+ Config = Config.Read();
+ User = HttpContext.GetUser();
+
+ ValidRequest = RequiredQueryParams.All(key => Request.Query.ContainsKey(key))
+ && Config.FindApp(Request.Query["client_id"]!) != null
+ && Request.Query["code_challenge_method"] == "S256"
+ && User != null;
+
+ RequestEntry = null;
+
+ if (ValidRequest)
+ {
+ using var db = new Database.Database();
+ RequestEntry = db.Add(new AuthRequest
+ {
+ InitTime = DateTime.Now,
+ ClientId = Request.Query["client_id"]!,
+ Code = PkceUtils.GenerateCode(),
+ State = Request.Query["state"],
+ Scopes = Request.Query["scope"].ToString().Split(" ").ToList(),
+ CodeChallenge = Request.Query["code_challenge"]!,
+ CodeChallengeMethod = Request.Query["code_challenge_method"]!,
+ Accepted = false
+ });
+ db.SaveChanges();
+ }
+ }
+}