Skip to content

Commit f0df10f

Browse files
AuthorizeHelper will no-op if endpoint routing is used (#10471)
1 parent 2567233 commit f0df10f

26 files changed

+761
-598
lines changed

src/Analyzers/Analyzers/test/TestFiles/CompilationFeatureDetectorTest/StartupWithUseSignalR.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ public class StartupWithUseSignalR
99
{
1010
public void Configure(IApplicationBuilder app)
1111
{
12+
#pragma warning disable CS0618 // Type or member is obsolete
1213
app.UseSignalR(routes =>
1314
{
1415

1516
});
17+
#pragma warning restore CS0618 // Type or member is obsolete
1618
}
1719
}
1820
}

src/SignalR/clients/csharp/Client/test/FunctionalTests/VersionStartup.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.IdentityModel.Tokens.Jwt;
6-
using System.Security.Claims;
7-
using Microsoft.AspNetCore.Authentication.JwtBearer;
84
using Microsoft.AspNetCore.Builder;
9-
using Microsoft.AspNetCore.Http;
10-
using Microsoft.AspNetCore.Http.Connections;
115
using Microsoft.AspNetCore.SignalR.Protocol;
126
using Microsoft.Extensions.DependencyInjection;
137
using Microsoft.Extensions.DependencyInjection.Extensions;
14-
using Microsoft.IdentityModel.Tokens;
15-
using Newtonsoft.Json;
168

