Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Core/Grand.Domain/Customers/CustomerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ public class CustomerSettings : ISettings
/// </summary>
public int FailedPasswordLockoutMinutes { get; set; }

/// <summary>
/// Gets or sets a value indicating whether signing out ends every sign-in of the account, on other devices too
/// </summary>
public bool SignOutOnAllDevices { get; set; }

/// <summary>
/// Gets or sets a number of days for password expiration
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
RegistrationFreeShipping: false,
AllowUsersToDeleteAccount: false,
AllowUsersToExportData: false,
SignOutOnAllDevices: false,
HideReviewsTab: false,
HideCoursesTab: false,
TwoFactorAuthenticationEnabled: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
HideNotesTab: false,
AllowUsersToDeleteAccount: false,
AllowUsersToExportData: false,
SignOutOnAllDevices: false,
NewsletterEnabled: true,
NewsletterTickedByDefault: false,
HideNewsletterBlock: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,64 @@ public async Task SignOut_EndsOidcSession()
_oidcSessionService.Verify(x => x.End("session-1", OidcSessionEndReason.SignOut), Times.Once);
}

//a copy of the cookie taken before the sign-out is not accepted after it
[TestMethod]
public async Task SignOut_OnAllDevices_ReplacesThePasswordToken()
{
_customerSettings.SignOutOnAllDevices = true;
var customer = RegisteredCustomer();
_authServiceMock.Setup(x => x.AuthenticateAsync(It.IsAny<HttpContext>(), GrandCookieAuthenticationDefaults.AuthenticationScheme))
.ReturnsAsync(Ticket());

await _cookieAuthService.SignOut();

_customerServiceMock.Verify(x => x.UpdateUserField(customer, SystemCustomerFieldNames.PasswordToken,
It.Is<string>(v => !string.IsNullOrEmpty(v) && v != "token"), It.IsAny<string>()), Times.Once);
}

