From 9f1a222d904358ac5a3c3c284b78c8074f24059a Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 9 Nov 2018 07:11:55 +1300 Subject: [PATCH] React to AuthorizationMiddleware (#8697) --- build/dependencies.props | 140 +++++++++--------- .../AuthorizationAppBuilderExtensions.cs | 21 --- .../AuthorizationEndpointConventionBuilder.cs | 16 -- .../AuthorizationMiddleware.cs | 52 ------- .../AuthorizeMetadataAttribute.cs | 20 --- .../MvcSandbox/Controllers/HomeController.cs | 1 - samples/MvcSandbox/Startup.cs | 3 - .../Authorization/AuthorizeFilterTest.cs | 3 + .../AuthorizeFilterIntegrationTest.cs | 5 + 9 files changed, 78 insertions(+), 183 deletions(-) delete mode 100644 samples/MvcSandbox/AuthorizationMiddleware/AuthorizationAppBuilderExtensions.cs delete mode 100644 samples/MvcSandbox/AuthorizationMiddleware/AuthorizationEndpointConventionBuilder.cs delete mode 100644 samples/MvcSandbox/AuthorizationMiddleware/AuthorizationMiddleware.cs delete mode 100644 samples/MvcSandbox/AuthorizationMiddleware/AuthorizeMetadataAttribute.cs diff --git a/build/dependencies.props b/build/dependencies.props index 778259b129..d09baf72d2 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -16,88 +16,88 @@ 0.43.0 2.1.1.1 2.1.1 - 3.0.0-alpha1-10657 + 3.0.0-preview-181106-14 3.0.0-alpha1-10670 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 + 3.0.0-preview-181106-14 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-a-alpha1-authz-middleware-16949 + 3.0.0-preview-181106-14 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 2.0.0 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 2.2.0-rtm-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10670 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 + 3.0.0-preview-181106-14 + 3.0.0-alpha1-10716 5.2.6 15.8.166 2.8.0 2.8.0 - 3.0.0-alpha1-10670 + 3.0.0-alpha1-10716 1.7.0 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10670 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 3.0.0-preview1-26907-05 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10670 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10657 - 3.0.0-alpha1-10670 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-alpha1-10716 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-preview-181106-14 + 3.0.0-alpha1-10716 2.0.9 2.1.3 3.0.0-preview1-26907-05 - 3.0.0-alpha1-10670 - 3.0.0-a-alpha1-sdk-31823 + 3.0.0-alpha1-10716 + 3.0.0-alpha1-10716 15.6.1 4.10.0 2.0.3 diff --git a/samples/MvcSandbox/AuthorizationMiddleware/AuthorizationAppBuilderExtensions.cs b/samples/MvcSandbox/AuthorizationMiddleware/AuthorizationAppBuilderExtensions.cs deleted file mode 100644 index 6a2fa48911..0000000000 --- a/samples/MvcSandbox/AuthorizationMiddleware/AuthorizationAppBuilderExtensions.cs +++ /dev/null @@ -1,21 +0,0 @@ -// 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 MvcSandbox.AuthorizationMiddleware; - -namespace Microsoft.AspNetCore.Builder -{ - public static class AuthorizationAppBuilderExtensions - { - public static IApplicationBuilder UseAuthorization(this IApplicationBuilder app) - { - if (app == null) - { - throw new ArgumentNullException(nameof(app)); - } - - return app.UseMiddleware(); - } - } -} \ No newline at end of file diff --git a/samples/MvcSandbox/AuthorizationMiddleware/AuthorizationEndpointConventionBuilder.cs b/samples/MvcSandbox/AuthorizationMiddleware/AuthorizationEndpointConventionBuilder.cs deleted file mode 100644 index 5fb990e1f3..0000000000 --- a/samples/MvcSandbox/AuthorizationMiddleware/AuthorizationEndpointConventionBuilder.cs +++ /dev/null @@ -1,16 +0,0 @@ -// 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 Microsoft.AspNetCore.Routing; - -namespace MvcSandbox.AuthorizationMiddleware -{ - public static class AuthorizationEndpointConventionBuilder - { - public static T RequireAuthorization(this T builder, params string[] roles) where T : IEndpointConventionBuilder - { - builder.Apply(model => model.Metadata.Add(new AuthorizeMetadataAttribute(roles))); - return builder; - } - } -} diff --git a/samples/MvcSandbox/AuthorizationMiddleware/AuthorizationMiddleware.cs b/samples/MvcSandbox/AuthorizationMiddleware/AuthorizationMiddleware.cs deleted file mode 100644 index 23a5e45f3f..0000000000 --- a/samples/MvcSandbox/AuthorizationMiddleware/AuthorizationMiddleware.cs +++ /dev/null @@ -1,52 +0,0 @@ -// 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 System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Http.Features; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Primitives; - -namespace MvcSandbox.AuthorizationMiddleware -{ - public class AuthorizationMiddleware - { - private readonly RequestDelegate _next; - - public AuthorizationMiddleware(RequestDelegate next) - { - if (next == null) - { - throw new ArgumentNullException(nameof(next)); - } - - _next = next; - } - - public async Task Invoke(HttpContext httpContext) - { - var endpoint = httpContext.Features.Get()?.Endpoint; - var metadata = endpoint?.Metadata?.GetMetadata(); - - // Only run authorization if endpoint has metadata - if (metadata != null) - { - // Check if role querystring value is a valid role - if (!httpContext.Request.Query.TryGetValue("role", out var role) || - !metadata.Roles.Contains(role.ToString(), StringComparer.OrdinalIgnoreCase)) - { - httpContext.Response.StatusCode = 401; - httpContext.Response.ContentType = "text/plain"; - await httpContext.Response.WriteAsync($"Unauthorized access to '{endpoint.DisplayName}'."); - return; - } - } - - await _next(httpContext); - } - } -} \ No newline at end of file diff --git a/samples/MvcSandbox/AuthorizationMiddleware/AuthorizeMetadataAttribute.cs b/samples/MvcSandbox/AuthorizationMiddleware/AuthorizeMetadataAttribute.cs deleted file mode 100644 index 7c95327d0d..0000000000 --- a/samples/MvcSandbox/AuthorizationMiddleware/AuthorizeMetadataAttribute.cs +++ /dev/null @@ -1,20 +0,0 @@ -// 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 System.Linq; -using System.Threading.Tasks; - -namespace MvcSandbox.AuthorizationMiddleware -{ - public class AuthorizeMetadataAttribute : Attribute - { - public AuthorizeMetadataAttribute(string[] roles) - { - Roles = roles; - } - - public string[] Roles { get; } - } -} diff --git a/samples/MvcSandbox/Controllers/HomeController.cs b/samples/MvcSandbox/Controllers/HomeController.cs index 87406bce53..2aa4ff6829 100644 --- a/samples/MvcSandbox/Controllers/HomeController.cs +++ b/samples/MvcSandbox/Controllers/HomeController.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.Mvc; -using MvcSandbox.AuthorizationMiddleware; namespace MvcSandbox.Controllers { diff --git a/samples/MvcSandbox/Startup.cs b/samples/MvcSandbox/Startup.cs index 12c1291669..5c241cdc32 100644 --- a/samples/MvcSandbox/Startup.cs +++ b/samples/MvcSandbox/Startup.cs @@ -12,7 +12,6 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using MvcSandbox.AuthorizationMiddleware; namespace MvcSandbox { @@ -47,8 +46,6 @@ namespace MvcSandbox app.UseDeveloperExceptionPage(); app.UseStaticFiles(); - app.UseAuthorization(); - app.UseEndpoint(); } diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Authorization/AuthorizeFilterTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Authorization/AuthorizeFilterTest.cs index fdde1514de..bff47d0533 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Authorization/AuthorizeFilterTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Authorization/AuthorizeFilterTest.cs @@ -236,6 +236,9 @@ namespace Microsoft.AspNetCore.Mvc.Authorization GetPolicyCalls++; return Task.FromResult(policyName == "true" ? _true : _false); } + + public Task GetRequiredPolicyAsync() + => Task.FromResult(null); } [Fact] diff --git a/test/Microsoft.AspNetCore.Mvc.IntegrationTests/AuthorizeFilterIntegrationTest.cs b/test/Microsoft.AspNetCore.Mvc.IntegrationTests/AuthorizeFilterIntegrationTest.cs index 596d5b8ab3..e7cf604430 100644 --- a/test/Microsoft.AspNetCore.Mvc.IntegrationTests/AuthorizeFilterIntegrationTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.IntegrationTests/AuthorizeFilterIntegrationTest.cs @@ -140,6 +140,11 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests }; return Task.FromResult(new AuthorizationPolicy(requirements, new string[] { })); } + + public Task GetRequiredPolicyAsync() + { + return Task.FromResult(null); + } } public class AuthorizeController