Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(process): slimline process identity config #361

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Portal.Backend.sln
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceAccountSync.Executor
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Registration.Common", "registration\Registration.Common\Registration.Common.csproj", "{09EF5799-B375-49F1-B78F-0A94D8109F8B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Keycloak.Authentication.Tests", "..\tests\keycloak\Keycloak.Authentication.Tests\Keycloak.Authentication.Tests.csproj", "{C7ACF748-DEF4-4646-A791-F1DA437CC965}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1522,6 +1524,18 @@ Global
{09EF5799-B375-49F1-B78F-0A94D8109F8B}.Release|x64.Build.0 = Release|Any CPU
{09EF5799-B375-49F1-B78F-0A94D8109F8B}.Release|x86.ActiveCfg = Release|Any CPU
{09EF5799-B375-49F1-B78F-0A94D8109F8B}.Release|x86.Build.0 = Release|Any CPU
{C7ACF748-DEF4-4646-A791-F1DA437CC965}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7ACF748-DEF4-4646-A791-F1DA437CC965}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7ACF748-DEF4-4646-A791-F1DA437CC965}.Debug|x64.ActiveCfg = Debug|Any CPU
{C7ACF748-DEF4-4646-A791-F1DA437CC965}.Debug|x64.Build.0 = Debug|Any CPU
{C7ACF748-DEF4-4646-A791-F1DA437CC965}.Debug|x86.ActiveCfg = Debug|Any CPU
{C7ACF748-DEF4-4646-A791-F1DA437CC965}.Debug|x86.Build.0 = Debug|Any CPU
{C7ACF748-DEF4-4646-A791-F1DA437CC965}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C7ACF748-DEF4-4646-A791-F1DA437CC965}.Release|Any CPU.Build.0 = Release|Any CPU
{C7ACF748-DEF4-4646-A791-F1DA437CC965}.Release|x64.ActiveCfg = Release|Any CPU
{C7ACF748-DEF4-4646-A791-F1DA437CC965}.Release|x64.Build.0 = Release|Any CPU
{C7ACF748-DEF4-4646-A791-F1DA437CC965}.Release|x86.ActiveCfg = Release|Any CPU
{C7ACF748-DEF4-4646-A791-F1DA437CC965}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1636,5 +1650,6 @@ Global
{B2E5EBAB-AE49-47B6-8220-4844AC9DA456} = {282CEF03-292F-4A49-83C6-997567D0FF5F}
{571DA63A-6B96-4C6C-8D82-D2C1F10BDAE5} = {323C198D-A8C6-4EB0-8B79-72624275E35F}
{09EF5799-B375-49F1-B78F-0A94D8109F8B} = {AB9C5AA2-DD5D-4A38-97C0-674A995C0AE0}
{C7ACF748-DEF4-4646-A791-F1DA437CC965} = {323C198D-A8C6-4EB0-8B79-72624275E35F}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ private async Task HandleSsiCreationAsync(
public async Task ApproveCredential(Guid credentialId, CancellationToken cancellationToken)
{
var companySsiRepository = _portalRepositories.GetInstance<ICompanySsiDetailsRepository>();
var userId = _identityService.IdentityData.UserId;
var userId = _identityService.IdentityId;
var (exists, data) = await companySsiRepository.GetSsiApprovalData(credentialId).ConfigureAwait(false);
if (!exists)
{
Expand Down Expand Up @@ -455,7 +455,7 @@ public async Task ApproveCredential(Guid credentialId, CancellationToken cancell
public async Task RejectCredential(Guid credentialId)
{
var companySsiRepository = _portalRepositories.GetInstance<ICompanySsiDetailsRepository>();
var userId = _identityService.IdentityData.UserId;
var userId = _identityService.IdentityId;
var (exists, status, type, requesterId, requesterEmail, requesterFirstname, requesterLastname) = await companySsiRepository.GetSsiRejectionData(credentialId).ConfigureAwait(false);
if (!exists)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public async Task ProcessClearinghouseSelfDescription(SelfDescriptionResponseDat
throw new ConflictException($"Connector {data.ExternalId} already has a document assigned");
}

await _sdFactoryBusinessLogic.ProcessFinishSelfDescriptionLpForConnector(data, _identityService.IdentityData.UserId, cancellationToken).ConfigureAwait(false);
await _sdFactoryBusinessLogic.ProcessFinishSelfDescriptionLpForConnector(data, _identityService.IdentityId, cancellationToken).ConfigureAwait(false);
await _portalRepositories.SaveAsync().ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public DocumentsBusinessLogic(IPortalRepositories portalRepositories, IIdentityS
public async Task<bool> DeleteDocumentAsync(Guid documentId)
{
var documentRepository = _portalRepositories.GetInstance<IDocumentRepository>();
var details = await documentRepository.GetDocumentDetailsForIdUntrackedAsync(documentId, _identityService.IdentityData.UserId).ConfigureAwait(false);
var details = await documentRepository.GetDocumentDetailsForIdUntrackedAsync(documentId, _identityService.IdentityId).ConfigureAwait(false);

if (details.DocumentId == Guid.Empty)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public IAsyncEnumerable<string> CreateOwnCompanyUsersAsync(IEnumerable<UserCreat

private async IAsyncEnumerable<string> CreateOwnCompanyUsersInternalAsync(IEnumerable<UserCreationInfo> userList)
{
var (companyNameIdpAliasData, nameCreatedBy) = await _userProvisioningService.GetCompanyNameSharedIdpAliasData(_identityService.IdentityData.UserId).ConfigureAwait(false);
var (companyNameIdpAliasData, nameCreatedBy) = await _userProvisioningService.GetCompanyNameSharedIdpAliasData(_identityService.IdentityId).ConfigureAwait(false);

var distinctRoles = userList.SelectMany(user => user.Roles).Distinct().ToList();

Expand Down Expand Up @@ -166,7 +166,7 @@ private Task<IEnumerable<UserRoleData>> GetOwnCompanyUserRoleData(IEnumerable<st

public async Task<Guid> CreateOwnCompanyIdpUserAsync(Guid identityProviderId, UserCreationInfoIdp userCreationInfo)
{
var (companyNameIdpAliasData, nameCreatedBy) = await _userProvisioningService.GetCompanyNameIdpAliasData(identityProviderId, _identityService.IdentityData.UserId).ConfigureAwait(false);
var (companyNameIdpAliasData, nameCreatedBy) = await _userProvisioningService.GetCompanyNameIdpAliasData(identityProviderId, _identityService.IdentityId).ConfigureAwait(false);
var displayName = await _userProvisioningService.GetIdentityProviderDisplayName(companyNameIdpAliasData.IdpAlias).ConfigureAwait(false);

if (!userCreationInfo.Roles.Any())
Expand Down Expand Up @@ -316,7 +316,7 @@ public Task<int> AddOwnCompanyUsersBusinessPartnerNumberAsync(Guid userId, strin

public async Task<CompanyOwnUserDetails> GetOwnUserDetails()
{
var userId = _identityService.IdentityData.UserId;
var userId = _identityService.IdentityId;
var userRoleIds = await _portalRepositories.GetInstance<IUserRolesRepository>()
.GetUserRoleIdsUntrackedAsync(_settings.UserAdminRoles).ToListAsync().ConfigureAwait(false);
var details = await _portalRepositories.GetInstance<IUserRepository>().GetUserDetailsUntrackedAsync(userId, userRoleIds).ConfigureAwait(false);
Expand All @@ -329,7 +329,7 @@ public async Task<CompanyOwnUserDetails> GetOwnUserDetails()

public async Task<CompanyUserDetails> UpdateOwnUserDetails(Guid companyUserId, OwnCompanyUserEditableDetails ownCompanyUserEditableDetails)
{
var userId = _identityService.IdentityData.UserId;
var userId = _identityService.IdentityId;
if (companyUserId != userId)
{
throw new ForbiddenException($"invalid userId {companyUserId} for user {userId}");
Expand Down Expand Up @@ -388,7 +388,7 @@ await _provisioningManager.UpdateSharedRealmUserAsync(

public async Task<int> DeleteOwnUserAsync(Guid companyUserId)
{
var userId = _identityService.IdentityData.UserId;
var userId = _identityService.IdentityId;
if (companyUserId != userId)
{
throw new ForbiddenException($"companyUser {companyUserId} is not the id of user {userId}");
Expand Down Expand Up @@ -528,7 +528,7 @@ public async Task<bool> ExecuteOwnCompanyUserPasswordReset(Guid companyUserId)
15,
_portalRepositories.GetInstance<IUserRepository>().GetOwnCompanyAppUsersPaginationSourceAsync(
appId,
_identityService.IdentityData.UserId,
_identityService.IdentityId,
new[] { OfferSubscriptionStatusId.ACTIVE },
new[] { UserStatusId.ACTIVE, UserStatusId.INACTIVE },
filter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using Org.Eclipse.TractusX.Portal.Backend.Administration.Service.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Async;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Linq;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
using Org.Eclipse.TractusX.Portal.Backend.Administration.Service.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Library;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.Keycloak.Authentication;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.Provisioning.Library.Models;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Library;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.PublicInfos;
using Org.Eclipse.TractusX.Portal.Backend.Keycloak.Authentication;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
using Org.Eclipse.TractusX.Portal.Backend.Administration.Service.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Library;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.Keycloak.Authentication;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.Provisioning.Library.Models;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
using Org.Eclipse.TractusX.Portal.Backend.Registration.Common;
using System.Text.Json.Serialization;
using RegistrationData = Org.Eclipse.TractusX.Portal.Backend.Registration.Common.RegistrationData;

namespace Org.Eclipse.TractusX.Portal.Backend.Administration.Service.Models;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.OnboardingServiceProvider.Library.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;

namespace Org.Eclipse.TractusX.Portal.Backend.OnboardingServiceProvider.Library;

Expand Down
1 change: 1 addition & 0 deletions src/framework/Framework.Models/PortalClaimTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static class PortalClaimTypes
{
private const string Base = "https://catena-x.net//schema/2023/05/identity/claims";
public const string Sub = "sub";
public const string PreferredUserName = "preferred_username";
public const string ResourceAccess = "resource_access";
public const string CompanyId = $"{Base}/company_id";
public const string IdentityId = $"{Base}/identity_id";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/********************************************************************************
* Copyright (c) 2021, 2023 BMW Group AG
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
Expand All @@ -24,14 +23,14 @@

namespace Org.Eclipse.TractusX.Portal.Backend.Framework.ProcessIdentity;

public class ProcessIdentityService : IIdentityService
public class ConfigurationIdentityIdDetermination : IIdentityIdDetermination
{
private readonly ProcessIdentitySettings _settings;

public ProcessIdentityService(IOptions<ProcessIdentitySettings> options) =>
public ConfigurationIdentityIdDetermination(IOptions<ProcessIdentitySettings> options)
{
_settings = options.Value;
}

/// <inheritdoc />
public IdentityData IdentityData =>
new(_settings.UserEntityId, _settings.ProcessUserId, _settings.IdentityTypeId, _settings.ProcessUserCompanyId);
public Guid IdentityId => _settings.ProcessUserId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Identities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Identities;

namespace Org.Eclipse.TractusX.Portal.Backend.Framework.ProcessIdentity.DependencyInjection;

public static class ProcessIdentityServiceCollectionExtensions
{
public static IServiceCollection AddProcessIdentity(this IServiceCollection services, IConfigurationSection section)
public static IServiceCollection AddConfigurationIdentityIdDetermination(this IServiceCollection services, IConfigurationSection section)
{
services.AddOptions<ProcessIdentitySettings>()
.Bind(section)
.ValidateOnStart();

return services.AddTransient<IIdentityService, ProcessIdentityService>();
return services
.AddTransient<IIdentityIdDetermination, ConfigurationIdentityIdDetermination>()
.AddTransient<IIdentityService, IdentityService>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,12 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
using System.ComponentModel.DataAnnotations;

namespace Org.Eclipse.TractusX.Portal.Backend.Framework.ProcessIdentity.DependencyInjection;

public class ProcessIdentitySettings
{
[Required(AllowEmptyStrings = false)]
public string UserEntityId { get; set; } = null!;

[Required]
public Guid ProcessUserId { get; set; } = Guid.Empty;

[Required]
public IdentityTypeId IdentityTypeId { get; set; } = default;

[Required]
public Guid ProcessUserCompanyId { get; set; } = Guid.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\portalbackend\PortalBackend.DBAccess\PortalBackend.DBAccess.csproj" />
<ProjectReference Include="..\..\portalbackend\PortalBackend.PortalEntities\PortalBackend.PortalEntities.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/********************************************************************************
* Copyright (c) 2021, 2023 BMW Group AG
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
Expand All @@ -19,24 +18,21 @@
********************************************************************************/

using Microsoft.AspNetCore.Http;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Keycloak.Authentication;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Identities;

namespace Org.Eclipse.TractusX.Portal.Backend.Framework.Web;

public class IdentityService : IIdentityService
public class ClaimsIdentityIdDetermination : IIdentityIdDetermination
{
private readonly IHttpContextAccessor _httpContextAccessor;
private IdentityData? _identityData;
private Guid? _identityId;

public IdentityService(IHttpContextAccessor httpContextAccessor)
public ClaimsIdentityIdDetermination(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}

/// <inheritdoc />
public IdentityData IdentityData =>
_identityData ??= _httpContextAccessor.HttpContext?.User.GetIdentityData()
?? throw new ConflictException("The identity should be set here");
public Guid IdentityId => (_identityId ??= _httpContextAccessor.HttpContext?.User.GetIdentityId())!.Value;
Copy link
Contributor

@ntruchsess ntruchsess Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sonar marks this as a code smell. According to the smell's documentation it should accept
public Guid IdentityId => (_identityId ?? (_identityId = _httpContextAccessor.HttpContext?.User.GetIdentityId()))!.Value;
(I guess the rule doesn't yet know about the ??= operator, so maybe we should just mark this as a false positive?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd mark it as a false positive

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/********************************************************************************
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
*
* 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.Extensions.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Identities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Identities;

namespace Org.Eclipse.TractusX.Portal.Backend.Framework.Web;

public static class ClaimsIdentityServiceCollectionExtensions
{
public static IServiceCollection AddClaimsIdentityIdDetermination(this IServiceCollection services)
{
return services
.AddScoped<IIdentityIdDetermination, ClaimsIdentityIdDetermination>()
.AddTransient<IIdentityService, IdentityService>();
}
}
1 change: 1 addition & 0 deletions src/framework/Framework.Web/Framework.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<ProjectReference Include="..\Framework.Cors\Framework.Cors.csproj" />
<ProjectReference Include="..\Framework.DateTimeProvider\Framework.DateTimeProvider.csproj" />
<ProjectReference Include="..\Framework.ErrorHandling.Web\Framework.ErrorHandling.Web.csproj" />
<ProjectReference Include="..\Framework.ProcessIdentity\Framework.ProcessIdentity.csproj" />
<ProjectReference Include="..\Framework.PublicInfos\Framework.PublicInfos.csproj" />
<ProjectReference Include="..\Framework.Swagger\Framework.Swagger.csproj" />
<ProjectReference Include="..\Framework.Authorization\Framework.Authorization.csproj" />
Expand Down
4 changes: 1 addition & 3 deletions src/framework/Framework.Web/StartupServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/********************************************************************************
* Copyright (c) 2021, 2023 BMW Group AG
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
Expand Down Expand Up @@ -31,7 +30,6 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.Swagger;
using Org.Eclipse.TractusX.Portal.Backend.Keycloak.Authentication;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Identities;
using System.IdentityModel.Tokens.Jwt;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -102,7 +100,7 @@ public static IServiceCollection AddDefaultServices<TProgram>(this IServiceColle
.AddCheck<JwtBearerConfigurationHealthCheck>("JwtBearerConfiguration", tags: new[] { "keycloak" });

services.AddHttpContextAccessor();
services.AddScoped<IIdentityService, IdentityService>();
services.AddClaimsIdentityIdDetermination();

services.AddDateTimeProvider();
services.AddPublicInfos();
Expand Down
Loading
Loading