|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Security.Claims; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +using Microsoft.AspNetCore.Authorization; |
| 10 | +using Microsoft.AspNetCore.Builder; |
| 11 | +using Microsoft.AspNetCore.Routing.Patterns; |
| 12 | +using Microsoft.AspNetCore.Routing; |
| 13 | +using Microsoft.Extensions.DependencyInjection; |
| 14 | +using Microsoft.Extensions.Hosting; |
| 15 | +using Microsoft.IdentityModel.Tokens; |
| 16 | + |
| 17 | +using Xunit; |
| 18 | + |
| 19 | +namespace Microsoft.Identity.Web.Test.Resource |
| 20 | +{ |
| 21 | + public class RequiredScopeOrAppPermissionExtensionsTests |
| 22 | + { |
| 23 | + |
| 24 | + private const string PolicyName = "foo"; |
| 25 | + private const string AppPermission = "access_as_app"; |
| 26 | + private const string Scope = "user.read"; |
| 27 | + |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public async Task RequireScopeOrAppPermission_WithAppPermission_SucceedsAsync() |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + var authorizationService = BuildAuthorizationService(PolicyName, null, Scope); |
| 34 | + |
| 35 | + var user = new ClaimsPrincipal( |
| 36 | + new CaseSensitiveClaimsIdentity([new Claim(ClaimConstants.Role, AppPermission)])); |
| 37 | + |
| 38 | + var testBuilder = new TestEndpointConventionBuilder() |
| 39 | + .RequireScopeOrAppPermission([], [AppPermission]); |
| 40 | + |
| 41 | + var convention = Assert.Single(testBuilder.Conventions); |
| 42 | + |
| 43 | + var endpointModel = new RouteEndpointBuilder((context) => Task.CompletedTask, RoutePatternFactory.Parse("/"), 0); |
| 44 | + convention(endpointModel); |
| 45 | + var endpoint = endpointModel.Build(); |
| 46 | + |
| 47 | + // Act |
| 48 | + var allowed = await authorizationService.AuthorizeAsync(user, endpoint, PolicyName); |
| 49 | + |
| 50 | + // Assert |
| 51 | + Assert.True(allowed.Succeeded); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + private static IAuthorizationService BuildAuthorizationService(string policy, string? appPermission, string? scope) |
| 56 | + { |
| 57 | + IHostBuilder hostBuilder = Host.CreateDefaultBuilder() |
| 58 | + .ConfigureServices(services => |
| 59 | + { |
| 60 | + services.AddAuthorization(options => |
| 61 | + { |
| 62 | + options.AddPolicy(policy, policyBuilder => |
| 63 | + { |
| 64 | + policyBuilder.RequireScopeOrAppPermission(scope?.Split(' ')!, appPermission?.Split(' ')!); |
| 65 | + }); |
| 66 | + }); |
| 67 | + services.AddLogging(); |
| 68 | + services.AddOptions(); |
| 69 | + services.AddRequiredScopeOrAppPermissionAuthorization(); |
| 70 | + }); |
| 71 | + |
| 72 | + var provider = hostBuilder.Build().Services; |
| 73 | + return provider.GetRequiredService<IAuthorizationService>(); |
| 74 | + } |
| 75 | + |
| 76 | + private class TestEndpointConventionBuilder : IEndpointConventionBuilder |
| 77 | + { |
| 78 | + public List<Action<EndpointBuilder>> Conventions { get; } = []; |
| 79 | + |
| 80 | + public void Add(Action<EndpointBuilder> convention) |
| 81 | + { |
| 82 | + Conventions.Add(convention); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments