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