< Summary

Information
Class: Anichron.API.Infrastructure.MustChangePasswordResultHandler
Assembly: Anichron.API
File(s): /home/runner/work/anichron/anichron/src/Anichron.API/Infrastructure/MustChangePassword.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 5
Uncovered lines: 0
Coverable lines: 5
Total lines: 35
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
HandleAsync(...)100%44100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.API/Infrastructure/MustChangePassword.cs

#LineLine coverage
 1using Anichron.API.Security;
 2using Microsoft.AspNetCore.Authorization;
 3using Microsoft.AspNetCore.Authorization.Policy;
 4
 5namespace Anichron.API.Infrastructure;
 6
 7/// <summary>Refuses a user whose password must change, unless the endpoint carries <see cref="AllowedWhileMustChangePas
 8internal sealed class MustChangePasswordRequirement
 9    : AuthorizationHandler<MustChangePasswordRequirement>, IAuthorizationRequirement
 10{
 11    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, MustChangePasswordRequirement re
 12    {
 13        if (!context.User.HasClaim(AppClaimTypes.MustChangePassword, "true") || IsAllowed(context.Resource as HttpContex
 14            context.Succeed(requirement);
 15        return Task.CompletedTask;
 16    }
 17
 18    private static bool IsAllowed(HttpContext? http)
 19        => http?.GetEndpoint()?.Metadata.GetMetadata<AllowedWhileMustChangePasswordAttribute>() is not null;
 20}
 21
 22[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
 23internal sealed class AllowedWhileMustChangePasswordAttribute : Attribute;
 24
 25/// <summary>Answers a refusal by <see cref="MustChangePasswordRequirement"/> with its own Problem Details.</summary>
 26internal sealed class MustChangePasswordResultHandler : IAuthorizationMiddlewareResultHandler
 27{
 10528    private readonly AuthorizationMiddlewareResultHandler defaultHandler = new();
 29
 30    public Task HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationRe
 12931        => authorizeResult.AuthorizationFailure?.FailedRequirements.OfType<MustChangePasswordRequirement>().Any() == tru
 12932            ? ApiProblems.Problem(StatusCodes.Status403Forbidden, ErrorCodes.PasswordChangeRequired, ProblemMessages.Mus
 12933                .ExecuteAsync(context)
 12934            : defaultHandler.HandleAsync(next, context, policy, authorizeResult);
 35}