Add ability to approve & deny authentication attempts

This commit is contained in:
jvyden 2021-11-21 20:41:30 -05:00
commit d33670060b
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
4 changed files with 65 additions and 11 deletions

View file

@ -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<IActionResult> 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<IActionResult> 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");
}
}
}

View file

@ -22,10 +22,10 @@ else
<div class="ui red segment">
<p>A <b>@authAttempt.Platform</b> authentication request was logged at <b>@timestamp.ToString("MM/dd/yyyy @ h:mm tt") UTC</b> from the IP address <b>@authAttempt.IPAddress</b>.</p>
<div>
<a href="/authentication/approve/@authAttempt.GameTokenId">
<a href="/authentication/approve/@authAttempt.AuthenticationAttemptId">
<button class="ui tiny green button">Approve</button>
</a>
<a href="/authentication/deny/@authAttempt.GameTokenId">
<a href="/authentication/deny/@authAttempt.AuthenticationAttemptId">
<button class="ui tiny red button">Deny</button>
</a>
</div>

View file

@ -23,7 +23,7 @@
<link rel="stylesheet" type="text/css" href="~/css/styles.css">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/fomantic-ui@2.8.8/dist/semantic.min.css">
</head>
<header>
<header class="lighthouse-header">
<div class="ui attached menu">
<div class="ui container">
@foreach (PageNavigationItem navigationItem in Model!.NavigationItems)

View file

@ -3,11 +3,3 @@ footer.lighthouse-footer {
bottom: 0;
position: fixed;
}
div.authentication-attempt {
background-color: lightgray;
display: flex;
flex-direction: column;
vertical-align: center;
padding: 3px;
}