Class TwoFactorRecoveryCodeService
Service for managing two-factor recovery codes. Uses raw SQL for writes and compiled queries for reads.
public sealed class TwoFactorRecoveryCodeService : BaseService<TwoFactorRecoveryCodeEntity>, ITwoFactorRecoveryCodeService
- Inheritance
-
TwoFactorRecoveryCodeService
- Implements
- Inherited Members
Constructors
TwoFactorRecoveryCodeService(INpgsqlDbContextFactory)
public TwoFactorRecoveryCodeService(INpgsqlDbContextFactory dbContextFactory)
Parameters
dbContextFactoryINpgsqlDbContextFactory
Methods
ConsumeCodeAsync(string, string, CancellationToken)
Atomically marks a single unused recovery code as consumed by setting used_at. Returns success only if the code was found and was previously unused.
public Task<DatabaseResult> ConsumeCodeAsync(string accountName, string codeHash, CancellationToken cancellationToken = default)
Parameters
accountNamestringThe account that owns the code.
codeHashstringThe hashed code to consume.
cancellationTokenCancellationTokenCancellation token.
Returns
DeleteAllForAccountAsync(string, CancellationToken)
Deletes all recovery codes (used and unused) for an account. Called when TOTP is disabled or recovery codes are regenerated.
public Task<DatabaseResult> DeleteAllForAccountAsync(string accountName, CancellationToken cancellationToken = default)
Parameters
accountNamestringcancellationTokenCancellationToken
Returns
FetchUnusedByAccountAsync(string, CancellationToken)
Fetches all unused recovery codes for an account.
public Task<DatabaseResult<List<TwoFactorRecoveryCodeData>>> FetchUnusedByAccountAsync(string accountName, CancellationToken cancellationToken = default)
Parameters
accountNamestringcancellationTokenCancellationToken
Returns
PersistManyAsync(string, IReadOnlyList<string>, CancellationToken)
Batch-inserts a set of pre-hashed recovery codes for an account. Called during TOTP enrollment or recovery code regeneration.
public Task<DatabaseResult> PersistManyAsync(string accountName, IReadOnlyList<string> codeHashes, CancellationToken cancellationToken = default)
Parameters
accountNamestringThe account to associate codes with.
codeHashesIReadOnlyList<string>Pre-hashed recovery codes to store.
cancellationTokenCancellationTokenCancellation token.