| | | 1 | | using Chronicis.Shared.DTOs; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Api.Services; |
| | | 4 | | |
| | | 5 | | public class Auth0HealthCheckService : HealthCheckServiceBase |
| | | 6 | | { |
| | | 7 | | private readonly HttpClient _httpClient; |
| | | 8 | | private readonly IConfiguration _configuration; |
| | | 9 | | |
| | | 10 | | public Auth0HealthCheckService( |
| | | 11 | | HttpClient httpClient, |
| | | 12 | | IConfiguration configuration, |
| | | 13 | | ILogger<Auth0HealthCheckService> logger) |
| | 0 | 14 | | : base(logger) |
| | | 15 | | { |
| | 0 | 16 | | _httpClient = httpClient; |
| | 0 | 17 | | _configuration = configuration; |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | protected override async Task<(string Status, string? Message)> PerformHealthCheckAsync() |
| | | 21 | | { |
| | 0 | 22 | | var domain = _configuration["Auth0:Domain"]; |
| | | 23 | | |
| | 0 | 24 | | if (string.IsNullOrEmpty(domain)) |
| | | 25 | | { |
| | 0 | 26 | | return (HealthStatus.Unhealthy, "Auth0 domain not configured"); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | try |
| | | 30 | | { |
| | 0 | 31 | | var wellKnownUrl = $"https://{domain}/.well-known/openid-configuration"; |
| | 0 | 32 | | var response = await _httpClient.GetAsync(wellKnownUrl); |
| | | 33 | | |
| | 0 | 34 | | if (response.IsSuccessStatusCode) |
| | | 35 | | { |
| | 0 | 36 | | return (HealthStatus.Healthy, "Auth0 well-known endpoint accessible"); |
| | | 37 | | } |
| | | 38 | | else |
| | | 39 | | { |
| | 0 | 40 | | return (HealthStatus.Degraded, $"Auth0 returned {response.StatusCode}"); |
| | | 41 | | } |
| | | 42 | | } |
| | 0 | 43 | | catch (Exception ex) |
| | | 44 | | { |
| | 0 | 45 | | return (HealthStatus.Unhealthy, $"Auth0 connectivity error: {ex.Message}"); |
| | | 46 | | } |
| | 0 | 47 | | } |
| | | 48 | | } |