Move projects to src; add Neutrino secret
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
namespace RecNet.Application.Common.Interfaces;
|
||||
|
||||
public interface IConfigService
|
||||
{
|
||||
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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
10
src/RecNet.Application/Common/Interfaces/ITokenService.cs
Normal file
10
src/RecNet.Application/Common/Interfaces/ITokenService.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using RecNet.Application.Common.Tokens;
|
||||
using RecNet.Domain.Profiles;
|
||||
|
||||
namespace RecNet.Application.Common.Interfaces;
|
||||
|
||||
public interface ITokenService
|
||||
{
|
||||
TokenResult GenerateProfileToken(Profile profile);
|
||||
TokenVerifyResult VerifyToken(string token);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using AutoMapper;
|
||||
using RecNet.Application.Profiles;
|
||||
|
||||
namespace RecNet.Application.Common.Mapping;
|
||||
|
||||
public class ApplicationMappingProfile : Profile
|
||||
{
|
||||
public ApplicationMappingProfile()
|
||||
{
|
||||
CreateMap<Domain.Profiles.Profile, ProfileDTO>();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace RecNet.Application.Common.Security;
|
||||
|
||||
public static class ClaimsPrincipalExtensions
|
||||
{
|
||||
public static Guid GetProfileId(this ClaimsPrincipal user)
|
||||
{
|
||||
var value =
|
||||
user.FindFirst("sub")?.Value ??
|
||||
user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
|
||||
if (!Guid.TryParse(value, out var profileId))
|
||||
throw new UnauthorizedAccessException(
|
||||
"Account id claim is missing or invalid.");
|
||||
|
||||
return profileId;
|
||||
}
|
||||
}
|
||||
21
src/RecNet.Application/Common/Steam/SteamAuthResult.cs
Normal file
21
src/RecNet.Application/Common/Steam/SteamAuthResult.cs
Normal 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);
|
||||
}
|
||||
5
src/RecNet.Application/Common/Tokens/TokenResult.cs
Normal file
5
src/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
src/RecNet.Application/Common/Tokens/TokenVerifyResult.cs
Normal file
12
src/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);
|
||||
}
|
||||
20
src/RecNet.Application/DependencyInjection.cs
Normal file
20
src/RecNet.Application/DependencyInjection.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using RecNet.Application.Neutrino;
|
||||
using RecNet.Application.Profiles;
|
||||
|
||||
namespace RecNet.Application;
|
||||
|
||||
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>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using RecNet.Application.Common.Interfaces;
|
||||
using RecNet.Domain.Profiles;
|
||||
|
||||
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 || profile.IsBanned)
|
||||
return NeutrinoAuthorizationResult.AuthenticationFailed();
|
||||
|
||||
return NeutrinoAuthorizationResult.Success(profile.ProfileId, profile.Name);
|
||||
}
|
||||
}
|
||||
8
src/RecNet.Application/Profiles/IProfileService.cs
Normal file
8
src/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);
|
||||
}
|
||||
10
src/RecNet.Application/Profiles/LoginProfileCommand.cs
Normal file
10
src/RecNet.Application/Profiles/LoginProfileCommand.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using RecNet.Domain.Common;
|
||||
|
||||
namespace RecNet.Application.Profiles;
|
||||
|
||||
public sealed record LoginProfileCommand(
|
||||
string AppVersion,
|
||||
string DeviceId,
|
||||
PlatformType PlatformType,
|
||||
string PlatformId,
|
||||
string PlatformAuthentication);
|
||||
33
src/RecNet.Application/Profiles/LoginProfileResult.cs
Normal file
33
src/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
|
||||
};
|
||||
}
|
||||
}
|
||||
17
src/RecNet.Application/Profiles/NameGenerator.cs
Normal file
17
src/RecNet.Application/Profiles/NameGenerator.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using RecNet.Application.Common.Interfaces;
|
||||
using RecNet.Domain.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}";
|
||||
}
|
||||
}
|
||||
12
src/RecNet.Application/Profiles/ProfileDto.cs
Normal file
12
src/RecNet.Application/Profiles/ProfileDto.cs
Normal file
@@ -0,0 +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; }
|
||||
}
|
||||
95
src/RecNet.Application/Profiles/ProfileService.cs
Normal file
95
src/RecNet.Application/Profiles/ProfileService.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using AutoMapper;
|
||||
using RecNet.Application.Common.Interfaces;
|
||||
using RecNet.Domain.GameVersions;
|
||||
using RecNet.Domain.Profiles;
|
||||
using ProfileEntity = RecNet.Domain.Profiles.Profile;
|
||||
|
||||
namespace RecNet.Application.Profiles;
|
||||
|
||||
public class ProfileService(
|
||||
IEnumerable<IPlatformAuthValidator> validators,
|
||||
IGameVersionRepository gameVersionRepository,
|
||||
IProfileRepository profileRepository,
|
||||
IConfigService configService,
|
||||
NameGenerator nameGenerator,
|
||||
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)
|
||||
{
|
||||
// 1. Validate Game Version
|
||||
if (string.IsNullOrWhiteSpace(command.AppVersion))
|
||||
return LoginProfileResult.Fail("App version is required.");
|
||||
|
||||
var isValidGameVersion = await gameVersionRepository.IsValidAsync(command.AppVersion, ct);
|
||||
if (!isValidGameVersion)
|
||||
return LoginProfileResult.Fail("Invalid app version.");
|
||||
|
||||
// 2. Validate Platform Authentication
|
||||
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}");
|
||||
|
||||
// 3. Get or Create Profile
|
||||
var profile = await profileRepository.GetByPlatform(
|
||||
command.PlatformType,
|
||||
command.PlatformId,
|
||||
ct);
|
||||
|
||||
if (profile is null)
|
||||
{
|
||||
var name = await nameGenerator.GenerateAsync(ct);
|
||||
|
||||
profile = ProfileEntity.Create(
|
||||
name,
|
||||
command.PlatformType,
|
||||
command.PlatformId);
|
||||
|
||||
await profileRepository.AddAsync(profile, ct);
|
||||
}
|
||||
|
||||
if (profile.IsBanned)
|
||||
return LoginProfileResult.Fail("Profile is banned");
|
||||
|
||||
profile.RecordSuccessfulLogin(command.DeviceId, auth.Name);
|
||||
|
||||
await profileRepository.SaveChangesAsync(ct);
|
||||
|
||||
var token = tokenService.GenerateProfileToken(profile);
|
||||
|
||||
return LoginProfileResult.Success(
|
||||
profile: mapper.Map<ProfileDTO>(profile),
|
||||
accessToken: token.AccessToken,
|
||||
expiresIn: token.ExpiresIn
|
||||
);
|
||||
}
|
||||
}
|
||||
21
src/RecNet.Application/RecNet.Application.csproj
Normal file
21
src/RecNet.Application/RecNet.Application.csproj
Normal file
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\RecNet.Domain\RecNet.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="16.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Authentication\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user