< Summary

Information
Class: Anichron.API.Endpoints.AuthEndpoints
Assembly: Anichron.API
File(s): /home/runner/work/anichron/anichron/src/Anichron.API/Endpoints/AuthEndpoints.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 51
Uncovered lines: 0
Coverable lines: 51
Total lines: 114
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MapAuthEndpoints(...)100%11100%
RegisterAsync()100%11100%
LoginWebAsync(...)100%11100%
LoginMobileAsync(...)100%11100%
HandleLoginAsync()100%11100%
RefreshAsync()100%66100%
LogoutAsync()100%66100%
PasswordResetRequest()100%11100%
PasswordResetConfirm()100%11100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.API/Endpoints/AuthEndpoints.cs

#LineLine coverage
 1using Anichron.API.Infrastructure;
 2using Anichron.API.Services;
 3using Microsoft.AspNetCore.Mvc;
 4using System.Text.Json.Serialization;
 5
 6namespace Anichron.API.Endpoints;
 7
 8public static class AuthEndpoints
 9{
 10    internal const string RefreshTokenCookieName = "refresh_token";
 11
 12    public static IEndpointRouteBuilder MapAuthEndpoints(this IEndpointRouteBuilder app)
 7413    {
 7414        var group = app.MapGroup(ApiPaths.Auth.Group).WithTags("Auth");
 15
 7416        group.MapPost(ApiPaths.Auth.Register, RegisterAsync).AllowAnonymous().RequireRateLimit(AuthRateLimitPolicies.Sen
 7417             .Produces<SessionResponse>()
 7418             .ProducesValidationProblem(StatusCodes.Status409Conflict)
 7419             .ProducesValidationProblem(StatusCodes.Status422UnprocessableEntity);
 7420        group.MapPost(ApiPaths.Auth.Login, LoginWebAsync).AllowAnonymous().RequireRateLimit(AuthRateLimitPolicies.Sensit
 7421             .Produces<SessionResponse>()
 7422             .ProducesProblem(StatusCodes.Status401Unauthorized);
 7423        group.MapPost(ApiPaths.Auth.LoginMobile, LoginMobileAsync).AllowAnonymous().RequireRateLimit(AuthRateLimitPolici
 7424             .Produces<SessionResponse>()
 7425             .ProducesProblem(StatusCodes.Status401Unauthorized);
 7426        group.MapPost(ApiPaths.Auth.Refresh, RefreshAsync).AllowAnonymous().RequireRateLimit(AuthRateLimitPolicies.Refre
 7427             .Produces<SessionResponse>()
 7428             .ProducesProblem(StatusCodes.Status401Unauthorized);
 7429        group.MapPost(ApiPaths.Auth.Logout, LogoutAsync).RequireUserAllowedWhileMustChangePassword()
 7430             .Produces(StatusCodes.Status204NoContent);
 7431        group.MapPost(ApiPaths.Auth.PasswordResetRequest, PasswordResetRequest).AllowAnonymous().RequireRateLimit(AuthRa
 7432             .Produces(StatusCodes.Status501NotImplemented);
 7433        group.MapPost(ApiPaths.Auth.PasswordResetConfirm, PasswordResetConfirm).AllowAnonymous().RequireRateLimit(AuthRa
 7434             .Produces(StatusCodes.Status501NotImplemented);
 35
 7436        return app;
 7437    }
 38
 39    internal static async Task<IResult> RegisterAsync(
 40        RegisterRequest request,
 41        IAuthService auth,
 42        ISessionResponseMapper mapper,
 43        HttpContext http,
 44        CancellationToken ct)
 1145    {
 1146        var result = await auth.RegisterAsync(request.Username, request.Email, request.Password, request.InviteToken, ct
 1147        return mapper.GetRegistrationResult(result, http);
 1148    }
 49
 50    internal static Task<IResult> LoginWebAsync(
 51        LoginRequest request, IAuthService auth, ISessionResponseMapper mapper, HttpContext http, CancellationToken ct)
 1652        => 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)
 656        => 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
 2260    {
 2261        var result = await auth.LoginAsync(request.UsernameOrEmail, request.Password, ct);
 2262        return mapper.GetLoginResult(result, http, setCookie);
 2263    }
 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)
 7173    {
 7174        var rawToken = http.Request.Cookies[RefreshTokenCookieName] ?? request?.RefreshToken;
 7175        if (rawToken is null)
 276        {
 277            return ApiProblems.Problem(
 278                StatusCodes.Status401Unauthorized, ErrorCodes.RefreshTokenRequired, ProblemMessages.RefreshTokenRequired
 79        }
 80
 6981        var result = await auth.RefreshAsync(rawToken, ct);
 6882        return mapper.GetRefreshResult(result, http);
 7083    }
 84
 85    internal static async Task<IResult> LogoutAsync(
 86        HttpContext http, IAuthService auth, ISessionResponseMapper mapper, [FromBody] RefreshRequest? request, Cancella
 487    {
 488        var rawToken = http.Request.Cookies[RefreshTokenCookieName] ?? request?.RefreshToken;
 489        mapper.ClearSessionCookies(http);
 490        if (rawToken is not null)
 391            await auth.RevokeAsync(rawToken, ct);
 492        return Results.NoContent();
 493    }
 94
 95    // Not yet implemented — planned for Epic 8 (email notifications with deep links).
 196    private static IResult PasswordResetRequest() => Results.StatusCode(StatusCodes.Status501NotImplemented);
 97
 198    private static IResult PasswordResetConfirm() => Results.StatusCode(StatusCodes.Status501NotImplemented);
 99}
 100
 101public sealed record RegisterRequest(string Username, string Email, string Password, string InviteToken);
 102public sealed record LoginRequest(string UsernameOrEmail, string Password);
 103public 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>
 110public sealed record SessionResponse(
 111    string AccessToken,
 112    int ExpiresIn,
 113    UserProfileResponse User,
 114    [property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] string? RefreshToken = null);