11using System . Collections . Concurrent ;
2+ using System . Diagnostics . CodeAnalysis ;
23using System . Reflection ;
34using System . Security . Claims ;
5+ using System . Threading . Channels ;
46using Microsoft . AspNetCore . SignalR ;
57
68namespace ManagedCode . Orleans . Identity . Client . Filters ;
@@ -10,6 +12,8 @@ public sealed class SignalRAuthorizationFilter : IHubFilter
1012 private static readonly ConcurrentDictionary < Type , Func < object , ClaimsPrincipal ? , object > ? > StreamWrappers = new ( ) ;
1113 private static readonly MethodInfo WrapStreamMethod = typeof ( SignalRAuthorizationFilter )
1214 . GetMethod ( nameof ( WrapStream ) , BindingFlags . NonPublic | BindingFlags . Static ) ! ;
15+ private static readonly MethodInfo WrapChannelMethod = typeof ( SignalRAuthorizationFilter )
16+ . GetMethod ( nameof ( WrapChannel ) , BindingFlags . NonPublic | BindingFlags . Static ) ! ;
1317
1418 public async ValueTask < object ? > InvokeMethodAsync (
1519 HubInvocationContext invocationContext ,
@@ -39,9 +43,22 @@ public sealed class SignalRAuthorizationFilter : IHubFilter
3943 }
4044
4145 var stream = IsAsyncEnumerable ( returnType ) ? returnType : Array . Find ( returnType . GetInterfaces ( ) , IsAsyncEnumerable ) ;
42- return stream is null ? null : WrapStreamMethod
43- . MakeGenericMethod ( stream . GenericTypeArguments [ 0 ] )
44- . CreateDelegate < Func < object , ClaimsPrincipal ? , object > > ( ) ;
46+ if ( stream is not null )
47+ {
48+ return WrapStreamMethod . MakeGenericMethod ( stream . GenericTypeArguments [ 0 ] )
49+ . CreateDelegate < Func < object , ClaimsPrincipal ? , object > > ( ) ;
50+ }
51+
52+ for ( Type ? type = returnType ; type is not null ; type = type . BaseType )
53+ {
54+ if ( type . IsConstructedGenericType && type . GetGenericTypeDefinition ( ) == typeof ( ChannelReader < > ) )
55+ {
56+ return WrapChannelMethod . MakeGenericMethod ( type . GenericTypeArguments [ 0 ] )
57+ . CreateDelegate < Func < object , ClaimsPrincipal ? , object > > ( ) ;
58+ }
59+ }
60+
61+ return null ;
4562 }
4663
4764 private static bool IsAsyncEnumerable ( Type type ) =>
@@ -83,6 +100,47 @@ public async ValueTask DisposeAsync()
83100 }
84101 }
85102
103+ private static object WrapChannel < T > ( object channel , ClaimsPrincipal ? user ) =>
104+ new ScopedChannelReader < T > ( ( ChannelReader < T > ) channel , user ) ;
105+
106+ private sealed class ScopedChannelReader < T > ( ChannelReader < T > reader , ClaimsPrincipal ? user ) : ChannelReader < T >
107+ {
108+ public override Task Completion => reader . Completion ;
109+ public override bool CanCount => reader . CanCount ;
110+ public override bool CanPeek => reader . CanPeek ;
111+ public override int Count => reader . Count ;
112+
113+ public override bool TryRead ( [ MaybeNullWhen ( false ) ] out T item )
114+ {
115+ using var scope = new OrleansRequestContextScope ( user ) ;
116+ return reader . TryRead ( out item ) ;
117+ }
118+
119+ public override bool TryPeek ( [ MaybeNullWhen ( false ) ] out T item )
120+ {
121+ using var scope = new OrleansRequestContextScope ( user ) ;
122+ return reader . TryPeek ( out item ) ;
123+ }
124+
125+ public override async ValueTask < bool > WaitToReadAsync ( CancellationToken cancellationToken = default )
126+ {
127+ using var scope = new OrleansRequestContextScope ( user ) ;
128+ return await reader . WaitToReadAsync ( cancellationToken ) ;
129+ }
130+
131+ public override async ValueTask < T > ReadAsync ( CancellationToken cancellationToken = default )
132+ {
133+ using var scope = new OrleansRequestContextScope ( user ) ;
134+ return await reader . ReadAsync ( cancellationToken ) ;
135+ }
136+
137+ public override IAsyncEnumerable < T > ReadAllAsync ( CancellationToken cancellationToken = default )
138+ {
139+ using var scope = new OrleansRequestContextScope ( user ) ;
140+ return new ScopedStream < T > ( reader . ReadAllAsync ( cancellationToken ) , user ) ;
141+ }
142+ }
143+
86144 public async Task OnConnectedAsync ( HubLifetimeContext context , Func < HubLifetimeContext , Task > next )
87145 {
88146 using var scope = new OrleansRequestContextScope ( context . Context . User ) ;
0 commit comments