< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Authentication.AuthenticationRegistry
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Authentication/AuthenticationRegistry.cs
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
32%
Covered lines: 14
Uncovered lines: 29
Coverable lines: 43
Total lines: 201
Line coverage: 32.5%
Branch coverage
15%
Covered branches: 4
Total branches: 26
Branch coverage: 15.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 09/16/2025 - 04:01:29 Line coverage: 32.5% (14/43) Branch coverage: 15.3% (4/26) Total lines: 201 Tag: Kestrun/Kestrun@e5263347b0baba68d9fd62ffbf60a7dd87f994bb 09/16/2025 - 04:01:29 Line coverage: 32.5% (14/43) Branch coverage: 15.3% (4/26) Total lines: 201 Tag: Kestrun/Kestrun@e5263347b0baba68d9fd62ffbf60a7dd87f994bb

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
Register(...)100%11100%
Register(...)100%22100%
Upsert(...)0%4260%
Upsert(...)0%620%
Exists(...)100%210%
TryGet(...)100%210%
TryGet(...)0%2040%
Get(...)0%620%
Get(...)0%620%
Remove(...)0%2040%
Clear()100%210%
Items()100%210%
.ctor(...)100%11100%
Equals(...)0%620%
GetHashCode(...)100%11100%

File(s)

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

