Log actual OIDC discovery failure at startup (SOR.Admin)
Eagerly fetch the Keycloak discovery document after app.Build() so any network/DNS/SSL failure appears in startup logs with the real exception and the exact URL being fetched, rather than surfacing later as the cryptic "Cannot redirect to the authorization endpoint" message. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Authentication.Cookies;
|
|||||||
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.HttpOverrides;
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@@ -223,6 +224,12 @@ app.MapRazorComponents<App>()
|
|||||||
// ── Apply pending migrations ───────────────────────────────────────────────
|
// ── Apply pending migrations ───────────────────────────────────────────────
|
||||||
await MigrateAllAsync(app);
|
await MigrateAllAsync(app);
|
||||||
|
|
||||||
|
// ── Eager OIDC discovery ───────────────────────────────────────────────────
|
||||||
|
// Fetch the Keycloak discovery document at startup so any network/DNS failure
|
||||||
|
// surfaces as a clear log error rather than the cryptic "Cannot redirect to
|
||||||
|
// the authorization endpoint" message on the first browser request.
|
||||||
|
await CheckOidcDiscoveryAsync(app);
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
// ── Helpers ────────────────────────────────────────────────────────────────
|
// ── Helpers ────────────────────────────────────────────────────────────────
|
||||||
@@ -240,6 +247,27 @@ static async Task MigrateAllAsync(WebApplication app)
|
|||||||
await sp.GetRequiredService<SupportDbContext>().Database.MigrateAsync();
|
await sp.GetRequiredService<SupportDbContext>().Database.MigrateAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async Task CheckOidcDiscoveryAsync(WebApplication app)
|
||||||
|
{
|
||||||
|
var opts = app.Services
|
||||||
|
.GetRequiredService<IOptionsMonitor<OpenIdConnectOptions>>()
|
||||||
|
.Get("Keycloak");
|
||||||
|
var url = opts.MetadataAddress
|
||||||
|
?? $"{opts.Authority}/.well-known/openid-configuration";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await opts.ConfigurationManager!.GetConfigurationAsync(CancellationToken.None);
|
||||||
|
app.Logger.LogInformation("Keycloak OIDC discovery OK: {Url}", url);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
app.Logger.LogError(ex,
|
||||||
|
"Keycloak OIDC discovery FAILED from {Url}. " +
|
||||||
|
"If the pod cannot reach the public auth domain, set " +
|
||||||
|
"Keycloak__MetadataAddress to the internal k8s service URL.", url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Decodes the Keycloak access token and adds realm_access.roles and groups
|
// Decodes the Keycloak access token and adds realm_access.roles and groups
|
||||||
// as ClaimTypes.Role claims. Called from OnTicketReceived where SaveTokens
|
// as ClaimTypes.Role claims. Called from OnTicketReceived where SaveTokens
|
||||||
// has already run, so the token is guaranteed present in auth properties.
|
// has already run, so the token is guaranteed present in auth properties.
|
||||||
|
|||||||
Reference in New Issue
Block a user