|
| 1 | +/* |
| 2 | + * Copyright 2022 MONAI Consortium |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using System.Net; |
| 18 | +using System.Text; |
| 19 | +using Microsoft.AspNetCore.Authentication.JwtBearer; |
| 20 | +using Microsoft.AspNetCore.TestHost; |
| 21 | +using Microsoft.IdentityModel.Protocols.OpenIdConnect; |
| 22 | +using Monai.Deploy.Security.Authentication.Configurations; |
| 23 | +using Monai.Deploy.Security.Authentication.Extensions; |
| 24 | + |
| 25 | +namespace Monai.Deploy.Security.Authentication.Tests |
| 26 | +{ |
| 27 | + public partial class BasicAuthorizationMiddlewareTest |
| 28 | + { |
| 29 | + |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public async Task GivenConfigurationFileToBypassAuthentication_ExpectToBypassAuthentication() |
| 33 | + { |
| 34 | + using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basicbypass.json")).StartAsync().ConfigureAwait(false); |
| 35 | + |
| 36 | + var server = host.GetTestServer(); |
| 37 | + server.BaseAddress = new Uri("https://example.com/"); |
| 38 | + |
| 39 | + var client = server.CreateClient(); |
| 40 | + var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false); |
| 41 | + |
| 42 | + Assert.True(responseMessage.IsSuccessStatusCode); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public async Task GivenConfigurationFileWithBasicConfigured_WhenUserIsNotAuthenticated_ExpectToDenyRequest() |
| 49 | + { |
| 50 | + using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(false); |
| 51 | + |
| 52 | + var server = host.GetTestServer(); |
| 53 | + server.BaseAddress = new Uri("https://example.com/"); |
| 54 | + |
| 55 | + var client = server.CreateClient(); |
| 56 | + var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false); |
| 57 | + |
| 58 | + Assert.Equal(HttpStatusCode.Unauthorized, responseMessage.StatusCode); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public async Task GivenConfigurationFileWithBasicConfigured_WhenUserIsAuthenticated_ExpectToAllowRequest() |
| 63 | + { |
| 64 | + using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(false); |
| 65 | + |
| 66 | + var server = host.GetTestServer(); |
| 67 | + server.BaseAddress = new Uri("https://example.com/"); |
| 68 | + |
| 69 | + var client = server.CreateClient(); |
| 70 | + client.DefaultRequestHeaders.Add("Authorization", $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes("user:pass"))}"); |
| 71 | + var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false); |
| 72 | + |
| 73 | + Assert.Equal(HttpStatusCode.OK, responseMessage.StatusCode); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public async Task GivenConfigurationFileWithBasicConfigured_WhenHeaderIsInvalid_ExpectToDenyRequest() |
| 78 | + { |
| 79 | + using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(false); |
| 80 | + |
| 81 | + var server = host.GetTestServer(); |
| 82 | + server.BaseAddress = new Uri("https://example.com/"); |
| 83 | + |
| 84 | + var client = server.CreateClient(); |
| 85 | + client.DefaultRequestHeaders.Add("Authorization", $"BasicBad {Convert.ToBase64String(Encoding.UTF8.GetBytes("user:pass"))}"); |
| 86 | + var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false); |
| 87 | + |
| 88 | + Assert.Equal(HttpStatusCode.Unauthorized, responseMessage.StatusCode); |
| 89 | + } |
| 90 | + |
| 91 | + private static Action<IWebHostBuilder> SetupWebServer(string configFile) => webBuilder => |
| 92 | + { |
| 93 | + webBuilder |
| 94 | + .ConfigureAppConfiguration((builderContext, config) => |
| 95 | + { |
| 96 | + config.Sources.Clear(); |
| 97 | + config.AddJsonFile(configFile, optional: false); |
| 98 | + _ = config.Build(); |
| 99 | + }) |
| 100 | + .ConfigureLogging((hostContext, logging) => |
| 101 | + { |
| 102 | + logging.AddDebug(); |
| 103 | + logging.AddConsole(); |
| 104 | + }) |
| 105 | + .ConfigureServices((hostContext, services) => |
| 106 | + { |
| 107 | + services.AddOptions<AuthenticationOptions>().Bind(hostContext.Configuration.GetSection("MonaiDeployAuthentication")); |
| 108 | + |
| 109 | + services.AddControllers(); |
| 110 | + services.AddMonaiAuthentication(); |
| 111 | + |
| 112 | + services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, op => |
| 113 | + { |
| 114 | + var config = new OpenIdConnectConfiguration() |
| 115 | + { |
| 116 | + Issuer = Guid.NewGuid().ToString(), |
| 117 | + }; |
| 118 | + |
| 119 | + config.SigningKeys.Add(EndpointAuthorizationMiddlewareTest.MockJwtTokenHandler.SecurityKey); |
| 120 | + op.Configuration = config; |
| 121 | + }); |
| 122 | + }) |
| 123 | + .Configure(app => |
| 124 | + { |
| 125 | + app.UseHttpsRedirection(); |
| 126 | + app.UseRouting(); |
| 127 | + app.UseAuthentication(); |
| 128 | + app.UseAuthorization(); |
| 129 | + app.UseEndpointAuthorizationMiddleware(); |
| 130 | + app.UseEndpoints(endpoints => |
| 131 | + { |
| 132 | + endpoints.MapControllers(); |
| 133 | + }); |
| 134 | + }) |
| 135 | + .UseTestServer(); |
| 136 | + }; |
| 137 | + } |
| 138 | +} |
0 commit comments