#LineLine coverage
 1using System.Collections.Concurrent;
 2using Microsoft.AspNetCore.Authentication;
 3
 4namespace Kestrun.Authentication;
 5
 6/// <summary>
 7/// Registry of authentication options keyed by (schema, type).
 8/// Stores as base AuthenticationSchemeOptions, with typed helpers.
 9/// </summary>
 10public sealed class AuthenticationRegistry
 11{
 12    private readonly ConcurrentDictionary<(string schema, string type), AuthenticationSchemeOptions> _map;
 13    private readonly StringComparer _stringComparer;
 14
 15    /// <summary>
 16    /// Initializes a new instance of the <see cref="AuthenticationRegistry"/> class.
 17    /// </summary>
 18    /// <param name="comparer">The string comparer to use for matching schemas and types.</param>
 32319    public AuthenticationRegistry(StringComparer? comparer = null)
 20    {
 32321        _stringComparer = comparer ?? StringComparer.Ordinal;
 32322        _map = new ConcurrentDictionary<(string, string), AuthenticationSchemeOptions>(new TupleComparer(_stringComparer
 32323    }
 24
 25    // ---------- Register / Upsert ----------
 26
 27    /// <summary>
 28    /// Registers an authentication scheme with the specified options.
 29    /// </summary>
 30    /// <param name="schema">The schema to match for the authentication scheme.</param>
 31    /// <param name="type">The HTTP type to match for the authentication scheme.</param>
 32    /// <param name="options">The options to configure the authentication scheme.</param>
 33    /// <returns>True if the registration was successful; otherwise, false.</returns>
 34    public bool Register(string schema, string type, AuthenticationSchemeOptions options)
 35    {
 2036        ArgumentNullException.ThrowIfNull(schema);
 2037        ArgumentNullException.ThrowIfNull(type);
 2038        ArgumentNullException.ThrowIfNull(options);
 2039        return _map.TryAdd((schema, type), options);
 40    }
 41
 42    /// <summary>
 43    /// Registers an authentication scheme with the specified options and configuration.
 44    /// </summary>
 45    /// <typeparam name="TOptions">The type of the options for the authentication scheme.</typeparam>
 46    /// <param name="schema">The schema to match for the authentication scheme.</param>
 47    /// <param name="type">The HTTP type to match for the authentication scheme.</param>
 48    /// <param name="configure">An optional action to configure the authentication options.</param>
 49    /// <returns>True if the registration was successful; otherwise, false.</returns>
 50    public bool Register<TOptions>(string schema, string type, Action<TOptions>? configure = null)
 51        where TOptions : AuthenticationSchemeOptions, new()
 52    {
 1953        var opts = new TOptions();
 1954        configure?.Invoke(opts);
 1955        return Register(schema, type, opts);
 56    }
 57
 58    /// <summary>
 59    /// Upserts (adds or replaces) an entry.
 60    /// </summary>
 61    /// <param name="schema">The schema to match for the authentication scheme.</param>
 62    /// <param name="type">The HTTP type to match for the authentication scheme.</param>
 63    /// <param name="options">The options to configure the authentication scheme.</param>
 64    /// <exception cref="ArgumentNullException">Thrown when any of the arguments are null.</exception>
 65    public void Upsert(string schema, string type, AuthenticationSchemeOptions options)
 66    {
 067        _map[(schema ?? throw new ArgumentNullException(nameof(schema)),
 068             type ?? throw new ArgumentNullException(nameof(type)))] = options ?? throw new ArgumentNullException(nameof
 069    }
 70
 71    /// <summary>
 72    /// Upserts (adds or replaces) an entry.
 73    /// </summary>
 74    /// <typeparam name="TOptions">The type of the options for the authentication scheme.</typeparam>
 75    /// <param name="schema">The schema to match for the authentication scheme.</param>
 76    /// <param name="type">The HTTP type to match for the authentication scheme.</param>
 77    /// <param name="configure">An optional action to configure the authentication options.</param>
 78    public void Upsert<TOptions>(string schema, string type, Action<TOptions>? configure = null)
 79        where TOptions : AuthenticationSchemeOptions, new()
 80    {
 081        var opts = new TOptions();
 082        configure?.Invoke(opts);
 083        Upsert(schema, type, opts);
 084    }
 85
 86    // ---------- Exists / TryGet / Get ----------
 87
 88    /// <summary>
 89    /// Checks if an authentication scheme exists for the specified schema and type.
 90    /// </summary>
 91    /// <param name="schema">The schema to match for the authentication scheme.</param>
 92    /// <param name="type">The HTTP type to match for the authentication scheme.</param>
 93    /// <returns>True if an authentication scheme exists; otherwise, false.</returns>
 94    public bool Exists(string schema, string type)
 95    {
 096        ArgumentNullException.ThrowIfNull(schema);
 097        ArgumentNullException.ThrowIfNull(type);
 098        return _map.ContainsKey((schema, type));
 99    }
 100
 101    /// <summary>
 102    /// Tries to get the authentication options for the specified schema and type.
 103    /// </summary>
 104    /// <param name="schema">The schema to match for the authentication scheme.</param>
 105    /// <param name="type">The HTTP type to match for the authentication scheme.</param>
 106    /// <param name="options">The options for the authentication scheme.</param>
 107    /// <returns>True if the authentication options were found; otherwise, false.</returns>
 108    public bool TryGet(string schema, string type, out AuthenticationSchemeOptions? options)
 109    {
 0110        ArgumentNullException.ThrowIfNull(schema);
 0111        ArgumentNullException.ThrowIfNull(type);
 0112        return _map.TryGetValue((schema, type), out options);
 113    }
 114
 115    /// <summary>
 116    /// Tries to get the authentication options of the specified type for the given schema and type.
 117    /// </summary>
 118    /// <typeparam name="TOptions">The type of the authentication options.</typeparam>
 119    /// <param name="schema">The schema to match for the authentication scheme.</param>
 120    /// <param name="type">The HTTP type to match for the authentication scheme.</param>
 121    /// <param name="options">The options for the authentication scheme.</param>
 122    /// <returns>True if the authentication options were found; otherwise, false.</returns>
 123    public bool TryGet<TOptions>(string schema, string type, out TOptions? options)
 124        where TOptions : AuthenticationSchemeOptions
 125    {
 0126        if (_map.TryGetValue((schema, type), out var baseOpts) && baseOpts is TOptions typed)
 127        {
 0128            options = typed;
 0129            return true;
 130        }
 0131        options = null;
 0132        return false;
 133    }
 134
 135    /// <summary>
 136    /// Gets the authentication options for the specified schema and type.
 137    /// </summary>
 138    /// <param name="schema">The schema to match for the authentication scheme.</param>
 139    /// <param name="type">The HTTP type to match for the authentication scheme.</param>
 140    /// <returns>The authentication options for the specified schema and type.</returns>
 141    /// <exception cref="KeyNotFoundException">Thrown when no authentication options are registered for the specified sc
 142    public AuthenticationSchemeOptions Get(string schema, string type)
 143    {
 0144        return !TryGet(schema, type, out var opts)
 0145            ? throw new KeyNotFoundException($"No authentication registered for schema='{schema}', type='{type}'.")
 0146            : opts!;
 147    }
 148
 149    /// <summary>
 150    /// Gets the authentication options of the specified type for the given schema and type.
 151    /// </summary>
 152    /// <typeparam name="TOptions">The type of the authentication options.</typeparam>
 153    /// <param name="schema">The schema to match for the authentication scheme.</param>
 154    /// <param name="type">The HTTP type to match for the authentication scheme.</param>
 155    /// <returns>The authentication options of the specified type for the given schema and type.</returns>
 156    /// <exception cref="KeyNotFoundException">Thrown when no authentication options of the specified type are registere
 157    public TOptions Get<TOptions>(string schema, string type)
 158        where TOptions : AuthenticationSchemeOptions
 159    {
 0160        return !TryGet<TOptions>(schema, type, out var opts)
 0161            ? throw new KeyNotFoundException($"No authentication of type {typeof(TOptions).Name} for schema='{schema}', 
 0162            : opts!;
 163    }
 164
 165    // ---------- Remove / Clear / Enumerate ----------
 166
 167    /// <summary>
 168    /// Removes the authentication scheme for the specified schema and type.
 169    /// </summary>
 170    /// <param name="schema">The schema to match for the authentication scheme.</param>
 171    /// <param name="type">The HTTP type to match for the authentication scheme.</param>
 172    /// <returns>True if the authentication scheme was removed; otherwise, false.</returns>
 173    /// <exception cref="ArgumentNullException">Thrown when either schema or type is null.</exception>
 174    public bool Remove(string schema, string type)
 0175        => _map.TryRemove((schema ?? throw new ArgumentNullException(nameof(schema)),
 0176                           type ?? throw new ArgumentNullException(nameof(type))), out _);
 177
 178    /// <summary>
 179    /// Clears all registered authentication schemes.
 180    /// </summary>
 0181    public void Clear() => _map.Clear();
 182
 183    /// <summary>
 184    /// Enumerates all registered authentication schemes.
 185    /// </summary>
 186    /// <returns>A collection of key-value pairs representing the registered authentication schemes.</returns>
 187    public IEnumerable<KeyValuePair<(string schema, string type), AuthenticationSchemeOptions>> Items()
 0188        => _map;
 189
 190    // ---------- Internal tuple comparer (case-insensitive support) ----------
 191
 323192    private sealed class TupleComparer(StringComparer cmp) : IEqualityComparer<(string schema, string type)>
 193    {
 323194        private readonly StringComparer _cmp = cmp;
 195
 196        public bool Equals((string schema, string type) x, (string schema, string type) y)
 0197            => _cmp.Equals(x.schema, y.schema) && _cmp.Equals(x.type, y.type);
 198        public int GetHashCode((string schema, string type) obj)
 20199            => HashCode.Combine(_cmp.GetHashCode(obj.schema), _cmp.GetHashCode(obj.type));
 200    }
 201}