Skip to content

Commit

Permalink
fix: implement expiry for bpnl and membership credentials (eclipse-tr…
Browse files Browse the repository at this point in the history
…actusx#4)

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Phil Schneider <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Evelyn Gurschler <[email protected]>
  • Loading branch information
4 people authored Jul 30, 2024
1 parent 0efa7a1 commit 134de56
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,15 @@ public async Task<Guid> CreateBpnCredential(CreateBpnCredentialRequest requestDa
{
var companyCredentialDetailsRepository = _repositories.GetInstance<ICompanySsiDetailsRepository>();
var holderDid = await GetHolderInformation(requestData.Holder, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
var expiryDate = DateTimeOffset.UtcNow.AddMonths(12);
var schemaData = new BpnCredential(
Guid.NewGuid(),
Context,
new[] { "VerifiableCredential", "BpnCredential" },
"BpnCredential",
"Bpn Credential",
DateTimeOffset.UtcNow,
DateTimeOffset.UtcNow.AddMonths(12),
expiryDate,
_settings.IssuerDid,
new BpnCredentialSubject(
holderDid,
Expand All @@ -357,22 +358,23 @@ public async Task<Guid> CreateBpnCredential(CreateBpnCredentialRequest requestDa
StatusList)
);
var schema = JsonSerializer.Serialize(schemaData, Options);
return await HandleCredentialProcessCreation(requestData.BusinessPartnerNumber, VerifiedCredentialTypeKindId.BPN, VerifiedCredentialTypeId.BUSINESS_PARTNER_NUMBER, schema, requestData.TechnicalUserDetails, null, requestData.CallbackUrl, companyCredentialDetailsRepository);
return await HandleCredentialProcessCreation(requestData.BusinessPartnerNumber, VerifiedCredentialTypeKindId.BPN, VerifiedCredentialTypeId.BUSINESS_PARTNER_NUMBER, expiryDate, schema, requestData.TechnicalUserDetails, null, requestData.CallbackUrl, companyCredentialDetailsRepository);
}

public async Task<Guid> CreateMembershipCredential(CreateMembershipCredentialRequest requestData, CancellationToken cancellationToken)
{
var companyCredentialDetailsRepository = _repositories.GetInstance<ICompanySsiDetailsRepository>();

var holderDid = await GetHolderInformation(requestData.Holder, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
var expiryDate = DateTimeOffset.UtcNow.AddMonths(12);
var schemaData = new MembershipCredential(
Guid.NewGuid(),
Context,
new[] { "VerifiableCredential", "MembershipCredential" },
"MembershipCredential",
"Membership Credential",
DateTimeOffset.UtcNow,
DateTimeOffset.UtcNow.AddMonths(12),
expiryDate,
_settings.IssuerDid,
new MembershipCredentialSubject(
holderDid,
Expand All @@ -384,7 +386,7 @@ public async Task<Guid> CreateMembershipCredential(CreateMembershipCredentialReq
StatusList)
);
var schema = JsonSerializer.Serialize(schemaData, Options);
return await HandleCredentialProcessCreation(requestData.HolderBpn, VerifiedCredentialTypeKindId.MEMBERSHIP, VerifiedCredentialTypeId.DISMANTLER_CERTIFICATE, schema, requestData.TechnicalUserDetails, null, requestData.CallbackUrl, companyCredentialDetailsRepository);
return await HandleCredentialProcessCreation(requestData.HolderBpn, VerifiedCredentialTypeKindId.MEMBERSHIP, VerifiedCredentialTypeId.DISMANTLER_CERTIFICATE, expiryDate, schema, requestData.TechnicalUserDetails, null, requestData.CallbackUrl, companyCredentialDetailsRepository);
}

public async Task<Guid> CreateFrameworkCredential(CreateFrameworkCredentialRequest requestData, CancellationToken cancellationToken)
Expand Down Expand Up @@ -455,12 +457,12 @@ public async Task<Guid> CreateFrameworkCredential(CreateFrameworkCredentialReque
StatusList)
);
var schema = JsonSerializer.Serialize(schemaData, Options);
return await HandleCredentialProcessCreation(requestData.HolderBpn, VerifiedCredentialTypeKindId.FRAMEWORK, requestData.UseCaseFrameworkId, schema, requestData.TechnicalUserDetails, requestData.UseCaseFrameworkVersionId, requestData.CallbackUrl, companyCredentialDetailsRepository);
return await HandleCredentialProcessCreation(requestData.HolderBpn, VerifiedCredentialTypeKindId.FRAMEWORK, requestData.UseCaseFrameworkId, result.Expiry, schema, requestData.TechnicalUserDetails, requestData.UseCaseFrameworkVersionId, requestData.CallbackUrl, companyCredentialDetailsRepository);
}

private async Task<string> GetHolderInformation(string didDocumentLocation, CancellationToken cancellationToken)
{
if (!Uri.TryCreate(didDocumentLocation, UriKind.Absolute, out var uri) || uri.Scheme != "https" || !string.IsNullOrEmpty(uri.Query) || !string.IsNullOrEmpty(uri.Fragment) || UrlPathInvalidCharsRegex.IsMatch(uri.AbsolutePath))
if (!Uri.TryCreate(didDocumentLocation, UriKind.Absolute, out var uri) || (uri.Scheme != "https" || !string.IsNullOrEmpty(uri.Query) || !string.IsNullOrEmpty(uri.Fragment) || UrlPathInvalidCharsRegex.IsMatch(uri.AbsolutePath))
{
throw ControllerArgumentException.Create(IssuerErrors.INVALID_DID_LOCATION, null, nameof(didDocumentLocation));
}
Expand All @@ -481,6 +483,7 @@ private async Task<Guid> HandleCredentialProcessCreation(
string bpnl,
VerifiedCredentialTypeKindId kindId,
VerifiedCredentialTypeId typeId,
DateTimeOffset expiryDate,
string schema,
TechnicalUserDetails? technicalUserDetails,
Guid? detailVersionId,
Expand Down Expand Up @@ -515,6 +518,7 @@ private async Task<Guid> HandleCredentialProcessCreation(
{
c.VerifiedCredentialExternalTypeDetailVersionId = detailVersionId;
c.ProcessId = processId;
c.ExpiryDate = expiryDate;
}).Id;
documentRepository.AssignDocumentToCompanySsiDetails(docId, ssiDetailId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,13 +676,18 @@ public async Task CreateBpnCredential_ReturnsExpected()
var didId = Guid.NewGuid().ToString();
var didDocument = new DidDocument(didId);
var data = new CreateBpnCredentialRequest("https://example.org/holder/BPNL12343546/did.json", Bpnl, null, null);
var detail = new CompanySsiDetail(CredentialId, _identity.Bpnl, VerifiedCredentialTypeId.BUSINESS_PARTNER_NUMBER, CompanySsiDetailStatusId.ACTIVE, IssuerBpnl, _identity.IdentityId, DateTimeOffset.Now);

HttpRequestMessage? request = null;
ConfigureHttpClientFactoryFixture(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(JsonSerializer.Serialize(didDocument))
}, requestMessage => request = requestMessage);

A.CallTo(() => _companySsiDetailsRepository.CreateSsiDetails(_identity.Bpnl, VerifiedCredentialTypeId.BUSINESS_PARTNER_NUMBER, CompanySsiDetailStatusId.ACTIVE, IssuerBpnl, _identity.IdentityId, A<Action<CompanySsiDetail>>._))
.Invokes((string bpnl, VerifiedCredentialTypeId verifiedCredentialTypeId, CompanySsiDetailStatusId companySsiDetailStatusId, string issuerBpn, string userId, Action<CompanySsiDetail>? setOptionalFields) => setOptionalFields?.Invoke(detail));

// Act
await _sut.CreateBpnCredential(data, CancellationToken.None);

Expand All @@ -694,6 +699,7 @@ public async Task CreateBpnCredential_ReturnsExpected()
A.CallTo(() => _documentRepository.AssignDocumentToCompanySsiDetails(A<Guid>._, A<Guid>._))
.MustHaveHappenedOnceExactly();
A.CallTo(() => _issuerRepositories.SaveAsync()).MustHaveHappenedOnceExactly();
Assert.NotNull(detail.ExpiryDate);
}

[Theory]
Expand Down Expand Up @@ -746,6 +752,8 @@ public async Task CreateMembershipCredential_ReturnsExpected()
var didId = Guid.NewGuid().ToString();
var didDocument = new DidDocument(didId);
var data = new CreateMembershipCredentialRequest("https://example.org/holder/BPNL12343546/did.json", Bpnl, "Test", null, null);
var detail = new CompanySsiDetail(CredentialId, _identity.Bpnl, VerifiedCredentialTypeId.DISMANTLER_CERTIFICATE, CompanySsiDetailStatusId.ACTIVE, IssuerBpnl, _identity.IdentityId, DateTimeOffset.Now);

HttpRequestMessage? request = null;
A.CallTo(() => _companySsiDetailsRepository.GetCertificateTypes(A<string>._))
.Returns(Enum.GetValues<VerifiedCredentialTypeId>().ToAsyncEnumerable());
Expand All @@ -755,6 +763,9 @@ public async Task CreateMembershipCredential_ReturnsExpected()
Content = new StringContent(JsonSerializer.Serialize(didDocument))
}, requestMessage => request = requestMessage);

A.CallTo(() => _companySsiDetailsRepository.CreateSsiDetails(_identity.Bpnl, VerifiedCredentialTypeId.DISMANTLER_CERTIFICATE, CompanySsiDetailStatusId.ACTIVE, IssuerBpnl, _identity.IdentityId, A<Action<CompanySsiDetail>>._))
.Invokes((string bpnl, VerifiedCredentialTypeId verifiedCredentialTypeId, CompanySsiDetailStatusId companySsiDetailStatusId, string issuerBpn, string userId, Action<CompanySsiDetail>? setOptionalFields) => setOptionalFields?.Invoke(detail));

// Act
await _sut.CreateMembershipCredential(data, CancellationToken.None);

Expand All @@ -766,6 +777,7 @@ public async Task CreateMembershipCredential_ReturnsExpected()
A.CallTo(() => _documentRepository.AssignDocumentToCompanySsiDetails(A<Guid>._, A<Guid>._))
.MustHaveHappenedOnceExactly();
A.CallTo(() => _issuerRepositories.SaveAsync()).MustHaveHappenedOnceExactly();
Assert.NotNull(detail.ExpiryDate);
}

#endregion
Expand Down

0 comments on commit 134de56

Please sign in to comment.