backend: Make refresh token rotation atomic
This commit is contained in:
1 parent
483bb10fbd
commit
fd37986f5e
5 files changed
+18
-12
No files matched your search
@@ -30,7 +30,7 @@ public interface IAppDbContext
|
|||||||
DbSet<PushDevice> PushDevices { get; }
|
DbSet<PushDevice> PushDevices { get; }
|
||||||
|
|
||||||
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
|
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
|
||||||
Task<IDbContextTransaction> BeginProposalWriteAsync(CancellationToken ct);
|
Task<IDbContextTransaction> BeginWriteAsync(CancellationToken ct);
|
||||||
Task<Proposal?> LockProposalAsync(int id, CancellationToken ct);
|
Task<Proposal?> LockProposalAsync(int id, CancellationToken ct);
|
||||||
Task RegisterPushDeviceAsync(int userId, string token, 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)
|
var token = await tokens.ValidateAsync(request.RefreshToken, TokenType.Refresh)
|
||||||
?? throw new UnauthorizedException("Neplatný refresh token.");
|
?? 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))
|
||||||
|
{
|
||||||
|
if (await revocation.TryConsumeAsync(token.Jti, TokenType.Refresh, ct))
|
||||||
{
|
{
|
||||||
await revocation.RevokeAllAsync(token.UserId, RevokeReason.ReuseDetected, ct);
|
|
||||||
throw new UnauthorizedException("Refresh token už byl použit. Přihlaste se znovu.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var user = await db.Users.SingleOrDefaultAsync(u => u.Id == token.UserId, ct);
|
var user = await db.Users.SingleOrDefaultAsync(u => u.Id == token.UserId, ct);
|
||||||
if (user is null || !user.IsActive)
|
if (user is null || !user.IsActive)
|
||||||
throw new UnauthorizedException("Účet neexistuje nebo je deaktivován.");
|
throw new UnauthorizedException("Účet neexistuje nebo je deaktivován.");
|
||||||
|
|
||||||
return await IssueTokenPairAsync(user, ct);
|
var response = await IssueTokenPairAsync(user, ct);
|
||||||
|
await transaction.CommitAsync(ct);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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>
|
/// <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);
|
await GetActorRoleAsync(ct);
|
||||||
var (title, description) = ValidateDetails(request);
|
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);
|
var proposal = await LockProposalAsync(id, ct);
|
||||||
if (!proposal.IsAuthor(currentUser.Id))
|
if (!proposal.IsAuthor(currentUser.Id))
|
||||||
@@ -63,7 +63,7 @@ public sealed partial class ProposalService(
|
|||||||
if (note is not null && Encoding.UTF8.GetByteCount(note) > MaxTextBytes)
|
if (note is not null && Encoding.UTF8.GetByteCount(note) > MaxTextBytes)
|
||||||
throw new BadRequestException("Vyjádření je příliš dlouhé.");
|
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 proposal = await LockProposalAsync(id, ct);
|
||||||
var previous = proposal.Status;
|
var previous = proposal.Status;
|
||||||
@@ -94,7 +94,7 @@ public sealed partial class ProposalService(
|
|||||||
{
|
{
|
||||||
await GetActorRoleAsync(ct);
|
await GetActorRoleAsync(ct);
|
||||||
var actorId = currentUser.Id;
|
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);
|
var proposal = await LockProposalAsync(id, ct);
|
||||||
if (proposal.IsAuthor(actorId))
|
if (proposal.IsAuthor(actorId))
|
||||||
throw new ForbiddenException("Autor nemůže hlasovat o vlastním návrhu.");
|
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);
|
Database.BeginTransactionAsync(IsolationLevel.ReadCommitted, ct);
|
||||||
|
|
||||||
public async Task<Proposal?> LockProposalAsync(int id, CancellationToken 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;
|
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.
|
- **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ž.
|
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