-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Project-MONAI/nds_addbasiceauth
adding basic auth
- Loading branch information
Showing
11 changed files
with
244 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2022 MONAI Consortium | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Monai.Deploy.Security.Authentication.Configurations | ||
{ | ||
public class BasicAuthOptions | ||
{ | ||
[ConfigurationKeyName("userName")] | ||
public string? Id { get; set; } | ||
|
||
[ConfigurationKeyName("password")] | ||
public string? Password { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/Authentication/Middleware/BasicAuthorizationMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2022 MONAI Consortium | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System.Net; | ||
using System.Net.Http.Headers; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Monai.Deploy.Security.Authentication.Configurations; | ||
using Monai.Deploy.Security.Authentication.Extensions; | ||
|
||
namespace Monai.Deploy.Security.Authentication.Middleware | ||
{ | ||
/// <summary> | ||
/// EndpointAuthorizationMiddleware for checking endpoint configuration. | ||
/// </summary> | ||
public class BasicAuthorizationMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly IOptions<AuthenticationOptions> _options; | ||
private readonly ILogger<BasicAuthorizationMiddleware> _logger; | ||
|
||
public BasicAuthorizationMiddleware( | ||
RequestDelegate next, | ||
IOptions<AuthenticationOptions> options, | ||
ILogger<BasicAuthorizationMiddleware> logger) | ||
{ | ||
_next = next; | ||
_options = options; | ||
_logger = logger; | ||
} | ||
|
||
|
||
public async Task InvokeAsync(HttpContext httpContext) | ||
{ | ||
|
||
if (_options.Value.BasicAuthEnabled(_logger) is false) | ||
{ | ||
await _next(httpContext).ConfigureAwait(false); | ||
return; | ||
} | ||
try | ||
{ | ||
var authHeader = AuthenticationHeaderValue.Parse(httpContext.Request.Headers["Authorization"]); | ||
if (authHeader.Scheme == "Basic") | ||
{ | ||
var credentialBytes = Convert.FromBase64String(authHeader.Parameter); | ||
var credentials = Encoding.UTF8.GetString(credentialBytes).Split(':', 2); | ||
var username = credentials[0]; | ||
var password = credentials[1]; | ||
if (string.Compare(username, _options.Value.BasicAuth.Id, false) is 0 && | ||
string.Compare(password, _options.Value.BasicAuth.Password, false) is 0) | ||
{ | ||
var claims = new[] { new Claim("name", credentials[0]) }; | ||
var identity = new ClaimsIdentity(claims, "Basic"); | ||
var claimsPrincipal = new ClaimsPrincipal(identity); | ||
httpContext.User = claimsPrincipal; | ||
return; | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Exception "); | ||
} | ||
httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/Authentication/Tests/test.auth-noclientid.json
100644 → 100755
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"MonaiDeployAuthentication": { | ||
"BypassAuthentication": false, | ||
"basicAuth": { | ||
"userName": "user", | ||
"password": "pass" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"MonaiDeployAuthentication": { | ||
"BypassAuthentication": false, | ||
"basicAuth": { | ||
"userName": "nopassword", | ||
"password": "sded" | ||
}, | ||
"openId": { | ||
"realm": "TEST-REALM", | ||
"realmKey": "l9ZRlbMQBt9k1klUUrlWFuke8WbqnEde", | ||
"audiences": [ "monai-app" ], | ||
"roleClaimType": "roles", | ||
"clientId": "monai-app-test" | ||
} | ||
} | ||
} |