Add application services for profiles and neutrino
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
using RecNet.Application.Profiles;
|
||||
|
||||
namespace API.Contracts.Profiles.Responses;
|
||||
@@ -7,10 +7,10 @@ public class LoginResponse
|
||||
{
|
||||
[JsonPropertyName("Profile")]
|
||||
public required ProfileDTO Profile { get; set; }
|
||||
|
||||
|
||||
[JsonPropertyName("AccessToken")]
|
||||
public required string AccessToken { get; set; }
|
||||
|
||||
|
||||
[JsonPropertyName("ExpiresIn")]
|
||||
public required int ExpiresIn { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using RecNet.Domain.Services.Configuration;
|
||||
using RecNet.Application.Common.Interfaces;
|
||||
|
||||
namespace API.Controllers.Config.V1;
|
||||
|
||||
|
||||
@@ -1,28 +1,23 @@
|
||||
using System.Security.Claims;
|
||||
using System.Text.Json;
|
||||
using API.Contracts.Neutrino.Enums;
|
||||
using API.Contracts.Neutrino.Requests;
|
||||
using API.Contracts.Neutrino.Responses;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using RecNet.Domain.Repositories;
|
||||
using RecNet.Domain.Services.Tokens;
|
||||
using RecNet.Application.Neutrino;
|
||||
|
||||
namespace API.Controllers.Neutrino;
|
||||
|
||||
[Route("[controller]")]
|
||||
[ApiController]
|
||||
public class NeutrinoController(
|
||||
IProfileRepository profileRepository,
|
||||
ITokenService tokenService) : ControllerBase
|
||||
public class NeutrinoController(INeutrinoAuthorizationService authorizationService) : ControllerBase
|
||||
{
|
||||
// This route will be kind of abysmal so bare with it.
|
||||
// Photon sends the request with Content-Type: text/plain instead of application/json.
|
||||
[Route("authorize")]
|
||||
public async Task<ActionResult<NeutrinoAuthenticateResponse>> AuthorizeNeutrinoAsync()
|
||||
public async Task<ActionResult<NeutrinoAuthenticateResponse>> AuthorizeNeutrinoAsync(
|
||||
CancellationToken ct)
|
||||
{
|
||||
// Photon sends the request with Content-Type: text/plain instead of application/json
|
||||
// This is a really janky workaround since we can't use [FromBody] to automatically parse the data.
|
||||
using var reader = new StreamReader(Request.Body);
|
||||
string rawText = await reader.ReadToEndAsync();
|
||||
string rawText = await reader.ReadToEndAsync(ct);
|
||||
|
||||
NeutrinoAuthenticateRequest? request;
|
||||
|
||||
@@ -41,42 +36,30 @@ public class NeutrinoController(
|
||||
AuthenticationResultCode.InvalidParameters));
|
||||
}
|
||||
|
||||
// 1. Check if request parameters aren't empty
|
||||
if (request is null ||
|
||||
request.ProfileId == Guid.Empty ||
|
||||
string.IsNullOrWhiteSpace(request.AccessToken))
|
||||
if (request is null)
|
||||
return Ok(
|
||||
NeutrinoAuthenticateResponse.Failure(
|
||||
AuthenticationResultCode.InvalidParameters));
|
||||
|
||||
// 2. Verify accessToken is valid
|
||||
var tokenVerifyResult = tokenService.VerifyToken(request.AccessToken);
|
||||
if (tokenVerifyResult.IsError || !TryGetProfileId(tokenVerifyResult.Claims, out var tokenAccountId))
|
||||
return Ok(
|
||||
NeutrinoAuthenticateResponse.Failure(
|
||||
AuthenticationResultCode.AuthenticationFailedWrongCredentials));
|
||||
var result = await authorizationService.AuthorizeAsync(
|
||||
new AuthorizeNeutrinoCommand(
|
||||
request.ProfileId,
|
||||
request.AccessToken),
|
||||
ct);
|
||||
|
||||
// 3. Verify that the accountId from the request matches the one from the token
|
||||
if (tokenAccountId != request.ProfileId)
|
||||
if (!result.Succeeded)
|
||||
return Ok(
|
||||
NeutrinoAuthenticateResponse.Failure(
|
||||
AuthenticationResultCode.AuthenticationFailedWrongCredentials));
|
||||
|
||||
// 4. Get the profile to check if it exists, and for the name
|
||||
var profile = await profileRepository.GetByIdAsync(request.ProfileId);
|
||||
if (profile is null)
|
||||
return Ok(
|
||||
NeutrinoAuthenticateResponse.Failure(
|
||||
AuthenticationResultCode.AuthenticationFailedWrongCredentials));
|
||||
MapFailure(result.Failure)));
|
||||
|
||||
return Ok(
|
||||
NeutrinoAuthenticateResponse.Success(
|
||||
userId: request.ProfileId.ToString(),
|
||||
nickname: profile.Name
|
||||
)
|
||||
);
|
||||
result.UserId!,
|
||||
result.Nickname!));
|
||||
}
|
||||
|
||||
private static bool TryGetProfileId(IEnumerable<Claim> claims, out Guid accountId)
|
||||
=> Guid.TryParse(claims.FirstOrDefault(c => c.Type == "sub")?.Value, out accountId);
|
||||
}
|
||||
private static AuthenticationResultCode MapFailure(NeutrinoAuthorizationFailure? failure)
|
||||
=> failure == NeutrinoAuthorizationFailure.InvalidParameters
|
||||
? AuthenticationResultCode.InvalidParameters
|
||||
: AuthenticationResultCode.AuthenticationFailedWrongCredentials;
|
||||
}
|
||||
|
||||
@@ -1,79 +1,53 @@
|
||||
using API.Contracts.Profiles.Responses;
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using RecNet.Application.Profiles;
|
||||
using RecNet.Domain.Repositories;
|
||||
using RecNet.Domain.Services.Tokens;
|
||||
using LoginRequest = API.Contracts.Profiles.Requests.LoginRequest;
|
||||
using Profile = RecNet.Domain.Entities.Profiles.Profile;
|
||||
|
||||
namespace API.Controllers.Profiles.V1;
|
||||
|
||||
[Route("api/[controller]/v1")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class ProfilesController(
|
||||
IProfileRepository profileRepository,
|
||||
IMapper mapper,
|
||||
ITokenService tokenService) : ControllerBase
|
||||
public class ProfilesController(IProfileService profileService) : ControllerBase
|
||||
{
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<ActionResult<ProfileDTO>> GetProfile(
|
||||
Guid id,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var profile = await profileRepository.GetByIdAsync(id, ct);
|
||||
var profile = await profileService.GetProfileAsync(id, ct);
|
||||
if (profile == null)
|
||||
return NotFound();
|
||||
|
||||
return mapper.Map<ProfileDTO>(profile);
|
||||
return profile;
|
||||
}
|
||||
|
||||
[HttpGet("bulk")]
|
||||
public async Task<IReadOnlyList<ProfileDTO>> GetBulkProfiles(
|
||||
[FromQuery(Name = "id")] List<Guid> ids,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var profiles = await profileRepository.GetByIdsAsync(ids, ct);
|
||||
=> await profileService.GetProfilesAsync(ids, ct);
|
||||
|
||||
return mapper.Map<List<ProfileDTO>>(profiles);
|
||||
}
|
||||
|
||||
// TODO: Implement
|
||||
[AllowAnonymous]
|
||||
[HttpPost("login")]
|
||||
public async Task<ActionResult<LoginResponse>> Login(
|
||||
[FromBody] LoginRequest request,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var name = GenerateRandomName();
|
||||
|
||||
var profile = await profileRepository.GetByPlatform(request.PlatformType, request.PlatformId, ct);
|
||||
if (profile is null)
|
||||
{
|
||||
profile = Profile.Create(name, request.PlatformType, request.PlatformId);
|
||||
await profileRepository.AddAsync(profile, ct);
|
||||
}
|
||||
var result = await profileService.LoginAsync(
|
||||
new LoginProfileCommand(
|
||||
request.DeviceId,
|
||||
request.PlatformType,
|
||||
request.PlatformId,
|
||||
request.PlatformAuthentication),
|
||||
ct);
|
||||
|
||||
profile.AddDeviceId(request.DeviceId);
|
||||
|
||||
await profileRepository.SaveChangesAsync(ct);
|
||||
|
||||
if (profile.IsBanned)
|
||||
return BadRequest();
|
||||
|
||||
var token = tokenService.GenerateProfileToken(profile);
|
||||
if (!result.Succeeded)
|
||||
return BadRequest(result.Message);
|
||||
|
||||
return new LoginResponse
|
||||
{
|
||||
Profile = mapper.Map<ProfileDTO>(profile),
|
||||
AccessToken = token.AccessToken,
|
||||
ExpiresIn = token.ExpiresIn
|
||||
Profile = result.Profile!,
|
||||
AccessToken = result.AccessToken!,
|
||||
ExpiresIn = result.ExpiresIn
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: REPLACE WITH BETTER GENERATOR, THIS IS TEMP.
|
||||
private static string GenerateRandomName()
|
||||
=> string.Concat("rr", Guid.NewGuid().ToString("N").AsSpan(0, 18));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user