Table of Contents

Interface IPasswordResetTokenService

Namespace
FishMMO.Database.Npgsql.Services.Interfaces
Assembly
FishMMO-DB.dll

Outstanding "forgot my password" tokens.

public interface IPasswordResetTokenService

Remarks

Every method takes the SHA-256 hex of the emailed token, never the token itself — hashing is the caller's job, so the redeemable value never reaches this layer at all. This mirrors IWebSessionService, which takes session hashes for the same reason.

Methods

CleanupExpiredAsync(DateTime, CancellationToken)

Deletes tokens that expired before the cutoff. Returns how many rows went.

Task<DatabaseResult<int>> CleanupExpiredAsync(DateTime cutoffUtc, CancellationToken cancellationToken = default)

Parameters

cutoffUtc DateTime
cancellationToken CancellationToken

Returns

Task<DatabaseResult<int>>

FetchByHashAsync(string, CancellationToken)

Fetches a token row by hash, whatever its state.

Task<DatabaseResult<PasswordResetTokenData>> FetchByHashAsync(string tokenHash, CancellationToken cancellationToken = default)

Parameters

tokenHash string
cancellationToken CancellationToken

Returns

Task<DatabaseResult<PasswordResetTokenData>>

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.

Task<DatabaseResult<bool>> HasRecentForAccountAsync(string accountName, DateTime sinceUtc, CancellationToken cancellationToken = default)

Parameters

accountName string
sinceUtc DateTime
cancellationToken CancellationToken

Returns

Task<DatabaseResult<bool>>

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.

Task<DatabaseResult<int>> InvalidateAllForAccountAsync(string accountName, string? exceptTokenHash, CancellationToken cancellationToken = default)

Parameters

accountName string

Account whose tokens go.

exceptTokenHash string

A hash to leave alone, or null to invalidate all.

cancellationToken CancellationToken

Cancellation 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.

Task<DatabaseResult<PasswordResetTokenData>> IssueAsync(string tokenHash, string accountName, DateTime expiresUtc, string? requestedIp, CancellationToken cancellationToken = default)

Parameters

tokenHash string

SHA-256 hex of the emailed token.

accountName string

Account the token resets.

expiresUtc DateTime

When the token stops being redeemable.

requestedIp string

Client address that asked, or null.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<PasswordResetTokenData>>

RedeemAsync(string, CancellationToken)

Spends a token: marks it used and returns the account it belongs to.

Task<DatabaseResult<string>> RedeemAsync(string tokenHash, CancellationToken cancellationToken = default)

Parameters

tokenHash string
cancellationToken CancellationToken

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.