Align OpenIddict endpoints with configured path base

This commit is contained in:
Warren Chen 2026-03-26 06:25:17 +09:00
parent 17279cb6e0
commit 293303c989

View File

@ -13,6 +13,7 @@ using OpenIddict.Validation.AspNetCore;
EnvLoader.LoadDotEnvIfDevelopment(); EnvLoader.LoadDotEnvIfDevelopment();
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
var pathBase = NormalizePathBase(builder.Configuration["PathBase"]);
builder.Services.AddDbContext<MemberCenterDbContext>(options => builder.Services.AddDbContext<MemberCenterDbContext>(options =>
{ {
@ -51,9 +52,12 @@ builder.Services.AddOpenIddict()
}) })
.AddServer(options => .AddServer(options =>
{ {
options.SetAuthorizationEndpointUris("/oauth/authorize"); options.SetAuthorizationEndpointUris(WithPathBase(pathBase, "/oauth/authorize"));
options.SetTokenEndpointUris("/oauth/token", "/auth/login", "/auth/refresh"); options.SetTokenEndpointUris(
options.SetLogoutEndpointUris("/auth/logout"); WithPathBase(pathBase, "/oauth/token"),
WithPathBase(pathBase, "/auth/login"),
WithPathBase(pathBase, "/auth/refresh"));
options.SetLogoutEndpointUris(WithPathBase(pathBase, "/auth/logout"));
var issuer = builder.Configuration["Auth:Issuer"]; var issuer = builder.Configuration["Auth:Issuer"];
if (!string.IsNullOrWhiteSpace(issuer)) if (!string.IsNullOrWhiteSpace(issuer))
{ {
@ -128,13 +132,11 @@ builder.Services.AddHttpClient<SendEngineWebhookPublisher>();
builder.Services.AddScoped<ISendEngineWebhookPublisher, SendEngineWebhookPublisher>(); builder.Services.AddScoped<ISendEngineWebhookPublisher, SendEngineWebhookPublisher>();
var app = builder.Build(); var app = builder.Build();
var pathBase = builder.Configuration["PathBase"];
app.UseForwardedHeaders(); app.UseForwardedHeaders();
if (!string.IsNullOrWhiteSpace(pathBase)) if (!string.IsNullOrWhiteSpace(pathBase))
{ {
var normalizedPathBase = pathBase.StartsWith('/') ? pathBase : $"/{pathBase}"; app.UsePathBase(pathBase);
app.UsePathBase(normalizedPathBase);
} }
app.UseRouting(); app.UseRouting();
@ -144,3 +146,22 @@ app.UseAuthorization();
app.MapControllers(); app.MapControllers();
app.Run(); app.Run();
static string? NormalizePathBase(string? pathBase)
{
if (string.IsNullOrWhiteSpace(pathBase))
{
return null;
}
var normalized = pathBase.StartsWith('/') ? pathBase : $"/{pathBase}";
return normalized.Length > 1 ? normalized.TrimEnd('/') : normalized;
}
static string WithPathBase(string? pathBase, string relativePath)
{
var normalizedRelativePath = relativePath.StartsWith('/') ? relativePath : $"/{relativePath}";
return string.IsNullOrWhiteSpace(pathBase)
? normalizedRelativePath
: $"{pathBase}{normalizedRelativePath}";
}