-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(authorization): add role authorization (#19)
- Loading branch information
Showing
11 changed files
with
162 additions
and
2 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 |
---|---|---|
|
@@ -31,3 +31,4 @@ spec: | |
targetPort: {{ .Values.portContainer }} | ||
selector: | ||
{{- include "dim.selectorLabels" . | nindent 4 }} | ||
|
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
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,28 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2024 BMW Group AG | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
namespace Dim.Web.Authentication; | ||
|
||
public static class CustomClaimTypes | ||
{ | ||
public const string Sub = "sub"; | ||
public const string ClientId = "clientId"; | ||
public const string PreferredUserName = "preferred_username"; | ||
public const string ResourceAccess = "resource_access"; | ||
} |
73 changes: 73 additions & 0 deletions
73
src/web/Dim.Web/Authentication/KeycloakClaimsTransformation.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,73 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2024 BMW Group AG | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.Extensions.Options; | ||
using Org.Eclipse.TractusX.Portal.Backend.Framework.Linq; | ||
using System.Json; | ||
using System.Security.Claims; | ||
|
||
namespace Dim.Web.Authentication | ||
{ | ||
public class KeycloakClaimsTransformation : IClaimsTransformation | ||
{ | ||
private readonly JwtBearerOptions _options; | ||
|
||
public KeycloakClaimsTransformation(IOptions<JwtBearerOptions> options) | ||
{ | ||
_options = options.Value; | ||
} | ||
|
||
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal) | ||
{ | ||
var claimsIdentity = new ClaimsIdentity(); | ||
if (AddRoles(principal, claimsIdentity)) | ||
{ | ||
principal.AddIdentity(claimsIdentity); | ||
} | ||
|
||
return Task.FromResult(principal); | ||
} | ||
|
||
private bool AddRoles(ClaimsPrincipal principal, ClaimsIdentity claimsIdentity) => | ||
principal.Claims | ||
.Where(claim => | ||
claim.Type == CustomClaimTypes.ResourceAccess && | ||
claim.ValueType == "JSON") | ||
.SelectMany(claim => | ||
JsonValue.Parse(claim.Value) is JsonObject jsonObject && | ||
jsonObject.TryGetValue( | ||
_options.TokenValidationParameters.ValidAudience, | ||
out var audience) && | ||
audience is JsonObject client && | ||
client.TryGetValue("roles", out var jsonRoles) && | ||
jsonRoles is JsonArray roles | ||
? roles.Where(x => x.JsonType == JsonType.String) | ||
.Select(role => new Claim(ClaimTypes.Role, role)) | ||
: Enumerable.Empty<Claim>()) | ||
.IfAny(claims => | ||
{ | ||
foreach (var claim in claims) | ||
{ | ||
claimsIdentity.AddClaim(claim); | ||
} | ||
}); | ||
} | ||
} |
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
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