Class TwoFactorResetRequestService
Delayed two-factor reset requests. Raw SQL for the state transitions, each a single conditional UPDATE; EF for reads.
public sealed class TwoFactorResetRequestService : BaseService<TwoFactorResetRequestEntity>, ITwoFactorResetRequestService
- Inheritance
-
TwoFactorResetRequestService
- Implements
- Inherited Members
Remarks
This service never reads or writes the account's two-factor columns. It records whether a reset may happen; the caller does the reset, in the same unit of work as CompleteAsync(long, string, CancellationToken).
Constructors
TwoFactorResetRequestService(INpgsqlDbContextFactory)
Creates the service.
public TwoFactorResetRequestService(INpgsqlDbContextFactory dbContextFactory)
Parameters
dbContextFactoryINpgsqlDbContextFactory
Methods
CancelAsync(string, string, string?, CancellationToken)
Cancels the account's pending request, if any.
public Task<DatabaseResult<bool>> CancelAsync(string accountName, string cancelledBy, string? staffReason, CancellationToken cancellationToken = default)
Parameters
accountNamestringThe account.
cancelledBystringThe staff account, or the account itself.
staffReasonstringWhy, for a staff cancellation; null keeps any earlier reason.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<bool>>
True when a pending request was cancelled, false when there was none.
Remarks
Called by staff, and by the sign-in path whenever the account passes two-factor normally — that proves the owner still has their factor, which is the case the waiting period exists to catch. No pending request is not a failure: the sign-in path calls this unconditionally.
CompleteAsync(long, string, CancellationToken)
Marks a request completed, if it is still pending, belongs to the account, and has taken effect.
public Task<DatabaseResult<bool>> CompleteAsync(long id, string accountName, CancellationToken cancellationToken = default)
Parameters
idlongThe request.
accountNamestringThe account that must own it.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<bool>>
True when this call completed it; false when it was not completable.
Remarks
One conditional UPDATE, so the checks and the mark cannot be separated: a request cancelled by a normal sign-in a moment earlier cannot then be completed, and two completions cannot both report success.
Completion never satisfies two-factor. The session completing it has proved the password only. The caller removes the old factor and sends the player to enrol a new one; it must not treat the sign-in as having passed a second factor.
FetchAsync(long, CancellationToken)
One request by id, in any state. NOT_FOUND when it does not exist.
public Task<DatabaseResult<TwoFactorResetRequestData>> FetchAsync(long id, CancellationToken cancellationToken = default)
Parameters
idlongcancellationTokenCancellationToken
Returns
FetchPendingAsync(string, CancellationToken)
The account's pending request, or a successful null when there is none.
public Task<DatabaseResult<TwoFactorResetRequestData?>> FetchPendingAsync(string accountName, CancellationToken cancellationToken = default)
Parameters
accountNamestringcancellationTokenCancellationToken
Returns
RequestAsync(string, TimeSpan, string?, CancellationToken)
Opens a request that takes effect after delay, or returns the one
already pending.
public Task<DatabaseResult<TwoFactorResetRequestData>> RequestAsync(string accountName, TimeSpan delay, string? requestedIp, CancellationToken cancellationToken = default)
Parameters
accountNamestringThe account; stored lowercase.
delayTimeSpanThe waiting period. Must be greater than zero.
requestedIpstringClient address that asked, or null.
cancellationTokenCancellationTokenCancellation token.
Returns
Remarks
An existing pending request is returned unchanged: asking again never restarts the clock (which would let an attacker keep the owner's cancellation window open indefinitely) and never shortens it (which would let them ask their way past it). The one-pending-per-account rule is a partial unique index, so two simultaneous requests produce one row and both callers get it.
SearchPendingAsync(int, int, CancellationToken)
Pending requests, soonest to take effect first. Page size is clamped to 200.
public Task<DatabaseResult<TwoFactorResetRequestPage>> SearchPendingAsync(int page, int pageSize, CancellationToken cancellationToken = default)
Parameters
pageintpageSizeintcancellationTokenCancellationToken
Returns
ShortenAsync(long, DateTime, string, string, CancellationToken)
Brings a pending request's effective time forward.
public Task<DatabaseResult> ShortenAsync(long id, DateTime newEffectiveUtc, string shortenedBy, string staffReason, CancellationToken cancellationToken = default)
Parameters
idlongThe request.
newEffectiveUtcDateTimeThe new, earlier, effective time.
shortenedBystringThe staff account.
staffReasonstringWhy. Required, at most 256 characters.
cancellationTokenCancellationTokenCancellation token.
Returns
Remarks
Staff may shorten, never extend: newEffectiveUtc must be earlier
than the current effective time. Extending is a cancel plus a new request, so a longer
wait always starts from a request the history shows. A time in the past is clamped to
now. The reason is required because shortening removes the protection the delay gives.