From 6a8a899bf968400cbee60136f807dd5bdc834633 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Sat, 19 Oct 2024 21:44:49 -0700 Subject: [PATCH] Add endpoint to remove a verified IP --- .../Database/GameDatabaseContext.Tokens.cs | 16 ++++++++++++++ .../ApiV3/ApiTypes/Errors/ApiNotFoundError.cs | 3 +++ .../ApiV3/AuthenticationApiEndpoints.cs | 22 +++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/Refresh.GameServer/Database/GameDatabaseContext.Tokens.cs b/Refresh.GameServer/Database/GameDatabaseContext.Tokens.cs index 16208eb8..542c77e2 100644 --- a/Refresh.GameServer/Database/GameDatabaseContext.Tokens.cs +++ b/Refresh.GameServer/Database/GameDatabaseContext.Tokens.cs @@ -182,6 +182,22 @@ public void AddVerifiedIp(GameUser user, string ipAddress, IDateTimeProvider tim }); } + public bool RemoveVerifiedIp(GameUser user, string ipAddress) + { + GameUserVerifiedIpRelation? verifiedIp = + this.GameUserVerifiedIpRelations.FirstOrDefault(r => r.User == user && r.IpAddress == ipAddress); + + if (verifiedIp == null) + return false; + + this.Write(() => + { + this.GameUserVerifiedIpRelations.Remove(verifiedIp); + }); + + return true; + } + public DatabaseList GetVerifiedIps(GameUser user, int skip, int count) => new(this.GameUserVerifiedIpRelations.Where(r => r.User == user), skip, count); diff --git a/Refresh.GameServer/Endpoints/ApiV3/ApiTypes/Errors/ApiNotFoundError.cs b/Refresh.GameServer/Endpoints/ApiV3/ApiTypes/Errors/ApiNotFoundError.cs index 85cba30a..56d6c57a 100644 --- a/Refresh.GameServer/Endpoints/ApiV3/ApiTypes/Errors/ApiNotFoundError.cs +++ b/Refresh.GameServer/Endpoints/ApiV3/ApiTypes/Errors/ApiNotFoundError.cs @@ -22,6 +22,9 @@ public class ApiNotFoundError : ApiError public const string ContestMissingErrorWhen = "The contest could not be found"; public static readonly ApiNotFoundError ContestMissingError = new(ContestMissingErrorWhen); + public const string VerifiedIpMissingErrorWhen = "The verified IP could not be found"; + public static readonly ApiNotFoundError VerifiedIpMissingError = new(VerifiedIpMissingErrorWhen); + private ApiNotFoundError() : base("The requested resource was not found", NotFound) {} diff --git a/Refresh.GameServer/Endpoints/ApiV3/AuthenticationApiEndpoints.cs b/Refresh.GameServer/Endpoints/ApiV3/AuthenticationApiEndpoints.cs index 5a4bf3f3..8f4a672c 100644 --- a/Refresh.GameServer/Endpoints/ApiV3/AuthenticationApiEndpoints.cs +++ b/Refresh.GameServer/Endpoints/ApiV3/AuthenticationApiEndpoints.cs @@ -208,6 +208,28 @@ public ApiListResponse GetVerifiedIps(RequestCont .FromOldList(verifiedIps, dataContext); } + [ApiV3Endpoint("removeVerifiedIp", HttpMethods.Delete), MinimumRole(GameUserRole.Restricted)] + [DocSummary("Removes the specified IP from the list of approved IP addresses")] + [DocError(typeof(ApiValidationError), ApiValidationError.IpAddressParseErrorWhen)] + [DocError(typeof(ApiNotFoundError), ApiNotFoundError.VerifiedIpMissingErrorWhen)] + [DocRequestBody("127.0.0.1")] + public ApiOkResponse RemoveVerifiedIp( + RequestContext context, + GameDatabaseContext database, + GameUser user, + string body) + { + string ipAddress = body.Trim(); + + if (!IPAddress.TryParse(ipAddress, out _)) + return ApiValidationError.IpAddressParseError; + + if (!database.RemoveVerifiedIp(user, ipAddress)) + return ApiNotFoundError.VerifiedIpMissingError; + + return new ApiOkResponse(); + } + [ApiV3Endpoint("verificationRequests/approve", HttpMethods.Put), MinimumRole(GameUserRole.Restricted)] [DocSummary("Approves a given IP, and clears all remaining verification requests. Send the IP in the body.")] [DocError(typeof(ApiValidationError), ApiValidationError.IpAddressParseErrorWhen)]