1
0
mirror of https://github.com/bitwarden/server synced 2025-12-26 13:13:24 +00:00

feat(OTP): [PM-18612] Change email OTP to six digits

* Change email OTP to 6 digits

* Added comment on base class

* Added tests

* Renamed tests.

* Fixed tests

* Renamed file to match class
This commit is contained in:
Todd Martin
2025-07-14 10:23:30 -04:00
committed by GitHub
parent 9b65e9f4cc
commit 2f8460f4db
8 changed files with 144 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
using System.Text;
using Bit.Core.Entities;
using Bit.Core.Services;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Caching.Distributed;
@@ -7,6 +8,9 @@ using Microsoft.Extensions.DependencyInjection;
namespace Bit.Core.Auth.Identity.TokenProviders;
/// <summary>
/// Generates and validates tokens for email OTPs.
/// </summary>
public class EmailTokenProvider : IUserTwoFactorTokenProvider<User>
{
private const string CacheKeyFormat = "EmailToken_{0}_{1}_{2}";
@@ -16,16 +20,25 @@ public class EmailTokenProvider : IUserTwoFactorTokenProvider<User>
public EmailTokenProvider(
[FromKeyedServices("persistent")]
IDistributedCache distributedCache)
IDistributedCache distributedCache,
IFeatureService featureService)
{
_distributedCache = distributedCache;
_distributedCacheEntryOptions = new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5)
};
if (featureService.IsEnabled(FeatureFlagKeys.Otp6Digits))
{
TokenLength = 6;
}
else
{
TokenLength = 8;
}
}
public int TokenLength { get; protected set; } = 8;
public int TokenLength { get; protected set; }
public bool TokenAlpha { get; protected set; } = false;
public bool TokenNumeric { get; protected set; } = true;

View File

@@ -4,19 +4,27 @@
using Bit.Core.Auth.Enums;
using Bit.Core.Auth.Models;
using Bit.Core.Entities;
using Bit.Core.Services;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.Core.Auth.Identity.TokenProviders;
/// <summary>
/// Generates tokens for email two-factor authentication.
/// It inherits from the EmailTokenProvider class, which manages the persistence and validation of tokens,
/// and adds additional validation to ensure that 2FA is enabled for the user.
/// </summary>
public class EmailTwoFactorTokenProvider : EmailTokenProvider
{
public EmailTwoFactorTokenProvider(
[FromKeyedServices("persistent")]
IDistributedCache distributedCache) :
base(distributedCache)
IDistributedCache distributedCache,
IFeatureService featureService) :
base(distributedCache, featureService)
{
// This can be removed when the pm-18612-otp-6-digits feature flag is removed because the base implementation will match.
TokenAlpha = false;
TokenNumeric = true;
TokenLength = 6;

View File

@@ -124,6 +124,7 @@ public static class FeatureFlagKeys
public const string SetInitialPasswordRefactor = "pm-16117-set-initial-password-refactor";
public const string ChangeExistingPasswordRefactor = "pm-16117-change-existing-password-refactor";
public const string RecoveryCodeLogin = "pm-17128-recovery-code-login";
public const string Otp6Digits = "pm-18612-otp-6-digits";
/* Autofill Team */
public const string IdpAutoSubmitLogin = "idp-auto-submit-login";