feat: sync guard - Blockieren alter Clients beim sync

This commit is contained in:
2026-08-20 23:54:04 +02:00
parent 7b883555bf
commit 59bd8abb45
19 changed files with 369 additions and 26 deletions
@@ -1,11 +1,12 @@
using System.Net;
using System.Net.Http.Json;
using LehrerApp.Sync;
namespace LehrerApp.Desktop.Services;
public class SyncAuthException(string userMessage) : Exception(userMessage);
public enum SyncConnectionTestResult { Ok, Unauthorized, Unreachable }
public enum SyncConnectionTestResult { Ok, Unauthorized, IncompatibleVersion, Unreachable }
/// <summary>
/// Login/Verbindungstest gegen den eigenen Sync-Server (LehrerApp.Api) — getrennt vom bereits
@@ -27,7 +28,10 @@ public class SyncAuthService(HttpClient http)
HttpResponseMessage resp;
try
{
resp = await http.PostAsJsonAsync(CombineUrl(serverUrl, "/api/auth/login"), new { username, password });
using var req = SyncProtocol.CreateRequest(HttpMethod.Post,
CombineUrl(serverUrl, "/api/auth/login"));
req.Content = JsonContent.Create(new { username, password });
resp = await http.SendAsync(req);
}
catch (HttpRequestException)
{
@@ -49,6 +53,7 @@ public class SyncAuthService(HttpClient http)
public async Task<SyncConnectionTestResult> TestConnectionAsync(string serverUrl, string? token)
{
using var req = new HttpRequestMessage(HttpMethod.Get, CombineUrl(serverUrl, "/api/sync/status"));
req.Headers.Add(SyncProtocol.VersionHeaderName, SyncProtocol.CurrentVersion);
if (token is not null)
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
@@ -56,6 +61,8 @@ public class SyncAuthService(HttpClient http)
{
var resp = await http.SendAsync(req);
if (resp.StatusCode == HttpStatusCode.Unauthorized) return SyncConnectionTestResult.Unauthorized;
if (resp.StatusCode == HttpStatusCode.UpgradeRequired)
return SyncConnectionTestResult.IncompatibleVersion;
return resp.IsSuccessStatusCode ? SyncConnectionTestResult.Ok : SyncConnectionTestResult.Unreachable;
}
catch (HttpRequestException) { return SyncConnectionTestResult.Unreachable; }