backend: Make refresh token rotation atomic
This commit is contained in:
1 parent
483bb10fbd
commit
fd37986f5e
5 files changed
+20
-14
No files matched your search
@@ -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>
|
||||
|
||||
Reference in new issue
Block a user