Add application services for profiles and neutrino

This commit is contained in:
Holden
2026-06-19 12:57:31 -05:00
parent 3eebfc9ba2
commit 0b81cb046a
25 changed files with 274 additions and 125 deletions

View File

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

View File

@@ -0,0 +1,10 @@
using RecNet.Application.Common.Tokens;
using RecNet.Domain.Entities.Profiles;
namespace RecNet.Application.Common.Interfaces;
public interface ITokenService
{
TokenResult GenerateProfileToken(Profile profile);
TokenVerifyResult VerifyToken(string token);
}

View File

@@ -0,0 +1,5 @@
namespace RecNet.Application.Common.Tokens;
public sealed record TokenResult(
string AccessToken,
int ExpiresIn);

View File

@@ -0,0 +1,12 @@
namespace RecNet.Application.Common.Tokens;
public sealed record TokenVerifyResult(
bool Succeeded,
Guid? ProfileId)
{
public static TokenVerifyResult Success(Guid profileId)
=> new(true, profileId);
public static TokenVerifyResult Failure()
=> new(false, null);
}

View File

@@ -1,4 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using RecNet.Application.Neutrino;
using RecNet.Application.Profiles;
namespace RecNet.Application;
@@ -7,9 +9,8 @@ public static class DependencyInjection
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services.AddAutoMapper(_ => { }, typeof(DependencyInjection).Assembly);
// Usually there would be CQRS, but I'm rushing out this server, so I didn't feel like adding it.
// Enjoy a VERY barebones application layer :D
services.AddScoped<IProfileService, ProfileService>();
services.AddScoped<INeutrinoAuthorizationService, NeutrinoAuthorizationService>();
return services;
}

View File

@@ -0,0 +1,5 @@
namespace RecNet.Application.Neutrino;
public sealed record AuthorizeNeutrinoCommand(
Guid ProfileId,
string AccessToken);

View File

@@ -0,0 +1,8 @@
namespace RecNet.Application.Neutrino;
public interface INeutrinoAuthorizationService
{
Task<NeutrinoAuthorizationResult> AuthorizeAsync(
AuthorizeNeutrinoCommand command,
CancellationToken ct = default);
}

View File

@@ -0,0 +1,7 @@
namespace RecNet.Application.Neutrino;
public enum NeutrinoAuthorizationFailure
{
InvalidParameters,
AuthenticationFailed
}

View File

@@ -0,0 +1,17 @@
namespace RecNet.Application.Neutrino;
public sealed record NeutrinoAuthorizationResult(
bool Succeeded,
NeutrinoAuthorizationFailure? Failure,
string? UserId,
string? Nickname)
{
public static NeutrinoAuthorizationResult Success(Guid userId, string nickname)
=> new(true, null, userId.ToString(), nickname);
public static NeutrinoAuthorizationResult InvalidParameters()
=> new(false, NeutrinoAuthorizationFailure.InvalidParameters, null, null);
public static NeutrinoAuthorizationResult AuthenticationFailed()
=> new(false, NeutrinoAuthorizationFailure.AuthenticationFailed, null, null);
}

View File

@@ -0,0 +1,30 @@
using RecNet.Application.Common.Interfaces;
using RecNet.Domain.Repositories;
namespace RecNet.Application.Neutrino;
public class NeutrinoAuthorizationService(
IProfileRepository profileRepository,
ITokenService tokenService) : INeutrinoAuthorizationService
{
public async Task<NeutrinoAuthorizationResult> AuthorizeAsync(
AuthorizeNeutrinoCommand command,
CancellationToken ct = default)
{
if (command.ProfileId == Guid.Empty || string.IsNullOrWhiteSpace(command.AccessToken))
return NeutrinoAuthorizationResult.InvalidParameters();
var tokenVerifyResult = tokenService.VerifyToken(command.AccessToken);
if (!tokenVerifyResult.Succeeded || tokenVerifyResult.ProfileId is null)
return NeutrinoAuthorizationResult.AuthenticationFailed();
if (tokenVerifyResult.ProfileId.Value != command.ProfileId)
return NeutrinoAuthorizationResult.AuthenticationFailed();
var profile = await profileRepository.GetByIdAsync(command.ProfileId, ct);
if (profile is null)
return NeutrinoAuthorizationResult.AuthenticationFailed();
return NeutrinoAuthorizationResult.Success(profile.ProfileId, profile.Name);
}
}

View File

@@ -0,0 +1,8 @@
namespace RecNet.Application.Profiles;
public interface IProfileService
{
Task<ProfileDTO?> GetProfileAsync(Guid profileId, CancellationToken ct = default);
Task<IReadOnlyList<ProfileDTO>> GetProfilesAsync(List<Guid> profileIds, CancellationToken ct = default);
Task<LoginProfileResult> LoginAsync(LoginProfileCommand command, CancellationToken ct = default);
}

View File

@@ -0,0 +1,9 @@
using RecNet.Domain.Common;
namespace RecNet.Application.Profiles;
public sealed record LoginProfileCommand(
string DeviceId,
PlatformType PlatformType,
string PlatformId,
string PlatformAuthentication);

View File

@@ -0,0 +1,33 @@
namespace RecNet.Application.Profiles;
public class LoginProfileResult
{
public bool Succeeded { get; set; }
public string? Message { get; set; }
public ProfileDTO? Profile { get; set; }
public string? AccessToken { get; set; }
public int ExpiresIn { get; set; }
public static LoginProfileResult Success(ProfileDTO profile, string accessToken, int expiresIn)
{
return new LoginProfileResult
{
Succeeded = true,
Profile = profile,
AccessToken = accessToken,
ExpiresIn = expiresIn
};
}
public static LoginProfileResult Fail(string? message = null)
{
return new LoginProfileResult
{
Succeeded = false,
Message = message
};
}
}

View File

@@ -1,12 +1,8 @@
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

@@ -0,0 +1,69 @@
using AutoMapper;
using RecNet.Application.Common.Interfaces;
using RecNet.Domain.Repositories;
using ProfileEntity = RecNet.Domain.Entities.Profiles.Profile;
namespace RecNet.Application.Profiles;
public class ProfileService(
IProfileRepository profileRepository,
ITokenService tokenService,
IMapper mapper) : IProfileService
{
public async Task<ProfileDTO?> GetProfileAsync(Guid profileId, CancellationToken ct = default)
{
var profile = await profileRepository.GetByIdAsync(profileId, ct);
return profile is null
? null
: mapper.Map<ProfileDTO>(profile);
}
public async Task<IReadOnlyList<ProfileDTO>> GetProfilesAsync(
List<Guid> profileIds,
CancellationToken ct = default)
{
var profiles = await profileRepository.GetByIdsAsync(profileIds, ct);
return mapper.Map<List<ProfileDTO>>(profiles);
}
public async Task<LoginProfileResult> LoginAsync(
LoginProfileCommand command,
CancellationToken ct = default)
{
var profile = await profileRepository.GetByPlatform(
command.PlatformType,
command.PlatformId,
ct);
if (profile is null)
{
profile = ProfileEntity.Create(
GenerateRandomName(),
command.PlatformType,
command.PlatformId);
await profileRepository.AddAsync(profile, ct);
}
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(
profile: mapper.Map<ProfileDTO>(profile),
accessToken: token.AccessToken,
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));
}