< Summary - Heimdall API

Information
Class: ArturRios.Heimdall.WebApi.Security.PersonTokenValidator
Assembly: ArturRios.Heimdall.WebApi
File(s): /home/ci/agent/workspace/heimdall-api-docs_main/app/src/Presentation/ArturRios.Heimdall.WebApi/Security/PersonTokenValidator.cs
Line coverage
100%
Covered lines: 12
Uncovered lines: 0
Coverable lines: 12
Total lines: 54
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%
ValidateAsync()100%44100%

File(s)

/home/ci/agent/workspace/heimdall-api-docs_main/app/src/Presentation/ArturRios.Heimdall.WebApi/Security/PersonTokenValidator.cs

#LineLine coverage
 1using ArturRios.Jwt;
 2using ArturRios.Util.WebApi.Security.Authentication;
 3using ArturRios.Util.WebApi.Security.Interfaces;
 4using ArturRios.Util.WebApi.Security.Records;
 5
 6namespace ArturRios.Heimdall.WebApi.Security;
 7
 8/// <summary>
 9///     The bearer-token validator <c>AuthenticationMiddleware</c> runs: the library's
 10///     <see cref="JwtTokenValidator" />, which checks the ES256 signature and lifetime and maps the
 11///     claims, followed by the issuer and audience check it does not make (<see cref="TokenOrigin" />).
 12/// </summary>
 13/// <remarks>
 14///     <para>
 15///         A token passes only under the configured issuer and one of the three person audiences
 16///         (<see cref="PersonTokenAudiences" />). The challenge audience is among them so that
 17///         <see cref="MfaPendingGuardFilter" /> still answers a challenge used as a bearer token with
 18///         its own refusal; the application audience is not, since an application token is never a
 19///         person (TH-35).
 20///     </para>
 21///     <para>
 22///         An HS256 token is refused like any other bad signature: the configuration pins ES256 and
 23///         holds EC keys alone, and the shared secret Heimdall once signed with is no longer read
 24///         (Threat Model TH-43).
 25///     </para>
 26///     <para>
 27///         A token refused here gets the answer a bad signature gets, so a correctly signed token for
 28///         another service cannot be told apart from a forged one.
 29///     </para>
 30/// </remarks>
 106131public sealed class PersonTokenValidator(
 106132    JwtTokenValidator inner,
 106133    JwtConfiguration configuration,
 106134    PersonTokenAudiences audiences) : ITokenValidator
 35{
 36    /// <summary>The error <see cref="JwtTokenValidator" /> gives a token whose signature or lifetime fails.</summary>
 37    public const string InvalidToken = "Invalid token";
 38
 39    /// <inheritdoc />
 40    public async Task<TokenValidationResult> ValidateAsync(string token, HttpContext context)
 41    {
 109442        var result = await inner.ValidateAsync(token, context);
 43
 109444        if (result.User is null)
 45        {
 11546            return result;
 47        }
 48
 97949        return TokenOrigin.IsIssuedFor(
 97950            token, configuration.Issuer, audiences.Person, audiences.TwoFactorChallenge, audiences.SubjectRights)
 97951            ? result
 97952            : new TokenValidationResult(null, InvalidToken);
 109453    }
 54}