Skip to content

Commit

Permalink
Always CheckDynamicClaims on OnConnectedAsync and `OnDisconnected…
Browse files Browse the repository at this point in the history
…Async` method.
  • Loading branch information
maliming committed May 8, 2024
1 parent 658ae6e commit ffc94a2
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class AbpAuthenticationHubFilter : IHubFilter
{
var currentPrincipalAccessor = invocationContext.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>();
var claimsPrincipal = invocationContext.Context.User;
await HandleDynamicClaimsPrincipalAsync(claimsPrincipal, invocationContext.ServiceProvider, invocationContext.Context);
await HandleDynamicClaimsPrincipalAsync(claimsPrincipal, invocationContext.ServiceProvider, invocationContext.Context, false);
using (currentPrincipalAccessor.Change(claimsPrincipal!))
{
return await next(invocationContext);
Expand All @@ -27,7 +27,7 @@ public virtual async Task OnConnectedAsync(HubLifetimeContext context, Func<HubL
{
var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>();
var claimsPrincipal = context.Context.User;
await HandleDynamicClaimsPrincipalAsync(claimsPrincipal, context.ServiceProvider, context.Context);
await HandleDynamicClaimsPrincipalAsync(claimsPrincipal, context.ServiceProvider, context.Context, true);
using (currentPrincipalAccessor.Change(claimsPrincipal!))
{
await next(context);
Expand All @@ -38,21 +38,23 @@ public virtual async Task OnDisconnectedAsync(HubLifetimeContext context, Except
{
var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>();
var claimsPrincipal = context.Context.User;
await HandleDynamicClaimsPrincipalAsync(claimsPrincipal, context.ServiceProvider, context.Context);
await HandleDynamicClaimsPrincipalAsync(claimsPrincipal, context.ServiceProvider, context.Context, true);
using (currentPrincipalAccessor.Change(claimsPrincipal!))
{
await next(context, exception);
}
}

protected virtual async Task HandleDynamicClaimsPrincipalAsync(ClaimsPrincipal? claimsPrincipal, IServiceProvider serviceProvider, HubCallerContext hubCallerContext)
protected virtual async Task HandleDynamicClaimsPrincipalAsync(ClaimsPrincipal? claimsPrincipal, IServiceProvider serviceProvider, HubCallerContext hubCallerContext, bool skipCheckDynamicClaimsInterval)
{
if (claimsPrincipal?.Identity != null &&
claimsPrincipal.Identity.IsAuthenticated &&
serviceProvider.GetRequiredService<IOptions<AbpClaimsPrincipalFactoryOptions>>().Value.IsDynamicClaimsEnabled)
serviceProvider.GetRequiredService<IOptions<AbpClaimsPrincipalFactoryOptions>>().Value
.IsDynamicClaimsEnabled)
{
var checkDynamicClaimsInterval = serviceProvider.GetRequiredService<IOptions<AbpSignalROptions>>().Value.CheckDynamicClaimsInterval;
if (checkDynamicClaimsInterval.HasValue &&
if (!skipCheckDynamicClaimsInterval &&
checkDynamicClaimsInterval.HasValue &&
hubCallerContext.Items.TryGetValue(nameof(HandleDynamicClaimsPrincipalAsync), out var lastCheckDynamicClaimsTime) &&
lastCheckDynamicClaimsTime is DateTime lastCheckDynamicClaimsTimeValue)
{
Expand All @@ -66,10 +68,13 @@ protected virtual async Task HandleDynamicClaimsPrincipalAsync(ClaimsPrincipal?
hubCallerContext.Items[nameof(HandleDynamicClaimsPrincipalAsync)] = DateTime.UtcNow;

claimsPrincipal = claimsPrincipal.Identity is ClaimsIdentity identity
? new ClaimsPrincipal(new ClaimsIdentity(claimsPrincipal.Claims, claimsPrincipal.Identity.AuthenticationType, identity.NameClaimType, identity.RoleClaimType))
: new ClaimsPrincipal(new ClaimsIdentity(claimsPrincipal.Claims, claimsPrincipal.Identity.AuthenticationType));
? new ClaimsPrincipal(new ClaimsIdentity(claimsPrincipal.Claims,
claimsPrincipal.Identity.AuthenticationType, identity.NameClaimType, identity.RoleClaimType))
: new ClaimsPrincipal(new ClaimsIdentity(claimsPrincipal.Claims,
claimsPrincipal.Identity.AuthenticationType));

claimsPrincipal = await serviceProvider.GetRequiredService<IAbpClaimsPrincipalFactory>().CreateDynamicAsync(claimsPrincipal);
claimsPrincipal = await serviceProvider.GetRequiredService<IAbpClaimsPrincipalFactory>()
.CreateDynamicAsync(claimsPrincipal);
if (claimsPrincipal.Identity?.IsAuthenticated == false)
{
hubCallerContext.Abort();
Expand Down

0 comments on commit ffc94a2

Please sign in to comment.