Skip to content

Commit 4f6a03e

Browse files
authored
Change IsTfaEnabled to IsTwoFactorEnabledAsync (#46258)
Also now public virtual
1 parent 5594b84 commit 4f6a03e

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
#nullable enable
2+
virtual Microsoft.AspNetCore.Identity.SignInManager<TUser>.IsTwoFactorEnabledAsync(TUser! user) -> System.Threading.Tasks.Task<bool>!

src/Identity/Core/src/SignInManager.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ public virtual async Task<SignInResult> CheckPasswordSignInAsync(TUser user, str
368368
{
369369
var alwaysLockout = AppContext.TryGetSwitch("Microsoft.AspNetCore.Identity.CheckPasswordSignInAlwaysResetLockoutOnSuccess", out var enabled) && enabled;
370370
// Only reset the lockout when not in quirks mode if either TFA is not enabled or the client is remembered for TFA.
371-
if (alwaysLockout || !await IsTfaEnabled(user) || await IsTwoFactorClientRememberedAsync(user))
371+
if (alwaysLockout || !await IsTwoFactorEnabledAsync(user) || await IsTwoFactorClientRememberedAsync(user))
372372
{
373373
await ResetLockout(user);
374374
}
@@ -741,7 +741,14 @@ internal async Task<ClaimsPrincipal> StoreRememberClient(TUser user)
741741
return new ClaimsPrincipal(rememberBrowserIdentity);
742742
}
743743

744-
private async Task<bool> IsTfaEnabled(TUser user)
744+
/// <summary>
745+
/// Check if the <paramref name="user"/> has two factor enabled.
746+
/// </summary>
747+
/// <param name="user"></param>
748+
/// <returns>
749+
/// The task object representing the asynchronous operation containing true if the user has two factor enabled.
750+
/// </returns>
751+
public virtual async Task<bool> IsTwoFactorEnabledAsync(TUser user)
745752
=> UserManager.SupportsUserTwoFactor &&
746753
await UserManager.GetTwoFactorEnabledAsync(user) &&
747754
(await UserManager.GetValidTwoFactorProvidersAsync(user)).Count > 0;
@@ -757,7 +764,7 @@ await UserManager.GetTwoFactorEnabledAsync(user) &&
757764
/// <returns>Returns a <see cref="SignInResult"/></returns>
758765
protected virtual async Task<SignInResult> SignInOrTwoFactorAsync(TUser user, bool isPersistent, string? loginProvider = null, bool bypassTwoFactor = false)
759766
{
760-
if (!bypassTwoFactor && await IsTfaEnabled(user))
767+
if (!bypassTwoFactor && await IsTwoFactorEnabledAsync(user))
761768
{
762769
if (!await IsTwoFactorClientRememberedAsync(user))
763770
{

src/Identity/test/Identity.Test/SignInManagerTest.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,47 @@ public async Task TwoFactorAuthenticatorSignInFailWithoutLockout()
424424
auth.Verify();
425425
}
426426

427+
[Theory]
428+
[InlineData(true, true, true)]
429+
[InlineData(true, true, false)]
430+
[InlineData(true, false, true)]
431+
[InlineData(true, false, false)]
432+
[InlineData(false, true, true)]
433+
[InlineData(false, true, false)]
434+
[InlineData(false, false, true)]
435+
[InlineData(false, false, false)]
436+
public async Task IsTwoFactorEnabled(bool userManagerSupportsTwoFactor, bool userTwoFactorEnabled, bool hasValidProviders)
437+
{
438+
// Setup
439+
var user = new PocoUser { UserName = "Foo" };
440+
var manager = SetupUserManager(user);
441+
manager.Setup(m => m.SupportsUserTwoFactor).Returns(userManagerSupportsTwoFactor).Verifiable();
442+
if (userManagerSupportsTwoFactor)
443+
{
444+
manager.Setup(m => m.GetTwoFactorEnabledAsync(user)).ReturnsAsync(userTwoFactorEnabled).Verifiable();
445+
if (userTwoFactorEnabled)
446+
{
447+
manager
448+
.Setup(m => m.GetValidTwoFactorProvidersAsync(user))
449+
.ReturnsAsync(hasValidProviders ? new string[1] { "Fake" } : Array.Empty<string>())
450+
.Verifiable();
451+
}
452+
}
453+
454+
var context = new DefaultHttpContext();
455+
var auth = MockAuth(context);
456+
var helper = SetupSignInManager(manager.Object, context);
457+
458+
// Act
459+
var result = await helper.IsTwoFactorEnabledAsync(user);
460+
461+
// Assert
462+
var expected = userManagerSupportsTwoFactor && userTwoFactorEnabled && hasValidProviders;
463+
Assert.Equal(expected, result);
464+
manager.Verify();
465+
auth.Verify();
466+
}
467+
427468
[Theory]
428469
[InlineData(true, true)]
429470
[InlineData(true, false)]

0 commit comments

Comments
 (0)