Add persisted runtime settings page with cookie login
This commit is contained in:
1 parent
b561924efa
commit
37987a394a
43 files changed
+1346
-61
No files matched your search
@@ -0,0 +1,22 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Web.Auth;
|
||||
|
||||
public sealed class AdminAuthenticator(IOptions<AdminOptions> options)
|
||||
{
|
||||
public bool IsConfigured => !string.IsNullOrEmpty(options.Value.Password);
|
||||
|
||||
public bool Verify(string? candidate)
|
||||
{
|
||||
if (!IsConfigured || string.IsNullOrEmpty(candidate))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return CryptographicOperations.FixedTimeEquals(Hash(candidate), Hash(options.Value.Password!));
|
||||
}
|
||||
|
||||
private static byte[] Hash(string value) => SHA256.HashData(Encoding.UTF8.GetBytes(value));
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Flypaper.Web.Auth;
|
||||
|
||||
public sealed class AdminOptions
|
||||
{
|
||||
public const string SectionName = "Flypaper:Admin";
|
||||
|
||||
public string? Password { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Antiforgery;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Flypaper.Web.Auth;
|
||||
|
||||
public static class AuthEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapAuthEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
app.MapPost("/auth/login", async (
|
||||
HttpContext context,
|
||||
AdminAuthenticator admin,
|
||||
[FromForm] string? password,
|
||||
[FromForm] string? returnUrl) =>
|
||||
{
|
||||
var target = IsLocalPath(returnUrl) ? returnUrl! : "/";
|
||||
|
||||
if (!admin.Verify(password))
|
||||
{
|
||||
await Task.Delay(750, context.RequestAborted);
|
||||
return Results.LocalRedirect($"/login?error=1&returnUrl={Uri.EscapeDataString(target)}");
|
||||
}
|
||||
|
||||
var identity = new ClaimsIdentity([new Claim(ClaimTypes.Name, "admin")], CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
await context.SignInAsync(new ClaimsPrincipal(identity));
|
||||
return Results.LocalRedirect(target);
|
||||
}).AllowAnonymous();
|
||||
|
||||
app.MapPost("/auth/logout", async (HttpContext context, IAntiforgery antiforgery) =>
|
||||
{
|
||||
if (!await antiforgery.IsRequestValidAsync(context))
|
||||
{
|
||||
return Results.BadRequest();
|
||||
}
|
||||
|
||||
await context.SignOutAsync();
|
||||
return Results.LocalRedirect("/login");
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
private static bool IsLocalPath(string? url) =>
|
||||
!string.IsNullOrEmpty(url) && url[0] == '/' && (url.Length == 1 || (url[1] != '/' && url[1] != '\\'));
|
||||
}
|
||||
@@ -14,7 +14,6 @@
|
||||
|
||||
<body>
|
||||
<Routes />
|
||||
<script src="@Assets["_framework/blazor.web.js"]"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,5 @@
|
||||
@inherits LayoutComponentBase
|
||||
|
||||
<main class="page narrow">
|
||||
@Body
|
||||
</main>
|
||||
@@ -1,5 +1,13 @@
|
||||
@inherits LayoutComponentBase
|
||||
|
||||
<main class="page">
|
||||
<nav class="nav">
|
||||
<NavLink href="/" Match="NavLinkMatch.All">Dashboard</NavLink>
|
||||
<NavLink href="/settings">Einstellungen</NavLink>
|
||||
<form method="post" action="/auth/logout" class="logout">
|
||||
<AntiforgeryToken />
|
||||
<button type="submit" class="link">Abmelden</button>
|
||||
</form>
|
||||
</nav>
|
||||
@Body
|
||||
</main>
|
||||
@@ -0,0 +1,10 @@
|
||||
@if (Errors.TryGetValue(Field, out var message))
|
||||
{
|
||||
<p class="error">@message</p>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter, EditorRequired] public required IReadOnlyDictionary<string, string> Errors { get; set; }
|
||||
|
||||
[Parameter, EditorRequired] public required string Field { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
@page "/login"
|
||||
@layout EmptyLayout
|
||||
@attribute [AllowAnonymous]
|
||||
@using Flypaper.Web.Auth
|
||||
@using Microsoft.AspNetCore.Authorization
|
||||
@inject AdminAuthenticator Admin
|
||||
|
||||
<PageTitle>Flypaper · Anmelden</PageTitle>
|
||||
|
||||
<h1 class="login-title">Flypaper</h1>
|
||||
|
||||
@if (!Admin.IsConfigured)
|
||||
{
|
||||
<div class="banner bad">
|
||||
<strong>Zugang gesperrt.</strong>
|
||||
Es ist kein Admin-Passwort gesetzt. Setze die Umgebungsvariable <code>Flypaper__Admin__Password</code> und starte den Container neu.
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<form method="post" action="/auth/login" class="card form">
|
||||
<AntiforgeryToken />
|
||||
<input type="hidden" name="returnUrl" value="@ReturnUrl" />
|
||||
<label for="password">Passwort</label>
|
||||
<input id="password" name="password" type="password" autocomplete="current-password" autofocus required />
|
||||
@if (Error is not null)
|
||||
{
|
||||
<p class="error">Passwort falsch.</p>
|
||||
}
|
||||
<button type="submit">Anmelden</button>
|
||||
</form>
|
||||
}
|
||||
|
||||
@code {
|
||||
[SupplyParameterFromQuery(Name = "error")] private string? Error { get; set; }
|
||||
|
||||
[SupplyParameterFromQuery(Name = "returnUrl")] private string? ReturnUrl { get; set; }
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
@page "/not-found"
|
||||
@layout MainLayout
|
||||
@layout EmptyLayout
|
||||
@attribute [Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||
|
||||
<h3>Not Found</h3>
|
||||
<p>Sorry, the content you are looking for does not exist.</p>
|
||||
@@ -0,0 +1,157 @@
|
||||
@page "/settings"
|
||||
@using System.Globalization
|
||||
@using Flypaper.Core
|
||||
@using Flypaper.Core.Strategies
|
||||
@using Flypaper.Http
|
||||
@using Flypaper.Storage
|
||||
@using Flypaper.Web.Auth
|
||||
@using Microsoft.Data.Sqlite
|
||||
@using Microsoft.Extensions.Options
|
||||
@inject IOptionsMonitor<FlypaperOptions> Core
|
||||
@inject IOptionsMonitor<DripOptions> Drip
|
||||
@inject IOptionsMonitor<HttpTrapOptions> Http
|
||||
@inject IConfiguration Configuration
|
||||
@inject RuntimeSettingsStore Store
|
||||
@inject AdminAuthenticator Admin
|
||||
@inject NavigationManager Navigation
|
||||
|
||||
<PageTitle>Flypaper · Einstellungen</PageTitle>
|
||||
|
||||
<header class="top">
|
||||
<h1>Einstellungen</h1>
|
||||
@if (_hasOverrides)
|
||||
{
|
||||
<span class="badge">Überschrieben</span>
|
||||
}
|
||||
</header>
|
||||
|
||||
@if (Saved == "1")
|
||||
{
|
||||
<div class="banner ok">Gespeichert. Die Änderungen gelten sofort.</div>
|
||||
}
|
||||
else if (Saved == "reset")
|
||||
{
|
||||
<div class="banner ok">Auf Standard zurückgesetzt.</div>
|
||||
}
|
||||
|
||||
<EditForm Model="Model" FormName="settings" OnSubmit="Save" class="card form">
|
||||
<h2>Anpassbar</h2>
|
||||
|
||||
<label for="trap-paths">Fallenpfade <span class="muted">(einer pro Zeile, mit / am Anfang, * am Ende als Präfix)</span></label>
|
||||
<InputTextArea id="trap-paths" @bind-Value="Model!.TrapPaths" rows="10" class="mono" />
|
||||
<FieldError Errors="_errors" Field="@nameof(SettingsModel.TrapPaths)" />
|
||||
|
||||
<label for="hosts">Zusätzliche bekannte Hosts <span class="muted">(einer pro Zeile, z. B. beispiel.de oder *.beispiel.de)</span></label>
|
||||
<InputTextArea id="hosts" @bind-Value="Model!.ExtraKnownHosts" rows="4" class="mono" />
|
||||
<FieldError Errors="_errors" Field="@nameof(SettingsModel.ExtraKnownHosts)" />
|
||||
|
||||
<div class="grid">
|
||||
<div>
|
||||
<label for="drip">Tröpfel-Intervall <span class="muted">(hh:mm:ss)</span></label>
|
||||
<InputText id="drip" @bind-Value="Model!.DripInterval" class="mono" />
|
||||
<FieldError Errors="_errors" Field="@nameof(SettingsModel.DripInterval)" />
|
||||
</div>
|
||||
<div>
|
||||
<label for="hold">Maximale Haltedauer <span class="muted">(hh:mm:ss)</span></label>
|
||||
<InputText id="hold" @bind-Value="Model!.MaxHoldDuration" class="mono" />
|
||||
<FieldError Errors="_errors" Field="@nameof(SettingsModel.MaxHoldDuration)" />
|
||||
</div>
|
||||
<div>
|
||||
<label for="max">Max. Verbindungen gesamt</label>
|
||||
<InputText id="max" @bind-Value="Model!.MaxConnections" class="mono" />
|
||||
<FieldError Errors="_errors" Field="@nameof(SettingsModel.MaxConnections)" />
|
||||
</div>
|
||||
<div>
|
||||
<label for="per-ip">Max. Verbindungen pro IP</label>
|
||||
<InputText id="per-ip" @bind-Value="Model!.MaxConnectionsPerIp" class="mono" />
|
||||
<FieldError Errors="_errors" Field="@nameof(SettingsModel.MaxConnectionsPerIp)" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="actions">
|
||||
<button type="submit">Speichern</button>
|
||||
</div>
|
||||
</EditForm>
|
||||
|
||||
<EditForm Model="_resetModel" FormName="reset" OnSubmit="Reset" class="card form">
|
||||
<h2>Standard</h2>
|
||||
<p class="muted">Löscht alle gespeicherten Änderungen. Danach gelten wieder die Werte aus Umgebungsvariablen und appsettings.</p>
|
||||
<div class="actions">
|
||||
<button type="submit" class="secondary" disabled="@(!_hasOverrides)" onclick="return confirm('Gespeicherte Einstellungen wirklich löschen?')">Auf Standard zurücksetzen</button>
|
||||
</div>
|
||||
</EditForm>
|
||||
|
||||
<section class="card">
|
||||
<h2>Nur per Umgebungsvariable</h2>
|
||||
<p class="muted">Gelten ab Containerstart und lassen sich hier nicht ändern.</p>
|
||||
<table>
|
||||
<tbody>
|
||||
<tr><th>Datenbank</th><td class="mono">@DatabasePath</td></tr>
|
||||
<tr><th>Bekannte Hosts</th><td class="mono">@Format(Configuration.GetSection("Flypaper:Http:KnownHosts").Get<string[]>())</td></tr>
|
||||
<tr><th>Vertrauenswürdige Proxies</th><td class="mono">@Format(Configuration.GetSection("Flypaper:Http:TrustedProxies").Get<string[]>())</td></tr>
|
||||
<tr><th>Admin-Passwort</th><td>@(Admin.IsConfigured ? "gesetzt" : "nicht gesetzt")</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
|
||||
@code {
|
||||
private readonly object _resetModel = new();
|
||||
private IReadOnlyDictionary<string, string> _errors = new Dictionary<string, string>();
|
||||
private bool _hasOverrides;
|
||||
|
||||
[SupplyParameterFromForm(FormName = "settings")] private SettingsModel? Model { get; set; }
|
||||
|
||||
[SupplyParameterFromQuery(Name = "saved")] private string? Saved { get; set; }
|
||||
|
||||
private string DatabasePath =>
|
||||
new SqliteConnectionStringBuilder(Configuration.GetConnectionString("Flypaper") ?? "Data Source=flypaper.db").DataSource;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
Model ??= CurrentValues();
|
||||
_hasOverrides = await Store.HasOverridesAsync();
|
||||
}
|
||||
|
||||
private async Task Save()
|
||||
{
|
||||
var result = RuntimeSettingsParser.Parse(new RuntimeSettingsInput(
|
||||
Model!.TrapPaths,
|
||||
Model.DripInterval,
|
||||
Model.MaxConnections,
|
||||
Model.MaxConnectionsPerIp,
|
||||
Model.MaxHoldDuration,
|
||||
Model.ExtraKnownHosts));
|
||||
|
||||
if (result.Settings is null)
|
||||
{
|
||||
_errors = result.Errors;
|
||||
return;
|
||||
}
|
||||
|
||||
await Store.SaveAsync(result.Settings);
|
||||
Navigation.NavigateTo("/settings?saved=1");
|
||||
}
|
||||
|
||||
private async Task Reset()
|
||||
{
|
||||
await Store.ResetAsync();
|
||||
Navigation.NavigateTo("/settings?saved=reset");
|
||||
}
|
||||
|
||||
private SettingsModel CurrentValues()
|
||||
{
|
||||
var core = Core.CurrentValue;
|
||||
var http = Http.CurrentValue;
|
||||
return new SettingsModel
|
||||
{
|
||||
TrapPaths = SettingsList.Join(http.TrapPaths),
|
||||
ExtraKnownHosts = SettingsList.Join(SettingsList.Split(http.ExtraKnownHosts)),
|
||||
DripInterval = Drip.CurrentValue.Interval.ToString("c", CultureInfo.InvariantCulture),
|
||||
MaxHoldDuration = core.MaxHoldDuration.ToString("c", CultureInfo.InvariantCulture),
|
||||
MaxConnections = core.MaxConnections.ToString(CultureInfo.InvariantCulture),
|
||||
MaxConnectionsPerIp = core.MaxConnectionsPerIp.ToString(CultureInfo.InvariantCulture)
|
||||
};
|
||||
}
|
||||
|
||||
private static string Format(string[]? values) => values is { Length: > 0 } ? string.Join(", ", values) : "–";
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace Flypaper.Web.Components.Pages;
|
||||
|
||||
public sealed class SettingsModel
|
||||
{
|
||||
public string? TrapPaths { get; set; }
|
||||
|
||||
public string? DripInterval { get; set; }
|
||||
|
||||
public string? MaxConnections { get; set; }
|
||||
|
||||
public string? MaxConnectionsPerIp { get; set; }
|
||||
|
||||
public string? MaxHoldDuration { get; set; }
|
||||
|
||||
public string? ExtraKnownHosts { get; set; }
|
||||
}
|
||||
@@ -1,16 +1,36 @@
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Http;
|
||||
using Flypaper.Storage;
|
||||
using Flypaper.Web.Auth;
|
||||
using Flypaper.Web.Components;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.WebHost.ConfigureKestrel(options => options.AddServerHeader = false);
|
||||
|
||||
builder.AddFlypaperStorage();
|
||||
builder.Services.AddRazorComponents();
|
||||
builder.Services.AddFlypaperCore(builder.Configuration);
|
||||
builder.Services.AddFlypaperHttp(builder.Configuration);
|
||||
builder.Services.AddFlypaperStorage(builder.Configuration.GetConnectionString("Flypaper") ?? "Data Source=flypaper.db");
|
||||
|
||||
builder.Services.Configure<AdminOptions>(builder.Configuration.GetSection(AdminOptions.SectionName));
|
||||
builder.Services.AddSingleton<AdminAuthenticator>();
|
||||
builder.Services
|
||||
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
||||
.AddCookie(options =>
|
||||
{
|
||||
options.LoginPath = "/login";
|
||||
options.AccessDeniedPath = "/login";
|
||||
options.Cookie.Name = "flypaper.auth";
|
||||
options.Cookie.HttpOnly = true;
|
||||
options.Cookie.SameSite = SameSiteMode.Strict;
|
||||
options.ExpireTimeSpan = TimeSpan.FromHours(8);
|
||||
options.SlidingExpiration = true;
|
||||
});
|
||||
builder.Services.AddAuthorizationBuilder()
|
||||
.SetFallbackPolicy(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build());
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -24,9 +44,12 @@ if (!app.Environment.IsDevelopment())
|
||||
}
|
||||
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.UseAntiforgery();
|
||||
|
||||
app.MapStaticAssets();
|
||||
app.MapStaticAssets().AllowAnonymous();
|
||||
app.MapAuthEndpoints();
|
||||
app.MapRazorComponents<App>();
|
||||
|
||||
await app.RunAsync();
|
||||
@@ -10,7 +10,12 @@
|
||||
"Interval": "00:00:01"
|
||||
},
|
||||
"Http": {
|
||||
"KnownHosts": [ "localhost" ]
|
||||
"KnownHosts": [
|
||||
"localhost"
|
||||
]
|
||||
},
|
||||
"Admin": {
|
||||
"Password": "dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,3 +183,160 @@ tbody tr:last-child td {
|
||||
max-width: 180px;
|
||||
}
|
||||
}
|
||||
|
||||
.narrow {
|
||||
max-width: 380px;
|
||||
padding-top: 15vh;
|
||||
}
|
||||
|
||||
.login-title {
|
||||
margin: 0 0 16px;
|
||||
font-size: 22px;
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.nav {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 16px;
|
||||
margin-bottom: 20px;
|
||||
padding-bottom: 12px;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.nav a {
|
||||
color: var(--muted);
|
||||
text-decoration: none;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.nav a:hover,
|
||||
.nav a.active {
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.nav .logout {
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.form label {
|
||||
margin-top: 8px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.form .grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
||||
gap: 0 16px;
|
||||
}
|
||||
|
||||
input,
|
||||
textarea {
|
||||
width: 100%;
|
||||
padding: 8px 10px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
font: inherit;
|
||||
}
|
||||
|
||||
input:focus,
|
||||
textarea:focus {
|
||||
outline: 2px solid var(--accent);
|
||||
outline-offset: -1px;
|
||||
}
|
||||
|
||||
textarea {
|
||||
resize: vertical;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 8px 16px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
font: inherit;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button.secondary {
|
||||
background: transparent;
|
||||
border: 1px solid var(--border);
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
button.link {
|
||||
padding: 0;
|
||||
background: none;
|
||||
color: var(--muted);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
button.link:hover {
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.45;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.actions {
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.error {
|
||||
margin: 0;
|
||||
color: var(--bad);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.banner {
|
||||
margin-bottom: 16px;
|
||||
padding: 12px 16px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.banner.ok {
|
||||
border-color: var(--ok);
|
||||
color: var(--ok);
|
||||
}
|
||||
|
||||
.banner.bad {
|
||||
border-color: var(--bad);
|
||||
color: var(--bad);
|
||||
}
|
||||
|
||||
.badge {
|
||||
padding: 2px 8px;
|
||||
border: 1px solid var(--accent);
|
||||
border-radius: 999px;
|
||||
color: var(--accent);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
th[scope],
|
||||
tbody th {
|
||||
width: 220px;
|
||||
color: var(--muted);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.card {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.top h1 {
|
||||
margin: 0;
|
||||
}
|
||||
Reference in new issue
Block a user