Add external auth toggle

This commit is contained in:
jvyden 2021-11-22 18:04:09 -05:00
commit 59c2e3e098
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
4 changed files with 34 additions and 18 deletions

View file

@ -54,28 +54,36 @@ namespace LBPUnion.ProjectLighthouse.Controllers
User? user = await this.database.UserFromGameToken(token, true);
if (user == null) return this.StatusCode(403, "");
string ipAddressAndName = $"{token.UserLocation}|{user.Username}";
if (DeniedAuthenticationHelper.RecentlyDenied(ipAddressAndName) || (DeniedAuthenticationHelper.GetAttempts(ipAddressAndName) > 3))
if (ServerSettings.Instance.UseExternalAuth)
{
this.database.AuthenticationAttempts.RemoveRange
(this.database.AuthenticationAttempts.Include(a => a.GameToken).Where(a => a.GameToken.UserId == user.UserId));
string ipAddressAndName = $"{token.UserLocation}|{user.Username}";
if (DeniedAuthenticationHelper.RecentlyDenied(ipAddressAndName) || (DeniedAuthenticationHelper.GetAttempts(ipAddressAndName) > 3))
{
this.database.AuthenticationAttempts.RemoveRange
(this.database.AuthenticationAttempts.Include(a => a.GameToken).Where(a => a.GameToken.UserId == user.UserId));
DeniedAuthenticationHelper.AddAttempt(ipAddressAndName);
DeniedAuthenticationHelper.AddAttempt(ipAddressAndName);
await this.database.SaveChangesAsync();
return this.StatusCode(403, "");
await this.database.SaveChangesAsync();
return this.StatusCode(403, "");
}
AuthenticationAttempt authAttempt = new()
{
GameToken = token,
GameTokenId = token.TokenId,
Timestamp = TimestampHelper.Timestamp,
IPAddress = userLocation,
Platform = token.GameVersion == GameVersion.LittleBigPlanetVita ? Platform.Vita : Platform.PS3, // TODO: properly identify RPCS3
};
this.database.AuthenticationAttempts.Add(authAttempt);
}
else
{
token.Approved = true;
}
AuthenticationAttempt authAttempt = new()
{
GameToken = token,
GameTokenId = token.TokenId,
Timestamp = TimestampHelper.Timestamp,
IPAddress = userLocation,
Platform = token.GameVersion == GameVersion.LittleBigPlanetVita ? Platform.Vita : Platform.PS3, // TODO: properly identify RPCS3
};
this.database.AuthenticationAttempts.Add(authAttempt);
await this.database.SaveChangesAsync();
Logger.Log($"Successfully logged in user {user.Username} as {token.GameVersion} client ({titleId})", LoggerLevelLogin.Instance);

View file

@ -3,6 +3,7 @@ using System.Linq;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Settings;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -17,6 +18,8 @@ namespace LBPUnion.ProjectLighthouse.Pages.ExternalAuth
public async Task<IActionResult> OnGet()
{
if (!ServerSettings.Instance.UseExternalAuth) return this.NotFound();
this.AuthenticationAttempts = this.Database.AuthenticationAttempts.Include
(a => a.GameToken)
.Where(a => a.GameToken.UserId == this.User.UserId)

View file

@ -1,5 +1,6 @@
@using LBPUnion.ProjectLighthouse.Helpers
@using LBPUnion.ProjectLighthouse.Types
@using LBPUnion.ProjectLighthouse.Types.Settings
@model LBPUnion.ProjectLighthouse.Pages.Layouts.BaseLayout
@{
@ -10,7 +11,10 @@
}
else
{
Model.NavigationItems.Add(new PageNavigationItem("Authentication", "/authentication", "key"));
if (ServerSettings.Instance.UseExternalAuth)
{
Model.NavigationItems.Add(new PageNavigationItem("Authentication", "/authentication", "key"));
}
Model.NavigationItems.Add(new PageNavigationItem("Log out", "/logout", "user alternate slash")); // should always be last
}
}

View file

@ -90,5 +90,6 @@ namespace LBPUnion.ProjectLighthouse.Types.Settings
public string ExternalUrl { get; set; } = "http://localhost:10060";
public string ServerDigestKey { get; set; }
public bool UseExternalAuth { get; set; }
}
}