< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
HandleRequirementAsync(...)100%44100%
IsAllowed(...)50%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
 12912    {
 12913        if (!context.User.HasClaim(AppClaimTypes.MustChangePassword, "true") || IsAllowed(context.Resource as HttpContex
 12514            context.Succeed(requirement);
 12915        return Task.CompletedTask;
 12916    }
 17
 18    private static bool IsAllowed(HttpContext? http)
 519        => 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{
 28    private readonly AuthorizationMiddlewareResultHandler defaultHandler = new();
 29
 30    public Task HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationRe
 31        => authorizeResult.AuthorizationFailure?.FailedRequirements.OfType<MustChangePasswordRequirement>().Any() == tru
 32            ? ApiProblems.Problem(StatusCodes.Status403Forbidden, ErrorCodes.PasswordChangeRequired, ProblemMessages.Mus
 33                .ExecuteAsync(context)
 34            : defaultHandler.HandleAsync(next, context, policy, authorizeResult);
 35}