Move projects to src; add Neutrino secret
This commit is contained in:
21
src/API/API.csproj
Normal file
21
src/API/API.csproj
Normal file
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>cfcf35c1-8c26-473a-a8e9-44ba009adea8</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.8"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\RecNet.Application\RecNet.Application.csproj" />
|
||||
<ProjectReference Include="..\RecNet.Infrastructure\RecNet.Infrastructure.csproj" />
|
||||
<ProjectReference Include="..\RecNet.ServiceDefaults\RecNet.ServiceDefaults.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
7
src/API/Configuration/NeutrinoOptions.cs
Normal file
7
src/API/Configuration/NeutrinoOptions.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace API.Configuration;
|
||||
|
||||
public class NeutrinoOptions
|
||||
{
|
||||
public string Secret { get; set; } = string.Empty;
|
||||
public bool ValidateSecret { get; set; } = false;
|
||||
}
|
||||
6
src/API/Configuration/RecNetOptions.cs
Normal file
6
src/API/Configuration/RecNetOptions.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace API.Configuration;
|
||||
|
||||
public class RecNetOptions
|
||||
{
|
||||
public bool UseForwardedHeaders { get; set; }
|
||||
}
|
||||
25
src/API/Contracts/Neutrino/Enums/AuthenticationResultCode.cs
Normal file
25
src/API/Contracts/Neutrino/Enums/AuthenticationResultCode.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace API.Contracts.Neutrino.Enums;
|
||||
|
||||
public enum AuthenticationResultCode
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates that authentication is incomplete and only the associated data is returned.
|
||||
/// </summary>
|
||||
/// <remarks>This value is typically used when launching the game with no account, in which data to authenticate is not provided.</remarks>
|
||||
AuthenticationIncomplete,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that authentication was successful.
|
||||
/// </summary>
|
||||
AuthenticationSuccessful,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that authentication failed due to incorrect credentials.
|
||||
/// </summary>
|
||||
AuthenticationFailedWrongCredentials,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the parameters provided to the operation are invalid.
|
||||
/// </summary>
|
||||
InvalidParameters
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace API.Contracts.Neutrino.Requests;
|
||||
|
||||
public class NeutrinoAuthenticateRequest
|
||||
{
|
||||
[JsonPropertyName("profileId")]
|
||||
public Guid ProfileId;
|
||||
|
||||
[JsonPropertyName("accessToken")]
|
||||
public required string AccessToken;
|
||||
|
||||
[JsonPropertyName("appVersion")]
|
||||
public required string AppVersion;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using API.Contracts.Neutrino.Enums;
|
||||
|
||||
namespace API.Contracts.Neutrino.Responses;
|
||||
|
||||
// {"ResultCode":1,"UserId":"their user id","Nickname":"splotybean"}
|
||||
public class NeutrinoAuthenticateResponse : NeutrinoResultCodeResponse
|
||||
{
|
||||
public static NeutrinoAuthenticateResponse Success(string userId, string nickname) => new()
|
||||
{
|
||||
ResultCode = (byte)AuthenticationResultCode.AuthenticationSuccessful,
|
||||
UserId = userId,
|
||||
Nickname = nickname
|
||||
};
|
||||
|
||||
public static NeutrinoAuthenticateResponse Failure(AuthenticationResultCode error) => new()
|
||||
{
|
||||
ResultCode = (byte)error
|
||||
};
|
||||
|
||||
[JsonPropertyName(name: "UserId")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? UserId { get; set; }
|
||||
|
||||
[JsonPropertyName(name: "Nickname")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? Nickname { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace API.Contracts.Neutrino.Responses;
|
||||
|
||||
public class NeutrinoResultCodeResponse
|
||||
{
|
||||
public static NeutrinoResultCodeResponse Success() => new NeutrinoResultCodeResponse
|
||||
{
|
||||
ResultCode = 0
|
||||
};
|
||||
|
||||
public static NeutrinoResultCodeResponse Failure(byte resultCode, string? message = null)
|
||||
{
|
||||
return new NeutrinoResultCodeResponse
|
||||
{
|
||||
Message = message,
|
||||
ResultCode = resultCode
|
||||
};
|
||||
}
|
||||
|
||||
[JsonPropertyName(name: "Data")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? Data { get; set; }
|
||||
|
||||
[JsonPropertyName(name: "Message")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? Message { get; set; }
|
||||
|
||||
[JsonPropertyName(name: "ResultCode")]
|
||||
public byte ResultCode { get; set; }
|
||||
}
|
||||
22
src/API/Contracts/Profiles/Requests/LoginRequest.cs
Normal file
22
src/API/Contracts/Profiles/Requests/LoginRequest.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using RecNet.Domain.Common;
|
||||
|
||||
namespace API.Contracts.Profiles.Requests;
|
||||
|
||||
public class LoginRequest
|
||||
{
|
||||
[Required]
|
||||
public required string AppVersion { get; set; }
|
||||
|
||||
[Required, StringLength(128, MinimumLength = 1)]
|
||||
public required string DeviceId { get; set; }
|
||||
|
||||
[Required]
|
||||
public required string PlatformAuthentication { get; set; }
|
||||
|
||||
[Required, StringLength(50, MinimumLength = 1)]
|
||||
public required string PlatformId { get; set; }
|
||||
|
||||
[EnumDataType(typeof(PlatformType))]
|
||||
public PlatformType PlatformType { get; set; }
|
||||
}
|
||||
16
src/API/Contracts/Profiles/Responses/LoginResponse.cs
Normal file
16
src/API/Contracts/Profiles/Responses/LoginResponse.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using RecNet.Application.Profiles;
|
||||
|
||||
namespace API.Contracts.Profiles.Responses;
|
||||
|
||||
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; }
|
||||
}
|
||||
17
src/API/Controllers/Config/V1/ConfigController.cs
Normal file
17
src/API/Controllers/Config/V1/ConfigController.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using RecNet.Application.Common.Interfaces;
|
||||
|
||||
namespace API.Controllers.Config.V1;
|
||||
|
||||
[Route("api/[controller]/v1")]
|
||||
[ApiController]
|
||||
public class ConfigController(IConfigService configService) : ControllerBase
|
||||
{
|
||||
[HttpGet("motd")]
|
||||
public async Task<ActionResult<string>> GetMotd(CancellationToken ct = default)
|
||||
{
|
||||
var motd = await configService.GetAsync("Config:MOTD", "Ten Whole Years!", ct);
|
||||
|
||||
return Ok(motd);
|
||||
}
|
||||
}
|
||||
75
src/API/Controllers/Neutrino/NeutrinoController.cs
Normal file
75
src/API/Controllers/Neutrino/NeutrinoController.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System.Text.Json;
|
||||
using API.Configuration;
|
||||
using API.Contracts.Neutrino.Enums;
|
||||
using API.Contracts.Neutrino.Requests;
|
||||
using API.Contracts.Neutrino.Responses;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using RecNet.Application.Neutrino;
|
||||
|
||||
namespace API.Controllers.Neutrino;
|
||||
|
||||
[Route("[controller]")]
|
||||
[ApiController]
|
||||
public class NeutrinoController(
|
||||
INeutrinoAuthorizationService authorizationService,
|
||||
IOptions<NeutrinoOptions> options) : ControllerBase
|
||||
{
|
||||
private readonly NeutrinoOptions _options = options.Value;
|
||||
|
||||
// Photon sends the request with Content-Type: text/plain instead of application/json.
|
||||
[Route("authorize")]
|
||||
public async Task<ActionResult<NeutrinoAuthenticateResponse>> AuthorizeNeutrinoAsync(
|
||||
[FromQuery(Name = "secret")] string? secret = null,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
if (_options.ValidateSecret && secret != _options.Secret)
|
||||
return Forbid();
|
||||
|
||||
using var reader = new StreamReader(Request.Body);
|
||||
string rawText = await reader.ReadToEndAsync(ct);
|
||||
|
||||
NeutrinoAuthenticateRequest? request;
|
||||
|
||||
try
|
||||
{
|
||||
request = JsonSerializer.Deserialize<NeutrinoAuthenticateRequest>(rawText, new JsonSerializerOptions
|
||||
{
|
||||
IncludeFields = true,
|
||||
PropertyNameCaseInsensitive = true
|
||||
});
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Ok(
|
||||
NeutrinoAuthenticateResponse.Failure(
|
||||
AuthenticationResultCode.InvalidParameters));
|
||||
}
|
||||
|
||||
if (request is null)
|
||||
return Ok(
|
||||
NeutrinoAuthenticateResponse.Failure(
|
||||
AuthenticationResultCode.InvalidParameters));
|
||||
|
||||
var result = await authorizationService.AuthorizeAsync(
|
||||
new AuthorizeNeutrinoCommand(
|
||||
request.ProfileId,
|
||||
request.AccessToken),
|
||||
ct);
|
||||
|
||||
if (!result.Succeeded)
|
||||
return Ok(
|
||||
NeutrinoAuthenticateResponse.Failure(
|
||||
MapFailure(result.Failure)));
|
||||
|
||||
return Ok(
|
||||
NeutrinoAuthenticateResponse.Success(
|
||||
result.UserId!,
|
||||
result.Nickname!));
|
||||
}
|
||||
|
||||
private static AuthenticationResultCode MapFailure(NeutrinoAuthorizationFailure? failure)
|
||||
=> failure == NeutrinoAuthorizationFailure.InvalidParameters
|
||||
? AuthenticationResultCode.InvalidParameters
|
||||
: AuthenticationResultCode.AuthenticationFailedWrongCredentials;
|
||||
}
|
||||
53
src/API/Controllers/Profiles/V1/ProfilesController.cs
Normal file
53
src/API/Controllers/Profiles/V1/ProfilesController.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using API.Contracts.Profiles.Responses;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using RecNet.Application.Profiles;
|
||||
using LoginRequest = API.Contracts.Profiles.Requests.LoginRequest;
|
||||
|
||||
namespace API.Controllers.Profiles.V1;
|
||||
|
||||
[Route("api/[controller]/v1")]
|
||||
[ApiController]
|
||||
public class ProfilesController(IProfileService profileService) : ControllerBase
|
||||
{
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<ActionResult<ProfileDTO>> GetProfile(
|
||||
Guid id,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var profile = await profileService.GetProfileAsync(id, ct);
|
||||
if (profile == null)
|
||||
return NotFound();
|
||||
|
||||
return profile;
|
||||
}
|
||||
|
||||
[HttpGet("bulk")]
|
||||
public async Task<IReadOnlyList<ProfileDTO>> GetBulkProfiles(
|
||||
[FromQuery(Name = "id")] List<Guid> ids,
|
||||
CancellationToken ct)
|
||||
=> await profileService.GetProfilesAsync(ids, ct);
|
||||
|
||||
[HttpPost("login")]
|
||||
public async Task<ActionResult<LoginResponse>> Login(
|
||||
[FromBody] LoginRequest request,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var result = await profileService.LoginAsync(
|
||||
new LoginProfileCommand(
|
||||
request.AppVersion,
|
||||
request.DeviceId,
|
||||
request.PlatformType,
|
||||
request.PlatformId,
|
||||
request.PlatformAuthentication
|
||||
), ct);
|
||||
if (!result.Succeeded)
|
||||
return BadRequest(result.Message);
|
||||
|
||||
return new LoginResponse
|
||||
{
|
||||
Profile = result.Profile!,
|
||||
AccessToken = result.AccessToken!,
|
||||
ExpiresIn = result.ExpiresIn
|
||||
};
|
||||
}
|
||||
}
|
||||
79
src/API/Program.cs
Normal file
79
src/API/Program.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System.Text;
|
||||
using API.Configuration;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using RecNet.Application;
|
||||
using RecNet.Infrastructure;
|
||||
using RecNet.Infrastructure.Services.Tokens;
|
||||
using RecNet.ServiceDefaults;
|
||||
|
||||
namespace API;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services
|
||||
.AddOptions<NeutrinoOptions>()
|
||||
.Bind(builder.Configuration.GetSection("Neutrino"))
|
||||
.ValidateOnStart();
|
||||
|
||||
var recNetOptions = builder.Configuration.GetSection("RecNet").Get<RecNetOptions>()
|
||||
?? new RecNetOptions();
|
||||
|
||||
var jwtOptions = builder.Configuration.GetSection("Jwt").Get<JwtOptions>()
|
||||
?? new JwtOptions();
|
||||
|
||||
builder.AddServiceDefaults();
|
||||
|
||||
builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
||||
{
|
||||
options.ForwardedHeaders =
|
||||
ForwardedHeaders.XForwardedFor |
|
||||
ForwardedHeaders.XForwardedHost |
|
||||
ForwardedHeaders.XForwardedProto;
|
||||
options.KnownIPNetworks.Clear();
|
||||
options.KnownProxies.Clear();
|
||||
});
|
||||
|
||||
builder.Services.AddApplication();
|
||||
builder.AddInfrastructure();
|
||||
|
||||
builder.Services
|
||||
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.Secret)),
|
||||
ValidateIssuer = true,
|
||||
ValidIssuer = jwtOptions.Issuer,
|
||||
ValidateAudience = true,
|
||||
ValidAudience = jwtOptions.Audience,
|
||||
ValidateLifetime = true,
|
||||
ClockSkew = TimeSpan.FromMinutes(1)
|
||||
};
|
||||
});
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
if (recNetOptions.UseForwardedHeaders)
|
||||
app.UseForwardedHeaders();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
app.MapDefaultEndpoints();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
23
src/API/Properties/launchSettings.json
Normal file
23
src/API/Properties/launchSettings.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5155",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7218;http://localhost:5155",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
src/API/appsettings.Development.json
Normal file
8
src/API/appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/API/appsettings.json
Normal file
9
src/API/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user