Skip to content

Commit

Permalink
PT-13919: SSO is reset to Password after some period. (#2708)
Browse files Browse the repository at this point in the history
fix: SSO is reset to Password after some period by implementing SecurityStampValidatorOptions.OnRefreshingPrincipal
  • Loading branch information
OlegoO authored Oct 13, 2023
1 parent 66091d2 commit 2ea6b6d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using VirtoCommerce.Platform.Security.Extensions;

namespace VirtoCommerce.Platform.Security
{
public class ConfigureSecurityStampValidatorOptions : IConfigureOptions<SecurityStampValidatorOptions>
{
public void Configure(SecurityStampValidatorOptions options)
{
options.ValidationInterval = TimeSpan.FromMinutes(30);

// When refreshing the principal, ensure custom claims that
// might have been set with an external identity continue
// to flow through to this new one.
options.OnRefreshingPrincipal = refreshingPrincipal =>
{
var newIdentity = refreshingPrincipal.NewPrincipal?.Identities.First();
var currentIdentity = refreshingPrincipal.CurrentPrincipal?.Identities.First();
if (currentIdentity is not null)
{
// Since this is refreshing an existing principal, we want to merge all claims.
newIdentity?.MergeAllClaims(currentIdentity);
}
return Task.CompletedTask;
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Linq;
using System.Security.Claims;

namespace VirtoCommerce.Platform.Security.Extensions
{
public static class MergeClaimsIdentityExtensions
{
public static void MergeAllClaims(this ClaimsIdentity destination, ClaimsIdentity source)
{
foreach (var claim in source.Claims
.Where(claim => !destination.HasClaim(claim.Type, claim.Value)))
{
destination.AddClaim(new Claim(claim.Type, claim.Value));
}
}
}
}
2 changes: 2 additions & 0 deletions src/VirtoCommerce.Platform.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ public void ConfigureServices(IServiceCollection services)
options.ClaimsIdentity.RoleClaimType = OpenIddictConstants.Claims.Role;
});

services.ConfigureOptions<ConfigureSecurityStampValidatorOptions>();

// Load server certificate (from DB or file) and register it as a global singleton
// to allow the platform hosting under the cert
ICertificateLoader certificateLoader;
Expand Down

0 comments on commit 2ea6b6d

Please sign in to comment.