Fix inherited hub auth and added tests (#535)
This commit is contained in:
parent
680c48d584
commit
1fad901843
|
|
@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.SignalR
|
||||||
public void MapHub<THub>(string path, Action<HttpSocketOptions> socketOptions) where THub : Hub<IClientProxy>
|
public void MapHub<THub>(string path, Action<HttpSocketOptions> socketOptions) where THub : Hub<IClientProxy>
|
||||||
{
|
{
|
||||||
// find auth attributes
|
// find auth attributes
|
||||||
var authorizeAttribute = typeof(THub).GetCustomAttribute<AuthorizeAttribute>();
|
var authorizeAttribute = typeof(THub).GetCustomAttribute<AuthorizeAttribute>(inherit: true);
|
||||||
var options = new HttpSocketOptions();
|
var options = new HttpSocketOptions();
|
||||||
if (authorizeAttribute != null)
|
if (authorizeAttribute != null)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
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);
|
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<AuthHub>("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<InheritedAuthHub>("path", httpSocketOptions =>
|
||||||
|
{
|
||||||
|
authCount += httpSocketOptions.AuthorizationData.Count;
|
||||||
|
}));
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
Assert.Equal(1, authCount);
|
||||||
|
}
|
||||||
|
|
||||||
private class InvalidHub : Hub
|
private class InvalidHub : Hub
|
||||||
{
|
{
|
||||||
public void OverloadedMethod(int num)
|
public void OverloadedMethod(int num)
|
||||||
|
|
@ -39,5 +84,14 @@ namespace Microsoft.AspNetCore.SignalR.Tests
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class InheritedAuthHub : AuthHub
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
private class AuthHub : Hub
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue