< Summary

Information
Class: ArturRios.Util.WebApi.Security.Interfaces.IAuthenticatedUserMapper
Assembly: ArturRios.Util.WebApi
File(s): /home/runner/work/dotnet-webapi-util/dotnet-webapi-util/src/Security/Interfaces/IAuthenticatedUserMapper.cs
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 27
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage
100%
Covered methods: 1
Fully covered methods: 1
Total methods: 1
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IdFromClaims(...)100%22100%

File(s)

/home/runner/work/dotnet-webapi-util/dotnet-webapi-util/src/Security/Interfaces/IAuthenticatedUserMapper.cs

#LineLine coverage
 1namespace ArturRios.Util.WebApi.Security.Interfaces;
 2
 3/// <summary>
 4/// Translates between the app's <see cref="IAuthenticatedUser"/> and the claims carried by its JSON Web
 5/// Tokens. One implementation owns both directions, so the claims written when a token is issued and the
 6/// claims read when it is validated cannot drift apart.
 7/// </summary>
 8/// <remarks>Implementations must return <see langword="null"/> for claims they cannot interpret rather
 9/// than throwing. Use <c>TryParse</c>, not <c>Parse</c>.</remarks>
 10public interface IAuthenticatedUserMapper
 11{
 12    /// <summary>Builds the claims to embed in a token for the given user, called by the app at login.</summary>
 13    /// <param name="user">The user the token will represent.</param>
 14    /// <returns>The claims to embed, keyed by claim name.</returns>
 15    Dictionary<string, string> ToClaims(IAuthenticatedUser user);
 16
 17    /// <summary>Rebuilds the user from a token's claims, without any data-store lookup.</summary>
 18    /// <param name="claims">The claims read from the token.</param>
 19    /// <returns>The user, or <see langword="null"/> if the claims cannot produce one.</returns>
 20    IAuthenticatedUser? FromClaims(IReadOnlyDictionary<string, string> claims);
 21
 22    /// <summary>Reads just the user id from a token's claims, used when the rest of the user is resolved
 23    /// from the data store. Override this when the token carries more than <see cref="FromClaims"/> requires.</summary>
 24    /// <param name="claims">The claims read from the token.</param>
 25    /// <returns>The user id, or <see langword="null"/> if the claims do not carry one.</returns>
 626    Guid? IdFromClaims(IReadOnlyDictionary<string, string> claims) => FromClaims(claims)?.Id;
 27}