< Summary - Heimdall API

Information
Class: ArturRios.Heimdall.Command.Services.TwoFactorFactorVerifier
Assembly: ArturRios.Heimdall.Command
File(s): /home/runner/work/heimdall-api/heimdall-api/src/Application/ArturRios.Heimdall.Command/Services/TwoFactorFactorVerifier.cs
Line coverage
100%
Covered lines: 28
Uncovered lines: 0
Coverable lines: 28
Total lines: 84
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage
100%
Covered methods: 4
Fully covered methods: 4
Total methods: 4
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
VerifyAsync()100%1212100%
FindMatchingEmailCodeAsync(...)100%11100%
FindMatchingRecoveryCodeAsync()100%22100%

File(s)

/home/runner/work/heimdall-api/heimdall-api/src/Application/ArturRios.Heimdall.Command/Services/TwoFactorFactorVerifier.cs

#LineLine coverage
 1using ArturRios.Data.Relational.Core.Interfaces;
 2using ArturRios.Heimdall.Domain.Entities;
 3using ArturRios.Heimdall.Domain.Persistence;
 4using Microsoft.EntityFrameworkCore;
 5
 6namespace ArturRios.Heimdall.Command.Services;
 7
 8/// <inheritdoc cref="ITwoFactorFactorVerifier" />
 3239public class TwoFactorFactorVerifier(
 32310    IAsyncReadOnlyRepository<TwoFactorEmailCode, long> emailCodeReader,
 32311    IAtomicWrites atomicWrites,
 32312    IAsyncReadOnlyRepository<TwoFactorRecoveryCode, long> recoveryCodeReader,
 32313    ITotpCodeVerifier totpCodeVerifier) : ITwoFactorFactorVerifier
 14{
 15    public async Task<TwoFactorFactorVerificationResult> VerifyAsync(
 16        TwoFactorAuth twoFactorAuth, string? code, string? recoveryCode)
 17    {
 20118        if (!string.IsNullOrWhiteSpace(recoveryCode))
 19        {
 7120            var matchingRecoveryCode = await FindMatchingRecoveryCodeAsync(twoFactorAuth.Id, recoveryCode);
 21
 7122            return matchingRecoveryCode is null
 7123                ? TwoFactorFactorVerificationResult.NoMatch
 7124                : TwoFactorFactorVerificationResult.ForRecoveryCode(matchingRecoveryCode);
 25        }
 26
 27        // ITotpCodeVerifier owns the secret, the window, and the single-use rule; this class only
 28        // decides which factor to try.
 13029        if (twoFactorAuth.AppEnabled && await totpCodeVerifier.VerifyAsync(twoFactorAuth, code))
 30        {
 2731            return TwoFactorFactorVerificationResult.AppCodeMatch;
 32        }
 33
 10334        if (twoFactorAuth.EmailEnabled)
 35        {
 3736            var matchingEmailCode = await FindMatchingEmailCodeAsync(twoFactorAuth.Id, code);
 37
 3738            if (matchingEmailCode is not null)
 39            {
 1240                return TwoFactorFactorVerificationResult.ForEmailCode(matchingEmailCode);
 41            }
 42        }
 43
 9144        return TwoFactorFactorVerificationResult.NoMatch;
 20145    }
 46
 47    /// <summary>
 48    ///     Finds a not-yet-used, not-yet-expired email code for this configuration that
 49    ///     <paramref name="code" /> hashes to — the same comparison
 50    ///     <c>ConfirmTwoFactorAuthCommandHandler</c> uses, through the shared
 51    ///     <see cref="TwoFactorEmailCodeVerification" />, which charges the guess against the code's
 52    ///     budget first. Returns <c>null</c> for a missing, incorrect, expired, already-used, or
 53    ///     exhausted code, all alike.
 54    /// </summary>
 55    private Task<TwoFactorEmailCode?> FindMatchingEmailCodeAsync(long twoFactorAuthId, string? code) =>
 3756        TwoFactorEmailCodeVerification.FindMatchingAsync(
 3757            emailCodeReader, atomicWrites, twoFactorAuthId, code);
 58
 59    /// <summary>
 60    ///     Finds an unused recovery code for this configuration whose hash matches
 61    ///     <paramref name="recoveryCode" />, normalised by <see cref="RecoveryCodes.DigestOf" /> —
 62    ///     hyphens, spaces and case ignored, in the current shape or the one issued before it; a
 63    ///     value of neither shape is not looked up at all. An unknown code and an already-used one
 64    ///     both return <c>null</c> here — the query itself excludes used rows, so the two cases are
 65    ///     indistinguishable by construction. A match is not spent here: the caller spends it, in a
 66    ///     write that only one request can win (<see cref="IAtomicWrites.TryConsumeRecoveryCodeAsync" />).
 67    /// </summary>
 68    private async Task<TwoFactorRecoveryCode?> FindMatchingRecoveryCodeAsync(
 69        long twoFactorAuthId, string recoveryCode)
 70    {
 7171        var hash = RecoveryCodes.DigestOf(recoveryCode);
 72
 7173        if (hash is null)
 74        {
 1675            return null;
 76        }
 77
 5578        var unused = await recoveryCodeReader.Query()
 5579            .Where(x => x.TwoFactorAuthId == twoFactorAuthId && !x.Used)
 5580            .ToListAsync();
 81
 12482        return unused.FirstOrDefault(x => x.CodeHash.SequenceEqual(hash));
 7183    }
 84}