Baustein 1: Server-Auth-Fix (Kapitel 10)
/api/auth/login und /api/auth/register akzeptierten zuvor jeden beliebigen Nutzernamen/Passwort und stellten ein gueltiges 30-Tage-JWT aus - konkrete, ausnutzbare Luecke bei echtem Deployment. - PasswordHasher (PBKDF2, Salt pro Nutzer) + UserStore (LiteDB) statt des ungeprueften Stubs - /api/auth/register ersatzlos entfernt (kein offener Registrierungs-Endpunkt fuer ein Einzel-/Familien-Deployment) - Neue Nutzer per CLI (dotnet LehrerApp.Api.dll create-user <name>), dokumentiert in docker/README.md - Neues Testprojekt LehrerApp.Api.Tests (bisher als einziges Projekt ohne Tests) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
using LiteDB;
|
||||
|
||||
namespace LehrerApp.Api;
|
||||
|
||||
public class UserStore(string dataPath) : IDisposable
|
||||
{
|
||||
private readonly LiteDatabase _db = new(Path.Combine(dataPath, "users.db"));
|
||||
|
||||
private ILiteCollection<UserEntry> Col
|
||||
{
|
||||
get
|
||||
{
|
||||
var col = _db.GetCollection<UserEntry>("users");
|
||||
col.EnsureIndex(x => x.Username, unique: true);
|
||||
return col;
|
||||
}
|
||||
}
|
||||
|
||||
public bool CreateUser(string username, string password)
|
||||
{
|
||||
if (Col.Exists(x => x.Username == username)) return false;
|
||||
Col.Insert(new UserEntry { Username = username, PasswordHash = PasswordHasher.Hash(password) });
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool VerifyPassword(string username, string password)
|
||||
{
|
||||
var user = Col.FindOne(x => x.Username == username);
|
||||
return user is not null && PasswordHasher.Verify(password, user.PasswordHash);
|
||||
}
|
||||
|
||||
public void Dispose() => _db.Dispose();
|
||||
}
|
||||
|
||||
internal class UserEntry
|
||||
{
|
||||
public ObjectId Id { get; set; } = ObjectId.NewObjectId();
|
||||
public string Username { get; set; } = "";
|
||||
public string PasswordHash { get; set; } = "";
|
||||
}
|
||||
Reference in New Issue
Block a user