|
| 1 | +namespace ServiceBus.Management.Infrastructure.Settings |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Text.Json.Serialization; |
| 5 | + using Microsoft.Extensions.Logging; |
| 6 | + using ServiceControl.Configuration; |
| 7 | + using ServiceControl.Infrastructure; |
| 8 | + |
| 9 | + public class OpenIdConnectSettings |
| 10 | + { |
| 11 | + readonly ILogger logger = LoggerUtil.CreateStaticLogger<OpenIdConnectSettings>(); |
| 12 | + |
| 13 | + public OpenIdConnectSettings(bool validateConfiguration) |
| 14 | + { |
| 15 | + Enabled = SettingsReader.Read(Settings.SettingsRootNamespace, "Authentication.Enabled", false); |
| 16 | + |
| 17 | + if (!Enabled) |
| 18 | + { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + Authority = SettingsReader.Read<string>(Settings.SettingsRootNamespace, "Authentication.Authority"); |
| 23 | + Audience = SettingsReader.Read<string>(Settings.SettingsRootNamespace, "Authentication.Audience"); |
| 24 | + ValidateIssuer = SettingsReader.Read(Settings.SettingsRootNamespace, "Authentication.ValidateIssuer", true); |
| 25 | + ValidateAudience = SettingsReader.Read(Settings.SettingsRootNamespace, "Authentication.ValidateAudience", true); |
| 26 | + ValidateLifetime = SettingsReader.Read(Settings.SettingsRootNamespace, "Authentication.ValidateLifetime", true); |
| 27 | + ValidateIssuerSigningKey = SettingsReader.Read(Settings.SettingsRootNamespace, "Authentication.ValidateIssuerSigningKey", true); |
| 28 | + RequireHttpsMetadata = SettingsReader.Read(Settings.SettingsRootNamespace, "Authentication.RequireHttpsMetadata", true); |
| 29 | + |
| 30 | + if (validateConfiguration) |
| 31 | + { |
| 32 | + Validate(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + [JsonPropertyName("enabled")] |
| 37 | + public bool Enabled { get; } |
| 38 | + |
| 39 | + [JsonPropertyName("authority")] |
| 40 | + public string Authority { get; } |
| 41 | + |
| 42 | + [JsonPropertyName("audience")] |
| 43 | + public string Audience { get; } |
| 44 | + |
| 45 | + [JsonPropertyName("validateIssuer")] |
| 46 | + public bool ValidateIssuer { get; } |
| 47 | + |
| 48 | + [JsonPropertyName("validateAudience")] |
| 49 | + public bool ValidateAudience { get; } |
| 50 | + |
| 51 | + [JsonPropertyName("validateLifetime")] |
| 52 | + public bool ValidateLifetime { get; } |
| 53 | + |
| 54 | + [JsonPropertyName("validateIssuerSigningKey")] |
| 55 | + public bool ValidateIssuerSigningKey { get; } |
| 56 | + |
| 57 | + [JsonPropertyName("requireHttpsMetadata")] |
| 58 | + public bool RequireHttpsMetadata { get; } |
| 59 | + |
| 60 | + void Validate() |
| 61 | + { |
| 62 | + if (!Enabled) |
| 63 | + { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (string.IsNullOrWhiteSpace(Authority)) |
| 68 | + { |
| 69 | + var message = "Authentication.Authority is required when authentication is enabled. Please provide a valid OpenID Connect authority URL (e.g., https://login.microsoftonline.com/{tenant-id}/v2.0)"; |
| 70 | + logger.LogCritical(message); |
| 71 | + throw new Exception(message); |
| 72 | + } |
| 73 | + |
| 74 | + if (!Uri.TryCreate(Authority, UriKind.Absolute, out var authorityUri)) |
| 75 | + { |
| 76 | + var message = $"Authentication.Authority must be a valid absolute URI. Current value: '{Authority}'"; |
| 77 | + logger.LogCritical(message); |
| 78 | + throw new Exception(message); |
| 79 | + } |
| 80 | + |
| 81 | + if (RequireHttpsMetadata && authorityUri.Scheme != Uri.UriSchemeHttps) |
| 82 | + { |
| 83 | + var message = $"Authentication.Authority must use HTTPS when RequireHttpsMetadata is true. Current value: '{Authority}'. Either use HTTPS or set Authentication.RequireHttpsMetadata to false (not recommended for production)"; |
| 84 | + logger.LogCritical(message); |
| 85 | + throw new Exception(message); |
| 86 | + } |
| 87 | + |
| 88 | + if (string.IsNullOrWhiteSpace(Audience)) |
| 89 | + { |
| 90 | + var message = "Authentication.Audience is required when authentication is enabled. Please provide a valid audience identifier (typically your API identifier or client ID)"; |
| 91 | + logger.LogCritical(message); |
| 92 | + throw new Exception(message); |
| 93 | + } |
| 94 | + |
| 95 | + if (!ValidateIssuer) |
| 96 | + { |
| 97 | + logger.LogWarning("Authentication.ValidateIssuer is set to false. This is not recommended for production environments as it may allow tokens from untrusted issuers"); |
| 98 | + } |
| 99 | + |
| 100 | + if (!ValidateAudience) |
| 101 | + { |
| 102 | + logger.LogWarning("Authentication.ValidateAudience is set to false. This is not recommended for production environments as it may allow tokens intended for other applications"); |
| 103 | + } |
| 104 | + |
| 105 | + if (!ValidateLifetime) |
| 106 | + { |
| 107 | + logger.LogWarning("Authentication.ValidateLifetime is set to false. This is not recommended as it may allow expired tokens to be accepted"); |
| 108 | + } |
| 109 | + |
| 110 | + if (!ValidateIssuerSigningKey) |
| 111 | + { |
| 112 | + logger.LogWarning("Authentication.ValidateIssuerSigningKey is set to false. This is a serious security risk and should only be used in development environments"); |
| 113 | + } |
| 114 | + |
| 115 | + logger.LogInformation("Authentication configuration validated successfully"); |
| 116 | + logger.LogInformation(" Authority: {Authority}", Authority); |
| 117 | + logger.LogInformation(" Audience: {Audience}", Audience); |
| 118 | + logger.LogInformation(" ValidateIssuer: {ValidateIssuer}", ValidateIssuer); |
| 119 | + logger.LogInformation(" ValidateAudience: {ValidateAudience}", ValidateAudience); |
| 120 | + logger.LogInformation(" ValidateLifetime: {ValidateLifetime}", ValidateLifetime); |
| 121 | + logger.LogInformation(" ValidateIssuerSigningKey: {ValidateIssuerSigningKey}", ValidateIssuerSigningKey); |
| 122 | + logger.LogInformation(" RequireHttpsMetadata: {RequireHttpsMetadata}", RequireHttpsMetadata); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments