Skip to content

Commit afaca88

Browse files
committed
Set RoleClaimType override
Signed-off-by: Victor Chang <[email protected]>
1 parent 7bbdcef commit afaca88

12 files changed

+149
-35
lines changed

src/Authentication/Configurations/AuthenticationOptions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@ public bool BypassAuth(ILogger logger)
4242

4343
if (OpenId is null)
4444
{
45-
throw new InvalidOperationException("OpenId configuration is invalid.");
45+
throw new InvalidOperationException("openId configuration is invalid.");
4646
}
4747
if (OpenId.Claims is null || OpenId.Claims.UserClaims!.IsNullOrEmpty() || OpenId.Claims.AdminClaims!.IsNullOrEmpty())
4848
{
49-
throw new InvalidOperationException("No claims defined for OpenId.");
49+
throw new InvalidOperationException("No claimMappings defined for OpenId.");
5050
}
5151
if (string.IsNullOrWhiteSpace(OpenId.ClientId))
5252
{
53-
throw new InvalidOperationException("No ClientId defined for OpenId.");
53+
throw new InvalidOperationException("No clientId defined for OpenId.");
5454
}
55-
if (string.IsNullOrWhiteSpace(OpenId.ServerRealmKey))
55+
if (string.IsNullOrWhiteSpace(OpenId.RealmKey))
5656
{
57-
throw new InvalidOperationException("No ServerRealmKey defined for OpenId.");
57+
throw new InvalidOperationException("No realmKey defined for OpenId.");
5858
}
59-
if (string.IsNullOrWhiteSpace(OpenId.ServerRealm))
59+
if (string.IsNullOrWhiteSpace(OpenId.Realm))
6060
{
61-
throw new InvalidOperationException("No ServerRealm defined for OpenId.");
61+
throw new InvalidOperationException("No realm defined for OpenId.");
6262
}
6363

6464
return false;

src/Authentication/Configurations/ClaimMappings.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public class ClaimMappings
2929

3030
public class ClaimMapping
3131
{
32-
[ConfigurationKeyName("claim")]
33-
public string Claim { get; set; } = string.Empty;
32+
[ConfigurationKeyName("claimType")]
33+
public string ClaimType { get; set; } = string.Empty;
3434

35-
[ConfigurationKeyName("roles")]
36-
public List<string> Roles { get; set; } = new List<string>();
35+
[ConfigurationKeyName("claimValues")]
36+
public List<string> ClaimValues { get; set; } = new List<string>();
3737

3838
[ConfigurationKeyName("endpoints")]
3939
public List<string>? Endpoints { get; set; } = default;

src/Authentication/Configurations/OpenIdOptions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ namespace Monai.Deploy.Security.Authentication.Configurations
2121
public class OpenIdOptions
2222
{
2323
[ConfigurationKeyName("realm")]
24-
public string? ServerRealm { get; set; }
24+
public string? Realm { get; set; }
2525

2626
[ConfigurationKeyName("realmKey")]
27-
public string? ServerRealmKey { get; set; }
27+
public string? RealmKey { get; set; }
2828

2929
[ConfigurationKeyName("clientId")]
3030
public string? ClientId { get; set; }
@@ -34,5 +34,8 @@ public class OpenIdOptions
3434

3535
[ConfigurationKeyName("audiences")]
3636
public IList<string>? Audiences { get; set; }
37+
38+
[ConfigurationKeyName("roleClaimType")]
39+
public string RoleClaimType { get; set; } = "roles";
3740
}
3841
}

