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: skip credential import if holder equals issuer #275

Merged
merged 1 commit into from
Oct 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ namespace Org.Eclipse.TractusX.SsiCredentialIssuer.DBAccess.Repositories;

public class CredentialRepository(IssuerDbContext dbContext) : ICredentialRepository
{
public Task<Guid?> GetWalletCredentialId(Guid credentialId) =>
dbContext.CompanySsiDetails.Where(x => x.Id == credentialId)
.Select(x => x.ExternalCredentialId)
.SingleOrDefaultAsync();

public Task<(HolderWalletData HolderWalletData, string? Credential, EncryptionTransformationData EncryptionInformation, string? CallbackUrl)> GetCredentialData(Guid credentialId) =>
public Task<(bool IsIssuerCompany, HolderWalletData HolderWalletData, string? Credential, EncryptionTransformationData EncryptionInformation, string? CallbackUrl)> GetCredentialData(Guid credentialId) =>
dbContext.CompanySsiDetails
.Where(x => x.Id == credentialId)
.Select(x => new ValueTuple<HolderWalletData, string?, EncryptionTransformationData, string?>(
.Select(x => new ValueTuple<bool, HolderWalletData, string?, EncryptionTransformationData, string?>(
x.Bpnl == x.IssuerBpn,
new HolderWalletData(x.CompanySsiProcessData!.HolderWalletUrl, x.CompanySsiProcessData.ClientId),
x.Credential,
new EncryptionTransformationData(x.CompanySsiProcessData!.ClientSecret, x.CompanySsiProcessData.InitializationVector, x.CompanySsiProcessData.EncryptionMode),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ namespace Org.Eclipse.TractusX.SsiCredentialIssuer.DBAccess.Repositories;

public interface ICredentialRepository
{
Task<Guid?> GetWalletCredentialId(Guid credentialId);
Task<(HolderWalletData HolderWalletData, string? Credential, EncryptionTransformationData EncryptionInformation, string? CallbackUrl)> GetCredentialData(Guid credentialId);
Task<(bool IsIssuerCompany, HolderWalletData HolderWalletData, string? Credential, EncryptionTransformationData EncryptionInformation, string? CallbackUrl)> GetCredentialData(Guid credentialId);
Task<(bool Exists, Guid CredentialId)> GetDataForProcessId(Guid processId);
Task<(VerifiedCredentialTypeKindId CredentialTypeKindId, JsonDocument Schema)> GetCredentialStorageInformationById(Guid credentialId);
Task<(Guid? ExternalCredentialId, VerifiedCredentialTypeKindId KindId, bool HasEncryptionInformation, string? CallbackUrl)> GetExternalCredentialAndKindId(Guid credentialId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace Org.Eclipse.TractusX.SsiCredentialIssuer.Wallet.Service.BusinessLogic;
public interface IWalletBusinessLogic
{
Task CreateSignedCredential(Guid companySsiDetailId, JsonDocument schema, CancellationToken cancellationToken);

Task CreateCredentialForHolder(Guid companySsiDetailId, string holderWalletUrl, string clientId, EncryptionInformation encryptionInformation, string credential, CancellationToken cancellationToken);

Task GetCredential(Guid credentialId, Guid externalCredentialId, VerifiedCredentialTypeKindId kindId, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ public class CredentialCreationProcessHandler(

public async Task<(IEnumerable<ProcessStepTypeId>? nextStepTypeIds, ProcessStepStatusId stepStatusId, bool modified, string? processMessage)> CreateCredentialForHolder(Guid credentialId, CancellationToken cancellationToken)
{
var (holderWalletData, credential, encryptionInformation, callbackUrl) = await issuerRepositories.GetInstance<ICredentialRepository>().GetCredentialData(credentialId).ConfigureAwait(ConfigureAwaitOptions.None);
var (isIssuerCompany, holderWalletData, credential, encryptionInformation, callbackUrl) = await issuerRepositories.GetInstance<ICredentialRepository>().GetCredentialData(credentialId).ConfigureAwait(ConfigureAwaitOptions.None);
if (isIssuerCompany)
{
return (
callbackUrl is null ? null : Enumerable.Repeat(ProcessStepTypeId.TRIGGER_CALLBACK, 1),
ProcessStepStatusId.SKIPPED,
false,
"ProcessStep was skipped because the holder is the issuer");
}

if (credential is null)
{
throw new ConflictException("Credential must be set here");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,6 @@ public async Task GetCredentialData_ReturnsExpectedDocument()

#endregion

#region GetWalletCredentialId

[Fact]
public async Task GetWalletCredentialId_ReturnsExpectedDocument()
{
// Arrange
var sut = await CreateSut();

// Act
var result = await sut.GetWalletCredentialId(new Guid("9f5b9934-4014-4099-91e9-7b1aee696b03"));

// Assert
result.Should().Be(new Guid("bd474c60-e7ce-450f-bdf4-73604546fc5e"));
}

#endregion

#region GetCredentialStorageInformationById

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public async Task CreateCredentialForHolder_WithCredentialNotSet_SkipsStep()
{
// Arrange
A.CallTo(() => _credentialRepository.GetCredentialData(_credentialId))
.Returns(default((HolderWalletData, string?, EncryptionTransformationData, string?)));
.Returns(default((bool, HolderWalletData, string?, EncryptionTransformationData, string?)));
Task Act() => _sut.CreateCredentialForHolder(_credentialId, CancellationToken.None);

// Act
Expand All @@ -151,7 +151,7 @@ public async Task CreateCredentialForHolder_WithClientIdNull_SkipsStep()
{
// Arrange
A.CallTo(() => _credentialRepository.GetCredentialData(_credentialId))
.Returns((new HolderWalletData(null, null), "test", _fixture.Create<EncryptionTransformationData>(), "https://example.org"));
.Returns((false, new HolderWalletData(null, null), "test", _fixture.Create<EncryptionTransformationData>(), "https://example.org"));
Task Act() => _sut.CreateCredentialForHolder(_credentialId, CancellationToken.None);

// Act
Expand All @@ -166,7 +166,7 @@ public async Task CreateCredentialForHolder_WithWalletUrlNull_SkipsStep()
{
// Arrange
A.CallTo(() => _credentialRepository.GetCredentialData(_credentialId))
.Returns((new HolderWalletData(null, "c1"), "test", _fixture.Create<EncryptionTransformationData>(), "https://example.org"));
.Returns((false, new HolderWalletData(null, "c1"), "test", _fixture.Create<EncryptionTransformationData>(), "https://example.org"));
Task Act() => _sut.CreateCredentialForHolder(_credentialId, CancellationToken.None);

// Act
Expand All @@ -182,6 +182,7 @@ public async Task CreateCredentialForHolder_WithEncryptionNotSet_SkipsStep()
// Arrange
A.CallTo(() => _credentialRepository.GetCredentialData(_credentialId))
.Returns((
false,
new HolderWalletData("https://example.org", "c1"),
"test",
new EncryptionTransformationData("test"u8.ToArray(), "test"u8.ToArray(), 0),
Expand All @@ -203,6 +204,7 @@ public async Task CreateCredentialForHolder_WithValidData_ReturnsExpected()
// Arrange
A.CallTo(() => _credentialRepository.GetCredentialData(_credentialId))
.Returns((
false,
new HolderWalletData("https://example.org", "c1"),
"test",
_fixture.Create<EncryptionTransformationData>(),
Expand All @@ -221,6 +223,31 @@ public async Task CreateCredentialForHolder_WithValidData_ReturnsExpected()
result.nextStepTypeIds.Should().ContainSingle().Which.Should().Be(ProcessStepTypeId.TRIGGER_CALLBACK);
}

[Fact]
public async Task CreateCredentialForHolder_WithIssuerAsHolder_ReturnsExpected()
{
// Arrange
A.CallTo(() => _credentialRepository.GetCredentialData(_credentialId))
.Returns((
true,
new HolderWalletData("https://example.org", "c1"),
"test",
_fixture.Create<EncryptionTransformationData>(),
"https://example.org"));

// Act
var result = await _sut.CreateCredentialForHolder(_credentialId, CancellationToken.None);

// Assert
A.CallTo(() => _walletBusinessLogic.CreateCredentialForHolder(A<Guid>._, A<string>._, A<string>._, A<EncryptionInformation>._, A<string>._, A<CancellationToken>._))
.MustNotHaveHappened();

result.modified.Should().BeFalse();
result.processMessage.Should().Be("ProcessStep was skipped because the holder is the issuer");
result.stepStatusId.Should().Be(ProcessStepStatusId.SKIPPED);
result.nextStepTypeIds.Should().ContainSingle().Which.Should().Be(ProcessStepTypeId.TRIGGER_CALLBACK);
}

#endregion

#region TriggerCallback
Expand Down