From b960e07e37144ccd10512a5ac669f54d938b24ab Mon Sep 17 00:00:00 2001 From: Nikita Potapenko Date: Tue, 22 Oct 2019 20:28:17 +0300 Subject: [PATCH] Add hubType to HubInvocationContext (#14459) --- ...osoft.AspNetCore.SignalR.Core.netcoreapp.cs | 2 ++ .../server/Core/src/HubInvocationContext.cs | 18 ++++++++++++++++++ .../Core/src/Internal/DefaultHubDispatcher.cs | 2 +- .../SignalR/test/HubConnectionHandlerTests.cs | 1 + 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/SignalR/server/Core/ref/Microsoft.AspNetCore.SignalR.Core.netcoreapp.cs b/src/SignalR/server/Core/ref/Microsoft.AspNetCore.SignalR.Core.netcoreapp.cs index 5309dae32c..f266b11a88 100644 --- a/src/SignalR/server/Core/ref/Microsoft.AspNetCore.SignalR.Core.netcoreapp.cs +++ b/src/SignalR/server/Core/ref/Microsoft.AspNetCore.SignalR.Core.netcoreapp.cs @@ -176,9 +176,11 @@ namespace Microsoft.AspNetCore.SignalR public partial class HubInvocationContext { public HubInvocationContext(Microsoft.AspNetCore.SignalR.HubCallerContext context, string hubMethodName, object[] hubMethodArguments) { } + public HubInvocationContext(Microsoft.AspNetCore.SignalR.HubCallerContext context, System.Type hubType, string hubMethodName, object[] hubMethodArguments) { } public Microsoft.AspNetCore.SignalR.HubCallerContext Context { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } public System.Collections.Generic.IReadOnlyList HubMethodArguments { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } public string HubMethodName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public System.Type HubType { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } } public abstract partial class HubLifetimeManager where THub : Microsoft.AspNetCore.SignalR.Hub { diff --git a/src/SignalR/server/Core/src/HubInvocationContext.cs b/src/SignalR/server/Core/src/HubInvocationContext.cs index a62706d6a8..967db6ebb5 100644 --- a/src/SignalR/server/Core/src/HubInvocationContext.cs +++ b/src/SignalR/server/Core/src/HubInvocationContext.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using Microsoft.AspNetCore.Authorization; @@ -11,6 +12,18 @@ namespace Microsoft.AspNetCore.SignalR /// public class HubInvocationContext { + /// + /// Instantiates a new instance of the class. + /// + /// Context for the active Hub connection and caller. + /// The type of the Hub. + /// The name of the Hub method being invoked. + /// The arguments provided by the client. + public HubInvocationContext(HubCallerContext context, Type hubType, string hubMethodName, object[] hubMethodArguments): this(context, hubMethodName, hubMethodArguments) + { + HubType = hubType; + } + /// /// Instantiates a new instance of the class. /// @@ -29,6 +42,11 @@ namespace Microsoft.AspNetCore.SignalR /// public HubCallerContext Context { get; } + /// + /// Gets the Hub type. + /// + public Type HubType { get; } + /// /// Gets the name of the Hub method being invoked. /// diff --git a/src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs b/src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs index bc0f8c9fc5..8d54b6d0ae 100644 --- a/src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs +++ b/src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs @@ -495,7 +495,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal return TaskCache.True; } - return IsHubMethodAuthorizedSlow(provider, hubConnectionContext.User, policies, new HubInvocationContext(hubConnectionContext.HubCallerContext, hubMethodName, hubMethodArguments)); + return IsHubMethodAuthorizedSlow(provider, hubConnectionContext.User, policies, new HubInvocationContext(hubConnectionContext.HubCallerContext, typeof(THub), hubMethodName, hubMethodArguments)); } private static async Task IsHubMethodAuthorizedSlow(IServiceProvider provider, ClaimsPrincipal principal, IList policies, HubInvocationContext resource) diff --git a/src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs b/src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs index c88f593db2..5c6f9d9def 100644 --- a/src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs +++ b/src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs @@ -2215,6 +2215,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests { Assert.NotNull(context.Resource); var resource = Assert.IsType(context.Resource); + Assert.Equal(typeof(MethodHub), resource.HubType); Assert.Equal(nameof(MethodHub.MultiParamAuthMethod), resource.HubMethodName); Assert.Equal(2, resource.HubMethodArguments?.Count); Assert.Equal("Hello", resource.HubMethodArguments[0]);