Skip to content

Commit

Permalink
Checking dynamic claims at intervals rather than every time.
Browse files Browse the repository at this point in the history
Related to #19579 and #19605
  • Loading branch information
maliming committed May 8, 2024
1 parent 5ceed1a commit 658ae6e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
namespace Volo.Abp.AspNetCore.SignalR;
using System;

namespace Volo.Abp.AspNetCore.SignalR;

public class AbpSignalROptions
{
public HubConfigList Hubs { get; }

/// <summary>
/// Default: 5 seconds.
/// </summary>
public TimeSpan? CheckDynamicClaimsInterval { get; set; }

public AbpSignalROptions()
{
Hubs = new HubConfigList();
CheckDynamicClaimsInterval = TimeSpan.FromSeconds(5);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp.Security.Claims;

Expand Down Expand Up @@ -49,6 +51,20 @@ protected virtual async Task HandleDynamicClaimsPrincipalAsync(ClaimsPrincipal?
claimsPrincipal.Identity.IsAuthenticated &&
serviceProvider.GetRequiredService<IOptions<AbpClaimsPrincipalFactoryOptions>>().Value.IsDynamicClaimsEnabled)
{
var checkDynamicClaimsInterval = serviceProvider.GetRequiredService<IOptions<AbpSignalROptions>>().Value.CheckDynamicClaimsInterval;
if (checkDynamicClaimsInterval.HasValue &&
hubCallerContext.Items.TryGetValue(nameof(HandleDynamicClaimsPrincipalAsync), out var lastCheckDynamicClaimsTime) &&
lastCheckDynamicClaimsTime is DateTime lastCheckDynamicClaimsTimeValue)
{
if (DateTime.UtcNow.Subtract(lastCheckDynamicClaimsTimeValue) < checkDynamicClaimsInterval.Value)
{
// Dynamic claims are not checked because the interval has not passed yet.
return;
}
}

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));
Expand Down

0 comments on commit 658ae6e

Please sign in to comment.