Files
LehrerApp/LehrerApp.Sync/Models/SyncModels.cs
T
adminandClaude Sonnet 5 dc21cb2319 fix: Geräte-Pairing - Schlüssel-Code stimmte nie mit dem angezeigten Code überein
SnapshotService.CreateAndUploadAsync lädt in zwei Schritten hoch: Schritt 1
holt einen Code vom Server, Schritt 2 verschlüsselt den Sync-Schlüssel mit
diesem Code und lädt erneut hoch. SnapshotStore.Store() vergab bei jedem
Aufruf bedingungslos einen neuen Zufallscode - der dem Nutzer am Ende
angezeigte Code war dadurch nie derselbe, mit dem der Schlüssel tatsächlich
verschlüsselt wurde. Jede Kopplung musste deterministisch an der
Schlüssel-Entschlüsselung scheitern.

SnapshotUploadRequest bekommt ein optionales Code-Feld; Store() aktualisiert
bei vorhandenem, passendem Code denselben Eintrag statt einen neuen mit
neuem Code anzulegen. Betrifft LehrerApp.Api - der Server muss neu deployt
werden.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-18 21:34:43 +02:00

106 lines
4.6 KiB
C#

using LiteDB;
namespace LehrerApp.Sync.Models;
// ── Events ────────────────────────────────────────────────────────────────────
/// <summary>Desktop-Event: Payload ist AES-256-GCM verschlüsselt.</summary>
public class SyncEvent
{
// LiteDB erkennt nur eine Property namens "Id" automatisch als Primärschlüssel.
// Ohne [BsonId] würde ein eigener, von EventId unabhängiger _id vergeben, wodurch
// EventQueue.Acknowledge()/_queue.Delete(EventId) niemals etwas löschen würde.
[BsonId]
public Guid EventId { get; init; } = Guid.NewGuid();
public string DeviceId { get; init; } = "";
public DeviceType DeviceType { get; init; }
public DateTime Timestamp { get; init; } = DateTime.UtcNow;
public long SequenceNr { get; init; }
public string EntityType { get; init; } = "";
public string EntityId { get; init; } = "";
public string Operation { get; init; } = "";
/// <summary>Verschlüsselt (Desktop) oder Klartext (Companion/WebApp).</summary>
public string Payload { get; init; } = "";
}
/// <summary>WebApp/Companion-Event: Payload ist Klartext-JSON.</summary>
public class PlainSyncEvent
{
public Guid EventId { get; init; } = Guid.NewGuid();
public string DeviceId { get; init; } = "";
public DeviceType DeviceType { get; init; } = DeviceType.Companion;
public DateTime Timestamp { get; init; } = DateTime.UtcNow;
public string EntityType { get; init; } = "";
public string EntityId { get; init; } = "";
public string Operation { get; init; } = "";
public string Payload { get; init; } = "";
}
// ── Sync API Responses ────────────────────────────────────────────────────────
public class PushResponse
{
public bool Success { get; init; }
public long ServerSequenceNr { get; init; }
public List<Guid> ConflictingEventIds { get; init; } = [];
}
public class PullResponse
{
public List<SyncEvent> Events { get; init; } = [];
public long ServerSequenceNr { get; init; }
}
public class PlainPushResponse
{
public bool Success { get; init; }
public long ServerSequenceNr { get; init; }
public List<Guid> RejectedEventIds { get; init; } = [];
}
// ── Snapshot Modelle ──────────────────────────────────────────────────────────
/// <summary>
/// Upload-Request für Device-Pairing.
/// EncryptedPayload: LiteDB verschlüsselt mit Sync-Key.
/// EncryptedSyncKey: Sync-Key verschlüsselt mit PBKDF2(Code).
/// → Empfänger braucht nur den Code um beides zu entschlüsseln.
/// </summary>
public class SnapshotUploadRequest
{
public string EncryptedPayload { get; init; } = "";
public string EncryptedSyncKey { get; init; } = "";
public DeviceType DeviceType { get; init; }
/// Gesetzt beim zweiten Upload-Schritt (Schlüssel nachreichen, siehe SnapshotService.
/// CreateAndUploadAsync) — muss dem im ersten Schritt vom Server vergebenen Code entsprechen,
/// damit derselbe Eintrag aktualisiert statt ein neuer (mit neuem Code) angelegt wird.
public string? Code { get; init; }
}
public class SnapshotUploadResponse
{
/// <summary>Format: WORT-ZZ-WORT, z.B. "TIGER-42-BLAU". 24h gültig, einmalig.</summary>
public string Code { get; init; } = "";
public DateTime ExpiresAt { get; init; }
}
public class SnapshotDownloadResponse
{
public string EncryptedPayload { get; init; } = "";
public string EncryptedSyncKey { get; init; } = "";
public DateTime CreatedAt { get; init; }
public DeviceType SourceDeviceType { get; init; }
}
// ── Status ────────────────────────────────────────────────────────────────────
public class SyncStatus
{
public SyncState State { get; set; } = SyncState.Idle;
public DateTime? LastSyncAt { get; set; }
public int PendingEvents { get; set; }
public int ConflictCount { get; set; }
public string? ErrorMessage { get; set; }
}
// ── Enums ─────────────────────────────────────────────────────────────────────
public enum DeviceType { Desktop, Companion }
public enum SyncState { Idle, Syncing, Error, Offline }