179
namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
1810
{
@@ -33,11 +25,12 @@ public void ConfigureServices(IServiceCollection services)
3325

3426
public void Configure(IApplicationBuilder app)
3527
{
28+
app.UseRouting();
3629
app.UseAuthentication();
3730

38-
app.UseSignalR(routes =>
31+
app.UseEndpoints(endpoints =>
3932
{
40-
routes.MapHub<VersionHub>("/version");
33+
endpoints.MapHub<VersionHub>("/version");
4134
});
4235
}
4336
}

src/SignalR/common/Http.Connections/ref/Microsoft.AspNetCore.Http.Connections.netcoreapp3.0.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static partial class ConnectionEndpointRouteBuilderExtensions
1212
}
1313
public static partial class ConnectionsAppBuilderExtensions
1414
{
15+
[System.ObsoleteAttribute("This method is obsolete and will be removed in a future version. The recommended alternative is to use MapConnections or MapConnectionHandler<TConnectionHandler> inside Microsoft.AspNetCore.Builder.UseEndpoints(...).")]
1516
public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseConnections(this Microsoft.AspNetCore.Builder.IApplicationBuilder app, System.Action<Microsoft.AspNetCore.Http.Connections.ConnectionsRouteBuilder> configure) { throw null; }
1617
}
1718
}
@@ -28,6 +29,7 @@ public partial class ConnectionOptionsSetup : Microsoft.Extensions.Options.IConf
2829
public ConnectionOptionsSetup() { }
2930
public void Configure(Microsoft.AspNetCore.Http.Connections.ConnectionOptions options) { }
3031
}
32+
[System.ObsoleteAttribute("This class is obsolete and will be removed in a future version. The recommended alternative is to use MapConnection and MapConnectionHandler<TConnectionHandler> inside Microsoft.AspNetCore.Builder.UseEndpoints(...).")]
3133
public partial class ConnectionsRouteBuilder
3234
{
3335
internal ConnectionsRouteBuilder() { }

src/SignalR/common/Http.Connections/src/ConnectionEndpointRouteBuilderExtensions.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
7-
using Microsoft.AspNetCore.Authorization;
86
using Microsoft.AspNetCore.Connections;
97
using Microsoft.AspNetCore.Http.Connections;
108
using Microsoft.AspNetCore.Http.Connections.Internal;
@@ -48,20 +46,14 @@ public static IEndpointConventionBuilder MapConnectionHandler<TConnectionHandler
4846
public static IEndpointConventionBuilder MapConnectionHandler<TConnectionHandler>(this IEndpointRouteBuilder endpoints, string pattern, Action<HttpConnectionDispatcherOptions> configureOptions) where TConnectionHandler : ConnectionHandler
4947
{
5048
var options = new HttpConnectionDispatcherOptions();
51-
// REVIEW: WE should consider removing this and instead just relying on the
52-
// AuthorizationMiddleware
53-
var attributes = typeof(TConnectionHandler).GetCustomAttributes(inherit: true);
54-
foreach (var attribute in attributes.OfType<AuthorizeAttribute>())
55-
{
56-
options.AuthorizationData.Add(attribute);
57-
}
5849
configureOptions?.Invoke(options);
5950

6051
var conventionBuilder = endpoints.MapConnections(pattern, options, b =>
6152
{
6253
b.UseConnectionHandler<TConnectionHandler>();
6354
});
6455

56+
var attributes = typeof(TConnectionHandler).GetCustomAttributes(inherit: true);
6557
conventionBuilder.Add(e =>
6658
{
6759
// Add all attributes on the ConnectionHandler has metadata (this will allow for things like)
@@ -93,7 +85,7 @@ public static IEndpointConventionBuilder MapConnections(this IEndpointRouteBuild
9385
var connectionDelegate = connectionBuilder.Build();
9486

9587
// REVIEW: Consider expanding the internals of the dispatcher as endpoint routes instead of
96-
// using if statemants we can let the matcher handle
88+
// using if statements we can let the matcher handle
9789

9890
var conventionBuilders = new List<IEndpointConventionBuilder>();
9991

src/SignalR/common/Http.Connections/src/ConnectionsAppBuilderExtensions.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33

44
using System;
55
using Microsoft.AspNetCore.Http.Connections;
6-
using Microsoft.AspNetCore.Http.Connections.Internal;
7-
using Microsoft.AspNetCore.Routing;
8-
using Microsoft.Extensions.DependencyInjection;
96

107
namespace Microsoft.AspNetCore.Builder
118
{
@@ -16,25 +13,29 @@ public static class ConnectionsAppBuilderExtensions
1613
{
1714
/// <summary>
1815
/// Adds support for ASP.NET Core Connection Handlers to the <see cref="IApplicationBuilder"/> request execution pipeline.
16+
/// <para>
17+
/// This method is obsolete and will be removed in a future version.
18+
/// The recommended alternative is to use MapConnections or MapConnectionHandler&#60;TConnectionHandler&#62; inside Microsoft.AspNetCore.Builder.UseEndpoints(...).
19+
/// </para>
1920
/// </summary>
2021
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
2122
/// <param name="configure">A callback to configure connection routes.</param>
2223
/// <returns>The same instance of the <see cref="IApplicationBuilder"/> for chaining.</returns>
24+
[Obsolete("This method is obsolete and will be removed in a future version. The recommended alternative is to use MapConnections or MapConnectionHandler<TConnectionHandler> inside Microsoft.AspNetCore.Builder.UseEndpoints(...).")]
2325
public static IApplicationBuilder UseConnections(this IApplicationBuilder app, Action<ConnectionsRouteBuilder> configure)
2426
{
2527
if (configure == null)
2628
{
2729
throw new ArgumentNullException(nameof(configure));
2830
}
2931

30-
var dispatcher = app.ApplicationServices.GetRequiredService<HttpConnectionDispatcher>();
31-
32-
var routes = new RouteBuilder(app);
33-
34-
configure(new ConnectionsRouteBuilder(routes, dispatcher));
35-
3632
app.UseWebSockets();
37-
app.UseRouter(routes.Build());
33+
app.UseRouting();
34+
app.UseAuthorization();
35+
app.UseEndpoints(endpoints =>
36+
{
37+
configure(new ConnectionsRouteBuilder(endpoints));
38+
});
3839
return app;
3940
}
4041
}

src/SignalR/common/Http.Connections/src/ConnectionsRouteBuilder.cs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Reflection;
6-
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Builder;
76
using Microsoft.AspNetCore.Connections;
8-
using Microsoft.AspNetCore.Http.Connections.Internal;
97
using Microsoft.AspNetCore.Routing;
108

119
namespace Microsoft.AspNetCore.Http.Connections
1210
{
1311
/// <summary>
1412
/// Maps routes to ASP.NET Core Connection Handlers.
13+
/// <para>
14+
/// This class is obsolete and will be removed in a future version.
15+
/// The recommended alternative is to use MapConnection and MapConnectionHandler&#60;TConnectionHandler&#62; inside Microsoft.AspNetCore.Builder.UseEndpoints(...).
16+
/// </para>
1517
/// </summary>
18+
[Obsolete("This class is obsolete and will be removed in a future version. The recommended alternative is to use MapConnection and MapConnectionHandler<TConnectionHandler> inside Microsoft.AspNetCore.Builder.UseEndpoints(...).")]
1619
public class ConnectionsRouteBuilder
1720
{
18-
private readonly HttpConnectionDispatcher _dispatcher;
19-
private readonly RouteBuilder _routes;
21+
private readonly IEndpointRouteBuilder _endpoints;
2022

21-
internal ConnectionsRouteBuilder(RouteBuilder routes, HttpConnectionDispatcher dispatcher)
23+
internal ConnectionsRouteBuilder(IEndpointRouteBuilder endpoints)
2224
{
23-
_routes = routes;
24-
_dispatcher = dispatcher;
25+
_endpoints = endpoints;
2526
}
2627

2728
/// <summary>
@@ -38,45 +39,24 @@ public void MapConnections(PathString path, Action<IConnectionBuilder> configure
3839
/// <param name="path">The request path.</param>
3940
/// <param name="options">Options used to configure the connection.</param>
4041
/// <param name="configure">A callback to configure the connection.</param>
41-
public void MapConnections(PathString path, HttpConnectionDispatcherOptions options, Action<IConnectionBuilder> configure)
42-
{
43-
var connectionBuilder = new ConnectionBuilder(_routes.ServiceProvider);
44-
configure(connectionBuilder);
45-
var socket = connectionBuilder.Build();
46-
_routes.MapRoute(path, c => _dispatcher.ExecuteAsync(c, options, socket));
47-
_routes.MapRoute(path + "/negotiate", c => _dispatcher.ExecuteNegotiateAsync(c, options));
48-
}
42+
public void MapConnections(PathString path, HttpConnectionDispatcherOptions options, Action<IConnectionBuilder> configure) =>
43+
_endpoints.MapConnections(path, options, configure);
4944

5045
/// <summary>
5146
/// Maps incoming requests with the specified path to the provided connection pipeline.
5247
/// </summary>
5348
/// <typeparam name="TConnectionHandler">The <see cref="ConnectionHandler"/> type.</typeparam>
5449
/// <param name="path">The request path.</param>
55-
public void MapConnectionHandler<TConnectionHandler>(PathString path) where TConnectionHandler : ConnectionHandler
56-
{
50+
public void MapConnectionHandler<TConnectionHandler>(PathString path) where TConnectionHandler : ConnectionHandler =>
5751
MapConnectionHandler<TConnectionHandler>(path, configureOptions: null);
58-
}
5952

6053
/// <summary>
6154
/// Maps incoming requests with the specified path to the provided connection pipeline.
6255
/// </summary>
6356
/// <typeparam name="TConnectionHandler">The <see cref="ConnectionHandler"/> type.</typeparam>
6457
/// <param name="path">The request path.</param>
6558
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
66-
public void MapConnectionHandler<TConnectionHandler>(PathString path, Action<HttpConnectionDispatcherOptions> configureOptions) where TConnectionHandler : ConnectionHandler
67-
{
68-
var authorizeAttributes = typeof(TConnectionHandler).GetCustomAttributes<AuthorizeAttribute>(inherit: true);
69-
var options = new HttpConnectionDispatcherOptions();
70-
foreach (var attribute in authorizeAttributes)
71-
{
72-
options.AuthorizationData.Add(attribute);
73-
}
74-
configureOptions?.Invoke(options);
75-
76-
MapConnections(path, options, builder =>
77-
{
78-
builder.UseConnectionHandler<TConnectionHandler>();
79-
});
80-
}
59+
public void MapConnectionHandler<TConnectionHandler>(PathString path, Action<HttpConnectionDispatcherOptions> configureOptions) where TConnectionHandler : ConnectionHandler =>
60+
_endpoints.MapConnectionHandler<TConnectionHandler>(path, configureOptions);
8161
}
8262
}

src/SignalR/common/Http.Connections/src/Internal/AuthorizeHelper.cs

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/SignalR/common/Http.Connections/src/Internal/HttpConnectionDispatcher.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ public async Task ExecuteAsync(HttpContext context, HttpConnectionDispatcherOpti
6161
var logScope = new ConnectionLogScope(GetConnectionId(context));
6262
using (_logger.BeginScope(logScope))
6363
{
64-
if (!await AuthorizeHelper.AuthorizeAsync(context, options.AuthorizationData))
65-
{
66-
return;
67-
}
68-
6964
if (HttpMethods.IsPost(context.Request.Method))
7065
{
7166
// POST /{path}
@@ -95,11 +90,6 @@ public async Task ExecuteNegotiateAsync(HttpContext context, HttpConnectionDispa
9590
var logScope = new ConnectionLogScope(connectionId: string.Empty);
9691
using (_logger.BeginScope(logScope))
9792
{
98-
if (!await AuthorizeHelper.AuthorizeAsync(context, options.AuthorizationData))
99-
{
100-
return;
101-
}
102-
10393
if (HttpMethods.IsPost(context.Request.Method))
10494
{
10595
// POST /{path}/negotiate

src/SignalR/common/Http.Connections/src/Microsoft.AspNetCore.Http.Connections.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<Compile Include="$(SignalRSharedSourceRoot)WebSocketExtensions.cs" Link="WebSocketExtensions.cs" />
1515
<Compile Include="$(SignalRSharedSourceRoot)StreamExtensions.cs" Link="StreamExtensions.cs" />
1616
<Compile Include="$(SignalRSharedSourceRoot)DuplexPipe.cs" Link="DuplexPipe.cs" />
17+
<Compile Include="$(SignalRSharedSourceRoot)TaskCache.cs" Link="Internal\TaskCache.cs" />
1718
</ItemGroup>
1819

1920
<ItemGroup>

0 commit comments

Comments
 (0)