Validate login request and refactor login handling

This commit is contained in:
Holden
2026-06-19 14:58:02 -05:00
parent 63730dbba0
commit 076fed09f2
10 changed files with 32 additions and 7 deletions

View File

@@ -1,12 +1,22 @@
using RecNet.Domain.Common; using System.ComponentModel.DataAnnotations;
using RecNet.Domain.Common;
namespace API.Contracts.Profiles.Requests; namespace API.Contracts.Profiles.Requests;
public class LoginRequest public class LoginRequest
{ {
[Required]
public required string AppVersion { get; set; } public required string AppVersion { get; set; }
[Required, StringLength(128, MinimumLength = 1)]
public required string DeviceId { get; set; } public required string DeviceId { get; set; }
[Required]
public required string PlatformAuthentication { get; set; } public required string PlatformAuthentication { get; set; }
[Required, StringLength(50, MinimumLength = 1)]
public required string PlatformId { get; set; } public required string PlatformId { get; set; }
[EnumDataType(typeof(PlatformType))]
public PlatformType PlatformType { get; set; } public PlatformType PlatformType { get; set; }
} }

View File

@@ -68,10 +68,7 @@ public class ProfileService(
if (profile.IsBanned) if (profile.IsBanned)
return LoginProfileResult.Fail("Profile is banned"); return LoginProfileResult.Fail("Profile is banned");
if (auth.Name is not null && profile.Name != auth.Name) profile.RecordSuccessfulLogin(command.DeviceId, auth.Name);
profile.SetName(auth.Name);
profile.AddDeviceId(command.DeviceId);
await profileRepository.SaveChangesAsync(ct); await profileRepository.SaveChangesAsync(ct);

View File

@@ -61,6 +61,14 @@ public class Profile
DeviceIds.Add(deviceId); DeviceIds.Add(deviceId);
} }
public void RecordSuccessfulLogin(string deviceId, string? platformName)
{
if (!string.IsNullOrWhiteSpace(platformName) && Name != platformName)
SetName(platformName);
AddDeviceId(deviceId);
}
public void Ban() public void Ban()
=> IsBanned = true; => IsBanned = true;

View File

@@ -16,8 +16,18 @@ public class SteamAuthValidator(ISteamAuthService steamAuthService) : IPlatformA
string platformId, string platformId,
CancellationToken ct = default) CancellationToken ct = default)
{ {
var steamParams = JsonSerializer.Deserialize<SteamPlatformAuth>(platformAuthentication); SteamPlatformAuth? steamParams;
if (steamParams == null)
try
{
steamParams = JsonSerializer.Deserialize<SteamPlatformAuth>(platformAuthentication);
}
catch (JsonException)
{
return PlatformAuthResult.Failure("Invalid authentication");
}
if (steamParams?.AppId is null || string.IsNullOrWhiteSpace(steamParams.Ticket))
return PlatformAuthResult.Failure("Invalid authentication"); return PlatformAuthResult.Failure("Invalid authentication");
var result = await steamAuthService.AuthorizeAsync(steamParams.Ticket, steamParams.AppId.AppId, ct); var result = await steamAuthService.AuthorizeAsync(steamParams.Ticket, steamParams.AppId.AppId, ct);