| | | 1 | | using Anichron.API.Infrastructure; |
| | | 2 | | using Anichron.API.Services; |
| | | 3 | | using Microsoft.AspNetCore.Mvc; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | |
| | | 6 | | namespace Anichron.API.Endpoints; |
| | | 7 | | |
| | | 8 | | public static class AuthEndpoints |
| | | 9 | | { |
| | | 10 | | internal const string RefreshTokenCookieName = "refresh_token"; |
| | | 11 | | |
| | | 12 | | public static IEndpointRouteBuilder MapAuthEndpoints(this IEndpointRouteBuilder app) |
| | 74 | 13 | | { |
| | 74 | 14 | | var group = app.MapGroup(ApiPaths.Auth.Group).WithTags("Auth"); |
| | | 15 | | |
| | 74 | 16 | | group.MapPost(ApiPaths.Auth.Register, RegisterAsync).AllowAnonymous().RequireRateLimit(AuthRateLimitPolicies.Sen |
| | 74 | 17 | | .Produces<SessionResponse>() |
| | 74 | 18 | | .ProducesValidationProblem(StatusCodes.Status409Conflict) |
| | 74 | 19 | | .ProducesValidationProblem(StatusCodes.Status422UnprocessableEntity); |
| | 74 | 20 | | group.MapPost(ApiPaths.Auth.Login, LoginWebAsync).AllowAnonymous().RequireRateLimit(AuthRateLimitPolicies.Sensit |
| | 74 | 21 | | .Produces<SessionResponse>() |
| | 74 | 22 | | .ProducesProblem(StatusCodes.Status401Unauthorized); |
| | 74 | 23 | | group.MapPost(ApiPaths.Auth.LoginMobile, LoginMobileAsync).AllowAnonymous().RequireRateLimit(AuthRateLimitPolici |
| | 74 | 24 | | .Produces<SessionResponse>() |
| | 74 | 25 | | .ProducesProblem(StatusCodes.Status401Unauthorized); |
| | 74 | 26 | | group.MapPost(ApiPaths.Auth.Refresh, RefreshAsync).AllowAnonymous().RequireRateLimit(AuthRateLimitPolicies.Refre |
| | 74 | 27 | | .Produces<SessionResponse>() |
| | 74 | 28 | | .ProducesProblem(StatusCodes.Status401Unauthorized); |
| | 74 | 29 | | group.MapPost(ApiPaths.Auth.Logout, LogoutAsync).RequireUserAllowedWhileMustChangePassword() |
| | 74 | 30 | | .Produces(StatusCodes.Status204NoContent); |
| | 74 | 31 | | group.MapPost(ApiPaths.Auth.PasswordResetRequest, PasswordResetRequest).AllowAnonymous().RequireRateLimit(AuthRa |
| | 74 | 32 | | .Produces(StatusCodes.Status501NotImplemented); |
| | 74 | 33 | | group.MapPost(ApiPaths.Auth.PasswordResetConfirm, PasswordResetConfirm).AllowAnonymous().RequireRateLimit(AuthRa |
| | 74 | 34 | | .Produces(StatusCodes.Status501NotImplemented); |
| | | 35 | | |
| | 74 | 36 | | return app; |
| | 74 | 37 | | } |
| | | 38 | | |
| | | 39 | | internal static async Task<IResult> RegisterAsync( |
| | | 40 | | RegisterRequest request, |
| | | 41 | | IAuthService auth, |
| | | 42 | | ISessionResponseMapper mapper, |
| | | 43 | | HttpContext http, |
| | | 44 | | CancellationToken ct) |
| | 11 | 45 | | { |
| | 11 | 46 | | var result = await auth.RegisterAsync(request.Username, request.Email, request.Password, request.InviteToken, ct |
| | 11 | 47 | | return mapper.GetRegistrationResult(result, http); |
| | 11 | 48 | | } |
| | | 49 | | |
| | | 50 | | internal static Task<IResult> LoginWebAsync( |
| | | 51 | | LoginRequest request, IAuthService auth, ISessionResponseMapper mapper, HttpContext http, CancellationToken ct) |
| | 16 | 52 | | => HandleLoginAsync(request, auth, mapper, http, setCookie: true, ct); |
| | | 53 | | |
| | | 54 | | internal static Task<IResult> LoginMobileAsync( |
| | | 55 | | LoginRequest request, IAuthService auth, ISessionResponseMapper mapper, HttpContext http, CancellationToken ct) |
| | 6 | 56 | | => HandleLoginAsync(request, auth, mapper, http, setCookie: false, ct); |
| | | 57 | | |
| | | 58 | | private static async Task<IResult> HandleLoginAsync( |
| | | 59 | | LoginRequest request, IAuthService auth, ISessionResponseMapper mapper, HttpContext http, bool setCookie, Cancel |
| | 22 | 60 | | { |
| | 22 | 61 | | var result = await auth.LoginAsync(request.UsernameOrEmail, request.Password, ct); |
| | 22 | 62 | | return mapper.GetLoginResult(result, http, setCookie); |
| | 22 | 63 | | } |
| | | 64 | | |
| | | 65 | | internal static async Task<IResult> RefreshAsync( |
| | | 66 | | HttpContext http, |
| | | 67 | | IAuthService auth, |
| | | 68 | | ISessionResponseMapper mapper, |
| | | 69 | | // [FromBody] is explicit here because the request is nullable — |
| | | 70 | | // web clients send no body (token arrives via cookie), mobile clients send it in the body. |
| | | 71 | | [FromBody] RefreshRequest? request, |
| | | 72 | | CancellationToken ct) |
| | 71 | 73 | | { |
| | 71 | 74 | | var rawToken = http.Request.Cookies[RefreshTokenCookieName] ?? request?.RefreshToken; |
| | 71 | 75 | | if (rawToken is null) |
| | 2 | 76 | | { |
| | 2 | 77 | | return ApiProblems.Problem( |
| | 2 | 78 | | StatusCodes.Status401Unauthorized, ErrorCodes.RefreshTokenRequired, ProblemMessages.RefreshTokenRequired |
| | | 79 | | } |
| | | 80 | | |
| | 69 | 81 | | var result = await auth.RefreshAsync(rawToken, ct); |
| | 68 | 82 | | return mapper.GetRefreshResult(result, http); |
| | 70 | 83 | | } |
| | | 84 | | |
| | | 85 | | internal static async Task<IResult> LogoutAsync( |
| | | 86 | | HttpContext http, IAuthService auth, ISessionResponseMapper mapper, [FromBody] RefreshRequest? request, Cancella |
| | 4 | 87 | | { |
| | 4 | 88 | | var rawToken = http.Request.Cookies[RefreshTokenCookieName] ?? request?.RefreshToken; |
| | 4 | 89 | | mapper.ClearSessionCookies(http); |
| | 4 | 90 | | if (rawToken is not null) |
| | 3 | 91 | | await auth.RevokeAsync(rawToken, ct); |
| | 4 | 92 | | return Results.NoContent(); |
| | 4 | 93 | | } |
| | | 94 | | |
| | | 95 | | // Not yet implemented — planned for Epic 8 (email notifications with deep links). |
| | 1 | 96 | | private static IResult PasswordResetRequest() => Results.StatusCode(StatusCodes.Status501NotImplemented); |
| | | 97 | | |
| | 1 | 98 | | private static IResult PasswordResetConfirm() => Results.StatusCode(StatusCodes.Status501NotImplemented); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | public sealed record RegisterRequest(string Username, string Email, string Password, string InviteToken); |
| | | 102 | | public sealed record LoginRequest(string UsernameOrEmail, string Password); |
| | | 103 | | public sealed record RefreshRequest(string RefreshToken); |
| | | 104 | | |
| | | 105 | | /// <summary>A new or renewed session, with the refresh token only when it is not set as a cookie.</summary> |
| | | 106 | | /// <param name="AccessToken">The access token.</param> |
| | | 107 | | /// <param name="ExpiresIn">The access token's remaining lifetime in whole seconds.</param> |
| | | 108 | | /// <param name="User">The signed-in user.</param> |
| | | 109 | | /// <param name="RefreshToken">The refresh token, for a client that keeps it itself.</param> |
| | | 110 | | public sealed record SessionResponse( |
| | | 111 | | string AccessToken, |
| | | 112 | | int ExpiresIn, |
| | | 113 | | UserProfileResponse User, |
| | | 114 | | [property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] string? RefreshToken = null); |