src/Authentication/Extensions/HttpContextExtension.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ public static List<string> GetValidEndpoints(this HttpContext httpContext, ILogg
4343

4444
foreach (var claim in adminClaims!)
4545
{
46-
foreach (var role in claim.Roles)
46+
foreach (var role in claim.ClaimValues)
4747
{
48-
logger.CheckingUserClaim(claim.Claim, role);
49-
if (httpContext.User.HasClaim(claim.Claim, role))
48+
logger.CheckingUserClaim(claim.ClaimType, role);
49+
if (httpContext.User.HasClaim(claim.ClaimType, role))
5050
{
5151
return new List<string> { "*" };
5252
}
@@ -56,10 +56,10 @@ public static List<string> GetValidEndpoints(this HttpContext httpContext, ILogg
5656
var endpoints = new List<string>();
5757
foreach (var claim in userClaims!)
5858
{
59-
foreach (var role in claim.Roles)
59+
foreach (var role in claim.ClaimValues)
6060
{
61-
logger.CheckingUserClaim(claim.Claim, role);
62-
if (httpContext.User.HasClaim(claim.Claim, role))
61+
logger.CheckingUserClaim(claim.ClaimType, role);
62+
if (httpContext.User.HasClaim(claim.ClaimType, role))
6363
{
6464
endpoints.AddRange(claim.Endpoints!);
6565
}

src/Authentication/Extensions/MonaiAuthenticationExtensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ public static IServiceCollection AddMonaiAuthentication(
5555
})
5656
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, AuthKeys.OpenId, options =>
5757
{
58-
options.Authority = configurations.Value.OpenId!.ServerRealm;
59-
options.Audience = configurations.Value.OpenId!.ServerRealm;
58+
options.Authority = configurations.Value.OpenId!.Realm;
59+
options.Audience = configurations.Value.OpenId!.Realm;
6060
options.RequireHttpsMetadata = false;
6161

6262
options.TokenValidationParameters = new TokenValidationParameters
6363
{
64-
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configurations.Value.OpenId!.ServerRealmKey!)),
65-
ValidIssuer = configurations.Value.OpenId.ServerRealm,
64+
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configurations.Value.OpenId!.RealmKey!)),
65+
RoleClaimType = configurations.Value.OpenId.RoleClaimType,
66+
ValidIssuer = configurations.Value.OpenId.Realm,
6667
ValidAudiences = configurations.Value.OpenId.Audiences,
6768
ValidateIssuerSigningKey = true,
6869
ValidateIssuer = true,

src/Authentication/Tests/EndpointAuthorizationMiddlewareTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public partial class EndpointAuthorizationMiddlewareTest
2828
[Theory]
2929
[InlineData("test.noauth.json")]
3030
[InlineData("test.emptyopenid.json")]
31+
[InlineData("test.auth-noclaims.json")]
32+
[InlineData("test.auth-noclientid.json")]
33+
[InlineData("test.auth-norealm.json")]
34+
[InlineData("test.auth-norealmkey.json")]
3135
public async Task GivenConfigurationFilesIsBad_ExpectExceptionToBeThrown(string configFile)
3236
{
3337
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"MonaiDeployAuthentication": {
3+
"bypassAuthentication": false,
4+
"openId": {
5+
"realm": "TEST-REALM",
6+
"realmKey": "l9ZRlbMQBt9k1klUUrlWFuke8WbqnEde",
7+
"audiences": [ "monai-app" ],
8+
"roleClaimType": "roles",
9+
"clientId": "monai-app-test"
10+
}
11+
}
12+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"MonaiDeployAuthentication": {
3+
"bypassAuthentication": false,
4+
"openId": {
5+
"realm": "TEST-REALM",
6+
"realmKey": "l9ZRlbMQBt9k1klUUrlWFuke8WbqnEde",
7+
"audiences": [ "monai-app" ],
8+
"roleClaimType": "roles",
9+
"claimMappings": {
10+
"userClaims": [
11+
{
12+
"claimType": "user_roles",
13+
"claimValues": [ "role-with-test" ],
14+
"endpoints": [ "test" ]
15+
},
16+
{
17+
"claimType": "user_roles",
18+
"claimValues": [ "role-without-test" ],
19+
"endpoints": [ "no-test" ]
20+
}
21+
],
22+
"adminClaims": [
23+
{
24+
"claimType": "user_roles",
25+
"claimValues": [ "monai-role-admin" ]
26+
}
27+
]
28+
}
29+
}
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"MonaiDeployAuthentication": {
3+
"bypassAuthentication": false,
4+
"openId": {
5+
"realmKey": "l9ZRlbMQBt9k1klUUrlWFuke8WbqnEde",
6+
"audiences": [ "monai-app" ],
7+
"roleClaimType": "roles",
8+
"clientId": "monai-app-test",
9+
"claimMappings": {
10+
"userClaims": [
11+
{
12+
"claimType": "user_roles",
13+
"claimValues": [ "role-with-test" ],
14+
"endpoints": [ "test" ]
15+
},
16+
{
17+
"claimType": "user_roles",
18+
"claimValues": [ "role-without-test" ],
19+
"endpoints": [ "no-test" ]
20+
}
21+
],
22+
"adminClaims": [
23+
{
24+
"claimType": "user_roles",
25+
"claimValues": [ "monai-role-admin" ]
26+
}
27+
]
28+
}
29+
}
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"MonaiDeployAuthentication": {
3+
"bypassAuthentication": false,
4+
"openId": {
5+
"realm": "TEST-REALM",
6+
"audiences": [ "monai-app" ],
7+
"roleClaimType": "roles",
8+
"clientId": "monai-app-test",
9+
"claimMappings": {
10+
"userClaims": [
11+
{
12+
"claimType": "user_roles",
13+
"claimValues": [ "role-with-test" ],
14+
"endpoints": [ "test" ]
15+
},
16+
{
17+
"claimType": "user_roles",
18+
"claimValues": [ "role-without-test" ],
19+
"endpoints": [ "no-test" ]
20+
}
21+
],
22+
"adminClaims": [
23+
{
24+
"claimType": "user_roles",
25+
"claimValues": [ "monai-role-admin" ]
26+
}
27+
]
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)