Skip to content

Commit d8f68d2

Browse files
authored
Merge commit from fork
1 parent ff88617 commit d8f68d2

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,17 @@ public AuthenticationController(
132132
AuthorizationPolicies.BackOfficeAccess)] // Needed to enforce the principle set on the request, if one exists.
133133
public IDictionary<string, object> GetPasswordConfig(int userId)
134134
{
135+
if (HttpContext.HasActivePasswordResetFlowSession(userId))
136+
{
137+
return _passwordConfiguration.GetConfiguration();
138+
}
139+
135140
Attempt<int> currentUserId =
136141
_backofficeSecurityAccessor.BackOfficeSecurity?.GetUserId() ?? Attempt<int>.Fail();
137-
return _passwordConfiguration.GetConfiguration(
138-
currentUserId.Success
139-
? currentUserId.Result != userId
140-
: true);
142+
143+
return currentUserId.Success
144+
? _passwordConfiguration.GetConfiguration(currentUserId.Result != userId)
145+
: new Dictionary<string, object>();
141146
}
142147

143148
/// <summary>
@@ -345,6 +350,8 @@ public async Task<bool> IsAuthenticated()
345350
[Authorize(Policy = AuthorizationPolicies.DenyLocalLoginIfConfigured)]
346351
public async Task<ActionResult<UserDetail?>> PostLogin(LoginModel loginModel)
347352
{
353+
HttpContext.EndPasswordResetFlowSession();
354+
348355
// Start a timed scope to ensure failed responses return is a consistent time
349356
await using var timedScope = new TimedScope(GetLoginDuration(), CancellationToken.None);
350357

@@ -440,6 +447,8 @@ public async Task<IActionResult> PostRequestPasswordReset(RequestPasswordResetMo
440447
return BadRequest();
441448
}
442449

450+
HttpContext.EndPasswordResetFlowSession();
451+
443452
BackOfficeIdentityUser? identityUser = await _userManager.FindByEmailAsync(model.Email);
444453

445454
await Task.Delay(RandomNumberGenerator.GetInt32(400, 2500)); // To randomize response time preventing user enumeration
@@ -593,6 +602,8 @@ public async Task<IActionResult> PostSend2FACode([FromBody] string provider)
593602
[AllowAnonymous]
594603
public async Task<IActionResult> PostSetPassword(SetPasswordModel model)
595604
{
605+
HttpContext.EndPasswordResetFlowSession();
606+
596607
BackOfficeIdentityUser? identityUser =
597608
await _userManager.FindByIdAsync(model.UserId.ToString(CultureInfo.InvariantCulture));
598609

src/Umbraco.Web.BackOffice/Controllers/BackOfficeController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ public async Task<IActionResult> ValidatePasswordResetCode([Bind(Prefix = "u")]
370370
var result = await _userManager.VerifyUserTokenAsync(user, "Default", "ResetPassword", resetCode);
371371
if (result)
372372
{
373+
HttpContext.StartPasswordResetFlowSession(userId);
374+
373375
//Add a flag and redirect for it to be displayed
374376
TempData[ViewDataExtensions.TokenPasswordResetCode] =
375377
_jsonSerializer.Serialize(

src/Umbraco.Web.BackOffice/Extensions/HttpContextExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@ namespace Umbraco.Extensions;
55

66
public static class HttpContextExtensions
77
{
8+
private const string PasswordResetFlowSessionKey = nameof(PasswordResetFlowSessionKey);
9+
810
public static void SetExternalLoginProviderErrors(this HttpContext httpContext, BackOfficeExternalLoginProviderErrors errors)
911
=> httpContext.Items[nameof(BackOfficeExternalLoginProviderErrors)] = errors;
1012

1113
public static BackOfficeExternalLoginProviderErrors? GetExternalLoginProviderErrors(this HttpContext httpContext)
1214
=> httpContext.Items[nameof(BackOfficeExternalLoginProviderErrors)] as BackOfficeExternalLoginProviderErrors;
15+
16+
internal static void StartPasswordResetFlowSession(this HttpContext httpContext, int userId)
17+
=> httpContext.Session.SetInt32(PasswordResetFlowSessionKey, userId);
18+
19+
internal static void EndPasswordResetFlowSession(this HttpContext httpContext)
20+
=> httpContext.Session.Remove(PasswordResetFlowSessionKey);
21+
22+
internal static bool HasActivePasswordResetFlowSession(this HttpContext httpContext, int userId)
23+
=> httpContext.Session.GetInt32(PasswordResetFlowSessionKey) == userId;
1324
}

0 commit comments

Comments
 (0)