From 1fad9018438bd81913a2709d8a03d91a75ba4a39 Mon Sep 17 00:00:00 2001 From: BrennanConroy Date: Tue, 6 Jun 2017 23:55:34 -0700 Subject: [PATCH] Fix inherited hub auth and added tests (#535) --- .../HubRouteBuilder.cs | 2 +- .../MapSignalRTests.cs | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.SignalR.Http/HubRouteBuilder.cs b/src/Microsoft.AspNetCore.SignalR.Http/HubRouteBuilder.cs index 53369bd3ee..7e720b2fbd 100644 --- a/src/Microsoft.AspNetCore.SignalR.Http/HubRouteBuilder.cs +++ b/src/Microsoft.AspNetCore.SignalR.Http/HubRouteBuilder.cs @@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.SignalR public void MapHub(string path, Action socketOptions) where THub : Hub { // find auth attributes - var authorizeAttribute = typeof(THub).GetCustomAttribute(); + var authorizeAttribute = typeof(THub).GetCustomAttribute(inherit: true); var options = new HttpSocketOptions(); if (authorizeAttribute != null) { diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/MapSignalRTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/MapSignalRTests.cs index aeabb2501a..0b097e0101 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/MapSignalRTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/MapSignalRTests.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; @@ -29,6 +30,50 @@ namespace Microsoft.AspNetCore.SignalR.Tests Assert.Equal("Duplicate definitions of 'OverloadedMethod'. Overloading is not supported.", ex.Message); } + [Fact] + public void MapHubFindsAuthAttributeOnHub() + { + var authCount = 0; + var builder = new WebHostBuilder() + .UseKestrel() + .ConfigureServices(services => + { + services.AddSignalR(); + }) + .Configure(app => + { + app.UseSignalR(options => options.MapHub("path", httpSocketOptions => + { + authCount += httpSocketOptions.AuthorizationData.Count; + })); + }) + .Build(); + + Assert.Equal(1, authCount); + } + + [Fact] + public void MapHubFindsAuthAttributeOnInheritedHub() + { + var authCount = 0; + var builder = new WebHostBuilder() + .UseKestrel() + .ConfigureServices(services => + { + services.AddSignalR(); + }) + .Configure(app => + { + app.UseSignalR(options => options.MapHub("path", httpSocketOptions => + { + authCount += httpSocketOptions.AuthorizationData.Count; + })); + }) + .Build(); + + Assert.Equal(1, authCount); + } + private class InvalidHub : Hub { public void OverloadedMethod(int num) @@ -39,5 +84,14 @@ namespace Microsoft.AspNetCore.SignalR.Tests { } } + + private class InheritedAuthHub : AuthHub + { + } + + [Authorize] + private class AuthHub : Hub + { + } } }