< Summary - Heimdall API

Information
Class: ArturRios.Heimdall.Query.Handlers.GetTwoFactorStatusQueryHandler
Assembly: ArturRios.Heimdall.Query
File(s): /home/runner/work/heimdall-api/heimdall-api/src/Application/ArturRios.Heimdall.Query/Handlers/GetTwoFactorStatusQueryHandler.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 89
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage
100%
Covered methods: 2
Fully covered methods: 2
Total methods: 2
Method coverage: 100%
Full method coverage: 100%

Metrics

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

File(s)

/home/runner/work/heimdall-api/heimdall-api/src/Application/ArturRios.Heimdall.Query/Handlers/GetTwoFactorStatusQueryHandler.cs

#LineLine coverage
 1using ArturRios.Data.Relational.Core.Interfaces;
 2using ArturRios.Heimdall.Domain.Entities;
 3using ArturRios.Heimdall.Query.Input;
 4using ArturRios.Heimdall.Query.Output;
 5using ArturRios.Heimdall.Shared.Messages;
 6using ArturRios.Mediator.Query.Interfaces;
 7using ArturRios.Output;
 8using Microsoft.EntityFrameworkCore;
 9
 10namespace ArturRios.Heimdall.Query.Handlers;
 11
 12/// <summary>
 13///     Handles <see cref="GetTwoFactorStatusQuery" /> (FR-2F-15): reports the caller's own two-factor
 14///     state — active or not, which methods are configured, and how many recovery codes remain.
 15/// </summary>
 16/// <remarks>
 17///     <para>
 18///         A caller who never enabled two-factor authentication is answered with every flag
 19///         <c>false</c> and a zero count, not with a refusal. That is the ordinary state of most
 20///         accounts, and a client's settings screen should not have to render its most common state
 21///         out of an error branch. <c>NotActive</c>'s 404 stays what UC-39 and UC-40 use it for:
 22///         refusing an operation that requires an active configuration.
 23///     </para>
 24///     <para>
 25///         A caller who is not an eligible person is refused with <c>NotEligible</c> (403), exactly
 26///         as UC-36's AF-36b refuses the same caller. <see cref="GoogleUser" /> and
 27///         <see cref="Person" /> are separate tables with separate <c>PublicId</c> spaces, so a
 28///         Google-issued token's subject never resolves here — and FR-2F-01 makes Google Users
 29///         permanently ineligible, which an all-false success would misreport as "off, and you could
 30///         turn it on". The same miss covers a token naming a person who no longer exists;
 31///         <c>ActorLivenessFilter</c> already answers 401 for that case before a request arrives, so
 32///         this branch is defence in depth.
 33///     </para>
 34/// </remarks>
 1035public class GetTwoFactorStatusQueryHandler(
 1036    IAsyncReadOnlyRepository<Person, long> personReader,
 1037    IAsyncReadOnlyRepository<TwoFactorAuth, long> twoFactorReader,
 1038    IAsyncReadOnlyRepository<TwoFactorRecoveryCode, long> recoveryCodeReader)
 39    : IQueryHandlerAsync<GetTwoFactorStatusQuery, TwoFactorStatusOutput>
 40{
 41    public async Task<DataOutput<TwoFactorStatusOutput?>> HandleAsync(GetTwoFactorStatusQuery query, CancellationToken c
 42    {
 1043        var output = DataOutput<TwoFactorStatusOutput?>.New;
 44
 45        // AF-36b: the caller must be a live person. A Google User misses this lookup entirely.
 1046        var personId = await personReader.Query()
 1047            .Where(person => person.PublicId == query.ActingPersonId && !person.IsDeleted)
 1048            .Select(person => (long?)person.Id)
 1049            .FirstOrDefaultAsync();
 50
 1051        if (personId is null)
 52        {
 353            return output.WithError(TwoFactorMessages.NotEligible);
 54        }
 55
 56        // Only these four fields are ever read below; projecting keeps the encrypted TOTP secret
 57        // out of process memory and off the change tracker entirely.
 758        var configuration = await twoFactorReader.Query()
 759            .Where(x => x.PersonId == personId.Value)
 760            .Select(x => new
 761            {
 762                x.Id,
 763                x.IsActive,
 764                x.AppEnabled,
 765                x.EmailEnabled
 766            })
 767            .FirstOrDefaultAsync();
 68
 769        if (configuration is null)
 70        {
 271            return output
 272                .WithData(new TwoFactorStatusOutput())
 273                .WithMessage(TwoFactorMessages.StatusRetrieved);
 74        }
 75
 576        var remainingRecoveryCodes = await recoveryCodeReader.Query()
 577            .CountAsync(code => code.TwoFactorAuthId == configuration.Id && !code.Used);
 78
 579        return output
 580            .WithData(new TwoFactorStatusOutput
 581            {
 582                IsActive = configuration.IsActive,
 583                AppEnabled = configuration.AppEnabled,
 584                EmailEnabled = configuration.EmailEnabled,
 585                RemainingRecoveryCodes = remainingRecoveryCodes
 586            })
 587            .WithMessage(TwoFactorMessages.StatusRetrieved);
 1088    }
 89}