Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint to remove a verified IP #678

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Refresh.GameServer/Database/GameDatabaseContext.Tokens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GameUserVerifiedIpRelation> GetVerifiedIps(GameUser user, int skip, int count)
=> new(this.GameUserVerifiedIpRelations.Where(r => r.User == user), skip, count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{}

Expand Down
22 changes: 22 additions & 0 deletions Refresh.GameServer/Endpoints/ApiV3/AuthenticationApiEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,28 @@ public ApiListResponse<ApiGameUserVerifiedIpResponse> GetVerifiedIps(RequestCont
.FromOldList<ApiGameUserVerifiedIpResponse, GameUserVerifiedIpRelation>(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)]
Expand Down
Loading