Skip to content
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
17 changes: 14 additions & 3 deletions Refresh.Database/GameDatabaseContext.Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,22 @@ public void RemoveRegistrationFromQueue(QueuedRegistration registration)

public bool IsRegistrationExpired(QueuedRegistration registration) => registration.ExpiryDate < this._time.Now;

public QueuedRegistration? GetQueuedRegistrationByUsername(string username)
public QueuedRegistration? GetQueuedRegistrationByUsername(string username, bool setToCorrectUsernameCasing = true)
{
#pragma warning disable CA1862
=> this.QueuedRegistrations.FirstOrDefault(q => q.UsernameLower == username.ToLower());
QueuedRegistration? registration = this.QueuedRegistrations.FirstOrDefault(q => q.UsernameLower == username.ToLower());
#pragma warning restore CA1862


// Correct the username's casing so we don't end up using this registration to create an account with a potentially
// wrongly cased username, leading to it not exactly matching to the ticket's username and preventing future logins.
if (registration != null && setToCorrectUsernameCasing)
{
registration.Username = username;
}

return registration;
}

public QueuedRegistration? GetQueuedRegistrationByObjectId(ObjectId id)
=> this.QueuedRegistrations.FirstOrDefault(q => q.RegistrationId == id);

Expand Down
33 changes: 33 additions & 0 deletions RefreshTests.GameServer/Tests/Users/RegistrationQueueTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Refresh.Database.Models.Authentication;
using Refresh.Database.Models.Users;

namespace RefreshTests.GameServer.Tests.Users;

public class RegistrationQueueTests : GameServerTest
{
[Test]
public void CreateAccountFromQueueDespiteWrongUsernameCasing()
{
using TestContext context = this.GetServer();
const string wrongUsername = "spellingHobby21";
const string correctUsername = "SpelliNgHobby21";

// Add new registration to queue with misspelled name
context.Database.AddRegistrationToQueue(wrongUsername, "[email protected]", "veri sekure");

// Now get it using the actually correct username, and check its attributes
QueuedRegistration? registration = context.Database.GetQueuedRegistrationByUsername(correctUsername);
Assert.That(registration, Is.Not.Null);
Assert.That(registration!.Username, Is.EqualTo(correctUsername));
Assert.That(registration!.UsernameLower, Is.EqualTo(wrongUsername.ToLower()));

// Now use it to create an account and check whether it's been properly created
GameUser user = context.Database.CreateUserFromQueuedRegistration(registration, TokenPlatform.PS3);
Assert.That(user.Username, Is.EqualTo(correctUsername));
Assert.That(user.UsernameLower, Is.EqualTo(wrongUsername.ToLower()));
Assert.That(context.Database.GetUserByUsername(correctUsername, true)?.Username, Is.EqualTo(correctUsername));
Assert.That(context.Database.GetUserByUsername(wrongUsername, false)?.Username, Is.EqualTo(correctUsername));
Assert.That(context.Database.GetUserByUsername(wrongUsername, false)?.UserId, Is.EqualTo(context.Database.GetUserByUsername(correctUsername, false)?.UserId));
Assert.That(context.Database.GetUserByUsername(wrongUsername, true)?.Username, Is.Null);
}
}