Cookie revalidation
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
using maintenance.Authentication.Configuration;
|
||||
using maintenance.Data;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace maintenance.Authentication.Core;
|
||||
|
||||
public sealed class AdminCookieValidationEvents : CookieAuthenticationEvents
|
||||
{
|
||||
private static readonly string[] GroupClaimTypes =
|
||||
{
|
||||
"groups",
|
||||
"group",
|
||||
"role",
|
||||
"roles",
|
||||
ClaimTypes.GroupSid,
|
||||
ClaimTypes.Role,
|
||||
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
|
||||
"http://schemas.xmlsoap.org/claims/Group"
|
||||
};
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly AppDbContext _dbContext;
|
||||
|
||||
public AdminCookieValidationEvents(IConfiguration configuration, AppDbContext dbContext)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public override async Task ValidatePrincipal(CookieValidatePrincipalContext context)
|
||||
{
|
||||
if (context.Principal?.Identity is not ClaimsIdentity identity || !identity.IsAuthenticated)
|
||||
{
|
||||
context.RejectPrincipal();
|
||||
await context.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
return;
|
||||
}
|
||||
|
||||
var path = context.HttpContext.Request.Path;
|
||||
if (!path.StartsWithSegments("/Admin", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var configuredGroups = (_configuration[AuthEnvironment.AdminGroups] ?? string.Empty)
|
||||
.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
|
||||
var groupSource = _configuration[AuthEnvironment.AdminGroupSource] ?? AdminGroupSources.Max;
|
||||
List<string> dbGroups;
|
||||
|
||||
if (groupSource.Equals(AdminGroupSources.Ad, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
dbGroups = await _dbContext.AuthorizationGroups
|
||||
.AsNoTracking()
|
||||
.Where(g => g.Active && g.Role == "admin")
|
||||
.Select(g => g.GroupClaimValue)
|
||||
.ToListAsync(context.HttpContext.RequestAborted);
|
||||
}
|
||||
else
|
||||
{
|
||||
dbGroups = await _dbContext.AdminGroups
|
||||
.AsNoTracking()
|
||||
.Select(g => g.GroupClaimValue)
|
||||
.ToListAsync(context.HttpContext.RequestAborted);
|
||||
}
|
||||
|
||||
var effectiveGroups = configuredGroups
|
||||
.Concat(dbGroups)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
var principalGroups = identity.Claims
|
||||
.Where(c => GroupClaimTypes.Contains(c.Type, StringComparer.OrdinalIgnoreCase))
|
||||
.Select(c => c.Value);
|
||||
|
||||
var isAdmin = effectiveGroups.Length > 0 &&
|
||||
principalGroups.Any(group =>
|
||||
effectiveGroups.Contains(group, StringComparer.OrdinalIgnoreCase));
|
||||
|
||||
if (!isAdmin)
|
||||
{
|
||||
context.RejectPrincipal();
|
||||
await context.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,9 +36,17 @@ public sealed class AdminRoleClaimsTransformation : IClaimsTransformation
|
||||
return principal;
|
||||
}
|
||||
|
||||
if (identity.Claims.Any(c => c.Type == ClaimTypes.Role && c.Value == AuthRoles.Admin))
|
||||
// Remove locally-issued admin role claims so revocations in DB/config take effect immediately.
|
||||
var localAdminClaims = identity.Claims
|
||||
.Where(c =>
|
||||
c.Type == ClaimTypes.Role &&
|
||||
c.Value == AuthRoles.Admin &&
|
||||
string.Equals(c.Issuer, ClaimsIdentity.DefaultIssuer, StringComparison.OrdinalIgnoreCase))
|
||||
.ToList();
|
||||
|
||||
foreach (var claim in localAdminClaims)
|
||||
{
|
||||
return principal;
|
||||
identity.RemoveClaim(claim);
|
||||
}
|
||||
|
||||
var configuredGroups = (_configuration[AuthEnvironment.AdminGroups] ?? string.Empty)
|
||||
|
||||
@@ -68,6 +68,7 @@ builder.Services.AddRazorPages(options =>
|
||||
});
|
||||
|
||||
builder.Services.AddScoped<IClaimsTransformation, AdminRoleClaimsTransformation>();
|
||||
builder.Services.AddScoped<AdminCookieValidationEvents>();
|
||||
builder.Services.AddSingleton<IIdentityContextFactory, IdentityContextFactory>();
|
||||
builder.Services.AddSingleton<IAuthProvider, AdfsWsFedAuthProvider>();
|
||||
builder.Services.AddSingleton<IAuthProvider, OidcAuthProvider>();
|
||||
@@ -87,6 +88,7 @@ authBuilder.AddCookie(options =>
|
||||
{
|
||||
options.Cookie.Name = "errorpages.auth";
|
||||
options.AccessDeniedPath = "/AccessDenied";
|
||||
options.EventsType = typeof(AdminCookieValidationEvents);
|
||||
});
|
||||
builder.Services
|
||||
.AddOptions<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme)
|
||||
|
||||
Reference in New Issue
Block a user