57f5239cfc
The "Auto-elevate ownership when permission scan is denied" setting was dead code: the toggle was persisted but never read, the audit flow never passed its onAccessDenied callback, and EnrichException wrapped every CSOM error (including ServerUnauthorizedAccessException) into a generic InvalidOperationException so the access-denied catch could never match. Centralize elevation instead of per-call-site callbacks: - Throw typed SharePointAccessDeniedException from EnrichException on access-denied, preserving the failing site URL and enriched diagnostic. - Add scoped IElevationCoordinator that catches it, and when AutoTakeOwnership is enabled takes site-collection admin via the tenant admin endpoint and retries the operation once. Per-site dedupe prevents loops; admin-host denials are not treated as ownership issues. Retry is safe because each wrapped operation closure re-issues its own CSOM loads. - Wrap all site-scoped operations (Storage, Permissions, Duplicates, Search, VersionCleanup, FolderStructure, BulkMembers, FileTransfer, Templates) and the UserAccessAudit per-site scan in the coordinator. - Drop the unused onAccessDenied parameter from IUserAccessAuditService. Elevation still requires SharePoint tenant admin rights on the signed-in account; the coordinator surfaces a clear message when that is missing. Also keeps the prior StorageService change that avoids admin-gated folder.StorageMetrics (403 for delegated non-admin tokens). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
281 lines
14 KiB
C#
281 lines
14 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
|
|
using Serilog;
|
|
using SharepointToolbox.Web.Core.Config;
|
|
using SharepointToolbox.Web.Core.Models;
|
|
using SharepointToolbox.Web.Infrastructure.Auth;
|
|
using SharepointToolbox.Web.Infrastructure.OAuth;
|
|
using SharepointToolbox.Web.Infrastructure.Persistence;
|
|
using SharepointToolbox.Web.Services;
|
|
using SharepointToolbox.Web.Services.Audit;
|
|
using SharepointToolbox.Web.Services.Auth;
|
|
using SharepointToolbox.Web.Services.Export;
|
|
using SharepointToolbox.Web.Services.OAuth;
|
|
using SharepointToolbox.Web.Services.Session;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// ── Serilog ───────────────────────────────────────────────────────────────────
|
|
var dataFolder = builder.Configuration["DataFolder"] ?? "/data";
|
|
Directory.CreateDirectory(dataFolder);
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
.ReadFrom.Configuration(builder.Configuration)
|
|
.WriteTo.Console()
|
|
.WriteTo.File(
|
|
Path.Combine(dataFolder, "logs", "app-.log"),
|
|
rollingInterval: RollingInterval.Day,
|
|
retainedFileCountLimit: 30)
|
|
.CreateLogger();
|
|
|
|
builder.Host.UseSerilog();
|
|
|
|
// ── Blazor / Razor Components ─────────────────────────────────────────────────
|
|
builder.Services.AddRazorComponents()
|
|
.AddInteractiveServerComponents();
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
// ── Authentication ────────────────────────────────────────────────────────────
|
|
if (builder.Environment.IsDevelopment())
|
|
{
|
|
// Dev: cookie-only, no OIDC. /account/login auto-signs in a hardcoded Admin.
|
|
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
|
.AddCookie(options =>
|
|
{
|
|
options.LoginPath = "/account/login";
|
|
options.LogoutPath = "/account/logout";
|
|
options.Cookie.SameSite = SameSiteMode.Lax;
|
|
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
|
|
options.ExpireTimeSpan = TimeSpan.FromHours(8);
|
|
options.SlidingExpiration = true;
|
|
});
|
|
}
|
|
else
|
|
{
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
|
|
})
|
|
.AddCookie(options =>
|
|
{
|
|
options.LoginPath = "/account/login";
|
|
options.LogoutPath = "/account/logout";
|
|
// Auth state lives entirely in the browser cookie (Data Protection encrypted)
|
|
options.SessionStore = null;
|
|
options.Cookie.SameSite = SameSiteMode.Lax;
|
|
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
|
|
options.ExpireTimeSpan = TimeSpan.FromHours(8);
|
|
options.SlidingExpiration = true;
|
|
})
|
|
.AddOpenIdConnect(options =>
|
|
{
|
|
var oidc = builder.Configuration.GetSection("Oidc");
|
|
options.Authority = $"https://login.microsoftonline.com/{oidc["TenantId"]}/v2.0";
|
|
options.ClientId = oidc["ClientId"];
|
|
options.ClientSecret = oidc["ClientSecret"];
|
|
options.ResponseType = OpenIdConnectResponseType.Code;
|
|
options.SaveTokens = true;
|
|
options.Scope.Add("openid");
|
|
options.Scope.Add("profile");
|
|
options.Scope.Add("email");
|
|
options.GetClaimsFromUserInfoEndpoint = true;
|
|
options.MapInboundClaims = false;
|
|
options.TokenValidationParameters.NameClaimType = "preferred_username";
|
|
|
|
options.Events.OnTokenValidated = async ctx =>
|
|
{
|
|
var userService = ctx.HttpContext.RequestServices.GetRequiredService<IUserService>();
|
|
await userService.ProvisionAsync(ctx.Principal!);
|
|
};
|
|
});
|
|
}
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
// ── Memory cache (used by OAuth flow cache) ───────────────────────────────────
|
|
builder.Services.AddMemoryCache();
|
|
builder.Services.AddHttpClient("oauth");
|
|
|
|
// ── ClientConnect options ─────────────────────────────────────────────────────
|
|
builder.Services.Configure<ClientConnectOptions>(builder.Configuration.GetSection("ClientConnect"));
|
|
|
|
// ── App config ────────────────────────────────────────────────────────────────
|
|
builder.Services.Configure<AppConfiguration>(opt =>
|
|
{
|
|
opt.DataFolder = dataFolder;
|
|
opt.ExportsFolder = Path.Combine(dataFolder, "exports");
|
|
Directory.CreateDirectory(opt.ExportsFolder);
|
|
});
|
|
|
|
// ── Persistence (Singleton — files on disk) ───────────────────────────────────
|
|
builder.Services.AddSingleton(new ProfileRepository(Path.Combine(dataFolder, "profiles.json")));
|
|
builder.Services.AddSingleton(new SettingsRepository(Path.Combine(dataFolder, "settings.json")));
|
|
builder.Services.AddSingleton(new TemplateRepository(Path.Combine(dataFolder, "templates")));
|
|
builder.Services.AddSingleton(new UserRepository(Path.Combine(dataFolder, "users.json")));
|
|
builder.Services.AddSingleton(new AuditRepository(Path.Combine(dataFolder, "audit.jsonl")));
|
|
|
|
// ── Auth infrastructure ───────────────────────────────────────────────────────
|
|
builder.Services.AddSingleton<IUserService, UserService>();
|
|
builder.Services.AddSingleton<IOAuthFlowCache, OAuthFlowCache>();
|
|
builder.Services.AddSingleton<IEntraDeviceCodeFlow, EntraDeviceCodeFlow>();
|
|
builder.Services.AddHttpClient<ITokenRefreshService, TokenRefreshService>();
|
|
builder.Services.AddHttpClient<IAppRegistrationService, AppRegistrationService>();
|
|
builder.Services.AddScoped<GraphClientFactory>();
|
|
|
|
// ── User session (Scoped = one per Blazor circuit = one per browser tab) ─────
|
|
builder.Services.AddScoped<IUserSessionService, UserSessionService>();
|
|
builder.Services.AddScoped<IUserContextAccessor, UserContextAccessor>();
|
|
builder.Services.AddScoped<ISessionCredentialStore, SessionCredentialStore>();
|
|
|
|
// ── Audit (Scoped — reads user context from circuit) ─────────────────────────
|
|
builder.Services.AddScoped<IAuditService, AuditService>();
|
|
|
|
// ── Business services (Scoped — each user circuit gets its own instances) ─────
|
|
builder.Services.AddScoped<ISessionManager, SessionManager>();
|
|
builder.Services.AddScoped<IPermissionsService, PermissionsService>();
|
|
builder.Services.AddScoped<IStorageService, StorageService>();
|
|
builder.Services.AddScoped<ISearchService, SearchService>();
|
|
builder.Services.AddScoped<IDuplicatesService, DuplicatesService>();
|
|
builder.Services.AddScoped<IFileTransferService, FileTransferService>();
|
|
builder.Services.AddScoped<IBulkMemberService, BulkMemberService>();
|
|
builder.Services.AddScoped<IBulkSiteService, BulkSiteService>();
|
|
builder.Services.AddScoped<IVersionCleanupService, VersionCleanupService>();
|
|
builder.Services.AddScoped<IUserAccessAuditService, UserAccessAuditService>();
|
|
builder.Services.AddScoped<IGraphUserDirectoryService, GraphUserDirectoryService>();
|
|
builder.Services.AddScoped<IFolderStructureService, FolderStructureService>();
|
|
builder.Services.AddScoped<ITemplateService, TemplateService>();
|
|
builder.Services.AddScoped<ICsvValidationService, CsvValidationService>();
|
|
builder.Services.AddScoped<ISharePointGroupResolver, SharePointGroupResolver>();
|
|
builder.Services.AddScoped<IOwnershipElevationService, OwnershipElevationService>();
|
|
builder.Services.AddScoped<IElevationCoordinator, ElevationCoordinator>();
|
|
|
|
// ── Export services (Scoped) ──────────────────────────────────────────────────
|
|
builder.Services.AddScoped<CsvExportService>();
|
|
builder.Services.AddScoped<HtmlExportService>();
|
|
builder.Services.AddScoped<StorageCsvExportService>();
|
|
builder.Services.AddScoped<StorageHtmlExportService>();
|
|
builder.Services.AddScoped<SearchCsvExportService>();
|
|
builder.Services.AddScoped<SearchHtmlExportService>();
|
|
builder.Services.AddScoped<DuplicatesCsvExportService>();
|
|
builder.Services.AddScoped<DuplicatesHtmlExportService>();
|
|
builder.Services.AddScoped<UserAccessCsvExportService>();
|
|
builder.Services.AddScoped<UserAccessHtmlExportService>();
|
|
builder.Services.AddScoped<VersionCleanupHtmlExportService>();
|
|
builder.Services.AddScoped<BulkResultCsvExportService>();
|
|
builder.Services.AddScoped<WebExportService>();
|
|
|
|
var app = builder.Build();
|
|
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Error", createScopeForErrors: true);
|
|
app.UseHsts();
|
|
}
|
|
|
|
// Re-execute unmatched (404) requests into the branded not-found page
|
|
app.UseStatusCodePagesWithReExecute("/not-found");
|
|
|
|
app.UseStaticFiles();
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
app.UseAntiforgery();
|
|
|
|
// ── Login / Logout endpoints ──────────────────────────────────────────────────
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.MapGet("/account/login", async (HttpContext ctx, string? returnUrl, IUserService userService) =>
|
|
{
|
|
const string devEmail = "dev@local.test";
|
|
const string devName = "Dev Admin";
|
|
|
|
// Provision the dev user in users.json (first run = Admin)
|
|
var provisionPrincipal = new ClaimsPrincipal(new ClaimsIdentity(
|
|
new[] { new Claim("preferred_username", devEmail), new Claim("name", devName) },
|
|
CookieAuthenticationDefaults.AuthenticationScheme));
|
|
var user = await userService.ProvisionAsync(provisionPrincipal);
|
|
|
|
// Sign in with full claims including app_role for HTTP endpoints
|
|
var principal = new ClaimsPrincipal(new ClaimsIdentity(
|
|
new Claim[] {
|
|
new("preferred_username", devEmail),
|
|
new("name", devName),
|
|
new("app_role", user.Role.ToString()),
|
|
},
|
|
CookieAuthenticationDefaults.AuthenticationScheme));
|
|
|
|
await ctx.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
|
|
ctx.Response.Redirect(string.IsNullOrEmpty(returnUrl) ? "/" : returnUrl);
|
|
});
|
|
|
|
app.MapGet("/account/logout", async (HttpContext ctx) =>
|
|
{
|
|
await ctx.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
ctx.Response.Redirect("/");
|
|
});
|
|
}
|
|
else
|
|
{
|
|
app.MapGet("/account/login", async (HttpContext ctx, string? returnUrl) =>
|
|
{
|
|
var props = new AuthenticationProperties
|
|
{
|
|
RedirectUri = string.IsNullOrEmpty(returnUrl) ? "/" : returnUrl
|
|
};
|
|
await ctx.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, props);
|
|
});
|
|
|
|
app.MapGet("/account/logout", async (HttpContext ctx) =>
|
|
{
|
|
await ctx.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
await ctx.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme,
|
|
new AuthenticationProperties { RedirectUri = "/" });
|
|
});
|
|
}
|
|
|
|
// ── OAuth2 connect endpoints ──────────────────────────────────────────────────
|
|
app.MapOAuthEndpoints();
|
|
|
|
// ── File download endpoint ────────────────────────────────────────────────────
|
|
app.MapGet("/export/download/{fileName}", async (string fileName, IOptions<AppConfiguration> opts, HttpContext ctx) =>
|
|
{
|
|
if (!ctx.User.Identity?.IsAuthenticated ?? true) return Results.Unauthorized();
|
|
var path = Path.Combine(opts.Value.ExportsFolder, Path.GetFileName(fileName));
|
|
if (!File.Exists(path)) return Results.NotFound();
|
|
var bytes = await File.ReadAllBytesAsync(path);
|
|
var ct = fileName.EndsWith(".csv") ? "text/csv" : "text/html";
|
|
return Results.File(bytes, ct, fileName);
|
|
});
|
|
|
|
// ── Audit CSV download ────────────────────────────────────────────────────────
|
|
app.MapGet("/audit/export", async (AuditRepository auditRepo, HttpContext ctx) =>
|
|
{
|
|
if (!ctx.User.Identity?.IsAuthenticated ?? true) return Results.Unauthorized();
|
|
// Role check via the app-role claim set during OIDC provisioning
|
|
var rolesClaim = ctx.User.FindFirst("app_role")?.Value;
|
|
if (rolesClaim != nameof(UserRole.Admin)) return Results.Forbid();
|
|
var entries = await auditRepo.LoadAllAsync();
|
|
var sb = new System.Text.StringBuilder();
|
|
sb.AppendLine("Timestamp,UserEmail,UserDisplay,UserRole,Action,Client,Sites,Details");
|
|
foreach (var e in entries.OrderByDescending(x => x.Timestamp))
|
|
{
|
|
string Esc(string v) => v.Contains(',') || v.Contains('"') || v.Contains('\n')
|
|
? $"\"{v.Replace("\"", "\"\"")}\"" : v;
|
|
sb.AppendLine(string.Join(",",
|
|
Esc(e.Timestamp.ToString("yyyy-MM-dd HH:mm:ss")),
|
|
Esc(e.UserEmail), Esc(e.UserDisplay), Esc(e.UserRole.ToString()),
|
|
Esc(e.Action), Esc(e.ClientName),
|
|
Esc(string.Join("; ", e.Sites)), Esc(e.Details)));
|
|
}
|
|
return Results.File(System.Text.Encoding.UTF8.GetBytes(sb.ToString()), "text/csv", "audit-log.csv");
|
|
});
|
|
|
|
app.MapRazorComponents<SharepointToolbox.Web.Components.App>()
|
|
.AddInteractiveServerRenderMode();
|
|
|
|
app.Run();
|