Browse Source

Merge pull request #19605 from abpframework/AbpAuthenticationHubFilter-abort

Abort signalr connection if session has expired.
pull/19614/head
Gizem Mutu Kurt 2 years ago
committed by GitHub
parent
commit
dc038e8930
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 46
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ClientProxyExceptionEventHandler.cs
  2. 34
      framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Authentication/AbpAuthenticationHubFilter.cs

46
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ClientProxyExceptionEventHandler.cs

@ -24,34 +24,46 @@ public class ClientProxyExceptionEventHandler : ILocalEventHandler<ClientProxyEx
public virtual async Task HandleEventAsync(ClientProxyExceptionEventData eventData) public virtual async Task HandleEventAsync(ClientProxyExceptionEventData eventData)
{ {
if (eventData.StatusCode == 401) using (var scope = ServiceProvider.CreateScope())
{ {
using (var scope = ServiceProvider.CreateScope()) switch (eventData.StatusCode)
{ {
var options = scope.ServiceProvider.GetRequiredService<IOptions<AbpAspNetCoreComponentsWebOptions>>(); case 401:
if (!options.Value.IsBlazorWebApp)
{ {
var navigationManager = scope.ServiceProvider.GetRequiredService<NavigationManager>(); var options = scope.ServiceProvider.GetRequiredService<IOptions<AbpAspNetCoreComponentsWebOptions>>();
var accessTokenProvider = scope.ServiceProvider.GetRequiredService<IAccessTokenProvider>();
var authenticationOptions = scope.ServiceProvider.GetRequiredService<IOptions<AbpAuthenticationOptions>>(); if (!options.Value.IsBlazorWebApp)
var result = await accessTokenProvider.RequestAccessToken();
if (result.Status != AccessTokenResultStatus.Success)
{ {
navigationManager.NavigateToLogout(authenticationOptions.Value.LogoutUrl); var navigationManager = scope.ServiceProvider.GetRequiredService<NavigationManager>();
return; var accessTokenProvider = scope.ServiceProvider.GetRequiredService<IAccessTokenProvider>();
} var authenticationOptions = scope.ServiceProvider.GetRequiredService<IOptions<AbpAuthenticationOptions>>();
var result = await accessTokenProvider.RequestAccessToken();
if (result.Status != AccessTokenResultStatus.Success)
{
navigationManager.NavigateToLogout(authenticationOptions.Value.LogoutUrl);
return;
}
result.TryGetToken(out var token); result.TryGetToken(out var token);
if (token != null && DateTimeOffset.Now >= token.Expires.AddMinutes(-5)) if (token != null && DateTimeOffset.Now >= token.Expires.AddMinutes(-5))
{
navigationManager.NavigateToLogout(authenticationOptions.Value.LogoutUrl);
}
}
else
{ {
navigationManager.NavigateToLogout(authenticationOptions.Value.LogoutUrl); var jsRuntime = scope.ServiceProvider.GetRequiredService<IJSRuntime>();
await jsRuntime.InvokeVoidAsync("eval", "setTimeout(function(){location.assign('/')}, 2000)");
} }
break;
} }
else case 403:
{ {
var jsRuntime = scope.ServiceProvider.GetRequiredService<IJSRuntime>(); var jsRuntime = scope.ServiceProvider.GetRequiredService<IJSRuntime>();
await jsRuntime.InvokeVoidAsync("eval", "setTimeout(function(){location.assign('/')}, 2000)"); await jsRuntime.InvokeVoidAsync("eval", "setTimeout(function(){location.assign('/')}, 2000)");
break;
} }
} }
} }

34
framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Authentication/AbpAuthenticationHubFilter.cs

@ -13,7 +13,9 @@ public class AbpAuthenticationHubFilter : IHubFilter
public virtual async ValueTask<object?> InvokeMethodAsync(HubInvocationContext invocationContext, Func<HubInvocationContext, ValueTask<object?>> next) public virtual async ValueTask<object?> InvokeMethodAsync(HubInvocationContext invocationContext, Func<HubInvocationContext, ValueTask<object?>> next)
{ {
var currentPrincipalAccessor = invocationContext.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>(); var currentPrincipalAccessor = invocationContext.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>();
using (currentPrincipalAccessor.Change((await GetDynamicClaimsPrincipalAsync(invocationContext.Context.User, invocationContext.ServiceProvider))!)) var claimsPrincipal = invocationContext.Context.User;
await HandleDynamicClaimsPrincipalAsync(claimsPrincipal, invocationContext.ServiceProvider, invocationContext.Context);
using (currentPrincipalAccessor.Change(claimsPrincipal!))
{ {
return await next(invocationContext); return await next(invocationContext);
} }
@ -22,7 +24,9 @@ public class AbpAuthenticationHubFilter : IHubFilter
public virtual async Task OnConnectedAsync(HubLifetimeContext context, Func<HubLifetimeContext, Task> next) public virtual async Task OnConnectedAsync(HubLifetimeContext context, Func<HubLifetimeContext, Task> next)
{ {
var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>(); var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>();
using (currentPrincipalAccessor.Change((await GetDynamicClaimsPrincipalAsync(context.Context.User, context.ServiceProvider))!)) var claimsPrincipal = context.Context.User;
await HandleDynamicClaimsPrincipalAsync(claimsPrincipal, context.ServiceProvider, context.Context);
using (currentPrincipalAccessor.Change(claimsPrincipal!))
{ {
await next(context); await next(context);
} }
@ -31,27 +35,29 @@ public class AbpAuthenticationHubFilter : IHubFilter
public virtual async Task OnDisconnectedAsync(HubLifetimeContext context, Exception? exception, Func<HubLifetimeContext, Exception?, Task> next) public virtual async Task OnDisconnectedAsync(HubLifetimeContext context, Exception? exception, Func<HubLifetimeContext, Exception?, Task> next)
{ {
var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>(); var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>();
using (currentPrincipalAccessor.Change((await GetDynamicClaimsPrincipalAsync(context.Context.User, context.ServiceProvider))!)) var claimsPrincipal = context.Context.User;
await HandleDynamicClaimsPrincipalAsync(claimsPrincipal, context.ServiceProvider, context.Context);
using (currentPrincipalAccessor.Change(claimsPrincipal!))
{ {
await next(context, exception); await next(context, exception);
} }
} }
protected virtual async Task<ClaimsPrincipal?> GetDynamicClaimsPrincipalAsync(ClaimsPrincipal? claimsPrincipal, IServiceProvider serviceProvider) protected virtual async Task HandleDynamicClaimsPrincipalAsync(ClaimsPrincipal? claimsPrincipal, IServiceProvider serviceProvider, HubCallerContext hubCallerContext)
{ {
if (claimsPrincipal == null) if (claimsPrincipal?.Identity != null &&
{
return claimsPrincipal;
}
if (claimsPrincipal.Identity != null &&
claimsPrincipal.Identity.IsAuthenticated && claimsPrincipal.Identity.IsAuthenticated &&
serviceProvider.GetRequiredService<IOptions<AbpClaimsPrincipalFactoryOptions>>().Value.IsDynamicClaimsEnabled) serviceProvider.GetRequiredService<IOptions<AbpClaimsPrincipalFactoryOptions>>().Value.IsDynamicClaimsEnabled)
{ {
var abpClaimsPrincipalFactory = serviceProvider.GetRequiredService<IAbpClaimsPrincipalFactory>(); claimsPrincipal = claimsPrincipal.Identity is ClaimsIdentity identity
claimsPrincipal = await abpClaimsPrincipalFactory.CreateDynamicAsync(claimsPrincipal); ? new ClaimsPrincipal(new ClaimsIdentity(claimsPrincipal.Claims, claimsPrincipal.Identity.AuthenticationType, identity.NameClaimType, identity.RoleClaimType))
} : new ClaimsPrincipal(new ClaimsIdentity(claimsPrincipal.Claims, claimsPrincipal.Identity.AuthenticationType));
return claimsPrincipal; claimsPrincipal = await serviceProvider.GetRequiredService<IAbpClaimsPrincipalFactory>().CreateDynamicAsync(claimsPrincipal);
if (claimsPrincipal.Identity?.IsAuthenticated == false)
{
hubCallerContext.Abort();
}
}
} }
} }

Loading…
Cancel
Save