From d33670060bc7741231ba431f7b4326c56ad1527b Mon Sep 17 00:00:00 2001 From: jvyden Date: Sun, 21 Nov 2021 20:41:30 -0500 Subject: [PATCH] Add ability to approve & deny authentication attempts --- .../ExternalAuth/AuthenticationController.cs | 62 +++++++++++++++++++ .../ExternalAuth/AuthenticationPage.cshtml | 4 +- .../Pages/Layouts/BaseLayout.cshtml | 2 +- ProjectLighthouse/StaticFiles/css/styles.css | 8 --- 4 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 ProjectLighthouse/Controllers/ExternalAuth/AuthenticationController.cs diff --git a/ProjectLighthouse/Controllers/ExternalAuth/AuthenticationController.cs b/ProjectLighthouse/Controllers/ExternalAuth/AuthenticationController.cs new file mode 100644 index 00000000..b1c5dd53 --- /dev/null +++ b/ProjectLighthouse/Controllers/ExternalAuth/AuthenticationController.cs @@ -0,0 +1,62 @@ +#nullable enable +using System.Threading.Tasks; +using LBPUnion.ProjectLighthouse.Types; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace LBPUnion.ProjectLighthouse.Controllers.ExternalAuth +{ + [ApiController] + [Route("/authentication")] + public class AuthenticationController : ControllerBase + { + private readonly Database database; + + public AuthenticationController(Database database) + { + this.database = database; + } + + [HttpGet("approve/{id:int}")] + public async Task Approve(int id) + { + User? user = this.database.UserFromWebRequest(this.Request); + if (user == null) return this.Redirect("/login"); + + AuthenticationAttempt? authAttempt = await this.database.AuthenticationAttempts.Include + (a => a.GameToken) + .FirstOrDefaultAsync(a => a.AuthenticationAttemptId == id); + if (authAttempt == null) return this.NotFound(); + + if (authAttempt.GameToken.UserId != user.UserId) return this.StatusCode(403, ""); + + authAttempt.GameToken.Approved = true; + this.database.AuthenticationAttempts.Remove(authAttempt); + + await this.database.SaveChangesAsync(); + + return this.Redirect("~/authentication"); + } + + [HttpGet("deny/{id:int}")] + public async Task Deny(int id) + { + User? user = this.database.UserFromWebRequest(this.Request); + if (user == null) return this.Redirect("/login"); + + AuthenticationAttempt? authAttempt = await this.database.AuthenticationAttempts.Include + (a => a.GameToken) + .FirstOrDefaultAsync(a => a.AuthenticationAttemptId == id); + if (authAttempt == null) return this.NotFound(); + + if (authAttempt.GameToken.UserId != user.UserId) return this.StatusCode(403, ""); + + this.database.GameTokens.Remove(authAttempt.GameToken); + this.database.AuthenticationAttempts.Remove(authAttempt); + + await this.database.SaveChangesAsync(); + + return this.Redirect("~/authentication"); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Pages/ExternalAuth/AuthenticationPage.cshtml b/ProjectLighthouse/Pages/ExternalAuth/AuthenticationPage.cshtml index 189e664a..9c80b815 100644 --- a/ProjectLighthouse/Pages/ExternalAuth/AuthenticationPage.cshtml +++ b/ProjectLighthouse/Pages/ExternalAuth/AuthenticationPage.cshtml @@ -22,10 +22,10 @@ else

A @authAttempt.Platform authentication request was logged at @timestamp.ToString("MM/dd/yyyy @ h:mm tt") UTC from the IP address @authAttempt.IPAddress.

- + - +
diff --git a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml index 363ef3f5..04df071d 100644 --- a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml +++ b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml @@ -23,7 +23,7 @@ -
+