|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
| 1 | +using System; |
4 | 2 | using System.Security.Claims; |
5 | | -using System.Text; |
6 | | -using System.Threading.Tasks; |
7 | 3 |
|
8 | 4 | namespace RCommon.Security.Claims |
9 | 5 | { |
10 | 6 | /// <summary> |
11 | | - /// Provides configurable constants for standard claim type URIs used throughout the security subsystem. |
12 | | - /// Each property defaults to the corresponding <see cref="ClaimTypes"/> value and can be overridden at startup. |
| 7 | + /// Provides claim type URI constants used throughout the security subsystem. |
| 8 | + /// Call <see cref="Configure"/> once at startup to override defaults. |
| 9 | + /// After configuration (or first property access), values are frozen. |
13 | 10 | /// </summary> |
14 | 11 | public static class ClaimTypesConst |
15 | 12 | { |
16 | | - /// <summary> |
17 | | - /// Default: <see cref="ClaimTypes.Name"/> |
18 | | - /// </summary> |
19 | | - public static string UserName { get; set; } = ClaimTypes.Name; |
| 13 | + private static ClaimTypesOptions? _options; |
| 14 | + private static bool _frozen; |
| 15 | + private static readonly object _lock = new(); |
20 | 16 |
|
21 | | - /// <summary> |
22 | | - /// Default: <see cref="ClaimTypes.GivenName"/> |
23 | | - /// </summary> |
24 | | - public static string Name { get; set; } = ClaimTypes.GivenName; |
| 17 | + public static string UserName => GetOptions().UserName; |
| 18 | + public static string Name => GetOptions().Name; |
| 19 | + public static string SurName => GetOptions().SurName; |
| 20 | + public static string UserId => GetOptions().UserId; |
| 21 | + public static string Role => GetOptions().Role; |
| 22 | + public static string Email => GetOptions().Email; |
| 23 | + public static string TenantId => GetOptions().TenantId; |
| 24 | + public static string ClientId => GetOptions().ClientId; |
25 | 25 |
|
26 | 26 | /// <summary> |
27 | | - /// Default: <see cref="ClaimTypes.Surname"/> |
| 27 | + /// Configures claim type mappings. May only be called once, before any property is accessed. |
28 | 28 | /// </summary> |
29 | | - public static string SurName { get; set; } = ClaimTypes.Surname; |
| 29 | + public static void Configure(Action<ClaimTypesOptions> configure) |
| 30 | + { |
| 31 | + Guard.IsNotNull(configure, nameof(configure)); |
30 | 32 |
|
31 | | - /// <summary> |
32 | | - /// Default: <see cref="ClaimTypes.NameIdentifier"/> |
33 | | - /// </summary> |
34 | | - public static string UserId { get; set; } = ClaimTypes.NameIdentifier; |
| 33 | + lock (_lock) |
| 34 | + { |
| 35 | + if (_frozen) |
| 36 | + { |
| 37 | + throw new InvalidOperationException( |
| 38 | + "ClaimTypesConst has already been configured or accessed. Configure may only be called once, before any property is read."); |
| 39 | + } |
35 | 40 |
|
36 | | - /// <summary> |
37 | | - /// Default: <see cref="ClaimTypes.Role"/> |
38 | | - /// </summary> |
39 | | - public static string Role { get; set; } = ClaimTypes.Role; |
| 41 | + var options = new ClaimTypesOptions(); |
| 42 | + configure(options); |
| 43 | + _options = options; |
| 44 | + _frozen = true; |
| 45 | + } |
| 46 | + } |
40 | 47 |
|
41 | | - /// <summary> |
42 | | - /// Default: <see cref="ClaimTypes.Email"/> |
43 | | - /// </summary> |
44 | | - public static string Email { get; set; } = ClaimTypes.Email; |
| 48 | + private static ClaimTypesOptions GetOptions() |
| 49 | + { |
| 50 | + if (_options != null) |
| 51 | + return _options; |
45 | 52 |
|
46 | | - /// <summary> |
47 | | - /// Default: "tenantid". |
48 | | - /// </summary> |
49 | | - public static string TenantId { get; set; } = "tenantid"; |
| 53 | + lock (_lock) |
| 54 | + { |
| 55 | + if (_options != null) |
| 56 | + return _options; |
| 57 | + |
| 58 | + _options = new ClaimTypesOptions(); |
| 59 | + _frozen = true; |
| 60 | + return _options; |
| 61 | + } |
| 62 | + } |
50 | 63 |
|
51 | 64 | /// <summary> |
52 | | - /// Default: "client_id". |
| 65 | + /// Resets configuration to allow reconfiguration. Internal — for test isolation only. |
53 | 66 | /// </summary> |
54 | | - public static string ClientId { get; set; } = "client_id"; |
| 67 | + internal static void Reset() |
| 68 | + { |
| 69 | + lock (_lock) |
| 70 | + { |
| 71 | + _options = null; |
| 72 | + _frozen = false; |
| 73 | + } |
| 74 | + } |
55 | 75 | } |
56 | 76 | } |
0 commit comments