Add application services for profiles and neutrino
This commit is contained in:
7
RecNet.Application/Common/Interfaces/IConfigService.cs
Normal file
7
RecNet.Application/Common/Interfaces/IConfigService.cs
Normal 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);
|
||||
}
|
||||
10
RecNet.Application/Common/Interfaces/ITokenService.cs
Normal file
10
RecNet.Application/Common/Interfaces/ITokenService.cs
Normal 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);
|
||||
}
|
||||
5
RecNet.Application/Common/Tokens/TokenResult.cs
Normal file
5
RecNet.Application/Common/Tokens/TokenResult.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace RecNet.Application.Common.Tokens;
|
||||
|
||||
public sealed record TokenResult(
|
||||
string AccessToken,
|
||||
int ExpiresIn);
|
||||
12
RecNet.Application/Common/Tokens/TokenVerifyResult.cs
Normal file
12
RecNet.Application/Common/Tokens/TokenVerifyResult.cs
Normal 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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
5
RecNet.Application/Neutrino/AuthorizeNeutrinoCommand.cs
Normal file
5
RecNet.Application/Neutrino/AuthorizeNeutrinoCommand.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace RecNet.Application.Neutrino;
|
||||
|
||||
public sealed record AuthorizeNeutrinoCommand(
|
||||
Guid ProfileId,
|
||||
string AccessToken);
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace RecNet.Application.Neutrino;
|
||||
|
||||
public interface INeutrinoAuthorizationService
|
||||
{
|
||||
Task<NeutrinoAuthorizationResult> AuthorizeAsync(
|
||||
AuthorizeNeutrinoCommand command,
|
||||
CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace RecNet.Application.Neutrino;
|
||||
|
||||
public enum NeutrinoAuthorizationFailure
|
||||
{
|
||||
InvalidParameters,
|
||||
AuthenticationFailed
|
||||
}
|
||||
17
RecNet.Application/Neutrino/NeutrinoAuthorizationResult.cs
Normal file
17
RecNet.Application/Neutrino/NeutrinoAuthorizationResult.cs
Normal 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);
|
||||
}
|
||||
30
RecNet.Application/Neutrino/NeutrinoAuthorizationService.cs
Normal file
30
RecNet.Application/Neutrino/NeutrinoAuthorizationService.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
8
RecNet.Application/Profiles/IProfileService.cs
Normal file
8
RecNet.Application/Profiles/IProfileService.cs
Normal 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);
|
||||
}
|
||||
9
RecNet.Application/Profiles/LoginProfileCommand.cs
Normal file
9
RecNet.Application/Profiles/LoginProfileCommand.cs
Normal 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);
|
||||
33
RecNet.Application/Profiles/LoginProfileResult.cs
Normal file
33
RecNet.Application/Profiles/LoginProfileResult.cs
Normal 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
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
|
||||
69
RecNet.Application/Profiles/ProfileService.cs
Normal file
69
RecNet.Application/Profiles/ProfileService.cs
Normal 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));
|
||||
}
|
||||
Reference in New Issue
Block a user