fix(sonar): réduit la complexité cognitive de Conf
This commit is contained in:
1 parent
b607574501
commit
d0e731a243
1 file changed
+83
-67
+83
-67
@@ -527,78 +527,99 @@ public partial class Program
|
||||
var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? Environment.GetEnvironmentVariable("JWT__ISSUER") ?? "heimdall";
|
||||
var jwtAudience = builder.Configuration["Jwt:Audience"] ?? Environment.GetEnvironmentVariable("JWT__AUDIENCE") ?? "heimdall-api";
|
||||
|
||||
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
||||
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
|
||||
{
|
||||
options.LoginPath = "/login";
|
||||
options.LogoutPath = "/_auth/signout";
|
||||
options.AccessDeniedPath = "/not-found";
|
||||
options.Cookie.Name = AuthState.AuthCookieName;
|
||||
options.Cookie.HttpOnly = true;
|
||||
options.Cookie.SameSite = SameSiteMode.Lax;
|
||||
// Use SameAsRequest to support both HTTP and HTTPS deployments
|
||||
// In production with HTTPS, the cookie will still be secure
|
||||
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
|
||||
options.ExpireTimeSpan = TimeSpan.FromDays(7);
|
||||
options.SlidingExpiration = true;
|
||||
var authBuilder = builder.Services
|
||||
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
ConfigureCookieAuthentication(authBuilder);
|
||||
ConfigureJwtBearerAuthentication(authBuilder, jwtKey, jwtIssuer, jwtAudience);
|
||||
authBuilder.AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>("ApiKey", null);
|
||||
|
||||
// Add logging events
|
||||
options.Events = new CookieAuthenticationEvents
|
||||
ConfigureAuthorizationPolicies(builder);
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
|
||||
builder.Services.AddScoped<HeimdallAuthStateProvider>();
|
||||
builder.Services.AddScoped<AuthenticationStateProvider>(
|
||||
sp => sp.GetRequiredService<HeimdallAuthStateProvider>());
|
||||
}
|
||||
|
||||
private static void ConfigureCookieAuthentication(AuthenticationBuilder authBuilder)
|
||||
{
|
||||
authBuilder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
|
||||
{
|
||||
options.LoginPath = "/login";
|
||||
options.LogoutPath = "/_auth/signout";
|
||||
options.AccessDeniedPath = "/not-found";
|
||||
options.Cookie.Name = AuthState.AuthCookieName;
|
||||
options.Cookie.HttpOnly = true;
|
||||
options.Cookie.SameSite = SameSiteMode.Lax;
|
||||
// Use SameAsRequest to support both HTTP and HTTPS deployments
|
||||
// In production with HTTPS, the cookie will still be secure
|
||||
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
|
||||
options.ExpireTimeSpan = TimeSpan.FromDays(7);
|
||||
options.SlidingExpiration = true;
|
||||
|
||||
// Add logging events
|
||||
options.Events = new CookieAuthenticationEvents
|
||||
{
|
||||
OnRedirectToLogin = context =>
|
||||
{
|
||||
OnRedirectToLogin = context =>
|
||||
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<Program>>();
|
||||
logger.LogWarning("Redirect to login: {Path} -> {LoginPath}",
|
||||
context.Request.Path, options.LoginPath);
|
||||
// API requests (JSON or /api/*) must get a 401, not a
|
||||
// browser redirect — otherwise unauthenticated API calls
|
||||
// silently follow the redirect and report 200.
|
||||
if (IsApiRequest(context.Request))
|
||||
{
|
||||
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<Program>>();
|
||||
logger.LogWarning("Redirect to login: {Path} -> {LoginPath}",
|
||||
context.Request.Path, options.LoginPath);
|
||||
// API requests (JSON or /api/*) must get a 401, not a
|
||||
// browser redirect — otherwise unauthenticated API calls
|
||||
// silently follow the redirect and report 200.
|
||||
if (IsApiRequest(context.Request))
|
||||
{
|
||||
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
context.Response.Redirect(context.RedirectUri);
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
OnRedirectToAccessDenied = context =>
|
||||
{
|
||||
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<Program>>();
|
||||
logger.LogWarning("Access denied: {Path} -> {AccessDeniedPath}",
|
||||
context.Request.Path, options.AccessDeniedPath);
|
||||
if (IsApiRequest(context.Request))
|
||||
{
|
||||
context.Response.StatusCode = StatusCodes.Status403Forbidden;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
context.Response.Redirect(context.RedirectUri);
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
OnValidatePrincipal = context =>
|
||||
{
|
||||
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<Program>>();
|
||||
logger.LogDebug("Validating principal for {Path}", context.HttpContext.Request.Path);
|
||||
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
})
|
||||
.AddJwtBearer("JwtBearer", options =>
|
||||
{
|
||||
options.TokenValidationParameters = GetJwtValidationParameters(jwtKey, jwtIssuer, jwtAudience);
|
||||
options.Events = new JwtBearerEvents
|
||||
context.Response.Redirect(context.RedirectUri);
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
OnRedirectToAccessDenied = context =>
|
||||
{
|
||||
OnMessageReceived = context =>
|
||||
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<Program>>();
|
||||
logger.LogWarning("Access denied: {Path} -> {AccessDeniedPath}",
|
||||
context.Request.Path, options.AccessDeniedPath);
|
||||
if (IsApiRequest(context.Request))
|
||||
{
|
||||
var accessToken = context.Request.Query["access_token"];
|
||||
var path = context.HttpContext.Request.Path;
|
||||
if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/hubs"))
|
||||
context.Token = accessToken;
|
||||
context.Response.StatusCode = StatusCodes.Status403Forbidden;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
})
|
||||
.AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>("ApiKey", null);
|
||||
context.Response.Redirect(context.RedirectUri);
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
OnValidatePrincipal = context =>
|
||||
{
|
||||
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<Program>>();
|
||||
logger.LogDebug("Validating principal for {Path}", context.HttpContext.Request.Path);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private static void ConfigureJwtBearerAuthentication(AuthenticationBuilder authBuilder, string jwtKey, string jwtIssuer, string jwtAudience)
|
||||
{
|
||||
authBuilder.AddJwtBearer("JwtBearer", options =>
|
||||
{
|
||||
options.TokenValidationParameters = GetJwtValidationParameters(jwtKey, jwtIssuer, jwtAudience);
|
||||
options.Events = new JwtBearerEvents
|
||||
{
|
||||
OnMessageReceived = context =>
|
||||
{
|
||||
var accessToken = context.Request.Query["access_token"];
|
||||
var path = context.HttpContext.Request.Path;
|
||||
if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/hubs"))
|
||||
context.Token = accessToken;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private static void ConfigureAuthorizationPolicies(WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.AddAuthorizationCore(options =>
|
||||
{
|
||||
var allPermissions = AppPermissions.RolePermissions.Values.SelectMany(p => p).Distinct();
|
||||
@@ -609,11 +630,6 @@ public partial class Program
|
||||
// Not a permission claim but a role-based gate for admin-only actions.
|
||||
options.AddPolicy("AdminUsers", policy => policy.RequireRole(AppRoles.Admin));
|
||||
});
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
|
||||
builder.Services.AddScoped<HeimdallAuthStateProvider>();
|
||||
builder.Services.AddScoped<AuthenticationStateProvider>(
|
||||
sp => sp.GetRequiredService<HeimdallAuthStateProvider>());
|
||||
}
|
||||
|
||||
private static void ConfigureSentry(WebApplicationBuilder builder)
|
||||
|
||||
Reference in new issue
Block a user