< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Jwt.JwtBuilderResult
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Jwt/JwtBuilderResult.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
97%
Covered lines: 37
Uncovered lines: 1
Coverable lines: 38
Total lines: 92
Line coverage: 97.3%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_IssuedAt()100%11100%
get_Expires()100%11100%
Token()100%11100%
GetValidationParameters(...)100%44100%
ValidateAsync()100%11100%
Validate(...)100%210%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Jwt/JwtBuilderResult.cs

#LineLine coverage
 1using Microsoft.IdentityModel.JsonWebTokens;
 2using Microsoft.IdentityModel.Tokens;
 3using System.Security.Claims;
 4
 5namespace Kestrun.Jwt;
 6
 7/// <summary>
 8/// Represents the result of building a JWT, including the token, key, builder, issue time, and expiration.
 9/// </summary>
 10/// <param name="token">The JWT compact string.</param>
 11/// <param name="key">The symmetric security key used for signing.</param>
 12/// <param name="builder">The JWT token builder instance.</param>
 13/// <param name="issuedAt">The time at which the token was issued.</param>
 14/// <param name="expires">The expiration time of the token.</param>
 2415public sealed class JwtBuilderResult(
 2416    string token,
 2417    SymmetricSecurityKey? key,
 2418    JwtTokenBuilder builder,
 2419    DateTime issuedAt,
 2420    DateTime expires)
 21{
 2422    private readonly string _token = token;
 2423    private readonly SymmetricSecurityKey? _key = key;
 2424    private readonly JwtTokenBuilder _builder = builder;
 25
 26    /// <summary>
 27    /// Gets the time at which the token was issued.
 28    /// </summary>
 2429    public DateTime IssuedAt { get; } = issuedAt;
 30    /// <summary>
 31    /// Gets the expiration time of the token.
 32    /// </summary>
 2433    public DateTime Expires { get; private set; } = expires;
 34
 35    /// <summary>Get the JWT compact string.</summary>
 1736    public string Token() => _token;
 37
 38
 39    /// <summary>
 40    /// Gets the <see cref="TokenValidationParameters"/> for validating the JWT token.
 41    /// </summary>
 42    /// <param name="clockSkew">Optional clock skew to allow when validating token lifetime.</param>
 43    /// <returns>The configured <see cref="TokenValidationParameters"/> instance.</returns>
 44    public TokenValidationParameters GetValidationParameters(TimeSpan? clockSkew = null)
 45    {
 1046        var tvp = new TokenValidationParameters
 1047        {
 1048            ValidateIssuer = _builder.Issuer is not null,
 1049            ValidIssuer = _builder.Issuer,
 1050            ValidateAudience = _builder.Audience is not null,
 1051            ValidAudience = _builder.Audience,
 1052            ValidateLifetime = true,
 1053            ClockSkew = clockSkew ?? TimeSpan.FromMinutes(1),
 1054
 1055            RequireSignedTokens = _key is not null,
 1056            ValidateIssuerSigningKey = _key is not null,
 1057            IssuerSigningKey = _key,
 1058            ValidAlgorithms = _builder.Algorithm != null ? [_builder.Algorithm] : [],
 1059            NameClaimType = ClaimTypes.Name,
 1060            //    NameClaimType = JwtRegisteredClaimNames.Sub,
 1061            RoleClaimType = ClaimTypes.Role
 1062        };
 1063        return tvp;
 64    }
 65
 66    /// <summary>
 67    /// Asynchronously validates the specified JWT using the configured validation parameters.
 68    /// </summary>
 69    /// <param name="jwt">The JWT compact string to validate.</param>
 70    /// <param name="clockSkew">Optional clock skew to allow when validating token lifetime.</param>
 71    /// <returns>A task that represents the asynchronous operation, containing the token validation result.</returns>
 72    public async Task<TokenValidationResult> ValidateAsync(string jwt, TimeSpan? clockSkew = null)
 73    {
 374        ArgumentNullException.ThrowIfNull(jwt);
 375        ArgumentNullException.ThrowIfNull(_key);
 76
 77        // validate the token using the parameters
 178        var validationParameters = GetValidationParameters(clockSkew);
 79
 180        var handler = new JsonWebTokenHandler();
 181        return await handler.ValidateTokenAsync(
 182            jwt, validationParameters).ConfigureAwait(false);
 183    }
 84
 85    /// <summary>
 86    /// Synchronously validates the specified JWT using the configured validation parameters.
 87    /// </summary>
 88    /// <param name="jwt">The JWT compact string to validate.</param>
 89    /// <param name="clockSkew">Optional clock skew to allow when validating token lifetime.</param>
 90    /// <returns>The token validation result.</returns>
 091    public TokenValidationResult Validate(string jwt, TimeSpan? clockSkew = null) => ValidateAsync(jwt, clockSkew).GetAw
 92}