Skip to content

Commit d1936af

Browse files
committed
added missing header and test
Signed-off-by: Neil South <neil.south@answerdigital.com>
1 parent 0e279a0 commit d1936af

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

src/Authentication/Configurations/BasicAuthOptions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-

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+
*/
216

317
using Microsoft.Extensions.Configuration;
418

src/Authentication/Middleware/BasicAuthorizationMiddleware.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Microsoft.Extensions.Logging;
2323
using Microsoft.Extensions.Options;
2424
using Monai.Deploy.Security.Authentication.Configurations;
25+
using Monai.Deploy.Security.Authentication.Extensions;
2526

2627
namespace Monai.Deploy.Security.Authentication.Middleware
2728
{
@@ -56,18 +57,21 @@ public async Task InvokeAsync(HttpContext httpContext)
5657
try
5758
{
5859
var authHeader = AuthenticationHeaderValue.Parse(httpContext.Request.Headers["Authorization"]);
59-
var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
60-
var credentials = Encoding.UTF8.GetString(credentialBytes).Split(':', 2);
61-
var username = credentials[0];
62-
var password = credentials[1];
63-
if (string.Compare(username, _options.Value.BasicAuth.Id, false) is 0 &&
64-
string.Compare(password, _options.Value.BasicAuth.Password, false) is 0)
60+
if (authHeader.Scheme == "Basic")
6561
{
66-
var claims = new[] { new Claim("name", credentials[0]) };
67-
var identity = new ClaimsIdentity(claims, "Basic");
68-
var claimsPrincipal = new ClaimsPrincipal(identity);
69-
httpContext.User = claimsPrincipal;
70-
return;
62+
var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
63+
var credentials = Encoding.UTF8.GetString(credentialBytes).Split(':', 2);
64+
var username = credentials[0];
65+
var password = credentials[1];
66+
if (string.Compare(username, _options.Value.BasicAuth.Id, false) is 0 &&
67+
string.Compare(password, _options.Value.BasicAuth.Password, false) is 0)
68+
{
69+
var claims = new[] { new Claim("name", credentials[0]) };
70+
var identity = new ClaimsIdentity(claims, "Basic");
71+
var claimsPrincipal = new ClaimsPrincipal(identity);
72+
httpContext.User = claimsPrincipal;
73+
return;
74+
}
7175
}
7276
}
7377
catch (Exception ex)

src/Authentication/Tests/EndpointAuthorizationMiddlewareTest.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public async Task GivenConfigurationFileWithBasicConfigured_WhenUserIsNotAuthent
152152
}
153153

154154
[Fact]
155-
public async Task GivenConfigurationFileWithBasicConfigured_WhenUserIsAuthenticated_ExpectToDenyRequest()
155+
public async Task GivenConfigurationFileWithBasicConfigured_WhenUserIsAuthenticated_ExpectToAllowRequest()
156156
{
157157
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(false);
158158

@@ -165,6 +165,20 @@ public async Task GivenConfigurationFileWithBasicConfigured_WhenUserIsAuthentica
165165

166166
Assert.Equal(HttpStatusCode.OK, responseMessage.StatusCode);
167167
}
168+
[Fact]
169+
public async Task GivenConfigurationFileWithBasicConfigured_WhenHeaderIsInvalid_ExpectToDenyRequest()
170+
{
171+
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(false);
172+
173+
var server = host.GetTestServer();
174+
server.BaseAddress = new Uri("https://example.com/");
175+
176+
var client = server.CreateClient();
177+
client.DefaultRequestHeaders.Add("Authorization", $"BasicBad {Convert.ToBase64String(Encoding.UTF8.GetBytes("user:pass"))}");
178+
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
179+
180+
Assert.Equal(HttpStatusCode.Unauthorized, responseMessage.StatusCode);
181+
}
168182

169183
private static Action<IWebHostBuilder> SetupWebServer(string configFile) => webBuilder =>
170184
{

0 commit comments

Comments
 (0)