Add Steam auth, platform validators & name gen
This commit is contained in:
17
RecNet.Application/Profiles/NameGenerator.cs
Normal file
17
RecNet.Application/Profiles/NameGenerator.cs
Normal 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}";
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
Reference in New Issue
Block a user