Add Steam auth, platform validators & name gen

This commit is contained in:
Holden
2026-06-19 14:48:19 -05:00
parent 0b81cb046a
commit 63730dbba0
24 changed files with 592 additions and 17 deletions

View File

@@ -2,6 +2,7 @@ namespace RecNet.Application.Common.Interfaces;
public interface IConfigService
{
Task<T?> GetAsync<T>(string key, T? defaultValue = default, CancellationToken ct = default);
Task<T?> GetAsync<T>(string key, CancellationToken ct = default);
Task<T> GetAsync<T>(string key, T defaultValue, CancellationToken ct = default);
Task SetAsync<T>(string key, T value, CancellationToken ct = default);
}

View File

@@ -0,0 +1,14 @@
using RecNet.Application.Common.PlatformAuth;
using RecNet.Domain.Common;
namespace RecNet.Application.Common.Interfaces;
public interface IPlatformAuthValidator
{
PlatformType PlatformType { get; }
Task<PlatformAuthResult> ValidateAsync(
string platformAuthentication,
string platformId,
CancellationToken ct = default);
}

View File

@@ -0,0 +1,11 @@
using RecNet.Application.Common.Steam;
namespace RecNet.Application.Common.Interfaces;
public interface ISteamAuthService
{
Task<SteamAuthResult> AuthorizeAsync(
string ticket,
uint appId,
CancellationToken ct = default);
}

View File

@@ -0,0 +1,21 @@
namespace RecNet.Application.Common.PlatformAuth;
public class PlatformAuthResult
{
public bool Succeeded { get; private set; }
public string? Message { get; private set; }
public string? Name { get; private set; }
private PlatformAuthResult(bool succeeded, string? message, string? name)
{
Succeeded = succeeded;
Message = message;
Name = name;
}
public static PlatformAuthResult Success(string? name)
=> new(true, null, name);
public static PlatformAuthResult Failure(string? message = null)
=> new(false, message, null);
}

View File

@@ -0,0 +1,21 @@
namespace RecNet.Application.Common.Steam;
public class SteamAuthResult
{
public bool Succeeded { get; private set; }
public string? SteamId { get; private set; }
public string? DisplayName { get; private set; }
private SteamAuthResult(bool succeeded, string? steamId, string? displayName)
{
Succeeded = succeeded;
SteamId = steamId;
DisplayName = displayName;
}
public static SteamAuthResult Success(string steamId, string? displayName)
=> new(true, steamId, displayName);
public static SteamAuthResult Failure()
=> new (false, null, null);
}

View File

@@ -9,6 +9,9 @@ public static class DependencyInjection
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services.AddAutoMapper(_ => { }, typeof(DependencyInjection).Assembly);
services.AddScoped<NameGenerator>();
services.AddScoped<IProfileService, ProfileService>();
services.AddScoped<INeutrinoAuthorizationService, NeutrinoAuthorizationService>();

View File

@@ -22,9 +22,9 @@ public class NeutrinoAuthorizationService(
return NeutrinoAuthorizationResult.AuthenticationFailed();
var profile = await profileRepository.GetByIdAsync(command.ProfileId, ct);
if (profile is null)
if (profile is null || profile.IsBanned)
return NeutrinoAuthorizationResult.AuthenticationFailed();
return NeutrinoAuthorizationResult.Success(profile.ProfileId, profile.Name);
}
}

View File

@@ -0,0 +1,17 @@
using RecNet.Application.Common.Interfaces;
using RecNet.Domain.Entities.Profiles.Names;
namespace RecNet.Application.Profiles;
public class NameGenerator(IConfigService configService)
{
public async Task<string> GenerateAsync(CancellationToken ct = default)
{
var config = await configService.GetAsync("Profiles:NameGen", DefaultNameGenConfig.Value, ct);
var adjective = config.Adjectives[Random.Shared.Next(config.Adjectives.Count)];
var noun = config.Nouns[Random.Shared.Next(config.Nouns.Count)];
return $"{adjective}{noun}";
}
}

View File

@@ -1,8 +1,12 @@
using System.Text.Json.Serialization;
namespace RecNet.Application.Profiles;
public class ProfileDTO
{
[JsonPropertyName("ProfileId")]
public Guid ProfileId { get; init; }
[JsonPropertyName("Name")]
public required string Name { get; init; }
}

View File

@@ -6,8 +6,11 @@ using ProfileEntity = RecNet.Domain.Entities.Profiles.Profile;
namespace RecNet.Application.Profiles;
public class ProfileService(
NameGenerator nameGenerator,
IProfileRepository profileRepository,
ITokenService tokenService,
IConfigService configService,
IEnumerable<IPlatformAuthValidator> validators,
IMapper mapper) : IProfileService
{
public async Task<ProfileDTO?> GetProfileAsync(Guid profileId, CancellationToken ct = default)
@@ -32,6 +35,19 @@ public class ProfileService(
LoginProfileCommand command,
CancellationToken ct = default)
{
var validator = validators.FirstOrDefault(x => x.PlatformType == command.PlatformType);
if (validator is null)
return LoginProfileResult.Fail("Invalid platform type");
var ignoreAuthValidation = await configService.GetAsync("Profiles:IgnoreAuthValidation", true, ct);
var auth = await validator.ValidateAsync(
command.PlatformAuthentication,
command.PlatformId,
ct);
if (!auth.Succeeded && !ignoreAuthValidation)
return LoginProfileResult.Fail($"Platform Auth Failed: {auth.Message}");
var profile = await profileRepository.GetByPlatform(
command.PlatformType,
command.PlatformId,
@@ -39,21 +55,26 @@ public class ProfileService(
if (profile is null)
{
var name = await nameGenerator.GenerateAsync(ct);
profile = ProfileEntity.Create(
GenerateRandomName(),
name,
command.PlatformType,
command.PlatformId);
await profileRepository.AddAsync(profile, ct);
}
if (profile.IsBanned)
return LoginProfileResult.Fail("Profile is banned");
if (auth.Name is not null && profile.Name != auth.Name)
profile.SetName(auth.Name);
profile.AddDeviceId(command.DeviceId);
await profileRepository.SaveChangesAsync(ct);
if (profile.IsBanned)
return LoginProfileResult.Fail("Profile is banned");
var token = tokenService.GenerateProfileToken(profile);
return LoginProfileResult.Success(
@@ -62,8 +83,4 @@ public class ProfileService(
expiresIn: token.ExpiresIn
);
}
// Temporary until platform/user-name verification becomes a real use case.
private static string GenerateRandomName()
=> string.Concat("rr", Guid.NewGuid().ToString("N").AsSpan(0, 18));
}

View File

@@ -14,4 +14,8 @@
<PackageReference Include="AutoMapper" Version="16.1.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Authentication\" />
</ItemGroup>
</Project>