Skip to content

feat(identity): add single-use Email/Phone 2FA token providers#25316

Merged
EngincanV merged 6 commits intodevfrom
feat/abp-two-factor-token-providers
Apr 27, 2026
Merged

feat(identity): add single-use Email/Phone 2FA token providers#25316
EngincanV merged 6 commits intodevfrom
feat/abp-two-factor-token-providers

Conversation

@maliming
Copy link
Copy Markdown
Member

@maliming maliming commented Apr 24, 2026

Replaces TOTP-based Email/Phone 2FA providers under TokenOptions.DefaultEmailProvider / DefaultPhoneProvider with DataProtector-backed single-use equivalents.

  • Encrypt the 6-digit code via IDataProtector
  • Store ciphertext + absolute UTC expiration in the user token table
  • Consume the stored entry on successful validation (single-use)
  • ConcurrencyStamp race returns false instead of 500
  • Configurable TokenLifespan via Options

AbpSingleActiveTokenProvider.GenerateAsync now checks the IdentityResult from UserManager.UpdateAsync so a silent persistence failure no longer returns a token that was not saved.

Because IdentityOptions.Tokens.ChangePhoneNumberTokenProvider defaults to TokenOptions.DefaultPhoneProvider, UserManager.GenerateChangePhoneNumberTokenAsync / VerifyChangePhoneNumberTokenAsync also inherit the new single-use, stored-token semantics. Phone-change tokens issued before upgrade stop working.

Related to #25314.

Replaces the TOTP-based Email/Phone 2FA providers under
TokenOptions.DefaultEmailProvider / DefaultPhoneProvider with
DataProtector-backed single-use equivalents.

- Encrypt the 6-digit code via IDataProtector (purpose chain isolated per
  provider + token purpose), store ciphertext + absolute UTC expiration
  (unix seconds) in the user token table
- Remove the stored entry on successful validation (true single-use)
- Concurrency race (ConcurrencyStamp failure) returns false instead of 500
- Configurable TokenLifespan (default 3 minutes) via Options

AbpSingleActiveTokenProvider.GenerateAsync now checks the IdentityResult
from UserManager.UpdateAsync so a silent persistence failure no longer
returns a token that was not saved.

Related to #25314.
Copilot AI review requested due to automatic review settings April 24, 2026 03:43
@maliming maliming added this to the 10.4-preview milestone Apr 24, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces ABP-managed Email and Phone two-factor token providers that generate and validate single-use numeric codes backed by Data Protection and persisted in the user token store, replacing the default ASP.NET Core Identity TOTP-based Email/Phone providers.

Changes:

  • Added AbpTwoFactorTokenProvider base implementation + configurable options (TokenLifespan, CodeLength) for single-use 2FA codes stored in user tokens.
  • Registered new email/phone 2FA providers under TokenOptions.DefaultEmailProvider / DefaultPhoneProvider and added comprehensive test coverage.
  • Hardened AbpSingleActiveTokenProvider.GenerateAsync to fail if UserManager.UpdateAsync doesn’t succeed.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpTwoFactorTokenProvider.cs Core single-use 2FA provider logic: generate, persist, validate, consume, cleanup.
modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpTwoFactorTokenProviderOptions.cs New shared options for code lifespan and length.
modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpEmailTwoFactorTokenProvider.cs Email provider implementation built on the shared base.
modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpEmailTwoFactorTokenProviderOptions.cs Email provider options type for configuration binding.
modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpPhoneNumberTwoFactorTokenProvider.cs Phone provider implementation built on the shared base.
modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpPhoneNumberTwoFactorTokenProviderOptions.cs Phone provider options type for configuration binding.
modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpIdentityAspNetCoreModule.cs Registers the new providers under Identity’s default Email/Phone provider keys.
modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpSingleActiveTokenProvider.cs Ensures persistence failures are not silently ignored when storing token hash.
modules/identity/test/Volo.Abp.Identity.AspNetCore.Tests/Volo/Abp/Identity/AspNetCore/AbpTwoFactorTokenProviderTestBase.cs Shared behavioral test suite for all ABP 2FA providers (single-use, cleanup, isolation, etc.).
modules/identity/test/Volo.Abp.Identity.AspNetCore.Tests/Volo/Abp/Identity/AspNetCore/AbpEmailTwoFactorTokenProvider_Tests.cs Email-specific tests (registration, options defaults, stamp change behavior, etc.).
modules/identity/test/Volo.Abp.Identity.AspNetCore.Tests/Volo/Abp/Identity/AspNetCore/AbpPhoneNumberTwoFactorTokenProvider_Tests.cs Phone-specific tests (registration, confirmed phone requirement, change-phone semantics, etc.).

@maliming maliming requested a review from Copilot April 24, 2026 04:12
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

@maliming maliming requested a review from Copilot April 24, 2026 04:43
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

@maliming maliming requested a review from EngincanV April 24, 2026 05:53
@EngincanV EngincanV merged commit 198d64d into dev Apr 27, 2026
7 checks passed
@EngincanV EngincanV deleted the feat/abp-two-factor-token-providers branch April 27, 2026 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants