Remove broken and useless BlockDeniedUsers functionality

This commit is contained in:
jvyden 2022-05-15 00:13:40 -04:00
parent 3e031a342f
commit 71a97894ad
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
5 changed files with 6 additions and 60 deletions

View file

@ -79,22 +79,6 @@ public class LoginController : ControllerBase
if (ServerConfiguration.Instance.Authentication.UseExternalAuth)
{
if (ServerConfiguration.Instance.Authentication.BlockDeniedUsers)
{
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);
await this.database.SaveChangesAsync();
Logger.LogWarn($"Too many recent denied logins from user {user.Username}, rejecting login", LogArea.Login);
return this.StatusCode(403, "");
}
}
if (this.database.UserApprovedIpAddresses.Where(a => a.UserId == user.UserId).Select(a => a.IpAddress).Contains(ipAddress))
{
token.Approved = true;

View file

@ -1,5 +1,4 @@
#nullable enable
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -54,8 +53,6 @@ public class AuthenticationController : ControllerBase
this.database.GameTokens.Remove(authAttempt.GameToken);
this.database.AuthenticationAttempts.Remove(authAttempt);
DeniedAuthenticationHelper.SetDeniedAt($"{authAttempt.IPAddress}|{user.Username}");
await this.database.SaveChangesAsync();
return this.Redirect("~/authentication");
@ -76,8 +73,6 @@ public class AuthenticationController : ControllerBase
{
this.database.GameTokens.Remove(authAttempt.GameToken);
this.database.AuthenticationAttempts.Remove(authAttempt);
DeniedAuthenticationHelper.SetDeniedAt($"{authAttempt.IPAddress}|{user.Username}");
}
await this.database.SaveChangesAsync();

View file

@ -1,37 +0,0 @@
using System.Collections.Generic;
namespace LBPUnion.ProjectLighthouse.Helpers;
public static class DeniedAuthenticationHelper
{
public static readonly Dictionary<string, long> IPAddressAndNameDeniedAt = new();
public static readonly Dictionary<string, int> AttemptsByIPAddressAndName = new();
public static void SetDeniedAt(string ipAddressAndName, long timestamp = 0)
{
if (timestamp == 0) timestamp = TimestampHelper.Timestamp;
if (IPAddressAndNameDeniedAt.TryGetValue(ipAddressAndName, out long _)) IPAddressAndNameDeniedAt.Remove(ipAddressAndName);
IPAddressAndNameDeniedAt.Add(ipAddressAndName, timestamp);
}
public static bool RecentlyDenied(string ipAddressAndName)
{
if (!IPAddressAndNameDeniedAt.TryGetValue(ipAddressAndName, out long timestamp)) return false;
return TimestampHelper.Timestamp < timestamp + 300;
}
public static void AddAttempt(string ipAddressAndName)
{
if (AttemptsByIPAddressAndName.TryGetValue(ipAddressAndName, out int attempts)) AttemptsByIPAddressAndName.Remove(ipAddressAndName);
AttemptsByIPAddressAndName.Add(ipAddressAndName, attempts + 1);
}
public static int GetAttempts(string ipAddressAndName)
{
if (!AttemptsByIPAddressAndName.TryGetValue(ipAddressAndName, out int attempts)) return 0;
return attempts;
}
}

View file

@ -1,8 +1,12 @@
using System;
namespace LBPUnion.ProjectLighthouse.Types.Settings.ConfigurationCategories;
public class AuthenticationConfiguration
{
public bool BlockDeniedUsers { get; set; } = true;
[Obsolete("Obsolete. This feature has been removed.", true)]
public bool BlockDeniedUsers { get; set; }
public bool RegistrationEnabled { get; set; } = true;
public bool UseExternalAuth { get; set; }
}

View file

@ -22,7 +22,7 @@ public class ServerConfiguration
// You can use an ObsoleteAttribute instead. Make sure you set it to error, though.
//
// Thanks for listening~
public const int CurrentConfigVersion = 2;
public const int CurrentConfigVersion = 3;
#region Meta