//a cookie whose token was replaced already does not end the sign-ins made after it
[TestMethod]
public async Task SignOut_OldCookie_PasswordTokenKept()
{
_customerSettings.SignOutOnAllDevices = true;
var customer = RegisteredCustomer();
customer.UserFields.Single(f => f.Key == SystemCustomerFieldNames.PasswordToken).Value = "newer";
_authServiceMock.Setup(x => x.AuthenticateAsync(It.IsAny<HttpContext>(), GrandCookieAuthenticationDefaults.AuthenticationScheme))
.ReturnsAsync(Ticket());

await _cookieAuthService.SignOut();

_customerServiceMock.Verify(x => x.UpdateUserField(It.IsAny<Customer>(), SystemCustomerFieldNames.PasswordToken,
It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}

//by default the other devices stay signed in
[TestMethod]
public async Task SignOut_ThisDeviceOnly_PasswordTokenKept()
{
RegisteredCustomer();
_authServiceMock.Setup(x => x.AuthenticateAsync(It.IsAny<HttpContext>(), GrandCookieAuthenticationDefaults.AuthenticationScheme))
.ReturnsAsync(Ticket());

await _cookieAuthService.SignOut();

_customerServiceMock.Verify(x => x.UpdateUserField(It.IsAny<Customer>(), SystemCustomerFieldNames.PasswordToken,
It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}

[TestMethod]
public async Task SignOut_NotSignedIn_PasswordTokenKept()
{
_customerSettings.SignOutOnAllDevices = true;
_authServiceMock.Setup(x => x.AuthenticateAsync(It.IsAny<HttpContext>(), GrandCookieAuthenticationDefaults.AuthenticationScheme))
.ReturnsAsync(AuthenticateResult.NoResult());

await _cookieAuthService.SignOut();

_customerServiceMock.Verify(x => x.UpdateUserField(It.IsAny<Customer>(), SystemCustomerFieldNames.PasswordToken,
It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}

[TestMethod]
public async Task HasOidcSession_SessionCookie_True()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@
</div>
</div>
</admin-card>
<admin-card>
<div class="grand-fields grand-fields--2col">
<admin-field asp-for="CustomerSettings.SignOutOnAllDevices"/>
</div>
</admin-card>
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public class CustomersSettingsModel : BaseModel
[GrandResourceDisplayName("Admin.Settings.Customer.FailedPasswordLockoutMinutes")]
public int FailedPasswordLockoutMinutes { get; set; }

[GrandResourceDisplayName("Admin.Settings.Customer.SignOutOnAllDevices")]
public bool SignOutOnAllDevices { get; set; }

[GrandResourceDisplayName("Admin.Settings.Customer.NewsletterEnabled")]
public bool NewsletterEnabled { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ public virtual async Task SignOut()
var oidcSessionId = authenticateResult?.Succeeded == true ? OidcSessionId(authenticateResult.Principal) : null;
if (!string.IsNullOrEmpty(oidcSessionId))
await EndOidcSession(oidcSessionId);
if (authenticateResult?.Succeeded == true && _customerSettings.SignOutOnAllDevices)
await ReplacePasswordToken(authenticateResult.Principal);

await _httpContextAccessor.HttpContext.SignOutAsync(GrandCookieAuthenticationDefaults
.AuthenticationScheme);
Expand Down Expand Up @@ -291,16 +293,8 @@ private async Task<Customer> RetrieveCustomer(ClaimsPrincipal principal)
private async Task<bool> IsValidCustomer(Customer customer, ClaimsPrincipal principal)
{
var passwordToken = customer.GetUserFieldFromEntity<string>(SystemCustomerFieldNames.PasswordToken);
if (!string.IsNullOrEmpty(passwordToken))
{
var tokenClaim = principal
.FindFirst(claim =>
claim.Type == ClaimTypes.UserData &&
claim.Issuer.Equals(_securityConfig.CookieClaimsIssuer, StringComparison.InvariantCultureIgnoreCase));

if (tokenClaim == null || tokenClaim.Value != passwordToken)
return false;
}
if (!string.IsNullOrEmpty(passwordToken) && PasswordTokenOf(principal) != passwordToken)
return false;

if (!customer.Active || customer.Deleted || !await _groupService.IsRegistered(customer))
return false;
Expand All @@ -319,6 +313,27 @@ private async Task<bool> IsValidCustomer(Customer customer, ClaimsPrincipal prin
return (await _externalSignInPolicy.PasswordSignIn(customer, SignInSurface.AdminPanel)).Allowed;
}

/// <summary>
/// The cookie is a ticket the server keeps no record of, so a copy taken before the sign-out would still be
/// accepted: the token it carries is replaced, which ends every sign-in of the account, on other devices too
/// </summary>
private async Task ReplacePasswordToken(ClaimsPrincipal principal)
{
var customer = await RetrieveCustomer(principal);
var passwordToken = customer?.GetUserFieldFromEntity<string>(SystemCustomerFieldNames.PasswordToken);
//an old cookie, whose token was replaced already, does not end the sign-ins made after it
if (string.IsNullOrEmpty(passwordToken) || PasswordTokenOf(principal) != passwordToken)
return;

await _customerService.UpdateUserField(customer, SystemCustomerFieldNames.PasswordToken,
Guid.NewGuid().ToString());
}

private string PasswordTokenOf(ClaimsPrincipal principal) =>
principal.FindFirst(claim =>
claim.Type == ClaimTypes.UserData &&
claim.Issuer.Equals(_securityConfig.CookieClaimsIssuer, StringComparison.InvariantCultureIgnoreCase))?.Value;

private async Task EndOidcSession(string sessionId)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@
</div>
</div>
</admin-card>
<admin-card>
<div class="grand-fields grand-fields--2col">
<admin-field asp-for="CustomerSettings.SignOutOnAllDevices"/>
</div>
</admin-card>
Binary file modified src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
Binary file not shown.
3 changes: 3 additions & 0 deletions src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2826,4 +2826,7 @@
<Resource Name="Admin.Orders.MerchandiseReturns.MerchandiseReturnNotes.DownloadNotOfMerchandiseReturn" Area="Admin">
<Value>The attached file was not uploaded for this merchandise return. Upload it again here.</Value>
</Resource>
<Resource Name="Admin.Settings.Customer.SignOutOnAllDevices" Area="Admin">
<Value>Sign out on all devices</Value>
</Resource>
</Language>
Loading