Table of Contents

Class PasswordResetTokenService

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

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

dbContextFactory INpgsqlDbContextFactory

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

cutoffUtc DateTime
cancellationToken CancellationToken

Returns

Task<DatabaseResult<int>>

FetchByHashAsync(string, CancellationToken)

Fetches a token row by hash, whatever its state.

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

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

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

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

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