| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using Microsoft.AspNetCore.Authentication; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.Authentication; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Registry of authentication options keyed by (schema, type). |
| | | 8 | | /// Stores as base AuthenticationSchemeOptions, with typed helpers. |
| | | 9 | | /// </summary> |
| | | 10 | | public 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> |
| | 323 | 19 | | public AuthenticationRegistry(StringComparer? comparer = null) |
| | | 20 | | { |
| | 323 | 21 | | _stringComparer = comparer ?? StringComparer.Ordinal; |
| | 323 | 22 | | _map = new ConcurrentDictionary<(string, string), AuthenticationSchemeOptions>(new TupleComparer(_stringComparer |
| | 323 | 23 | | } |
| | | 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 | | { |
| | 20 | 36 | | ArgumentNullException.ThrowIfNull(schema); |
| | 20 | 37 | | ArgumentNullException.ThrowIfNull(type); |
| | 20 | 38 | | ArgumentNullException.ThrowIfNull(options); |
| | 20 | 39 | | 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 | | { |
| | 19 | 53 | | var opts = new TOptions(); |
| | 19 | 54 | | configure?.Invoke(opts); |
| | 19 | 55 | | 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 | | { |
| | 0 | 67 | | _map[(schema ?? throw new ArgumentNullException(nameof(schema)), |
| | 0 | 68 | | type ?? throw new ArgumentNullException(nameof(type)))] = options ?? throw new ArgumentNullException(nameof |
| | 0 | 69 | | } |
| | | 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 | | { |
| | 0 | 81 | | var opts = new TOptions(); |
| | 0 | 82 | | configure?.Invoke(opts); |
| | 0 | 83 | | Upsert(schema, type, opts); |
| | 0 | 84 | | } |
| | | 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 | | { |
| | 0 | 96 | | ArgumentNullException.ThrowIfNull(schema); |
| | 0 | 97 | | ArgumentNullException.ThrowIfNull(type); |
| | 0 | 98 | | 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 | | { |
| | 0 | 110 | | ArgumentNullException.ThrowIfNull(schema); |
| | 0 | 111 | | ArgumentNullException.ThrowIfNull(type); |
| | 0 | 112 | | 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 | | { |
| | 0 | 126 | | if (_map.TryGetValue((schema, type), out var baseOpts) && baseOpts is TOptions typed) |
| | | 127 | | { |
| | 0 | 128 | | options = typed; |
| | 0 | 129 | | return true; |
| | | 130 | | } |
| | 0 | 131 | | options = null; |
| | 0 | 132 | | 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 | | { |
| | 0 | 144 | | return !TryGet(schema, type, out var opts) |
| | 0 | 145 | | ? throw new KeyNotFoundException($"No authentication registered for schema='{schema}', type='{type}'.") |
| | 0 | 146 | | : 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 | | { |
| | 0 | 160 | | return !TryGet<TOptions>(schema, type, out var opts) |
| | 0 | 161 | | ? throw new KeyNotFoundException($"No authentication of type {typeof(TOptions).Name} for schema='{schema}', |
| | 0 | 162 | | : 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) |
| | 0 | 175 | | => _map.TryRemove((schema ?? throw new ArgumentNullException(nameof(schema)), |
| | 0 | 176 | | type ?? throw new ArgumentNullException(nameof(type))), out _); |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Clears all registered authentication schemes. |
| | | 180 | | /// </summary> |
| | 0 | 181 | | 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() |
| | 0 | 188 | | => _map; |
| | | 189 | | |
| | | 190 | | // ---------- Internal tuple comparer (case-insensitive support) ---------- |
| | | 191 | | |
| | 323 | 192 | | private sealed class TupleComparer(StringComparer cmp) : IEqualityComparer<(string schema, string type)> |
| | | 193 | | { |
| | 323 | 194 | | private readonly StringComparer _cmp = cmp; |
| | | 195 | | |
| | | 196 | | public bool Equals((string schema, string type) x, (string schema, string type) y) |
| | 0 | 197 | | => _cmp.Equals(x.schema, y.schema) && _cmp.Equals(x.type, y.type); |
| | | 198 | | public int GetHashCode((string schema, string type) obj) |
| | 20 | 199 | | => HashCode.Combine(_cmp.GetHashCode(obj.schema), _cmp.GetHashCode(obj.type)); |
| | | 200 | | } |
| | | 201 | | } |