< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Authentication.ApiKeyAuthenticationOptions
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Authentication/ApiKeyAuthenticationOptions.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 108
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
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
get_HeaderName()100%11100%
get_AdditionalHeaderNames()100%11100%
get_AllowQueryStringFallback()100%11100%
get_ExpectedKey()100%11100%
get_ExpectedKeyBytes()100%22100%
get_Logger()100%11100%
get_RequireHttps()100%11100%
get_EmitChallengeHeader()100%11100%
get_ChallengeHeaderFormat()100%11100%
get_ValidateKeyAsync()100%11100%
get_ValidateCodeSettings()100%11100%
get_IssueClaims()100%11100%
get_IssueClaimsCodeSettings()100%11100%
get_ClaimPolicyConfig()100%11100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Authentication/ApiKeyAuthenticationOptions.cs

#LineLine coverage
 1using System.Security.Claims;
 2using System.Text;
 3using Kestrun.Claims;
 4using Microsoft.AspNetCore.Authentication;
 5
 6namespace Kestrun.Authentication;
 7
 8/// <summary>
 9/// Options for API key authentication, including header names, validation, and claims issuance.
 10/// </summary>
 11public class ApiKeyAuthenticationOptions : AuthenticationSchemeOptions, IAuthenticationCommonOptions
 12{
 13    /// <summary>
 14    /// Name of the header to look for the API key.
 15    /// </summary>
 6516    public string HeaderName { get; set; } = "X-Api-Key";
 17
 18    /// <summary>
 19    /// Other headers to try if the primary one is missing.
 20    /// <para>Defaults to empty.</para>
 21    /// <para>Use this to support multiple header names for the API key.</para>
 22    /// </summary>
 5123    public string[] AdditionalHeaderNames { get; set; } = [];
 24
 25    /// <summary>
 26    /// If true, also look for the key in the query string.
 27    /// <para>Defaults to false.</para>
 28    /// <para>Note: this is less secure, as query strings can be logged.</para>
 29    /// <para>Use with caution.</para>
 30    /// </summary>
 1931    public bool AllowQueryStringFallback { get; set; }
 32
 33    /// <summary>
 34    /// Single expected API key (used if ValidateKey is not set).
 35    /// <para>Defaults to null.</para>
 36    /// <para>Use this for simple scenarios where you have a known key.</para>
 37    /// </summary>
 5738    public string? ExpectedKey { get; set; }
 39
 40    /// <summary>
 41    /// Gets the expected API key as a UTF-8 byte array, or null if <see cref="ExpectedKey"/> is not set.
 42    /// </summary>
 2043    public byte[]? ExpectedKeyBytes => ExpectedKey is not null ? Encoding.UTF8.GetBytes(ExpectedKey) : null;
 44
 45    /// <summary>
 46    /// Logger for this authentication scheme.
 47    /// <para>Defaults to Serilog's global logger.</para>
 48    /// </summary>
 12749    public Serilog.ILogger Logger { get; set; } = Serilog.Log.ForContext<ApiKeyAuthenticationOptions>();
 50
 51    /// <summary>
 52    /// If true, requires HTTPS for API key requests.
 53    /// </summary>
 6154    public bool RequireHttps { get; set; } = true;
 55
 56    /// <summary>
 57    /// If true, includes the <c>WWW-Authenticate</c> header in 401 responses.
 58    /// <para>Default: <c>true</c>.</para>
 59    /// <para>Set to <c>false</c> to suppress automatic hints to clients.</para>
 60    /// </summary>
 5061    public bool EmitChallengeHeader { get; set; } = true;
 62
 63    /// <summary>
 64    /// Format for the <c>WWW-Authenticate</c> header in 401 responses.
 65    /// <para>
 66    /// If set to <c>ApiKeyHeader</c>, emits <c>ApiKey header="X-Api-Key"</c>.
 67    /// If set to <c>HeaderOnly</c>, emits just the header name.
 68    /// </para>
 69    /// </summary>
 1070    public ApiKeyChallengeFormat ChallengeHeaderFormat { get; set; } = ApiKeyChallengeFormat.ApiKeyHeader;
 71
 72    /// <summary>
 73    /// Called to validate the raw key string. Return true if valid.
 74    /// <para>This is called for every request, so it should be fast.</para>
 75    /// </summary>
 4476    public Func<HttpContext, string, byte[], Task<bool>> ValidateKeyAsync { get; set; } = (_, _, _) => Task.FromResult(f
 77
 78    /// <summary>
 79    /// Settings for the authentication code, if using a script.
 80    /// </summary>
 81    /// <remarks>
 82    /// This allows you to specify the language, code, and additional imports/refs.
 83    /// </remarks>
 4584    public AuthenticationCodeSettings ValidateCodeSettings { get; set; } = new();
 85
 86    /// <summary>
 87    /// After credentials are valid, this is called to add extra Claims.
 88    /// Parameters: HttpContext, username → IEnumerable of extra claims.
 89    /// </summary>
 1490    public Func<HttpContext, string, Task<IEnumerable<Claim>>>? IssueClaims { get; set; }
 91
 92    /// <summary>
 93    /// Settings for the claims issuing code, if using a script.
 94    /// </summary>
 95    /// <remarks>
 96    /// This allows you to specify the language, code, and additional imports/refs for claims issuance.
 97    /// </remarks>
 4598    public AuthenticationCodeSettings IssueClaimsCodeSettings { get; set; } = new();
 99
 100    /// <summary>
 101    /// Gets or sets the claim policy configuration.
 102    /// </summary>
 103    /// <remarks>
 104    /// This allows you to define multiple authorization policies based on claims.
 105    /// Each policy can specify a claim type and allowed values.
 106    /// </remarks>
 9107    public ClaimPolicyConfig? ClaimPolicyConfig { get; set; }
 108}