Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get dynamic claims in AbpAuthenticationHubFilter. #19579

Merged
merged 1 commit into from
Apr 19, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.Security.Claims;

namespace Volo.Abp.AspNetCore.SignalR.Authentication;
Expand All @@ -11,7 +13,7 @@ public class AbpAuthenticationHubFilter : IHubFilter
public virtual async ValueTask<object?> InvokeMethodAsync(HubInvocationContext invocationContext, Func<HubInvocationContext, ValueTask<object?>> next)
{
var currentPrincipalAccessor = invocationContext.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>();
using (currentPrincipalAccessor.Change(invocationContext.Context.User!))
using (currentPrincipalAccessor.Change((await GetDynamicClaimsPrincipalAsync(invocationContext.Context.User, invocationContext.ServiceProvider))!))
{
return await next(invocationContext);
}
Expand All @@ -20,7 +22,7 @@ public class AbpAuthenticationHubFilter : IHubFilter
public virtual async Task OnConnectedAsync(HubLifetimeContext context, Func<HubLifetimeContext, Task> next)
{
var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>();
using (currentPrincipalAccessor.Change(context.Context.User!))
using (currentPrincipalAccessor.Change((await GetDynamicClaimsPrincipalAsync(context.Context.User, context.ServiceProvider))!))
{
await next(context);
}
Expand All @@ -29,9 +31,27 @@ public virtual async Task OnConnectedAsync(HubLifetimeContext context, Func<HubL
public virtual async Task OnDisconnectedAsync(HubLifetimeContext context, Exception? exception, Func<HubLifetimeContext, Exception?, Task> next)
{
var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>();
using (currentPrincipalAccessor.Change(context.Context.User!))
using (currentPrincipalAccessor.Change((await GetDynamicClaimsPrincipalAsync(context.Context.User, context.ServiceProvider))!))
{
await next(context, exception);
}
}

protected virtual async Task<ClaimsPrincipal?> GetDynamicClaimsPrincipalAsync(ClaimsPrincipal? claimsPrincipal, IServiceProvider serviceProvider)
{
if (claimsPrincipal == null)
{
return claimsPrincipal;
}

if (claimsPrincipal.Identity != null &&
claimsPrincipal.Identity.IsAuthenticated &&
serviceProvider.GetRequiredService<IOptions<AbpClaimsPrincipalFactoryOptions>>().Value.IsDynamicClaimsEnabled)
{
var abpClaimsPrincipalFactory = serviceProvider.GetRequiredService<IAbpClaimsPrincipalFactory>();
claimsPrincipal = await abpClaimsPrincipalFactory.CreateDynamicAsync(claimsPrincipal);
}

return claimsPrincipal;
}
}
Loading