< Summary

Information
Class: ArturRios.Util.WebApi.Security.Authentication.TokenClaimsReader
Assembly: ArturRios.Util.WebApi
File(s): /home/runner/work/dotnet-webapi-util/dotnet-webapi-util/src/Security/Authentication/TokenClaimsReader.cs
Line coverage
100%
Covered lines: 12
Uncovered lines: 0
Coverable lines: 12
Total lines: 43
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
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
.cctor()100%11100%
Read(...)100%66100%

File(s)

/home/runner/work/dotnet-webapi-util/dotnet-webapi-util/src/Security/Authentication/TokenClaimsReader.cs

#LineLine coverage
 1using System.IdentityModel.Tokens.Jwt;
 2
 3namespace ArturRios.Util.WebApi.Security.Authentication;
 4
 5/// <summary>
 6/// Reads a JSON Web Token's claims into a dictionary, without validating its signature. Callers must
 7/// validate the signature before trusting the result.
 8/// </summary>
 9public static class TokenClaimsReader
 10{
 111    private static readonly JwtSecurityTokenHandler Handler = new();
 12
 13    /// <summary>Reads the token's claims. A repeated claim key keeps its first occurrence.</summary>
 14    /// <param name="token">The JWT to read. Its signature is not validated here.</param>
 15    /// <returns>The token's claims, or <see langword="null"/> if the token is blank or cannot be read.</returns>
 16    public static IReadOnlyDictionary<string, string>? Read(string token)
 17    {
 2318        if (string.IsNullOrWhiteSpace(token) || !Handler.CanReadToken(token))
 19        {
 220            return null;
 21        }
 22
 23        JwtSecurityToken jwt;
 24
 25        try
 26        {
 2127            jwt = Handler.ReadJwtToken(token);
 2028        }
 129        catch (Exception)
 30        {
 131            return null;
 32        }
 33
 2034        var claims = new Dictionary<string, string>();
 35
 31036        foreach (var claim in jwt.Claims)
 37        {
 13538            claims.TryAdd(claim.Type, claim.Value);
 39        }
 40
 2041        return claims;
 142    }
 43}

Methods/Properties

.cctor()
Read(System.String)