diff --git a/src/Core/Grand.Domain/Customers/CustomerSettings.cs b/src/Core/Grand.Domain/Customers/CustomerSettings.cs
index 3d8cb525c..b074e001c 100644
--- a/src/Core/Grand.Domain/Customers/CustomerSettings.cs
+++ b/src/Core/Grand.Domain/Customers/CustomerSettings.cs
@@ -170,6 +170,11 @@ public class CustomerSettings : ISettings
///
public int FailedPasswordLockoutMinutes { get; set; }
+ ///
+ /// Gets or sets a value indicating whether signing out ends every sign-in of the account, on other devices too
+ ///
+ public bool SignOutOnAllDevices { get; set; }
+
///
/// Gets or sets a number of days for password expiration
///
diff --git a/src/Tests/Grand.Mapping.Tests/AdminShared/SettingsMappingTests.CustomerSettingsModel_ToDomain.verified.txt b/src/Tests/Grand.Mapping.Tests/AdminShared/SettingsMappingTests.CustomerSettingsModel_ToDomain.verified.txt
index 0c7b23895..8ecd44133 100644
--- a/src/Tests/Grand.Mapping.Tests/AdminShared/SettingsMappingTests.CustomerSettingsModel_ToDomain.verified.txt
+++ b/src/Tests/Grand.Mapping.Tests/AdminShared/SettingsMappingTests.CustomerSettingsModel_ToDomain.verified.txt
@@ -22,6 +22,7 @@
RegistrationFreeShipping: false,
AllowUsersToDeleteAccount: false,
AllowUsersToExportData: false,
+ SignOutOnAllDevices: false,
HideReviewsTab: false,
HideCoursesTab: false,
TwoFactorAuthenticationEnabled: false,
diff --git a/src/Tests/Grand.Mapping.Tests/AdminShared/SettingsMappingTests.CustomerSettings_ToModel.verified.txt b/src/Tests/Grand.Mapping.Tests/AdminShared/SettingsMappingTests.CustomerSettings_ToModel.verified.txt
index ac2dcc141..a2ad56731 100644
--- a/src/Tests/Grand.Mapping.Tests/AdminShared/SettingsMappingTests.CustomerSettings_ToModel.verified.txt
+++ b/src/Tests/Grand.Mapping.Tests/AdminShared/SettingsMappingTests.CustomerSettings_ToModel.verified.txt
@@ -12,6 +12,7 @@
HideNotesTab: false,
AllowUsersToDeleteAccount: false,
AllowUsersToExportData: false,
+ SignOutOnAllDevices: false,
NewsletterEnabled: true,
NewsletterTickedByDefault: false,
HideNewsletterBlock: false,
diff --git a/src/Tests/Grand.Web.Common.Tests/Security/Authentication/CookieAuthenticationServiceTests.cs b/src/Tests/Grand.Web.Common.Tests/Security/Authentication/CookieAuthenticationServiceTests.cs
index 7c1499c65..ee47b3428 100644
--- a/src/Tests/Grand.Web.Common.Tests/Security/Authentication/CookieAuthenticationServiceTests.cs
+++ b/src/Tests/Grand.Web.Common.Tests/Security/Authentication/CookieAuthenticationServiceTests.cs
@@ -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(), GrandCookieAuthenticationDefaults.AuthenticationScheme))
+ .ReturnsAsync(Ticket());
+
+ await _cookieAuthService.SignOut();
+
+ _customerServiceMock.Verify(x => x.UpdateUserField(customer, SystemCustomerFieldNames.PasswordToken,
+ It.Is(v => !string.IsNullOrEmpty(v) && v != "token"), It.IsAny()), 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(), GrandCookieAuthenticationDefaults.AuthenticationScheme))
+ .ReturnsAsync(Ticket());
+
+ await _cookieAuthService.SignOut();
+
+ _customerServiceMock.Verify(x => x.UpdateUserField(It.IsAny(), SystemCustomerFieldNames.PasswordToken,
+ It.IsAny(), It.IsAny()), 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(), GrandCookieAuthenticationDefaults.AuthenticationScheme))
+ .ReturnsAsync(Ticket());
+
+ await _cookieAuthService.SignOut();
+
+ _customerServiceMock.Verify(x => x.UpdateUserField(It.IsAny(), SystemCustomerFieldNames.PasswordToken,
+ It.IsAny(), It.IsAny()), Times.Never);
+ }
+
+ [TestMethod]
+ public async Task SignOut_NotSignedIn_PasswordTokenKept()
+ {
+ _customerSettings.SignOutOnAllDevices = true;
+ _authServiceMock.Setup(x => x.AuthenticateAsync(It.IsAny(), GrandCookieAuthenticationDefaults.AuthenticationScheme))
+ .ReturnsAsync(AuthenticateResult.NoResult());
+
+ await _cookieAuthService.SignOut();
+
+ _customerServiceMock.Verify(x => x.UpdateUserField(It.IsAny(), SystemCustomerFieldNames.PasswordToken,
+ It.IsAny(), It.IsAny()), Times.Never);
+ }
+
[TestMethod]
public async Task HasOidcSession_SessionCookie_True()
{
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml
index e0eb2be83..69396390e 100644
--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml
+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml
@@ -81,3 +81,8 @@
+
+
+
diff --git a/src/Web/Grand.Web.AdminShared/Models/Settings/CustomerSettingsModel.cs b/src/Web/Grand.Web.AdminShared/Models/Settings/CustomerSettingsModel.cs
index f641b2902..62c966cca 100644
--- a/src/Web/Grand.Web.AdminShared/Models/Settings/CustomerSettingsModel.cs
+++ b/src/Web/Grand.Web.AdminShared/Models/Settings/CustomerSettingsModel.cs
@@ -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; }
diff --git a/src/Web/Grand.Web.Common/Security/Authentication/CookieAuthenticationService.cs b/src/Web/Grand.Web.Common/Security/Authentication/CookieAuthenticationService.cs
index 8cf36060b..f1f15250e 100644
--- a/src/Web/Grand.Web.Common/Security/Authentication/CookieAuthenticationService.cs
+++ b/src/Web/Grand.Web.Common/Security/Authentication/CookieAuthenticationService.cs
@@ -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);
@@ -291,16 +293,8 @@ private async Task RetrieveCustomer(ClaimsPrincipal principal)
private async Task IsValidCustomer(Customer customer, ClaimsPrincipal principal)
{
var passwordToken = customer.GetUserFieldFromEntity(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;
@@ -319,6 +313,27 @@ private async Task IsValidCustomer(Customer customer, ClaimsPrincipal prin
return (await _externalSignInPolicy.PasswordSignIn(customer, SignInSurface.AdminPanel)).Allowed;
}
+ ///
+ /// 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
+ ///
+ private async Task ReplacePasswordToken(ClaimsPrincipal principal)
+ {
+ var customer = await RetrieveCustomer(principal);
+ var passwordToken = customer?.GetUserFieldFromEntity(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
diff --git a/src/Web/Grand.Web.Store/Areas/Store/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml b/src/Web/Grand.Web.Store/Areas/Store/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml
index ffb19e528..dad19e09f 100644
--- a/src/Web/Grand.Web.Store/Areas/Store/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml
+++ b/src/Web/Grand.Web.Store/Areas/Store/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml
@@ -79,3 +79,8 @@
+
+
+
diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
index 886ca7780..6793f972d 100644
Binary files a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml and b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml differ
diff --git a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml
index a1d50f3e2..c8225fc40 100644
--- a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml
+++ b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml
@@ -2826,4 +2826,7 @@
The attached file was not uploaded for this merchandise return. Upload it again here.
+
+ Sign out on all devices
+