Improvements #30
No files matched your search
@@ -30,7 +30,7 @@ public interface IAppDbContext
|
||||
DbSet<PushDevice> PushDevices { get; }
|
||||
|
||||
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
|
||||
Task<IDbContextTransaction> BeginProposalWriteAsync(CancellationToken ct);
|
||||
Task<IDbContextTransaction> BeginWriteAsync(CancellationToken ct);
|
||||
Task<Proposal?> LockProposalAsync(int id, CancellationToken ct);
|
||||
Task RegisterPushDeviceAsync(int userId, string token, CancellationToken ct);
|
||||
}
|
||||
|
||||
@@ -95,17 +95,23 @@ public sealed class AuthService(
|
||||
var token = await tokens.ValidateAsync(request.RefreshToken, TokenType.Refresh)
|
||||
?? throw new UnauthorizedException("Neplatný refresh token.");
|
||||
|
||||
if (!await revocation.TryConsumeAsync(token.Jti, TokenType.Refresh, ct))
|
||||
// Původní token zůstane zamčený, dokud není nový pár uložený.
|
||||
await using (var transaction = await db.BeginWriteAsync(ct))
|
||||
{
|
||||
await revocation.RevokeAllAsync(token.UserId, RevokeReason.ReuseDetected, ct);
|
||||
throw new UnauthorizedException("Refresh token už byl použit. Přihlaste se znovu.");
|
||||
if (await revocation.TryConsumeAsync(token.Jti, TokenType.Refresh, ct))
|
||||
{
|
||||
var user = await db.Users.SingleOrDefaultAsync(u => u.Id == token.UserId, ct);
|
||||
if (user is null || !user.IsActive)
|
||||
throw new UnauthorizedException("Účet neexistuje nebo je deaktivován.");
|
||||
|
||||
var response = await IssueTokenPairAsync(user, ct);
|
||||
await transaction.CommitAsync(ct);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
var user = await db.Users.SingleOrDefaultAsync(u => u.Id == token.UserId, ct);
|
||||
if (user is null || !user.IsActive)
|
||||
throw new UnauthorizedException("Účet neexistuje nebo je deaktivován.");
|
||||
|
||||
return await IssueTokenPairAsync(user, ct);
|
||||
await revocation.RevokeAllAsync(token.UserId, RevokeReason.ReuseDetected, ct);
|
||||
throw new UnauthorizedException("Refresh token už byl použit. Přihlaste se znovu.");
|
||||
}
|
||||
|
||||
/// <summary>Odhlášení jednoho zařízení – zneplatní aktuální access token a k němu předaný refresh token.</summary>
|
||||
|
||||
@@ -30,7 +30,7 @@ public sealed partial class ProposalService(
|
||||
{
|
||||
await GetActorRoleAsync(ct);
|
||||
var (title, description) = ValidateDetails(request);
|
||||
await using (var transaction = await db.BeginProposalWriteAsync(ct))
|
||||
await using (var transaction = await db.BeginWriteAsync(ct))
|
||||
{
|
||||
var proposal = await LockProposalAsync(id, ct);
|
||||
if (!proposal.IsAuthor(currentUser.Id))
|
||||
@@ -63,7 +63,7 @@ public sealed partial class ProposalService(
|
||||
if (note is not null && Encoding.UTF8.GetByteCount(note) > MaxTextBytes)
|
||||
throw new BadRequestException("Vyjádření je příliš dlouhé.");
|
||||
|
||||
await using (var transaction = await db.BeginProposalWriteAsync(ct))
|
||||
await using (var transaction = await db.BeginWriteAsync(ct))
|
||||
{
|
||||
var proposal = await LockProposalAsync(id, ct);
|
||||
var previous = proposal.Status;
|
||||
@@ -94,7 +94,7 @@ public sealed partial class ProposalService(
|
||||
{
|
||||
await GetActorRoleAsync(ct);
|
||||
var actorId = currentUser.Id;
|
||||
await using var transaction = await db.BeginProposalWriteAsync(ct);
|
||||
await using var transaction = await db.BeginWriteAsync(ct);
|
||||
var proposal = await LockProposalAsync(id, ct);
|
||||
if (proposal.IsAuthor(actorId))
|
||||
throw new ForbiddenException("Autor nemůže hlasovat o vlastním návrhu.");
|
||||
|
||||
@@ -54,7 +54,7 @@ public sealed class HelpdeskDbContext(DbContextOptions<HelpdeskDbContext> option
|
||||
}
|
||||
}
|
||||
|
||||
public Task<IDbContextTransaction> BeginProposalWriteAsync(CancellationToken ct) =>
|
||||
public Task<IDbContextTransaction> BeginWriteAsync(CancellationToken ct) =>
|
||||
Database.BeginTransactionAsync(IsolationLevel.ReadCommitted, ct);
|
||||
|
||||
public async Task<Proposal?> LockProposalAsync(int id, CancellationToken ct)
|
||||
|
||||
@@ -615,7 +615,7 @@ SET revoked_at = UTC_TIMESTAMP(6), revoke_reason = 'USED'
|
||||
WHERE jti = ? AND token_type = 'REFRESH' AND revoked_at IS NULL;
|
||||
```
|
||||
|
||||
- **Změněn 1 řádek** → token se používá poprvé, server vydá nový pár tokenů.
|
||||
- **Změněn 1 řádek** → token se používá poprvé, server uloží nový pár tokenů ve stejné databázové transakci.
|
||||
- **Změněno 0 řádků** → token už byl použitý nebo zneplatněný. U refresh tokenu to znamená, že ho zneužil někdo jiný. Server zneplatní všechny tokeny uživatele (`REUSE_DETECTED`) a vrátí 401.
|
||||
|
||||
Ze dvou současných požadavků se stejným tokenem vždy uspěje jen jeden. Klienti (mobil i web) proto musí obnovu tokenů provádět vždy jen jednou současně. Jinak by souběžné obnovy vyhodnotil server jako krádež.
|
||||
|
||||
Reference in new issue
Block a user