Skip to content

Commit

Permalink
fix: compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Jun 22, 2023
1 parent b56ccbe commit 2a1482b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import static java.lang.String.format;
import static org.eclipse.edc.identityhub.spi.model.WebNodeInterfaceMethod.COLLECTIONS_QUERY;
import static org.eclipse.edc.identityhub.spi.model.WebNodeInterfaceMethod.COLLECTIONS_WRITE;
import static org.eclipse.edc.spi.http.FallbackFactories.statusMustBe;
import static org.eclipse.edc.spi.http.FallbackFactories.retryWhenStatusIsNot;
import static org.eclipse.edc.spi.result.Result.failure;

public class IdentityHubClientImpl implements IdentityHubClient {
Expand All @@ -70,7 +70,7 @@ public Result<Collection<CredentialEnvelope>> getVerifiableCredentials(String hu
.post(body)
.build();

return httpClient.execute(request, List.of(statusMustBe(200)), this::extractCredentials);
return httpClient.execute(request, List.of(retryWhenStatusIsNot(200)), this::extractCredentials);
}

@Override
Expand All @@ -96,7 +96,7 @@ public Result<Void> addVerifiableCredential(String hubBaseUrl, CredentialEnvelop
.post(toRequestBody(descriptor, result.getContent()))
.build();

return httpClient.execute(request, List.of(statusMustBe(200)), this::handleAddResponse);
return httpClient.execute(request, List.of(retryWhenStatusIsNot(200)), this::handleAddResponse);
}

private Result<CredentialEnvelope> parse(Object entry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.eclipse.edc.identityhub.processor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.crypto.ECDSASigner;
Expand All @@ -36,9 +35,11 @@
import org.eclipse.edc.transaction.spi.NoopTransactionContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.mockito.ArgumentCaptor;

import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -69,41 +70,6 @@ class CollectionsWriteProcessorTest {
private IdentityHubStore identityHubStore;
private CollectionsWriteProcessor writeProcessor;

private static Stream<Arguments> invalidInputProvider() throws JOSEException {
var missingRecordIdDescriptor = descriptorBuilder()
.method("test")
.dateCreated(Instant.now().getEpochSecond())
.build();

var missingDateCreatedDescriptor = descriptorBuilder()
.method("test")
.recordId(UUID.randomUUID().toString())
.build();

var verifiableCredentialWithoutId = new JWTClaimsSet.Builder()
.claim("vc", "{ \"credentialSubject\": { \"foo\": \"bar\" }}")
.issuer(ISSUER)
.subject(SUBJECT)
.expirationTime(null)
.notBeforeTime(null)
.build();
var dataWithInvalidVc = buildSignedJwt(verifiableCredentialWithoutId, generateEcKey()).serialize().getBytes(StandardCharsets.UTF_8);

var jws = new SignedJWT(new JWSHeader.Builder(JWSAlgorithm.ES256).build(), new JWTClaimsSet.Builder().build());
jws.sign(new ECDSASigner(generateEcKey().toECPrivateKey()));
var dataWithoutMandatoryVcField = jws.serialize().getBytes(StandardCharsets.UTF_8);

return Stream.of(
// valid descriptor but invalid data
Arguments.of(MessageRequestObject.Builder.newInstance().descriptor(getValidDescriptor()).data(dataWithInvalidVc).build()),
Arguments.of(MessageRequestObject.Builder.newInstance().descriptor(getValidDescriptor()).data(dataWithoutMandatoryVcField).build()),
Arguments.of(MessageRequestObject.Builder.newInstance().descriptor(getValidDescriptor()).data("{".getBytes(StandardCharsets.UTF_8)).build()),
Arguments.of(MessageRequestObject.Builder.newInstance().descriptor(getValidDescriptor()).data("invalid base64".getBytes(StandardCharsets.UTF_8)).build()),
// valid date but invalid descriptor
Arguments.of(MessageRequestObject.Builder.newInstance().descriptor(missingRecordIdDescriptor).data(getValidData()).build()),
Arguments.of(MessageRequestObject.Builder.newInstance().descriptor(missingDateCreatedDescriptor).data(getValidData()).build())
);
}

private static Descriptor.Builder descriptorBuilder() {
return Descriptor.Builder.newInstance();
Expand Down Expand Up @@ -139,7 +105,7 @@ void setUp() {
}

@ParameterizedTest
@MethodSource("invalidInputProvider")
@ArgumentsSource(InvalidArgumentProvider.class)
void writeCredentials_invalidInput(MessageRequestObject requestObject) {
// Arrange
var expectedResult = MessageResponseObject.Builder.newInstance().status(MessageStatus.MALFORMED_MESSAGE).build();
Expand Down Expand Up @@ -194,4 +160,43 @@ var record = captor.getValue();
assertThat(record.getCreatedAt()).isEqualTo(requestObject.getDescriptor().getDateCreated());
assertThat(record.getPayload()).isEqualTo(requestObject.getData());
}

private static class InvalidArgumentProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {
var missingRecordIdDescriptor = descriptorBuilder()
.method("test")
.dateCreated(Instant.now().getEpochSecond())
.build();

var missingDateCreatedDescriptor = descriptorBuilder()
.method("test")
.recordId(UUID.randomUUID().toString())
.build();

var verifiableCredentialWithoutId = new JWTClaimsSet.Builder()
.claim("vc", "{ \"credentialSubject\": { \"foo\": \"bar\" }}")
.issuer(ISSUER)
.subject(SUBJECT)
.expirationTime(null)
.notBeforeTime(null)
.build();
var dataWithInvalidVc = buildSignedJwt(verifiableCredentialWithoutId, generateEcKey()).serialize().getBytes(StandardCharsets.UTF_8);

var jws = new SignedJWT(new JWSHeader.Builder(JWSAlgorithm.ES256).build(), new JWTClaimsSet.Builder().build());
jws.sign(new ECDSASigner(generateEcKey().toECPrivateKey()));
var dataWithoutMandatoryVcField = jws.serialize().getBytes(StandardCharsets.UTF_8);

return Stream.of(
// valid descriptor but invalid data
Arguments.of(MessageRequestObject.Builder.newInstance().descriptor(getValidDescriptor()).data(dataWithInvalidVc).build()),
Arguments.of(MessageRequestObject.Builder.newInstance().descriptor(getValidDescriptor()).data(dataWithoutMandatoryVcField).build()),
Arguments.of(MessageRequestObject.Builder.newInstance().descriptor(getValidDescriptor()).data("{".getBytes(StandardCharsets.UTF_8)).build()),
Arguments.of(MessageRequestObject.Builder.newInstance().descriptor(getValidDescriptor()).data("invalid base64".getBytes(StandardCharsets.UTF_8)).build()),
// valid date but invalid descriptor
Arguments.of(MessageRequestObject.Builder.newInstance().descriptor(missingRecordIdDescriptor).data(getValidData()).build()),
Arguments.of(MessageRequestObject.Builder.newInstance().descriptor(missingDateCreatedDescriptor).data(getValidData()).build())
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import org.eclipse.edc.iam.did.spi.credentials.CredentialsVerifier;
import org.eclipse.edc.iam.did.spi.document.DidConstants;
import org.eclipse.edc.iam.did.spi.document.DidDocument;
import org.eclipse.edc.iam.did.spi.document.EllipticCurvePublicKey;
import org.eclipse.edc.iam.did.spi.document.Service;
import org.eclipse.edc.iam.did.spi.document.VerificationMethod;
import org.eclipse.edc.iam.did.spi.resolution.DidResolver;
import org.eclipse.edc.iam.did.spi.resolution.DidResolverRegistry;
import org.eclipse.edc.identityhub.client.IdentityHubClientImpl;
Expand All @@ -36,6 +36,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -66,11 +67,15 @@ class CredentialsVerifierExtensionTest {
private IdentityHubClient identityHubClient;

private static DidDocument createDidDocument(ECKey jwk) {
var ecKey = TYPE_MANAGER.readValue(jwk.toJSONString(), EllipticCurvePublicKey.class);
var vm = VerificationMethod.Builder.create()
.id(UUID.randomUUID().toString())
.type(DidConstants.ECDSA_SECP_256_K_1_VERIFICATION_KEY_2019)
.publicKeyJwk(jwk.toPublicJWK().toJSONObject())
.build();
return DidDocument.Builder.newInstance()
.id(SUBJECT)
.service(List.of(new Service("IdentityHub", "IdentityHub", API_URL)))
.verificationMethod(UUID.randomUUID().toString(), DidConstants.ECDSA_SECP_256_K_1_VERIFICATION_KEY_2019, ecKey)
.verificationMethod(Collections.singletonList(vm))
.build();
}

Expand Down

0 comments on commit 2a1482b

Please sign in to comment.