diff --git a/ProjectLighthouse/Controllers/LoginController.cs b/ProjectLighthouse/Controllers/LoginController.cs index fa37e672..19efa5e9 100644 --- a/ProjectLighthouse/Controllers/LoginController.cs +++ b/ProjectLighthouse/Controllers/LoginController.cs @@ -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); diff --git a/ProjectLighthouse/Pages/ExternalAuth/AuthenticationPage.cshtml.cs b/ProjectLighthouse/Pages/ExternalAuth/AuthenticationPage.cshtml.cs index 1f99c93d..f64d3102 100644 --- a/ProjectLighthouse/Pages/ExternalAuth/AuthenticationPage.cshtml.cs +++ b/ProjectLighthouse/Pages/ExternalAuth/AuthenticationPage.cshtml.cs @@ -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 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) diff --git a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml index 04df071d..ca46ebd3 100644 --- a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml +++ b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml @@ -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 } } diff --git a/ProjectLighthouse/Types/Settings/ServerSettings.cs b/ProjectLighthouse/Types/Settings/ServerSettings.cs index 63161736..512910c8 100644 --- a/ProjectLighthouse/Types/Settings/ServerSettings.cs +++ b/ProjectLighthouse/Types/Settings/ServerSettings.cs @@ -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; } } } \ No newline at end of file