109 lines
3.9 KiB
C#
109 lines
3.9 KiB
C#
// Importieren des Entity Frameworks für den Zugriff auf die DB
|
|
using Microsoft.EntityFrameworkCore;
|
|
// Importieren der Klassen
|
|
using SecDevOpsLab.Data;
|
|
using SecDevOpsLab.Components;
|
|
// NEU: Namespaces für Authentifizierung und Routing hinzufügen
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.AspNetCore.Components.Server;
|
|
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
// SECDEVOPS: Namespaces für Data Protection hinzufügen
|
|
using System.IO;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
// Initialisieren des Web Builders, der den Server und die Umgebung vorbereitet
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// ==========================================
|
|
// SECDEVOPS: Data Protection für K8s PVC & Key-Rotation
|
|
// ==========================================
|
|
var keysFolder = new DirectoryInfo(@"/app/data/dp-keys");
|
|
|
|
builder.Services.AddDataProtection()
|
|
.PersistKeysToFileSystem(keysFolder)
|
|
.SetApplicationName("SecDevOpsLabApp"); // Bindet die Keys fix an diese App (Wichtig für Replicas)
|
|
|
|
// NEU: Blazor-Authentifizierungsdienste registrieren
|
|
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
|
.AddCookie(options =>
|
|
{
|
|
options.LoginPath = "/login"; // Wohin unberechtigte Nutzer geleitet werden
|
|
options.ExpireTimeSpan = TimeSpan.FromHours(2);
|
|
});
|
|
|
|
// NEU: Erlaubt Blazor-Komponenten, den Login-Status abzufragen
|
|
builder.Services.AddCascadingAuthenticationState();
|
|
builder.Services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
|
|
|
|
// Hinzufügen des Blazor Service und die Interagierbarkeit des Frontends
|
|
builder.Services.AddRazorComponents()
|
|
.AddInteractiveServerComponents();
|
|
|
|
// Registrieren des Datenbankzugriffs (Sqlite)
|
|
builder.Services.AddDbContext<AppDbContext>(options =>
|
|
options.UseSqlite("Data Source=data/books.db"));
|
|
|
|
// Bauen des Webservers, der App
|
|
var app = builder.Build();
|
|
|
|
// Erstellen eines temporären Scopes
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
// Holen des DB Services
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
|
|
// Erstellen der books.db automatisch sofern nicht bereits vorhanden
|
|
db.Database.EnsureCreated();
|
|
}
|
|
|
|
// Hinzufügen einer Fehlerbehandlung -> Fehler wird auf der Website angezeigt
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Error", createScopeForErrors: true);
|
|
}
|
|
|
|
// Erlauben des Zugriffs auf wwwroot Dir
|
|
app.UseStaticFiles();
|
|
|
|
// NEU: Authentifizierungs-Middleware aktivieren (MUSS vor Antiforgery stehen!)
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
// Schützen vor CSRF Attacken
|
|
app.UseAntiforgery();
|
|
|
|
// Verknüpfen der Razor Seiten mit den entsprechenden URLs
|
|
app.MapRazorComponents<App>()
|
|
.AddInteractiveServerRenderMode();
|
|
|
|
// ==========================================
|
|
// SCHRITT 3: HIER EINGEFÜGT (Login-Endpunkt)
|
|
// ==========================================
|
|
app.MapPost("/api/auth/login", async (
|
|
[FromForm] string username,
|
|
[FromForm] string password,
|
|
HttpContext httpContext) =>
|
|
{
|
|
// Statischer User (Für SecDevOps später via Environment Variable aus K8s-Secret laden!)
|
|
const string StaticUser = "admin";
|
|
const string StaticPassword = "DevOpsPassword2026!";
|
|
|
|
if (username == StaticUser && password == StaticPassword)
|
|
{
|
|
var claims = new List<Claim> { new Claim(ClaimTypes.Name, username) };
|
|
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
await httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity));
|
|
|
|
return Results.Redirect("/"); // Erfolgreich eingeloggt -> zur Startseite
|
|
}
|
|
|
|
return Results.Redirect("/login?error=true"); // Fehler -> zurück zum Login
|
|
});
|
|
|
|
// Webserver wird gestartet. Port 8080 wird eröffnet
|
|
app.Run();
|