backend: Enforce notification access and clear push devices

This commit is contained in:
GonnaHetzMe committed 2026-12-04 21:42:00 +01:00
1 parent ee5f1d3d10
commit 4f95c8a379
12 files changed
+145 -46

No files matched your search

@@ -58,7 +58,7 @@ public sealed class AuthService(
await using var transaction = await db.BeginWriteAsync(ct);
var user = await ConsumeEmailTokenAsync(request.Token, TokenType.EmailConfirm, ct);
var revoked = await revocation.StageRevokeAllAsync([user.Id], RevokeReason.PasswordChanged, ct);
var revoked = await StageDisconnectAsync(user.Id, RevokeReason.PasswordChanged, ct);
user.ChangePasswordHash(passwordHasher.Hash(request.NewPassword));
user.ConfirmEmail();
await db.SaveChangesAsync(ct);
@@ -128,7 +128,7 @@ public sealed class AuthService(
return response;
}
var revoked = await revocation.StageRevokeAllAsync([user.Id], RevokeReason.ReuseDetected, ct);
var revoked = await StageDisconnectAsync(user.Id, RevokeReason.ReuseDetected, ct);
await transaction.CommitAsync(ct);
revocation.Publish(revoked);
throw new UnauthorizedException("Refresh token už byl použit. Přihlaste se znovu.");
@@ -146,8 +146,15 @@ public sealed class AuthService(
await revocation.RevokeAsync(currentUser.Id, jtis, RevokeReason.Logout, ct);
}
public Task LogoutAllAsync(ICurrentUser currentUser, CancellationToken ct) =>
revocation.RevokeAllAsync(currentUser.Id, RevokeReason.LogoutAll, ct);
public async Task LogoutAllAsync(ICurrentUser currentUser, CancellationToken ct)
{
var userId = currentUser.Id;
await using var transaction = await db.BeginWriteAsync(ct);
await db.LockUserAsync(userId, ct);
var revoked = await StageDisconnectAsync(userId, RevokeReason.LogoutAll, ct);
await transaction.CommitAsync(ct);
revocation.Publish(revoked);
}
/// <summary>Vždy uspěje, i pro neexistující e-mail – odpověď neprozradí, zda účet existuje.</summary>
public async Task ForgotPasswordAsync(ForgotPasswordRequest request, CancellationToken ct)
@@ -173,13 +180,22 @@ public sealed class AuthService(
await using var transaction = await db.BeginWriteAsync(ct);
var user = await ConsumeEmailTokenAsync(request.Token, TokenType.PasswordReset, ct);
var revoked = await revocation.StageRevokeAllAsync([user.Id], RevokeReason.PasswordChanged, ct);
var revoked = await StageDisconnectAsync(user.Id, RevokeReason.PasswordChanged, ct);
user.ChangePasswordHash(passwordHasher.Hash(request.NewPassword));
await db.SaveChangesAsync(ct);
await transaction.CommitAsync(ct);
revocation.Publish(revoked);
}
// Volat v transakci se zámkem účtu, který používá i registrace zařízení.
private async Task<List<TokenRevocationService.RevokedAccessToken>> StageDisconnectAsync(
int userId, RevokeReason reason, CancellationToken ct)
{
var revoked = await revocation.StageRevokeAllAsync([userId], reason, ct);
await db.PushDevices.Where(device => device.UserId == userId).ExecuteDeleteAsync(ct);
return revoked;
}
private async Task<AuthResponse> IssueTokenPairAsync(User user, CancellationToken ct)
{
var access = tokens.Issue(user, TokenType.Access);
@@ -34,10 +34,6 @@ public sealed class TokenRevocationService(IAppDbContext db, IRevokedTokenStore
Publish(await LoadRevokedAccessTokensAsync([userId], now, ct));
}
/// <summary>Zneplatní všechny platné tokeny uživatele – jeden UPDATE (kap. 10.1).</summary>
public async Task RevokeAllAsync(int userId, RevokeReason reason, CancellationToken ct) =>
Publish(await StageRevokeAllAsync([userId], reason, ct));
internal async Task<List<RevokedAccessToken>> StageRevokeAllAsync(int[] userIds, RevokeReason reason, CancellationToken ct)
{
var now = Now();