< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Jwt.JwtAlgorithmExtensions
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Jwt/JwtAlgorithm.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 106
Line coverage: 100%
Branch coverage
100%
Covered branches: 19
Total branches: 19
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
ToJwtString(...)100%1919100%

File(s)

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

#LineLine coverage
 1using Microsoft.IdentityModel.Tokens;
 2
 3namespace Kestrun.Jwt;
 4/// <summary>
 5/// Specifies supported JWT signing algorithms.
 6/// </summary>
 7public enum JwtAlgorithm
 8{
 9    /// <summary>
 10    /// Automatically selects the algorithm based on the key length.
 11    /// </summary>
 12    Auto,
 13
 14    /// <summary>
 15    /// HMAC using SHA-256.
 16    /// </summary>
 17    HS256,
 18    /// <summary>
 19    /// HMAC using SHA-384.
 20    /// </summary>
 21    HS384,
 22    /// <summary>
 23    /// HMAC using SHA-512.
 24    /// </summary>
 25    HS512,
 26    /// <summary>
 27    /// RSA using SHA-256.
 28    /// </summary>
 29    RS256,
 30    /// <summary>
 31    /// RSA using SHA-384.
 32    /// </summary>
 33    RS384,
 34    /// <summary>
 35    /// RSA using SHA-512.
 36    /// </summary>
 37    RS512,
 38    /// <summary>
 39    /// RSASSA-PSS using SHA-256.
 40    /// </summary>
 41    PS256,
 42    /// <summary>
 43    /// RSASSA-PSS using SHA-384.
 44    /// </summary>
 45    PS384,
 46    /// <summary>
 47    /// RSASSA-PSS using SHA-512.
 48    /// </summary>
 49    PS512,
 50    /// <summary>
 51    /// ECDSA using P-256 and SHA-256.
 52    /// </summary>
 53    ES256,
 54    /// <summary>
 55    /// ECDSA using P-384 and SHA-384.
 56    /// </summary>
 57    ES384,
 58    /// <summary>
 59    /// ECDSA using P-521 and SHA-512.
 60    /// </summary>
 61    ES512
 62}
 63
 64/// <summary>
 65/// Provides extension methods for the JwtAlgorithm enum.
 66/// </summary>
 67public static class JwtAlgorithmExtensions
 68{
 69    /// <summary>
 70    /// Converts the specified <see cref="JwtAlgorithm"/> to its corresponding JWT algorithm string.
 71    /// </summary>
 72    /// <param name="alg">The JWT algorithm to convert.</param>
 73    /// <param name="keyByteLength">The key length in bytes, used only when <see cref="JwtAlgorithm.Auto"/> is specified
 74    /// <returns>The JWT algorithm string representation.</returns>
 75    public static string ToJwtString(this JwtAlgorithm alg, int keyByteLength = 0)
 76    {
 77        // handle the “Auto” case only for HMAC
 3878        return alg == JwtAlgorithm.Auto
 3879            ? keyByteLength switch
 3880            {
 581                >= 64 => SecurityAlgorithms.HmacSha512,
 482                >= 48 => SecurityAlgorithms.HmacSha384,
 1683                _ => SecurityAlgorithms.HmacSha256
 3884            }
 3885            : alg switch
 3886            {
 187                JwtAlgorithm.HS256 => SecurityAlgorithms.HmacSha256,
 188                JwtAlgorithm.HS384 => SecurityAlgorithms.HmacSha384,
 189                JwtAlgorithm.HS512 => SecurityAlgorithms.HmacSha512,
 3890
 191                JwtAlgorithm.RS256 => SecurityAlgorithms.RsaSha256,
 192                JwtAlgorithm.RS384 => SecurityAlgorithms.RsaSha384,
 193                JwtAlgorithm.RS512 => SecurityAlgorithms.RsaSha512,
 3894
 195                JwtAlgorithm.PS256 => SecurityAlgorithms.RsaSsaPssSha256,
 196                JwtAlgorithm.PS384 => SecurityAlgorithms.RsaSsaPssSha384,
 197                JwtAlgorithm.PS512 => SecurityAlgorithms.RsaSsaPssSha512,
 3898
 199                JwtAlgorithm.ES256 => SecurityAlgorithms.EcdsaSha256,
 1100                JwtAlgorithm.ES384 => SecurityAlgorithms.EcdsaSha384,
 1101                JwtAlgorithm.ES512 => SecurityAlgorithms.EcdsaSha512,
 38102
 1103                _ => throw new ArgumentOutOfRangeException(nameof(alg), alg, null)
 38104            };
 105    }
 106}