Class PasswordResetTokenService
Outstanding "forgot my password" tokens. Raw SQL for writes and compiled queries for reads, following the existing service pattern.
public sealed class PasswordResetTokenService : BaseService<PasswordResetTokenEntity>, IPasswordResetTokenService
- Inheritance
-
PasswordResetTokenService
- Implements
- Inherited Members
Remarks
This layer only ever sees hashes. It also never inspects, and must never be extended to touch, anything two-factor: a reset replaces SRP credentials and nothing else.
Constructors
PasswordResetTokenService(INpgsqlDbContextFactory)
public PasswordResetTokenService(INpgsqlDbContextFactory dbContextFactory)
Parameters
dbContextFactoryINpgsqlDbContextFactory
Methods
CleanupExpiredAsync(DateTime, CancellationToken)
Deletes tokens that expired before the cutoff. Returns how many rows went.
public Task<DatabaseResult<int>> CleanupExpiredAsync(DateTime cutoffUtc, CancellationToken cancellationToken = default)
Parameters
cutoffUtcDateTimecancellationTokenCancellationToken
Returns
FetchByHashAsync(string, CancellationToken)
Fetches a token row by hash, whatever its state.
public Task<DatabaseResult<PasswordResetTokenData>> FetchByHashAsync(string tokenHash, CancellationToken cancellationToken = default)
Parameters
tokenHashstringcancellationTokenCancellationToken
Returns
Remarks
Returns the row for an expired or already-used token too; the caller applies those checks. Read-only — use RedeemAsync(string, CancellationToken) to actually spend a token, because only that path marks it used atomically.
HasRecentForAccountAsync(string, DateTime, CancellationToken)
Whether the account has been issued a token since sinceUtc.
public Task<DatabaseResult<bool>> HasRecentForAccountAsync(string accountName, DateTime sinceUtc, CancellationToken cancellationToken = default)
Parameters
accountNamestringsinceUtcDateTimecancellationTokenCancellationToken
Returns
Remarks
The resend cooldown. It exists so the request endpoint cannot be used to flood somebody's inbox, and it must never change what the endpoint answers.
InvalidateAllForAccountAsync(string, string?, CancellationToken)
Marks every outstanding token for an account used, optionally sparing one.
public Task<DatabaseResult<int>> InvalidateAllForAccountAsync(string accountName, string? exceptTokenHash, CancellationToken cancellationToken = default)
Parameters
accountNamestringAccount whose tokens go.
exceptTokenHashstringA hash to leave alone, or null to invalidate all.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<int>>
How many rows were invalidated.
Remarks
Called after a successful redemption. Without it, an attacker who requested a reset earlier would still hold a live token after the account holder had reset their password — which is exactly the case a reset exists to close.
IssueAsync(string, string, DateTime, string?, CancellationToken)
Records a newly issued reset token.
public Task<DatabaseResult<PasswordResetTokenData>> IssueAsync(string tokenHash, string accountName, DateTime expiresUtc, string? requestedIp, CancellationToken cancellationToken = default)
Parameters
tokenHashstringSHA-256 hex of the emailed token.
accountNamestringAccount the token resets.
expiresUtcDateTimeWhen the token stops being redeemable.
requestedIpstringClient address that asked, or null.
cancellationTokenCancellationTokenCancellation token.
Returns
RedeemAsync(string, CancellationToken)
Spends a token: marks it used and returns the account it belongs to.
public Task<DatabaseResult<string>> RedeemAsync(string tokenHash, CancellationToken cancellationToken = default)
Parameters
tokenHashstringcancellationTokenCancellationToken
Returns
- Task<DatabaseResult<string>>
The account name on success, DB_NOT_FOUND when the token is not spendable.
Remarks
One conditional UPDATE — used_utc IS NULL AND expires_utc > now — so the
check and the mark cannot be separated by a racing second redemption. A token that is
unknown, expired or already used all fail the same way, with DB_NOT_FOUND; the caller
must not distinguish them to the browser either.