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

Conversation

Phil91
Copy link
Member

@Phil91 Phil91 commented Nov 27, 2023

Description

All configuration values needed for the process identity were removed except for the identity id.

Why

The determination of the identity needed for the process worker is now done with a database request to get the needed values for the specified user.

Issue

N/A - Jira Issue: CPLP-3102

Corresponding CD PR

#130

Checklist

  • I have followed the contributing guidelines
  • I have performed a self-review of my own code
  • I have successfully tested my changes locally
  • I have added tests that prove my changes work
  • I have checked that new and existing tests pass locally with my changes
  • I have commented my code, particularly in hard-to-understand areas

@Phil91 Phil91 marked this pull request as ready for review November 27, 2023 13:40
claimsIdentity.AddClaim(new Claim(PortalClaimTypes.IdentityType, Enum.GetName(identityData.IdentityType) ?? throw new ConflictException($"IdentityType {(int)identityData.IdentityType} is out of range")));
claimsIdentity.AddClaim(new Claim(PortalClaimTypes.CompanyId, identityData.CompanyId.ToString()));
return true;
case false when !Guid.TryParse(preferredUserName, out var identityId):
Copy link
Contributor

Choose a reason for hiding this comment

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

the ! in when !Guid.TryParse looks incorrect. We should have a unit-test for the method TransformAsync

instead of the cumbersome switch / case that somewhat hides the actual flow of information I recommend a more traditional implementation:

private async ValueTask<bool> AddIdentity(ClaimsPrincipal principal, ClaimsIdentity claimsIdentity)
{
    var preferredUserName = principal.Claims.SingleOrDefault(x => x.Type == PortalClaimTypes.PreferredUserName)?.Value;

    if (!string.IsNullOrWhiteSpace(preferredUserName) && Guid.TryParse(preferredUserName, out var identityId))
    {
        claimsIdentity.AddClaim(new Claim(PortalClaimTypes.IdentityId, identityId.ToString()));
        return true;
    }

    var sub = principal.Claims.SingleOrDefault(x => x.Type == PortalClaimTypes.Sub)?.Value;
    _logger.LogInformation("Preferred user name {PreferredUserName} couldn't be parsed to uuid for userEntityId {Sub}", preferredUserName, sub);

    IdentityData? identityData;
    if (string.IsNullOrWhiteSpace(sub) || (identityData = await _portalDbRepositories.GetInstance<IUserRepository>().GetActiveUserDataByUserEntityId(sub).ConfigureAwait(false)) == null)
    {
        _logger.LogWarning("No identity found for userEntityId {Sub}", sub);
        return false;
    }

    claimsIdentity.AddClaim(new Claim(PortalClaimTypes.IdentityId, identityData.UserId.ToString()));
    return true;
}

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 did change the implementation and added unit tests.

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

Copy link

sonarcloud bot commented Nov 29, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@ntruchsess ntruchsess merged commit 4e1c4ba into eclipse-tractusx:dev Nov 29, 2023
5 checks passed
@ntruchsess ntruchsess deleted the feature/CPLP-3102-identity branch November 29, 2023 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants