Changesets · Branches · Tags · Compare
| Changeset | 14ab458f2fd23c293be54c14819eb064bea2fd1802abd601ee82da5234252e49 |
|---|---|
| Author | Paul Latzelsperger <43503240+paullatzelsperger@users.noreply.github.com> · 2024-08-22 16:17 +02:00 |
| Committer | GitHub <noreply@github.com> · 2024-08-22 16:17 +02:00 |
| Parents | 6e5c685ca469 |
38 files changed
modified core/common/lib/util-lib/src/main/java/org/eclipse/edc/util/collection/Cache.java +17 −0
| @@ -21,6 +21,8 @@ | ||
| 21 | 21 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 22 | 22 | import java.util.function.Function; |
| 23 | 23 | |
| 24 | +import static java.util.Optional.ofNullable; | |
| 25 | + | |
| 24 | 26 | /** |
| 25 | 27 | * Cache, that maintains a map of key-value pairs, where values have an individual expiry. When values expire, they are re-fetched |
| 26 | 28 | * and put back in the cache. |
| @@ -78,6 +80,21 @@ | ||
| 78 | 80 | return value; |
| 79 | 81 | } |
| 80 | 82 | |
| 83 | + /** | |
| 84 | + * Explicitly removes an entry from the cache | |
| 85 | + * | |
| 86 | + * @param key the key | |
| 87 | + * @return the value previously associated with "key", or null if no value was associated with the key | |
| 88 | + */ | |
| 89 | + public V evict(K key) { | |
| 90 | + lock.writeLock().lock(); | |
| 91 | + try { | |
| 92 | + return ofNullable(cache.remove(key)).map(TimestampedValue::value).orElse(null); | |
| 93 | + } finally { | |
| 94 | + lock.writeLock().unlock(); | |
| 95 | + } | |
| 96 | + } | |
| 97 | + | |
| 81 | 98 | private boolean isEntryExpired(K key) { |
| 82 | 99 | var timestampedValue = cache.get(key); |
| 83 | 100 | if (timestampedValue == null) return true; |
modified core/common/lib/util-lib/src/test/java/org/eclipse/edc/util/collection/CacheTest.java +23 −0
| @@ -77,6 +77,29 @@ | ||
| 77 | 77 | assertThat(testObject).isNull(); |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | + @Test | |
| 81 | + void evict_whenPresent() { | |
| 82 | + when(updateFunction.apply(anyString())).thenReturn(new TestObject(42)); | |
| 83 | + cache.get("foo"); | |
| 84 | + | |
| 85 | + assertThat(cache.evict("foo").value).isEqualTo(42); | |
| 86 | + | |
| 87 | + var testObject = cache.get("foo"); | |
| 88 | + assertThat(testObject.value()).isEqualTo(42); | |
| 89 | + verify(updateFunction, times(2)).apply(anyString()); | |
| 90 | + } | |
| 91 | + | |
| 92 | + @Test | |
| 93 | + void evict_whenNotPresent() { | |
| 94 | + when(updateFunction.apply(anyString())).thenReturn(new TestObject(42)); | |
| 95 | + | |
| 96 | + assertThat(cache.evict("foo")).isNull(); | |
| 97 | + | |
| 98 | + var testObject = cache.get("foo"); | |
| 99 | + assertThat(testObject.value()).isEqualTo(42); | |
| 100 | + verify(updateFunction, times(1)).apply(anyString()); | |
| 101 | + } | |
| 102 | + | |
| 80 | 103 | private record TestObject(int value) { |
| 81 | 104 | |
| 82 | 105 | } |
modified extensions/common/iam/identity-trust/identity-trust-core/src/main/java/org/eclipse/edc/iam/identitytrust/core/IdentityAndTrustExtension.java +14 −12
| @@ -28,9 +28,12 @@ | ||
| 28 | 28 | import org.eclipse.edc.iam.identitytrust.spi.SecureTokenService; |
| 29 | 29 | import org.eclipse.edc.iam.identitytrust.spi.validation.TokenValidationAction; |
| 30 | 30 | import org.eclipse.edc.iam.identitytrust.spi.verification.SignatureSuiteRegistry; |
| 31 | -import org.eclipse.edc.iam.verifiablecredentials.StatusList2021RevocationService; | |
| 32 | 31 | import org.eclipse.edc.iam.verifiablecredentials.VerifiableCredentialValidationServiceImpl; |
| 33 | -import org.eclipse.edc.iam.verifiablecredentials.spi.RevocationListService; | |
| 32 | +import org.eclipse.edc.iam.verifiablecredentials.revocation.bitstring.BitstringStatusListRevocationService; | |
| 33 | +import org.eclipse.edc.iam.verifiablecredentials.revocation.statuslist2021.StatusList2021RevocationService; | |
| 34 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.RevocationServiceRegistry; | |
| 35 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus; | |
| 36 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021.StatusList2021Status; | |
| 34 | 37 | import org.eclipse.edc.iam.verifiablecredentials.spi.validation.PresentationVerifier; |
| 35 | 38 | import org.eclipse.edc.iam.verifiablecredentials.spi.validation.TrustedIssuerRegistry; |
| 36 | 39 | import org.eclipse.edc.jsonld.spi.JsonLd; |
| @@ -123,9 +126,11 @@ | ||
| 123 | 126 | @Inject |
| 124 | 127 | private DcpParticipantAgentServiceExtension participantAgentServiceExtension; |
| 125 | 128 | |
| 129 | + @Inject | |
| 130 | + private RevocationServiceRegistry revocationServiceRegistry; | |
| 131 | + | |
| 126 | 132 | private PresentationVerifier presentationVerifier; |
| 127 | 133 | private CredentialServiceClient credentialServiceClient; |
| 128 | - private RevocationListService revocationListService; | |
| 129 | 134 | |
| 130 | 135 | @Override |
| 131 | 136 | public void initialize(ServiceExtensionContext context) { |
| @@ -152,6 +157,11 @@ | ||
| 152 | 157 | } |
| 153 | 158 | |
| 154 | 159 | participantAgentService.register(participantAgentServiceExtension); |
| 160 | + | |
| 161 | + // register revocation services | |
| 162 | + var validity = context.getConfig().getLong(REVOCATION_CACHE_VALIDITY, DEFAULT_REVOCATION_CACHE_VALIDITY_MILLIS); | |
| 163 | + revocationServiceRegistry.addService(StatusList2021Status.TYPE, new StatusList2021RevocationService(typeManager.getMapper(), validity)); | |
| 164 | + revocationServiceRegistry.addService(BitstringStatusListStatus.TYPE, new BitstringStatusListRevocationService(typeManager.getMapper(), validity)); | |
| 155 | 165 | } |
| 156 | 166 | |
| 157 | 167 | @Provider |
| @@ -160,7 +170,7 @@ | ||
| 160 | 170 | var validationAction = tokenValidationAction(); |
| 161 | 171 | |
| 162 | 172 | var credentialValidationService = new VerifiableCredentialValidationServiceImpl(createPresentationVerifier(context), |
| 163 | - trustedIssuerRegistry, createRevocationListService(context), clock); | |
| 173 | + trustedIssuerRegistry, revocationServiceRegistry, clock); | |
| 164 | 174 | |
| 165 | 175 | return new IdentityAndTrustService(secureTokenService, getOwnDid(context), |
| 166 | 176 | getCredentialServiceClient(context), validationAction, credentialServiceUrlResolver, claimTokenFunction, |
| @@ -194,14 +204,6 @@ | ||
| 194 | 204 | return presentationVerifier; |
| 195 | 205 | } |
| 196 | 206 | |
| 197 | - @Provider | |
| 198 | - public RevocationListService createRevocationListService(ServiceExtensionContext context) { | |
| 199 | - if (revocationListService == null) { | |
| 200 | - var validity = context.getConfig().getLong(REVOCATION_CACHE_VALIDITY, DEFAULT_REVOCATION_CACHE_VALIDITY_MILLIS); | |
| 201 | - revocationListService = new StatusList2021RevocationService(typeManager.getMapper(), validity); | |
| 202 | - } | |
| 203 | - return revocationListService; | |
| 204 | - } | |
| 205 | 207 | |
| 206 | 208 | @NotNull |
| 207 | 209 | private TokenValidationAction tokenValidationAction() { |
added extensions/common/iam/verifiable-credentials/src/main/java/org/eclipse/edc/iam/verifiablecredentials/RevocationServiceRegistryExtension.java +39 −0
| @@ -0,0 +1,39 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.iam.verifiablecredentials.revocation.RevocationServiceRegistryImpl; | |
| 18 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.RevocationServiceRegistry; | |
| 19 | +import org.eclipse.edc.runtime.metamodel.annotation.Extension; | |
| 20 | +import org.eclipse.edc.runtime.metamodel.annotation.Provider; | |
| 21 | +import org.eclipse.edc.spi.system.ServiceExtension; | |
| 22 | +import org.eclipse.edc.spi.system.ServiceExtensionContext; | |
| 23 | + | |
| 24 | +import static org.eclipse.edc.iam.verifiablecredentials.RevocationServiceRegistryExtension.NAME; | |
| 25 | + | |
| 26 | +@Extension(value = NAME) | |
| 27 | +public class RevocationServiceRegistryExtension implements ServiceExtension { | |
| 28 | + public static final String NAME = "Revocation Service Extension"; | |
| 29 | + | |
| 30 | + @Override | |
| 31 | + public String name() { | |
| 32 | + return NAME; | |
| 33 | + } | |
| 34 | + | |
| 35 | + @Provider | |
| 36 | + public RevocationServiceRegistry createRevocationListService(ServiceExtensionContext context) { | |
| 37 | + return new RevocationServiceRegistryImpl(context.getMonitor()); | |
| 38 | + } | |
| 39 | +} |
deleted extensions/common/iam/verifiable-credentials/src/main/java/org/eclipse/edc/iam/verifiablecredentials/StatusList2021RevocationService.java +0 −131
| @@ -1,131 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | - * | |
| 4 | - * This program and the accompanying materials are made available under the | |
| 5 | - * terms of the Apache License, Version 2.0 which is available at | |
| 6 | - * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | - * | |
| 8 | - * SPDX-License-Identifier: Apache-2.0 | |
| 9 | - * | |
| 10 | - * Contributors: | |
| 11 | - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | - * | |
| 13 | - */ | |
| 14 | - | |
| 15 | -package org.eclipse.edc.iam.verifiablecredentials; | |
| 16 | - | |
| 17 | -import com.fasterxml.jackson.databind.DeserializationFeature; | |
| 18 | -import com.fasterxml.jackson.databind.ObjectMapper; | |
| 19 | -import org.eclipse.edc.iam.verifiablecredentials.spi.RevocationListService; | |
| 20 | -import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiableCredential; | |
| 21 | -import org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist.BitString; | |
| 22 | -import org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist.StatusList2021Credential; | |
| 23 | -import org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist.StatusListStatus; | |
| 24 | -import org.eclipse.edc.spi.result.AbstractResult; | |
| 25 | -import org.eclipse.edc.spi.result.Result; | |
| 26 | -import org.eclipse.edc.util.collection.Cache; | |
| 27 | - | |
| 28 | -import java.io.IOException; | |
| 29 | -import java.net.URI; | |
| 30 | -import java.util.Map; | |
| 31 | -import java.util.stream.Collectors; | |
| 32 | - | |
| 33 | -import static org.eclipse.edc.spi.result.Result.success; | |
| 34 | - | |
| 35 | -/** | |
| 36 | - * Service to check if a particular {@link VerifiableCredential} is "valid", where "validity" is defined as not revoked and not suspended. | |
| 37 | - * Credentials, that don't have a {@code credentialStatus} object are deemed "valid" as well. | |
| 38 | - * <p> | |
| 39 | - * To achieve that, the {@link VerifiableCredential#getCredentialStatus()} object is inspected and checked against the status list credential referenced therein. | |
| 40 | - * <p> | |
| 41 | - * To limit traffic on the actual StatusList2021 credential, it is cached in a thread-safe {@link Map}, and only re-downloaded if the cache is expired. | |
| 42 | - */ | |
| 43 | -public class StatusList2021RevocationService implements RevocationListService { | |
| 44 | - private final ObjectMapper objectMapper; | |
| 45 | - private final Cache<String, VerifiableCredential> cache; | |
| 46 | - | |
| 47 | - public StatusList2021RevocationService(ObjectMapper objectMapper, long cacheValidity) { | |
| 48 | - this.objectMapper = objectMapper.copy() | |
| 49 | - .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) // technically, credential subjects and credential status can be objects AND Arrays | |
| 50 | - .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // let's make sure this is disabled, because the "@context" would cause problems | |
| 51 | - cache = new Cache<>(this::downloadStatusListCredential, cacheValidity); | |
| 52 | - } | |
| 53 | - | |
| 54 | - @Override | |
| 55 | - public Result<Void> checkValidity(VerifiableCredential credential) { | |
| 56 | - return credential.getCredentialStatus().stream().map(StatusListStatus::parse) | |
| 57 | - .map(this::checkStatus) | |
| 58 | - .reduce(Result::merge) | |
| 59 | - .orElse(Result.failure("Could not check the validity of the credential with ID '%s'".formatted(credential.getId()))); | |
| 60 | - } | |
| 61 | - | |
| 62 | - @Override | |
| 63 | - public Result<String> getStatusPurpose(VerifiableCredential credential) { | |
| 64 | - if (credential.getCredentialStatus().isEmpty()) { | |
| 65 | - return success(null); | |
| 66 | - } | |
| 67 | - var res = credential.getCredentialStatus().stream() | |
| 68 | - .map(StatusListStatus::parse) | |
| 69 | - .map(this::getStatusInternal) | |
| 70 | - .collect(Collectors.groupingBy(AbstractResult::succeeded)); //partition by succeeded/failed | |
| 71 | - | |
| 72 | - if (res.containsKey(false)) { | |
| 73 | - return Result.failure(res.get(false).stream().map(AbstractResult::getFailureDetail).toList()); | |
| 74 | - } | |
| 75 | - | |
| 76 | - var list = res.get(true).stream() | |
| 77 | - .filter(r -> r.getContent() != null) | |
| 78 | - .map(AbstractResult::getContent).toList(); | |
| 79 | - | |
| 80 | - // get(0) is OK, because there should only be 1 credentialStatus | |
| 81 | - return list.isEmpty() ? success(null) : success(list.get(0)); | |
| 82 | - | |
| 83 | - } | |
| 84 | - | |
| 85 | - private Result<Void> checkStatus(StatusListStatus status) { | |
| 86 | - var index = status.getStatusListIndex(); | |
| 87 | - return getStatusInternal(status) | |
| 88 | - .compose(purpose -> purpose != null ? | |
| 89 | - Result.failure("Credential status is '%s', status at index %d is '1'".formatted(purpose, index)) : | |
| 90 | - success()); | |
| 91 | - } | |
| 92 | - | |
| 93 | - /** | |
| 94 | - * Obtains the status purpose for a particular credentialStatus entry if it is set, otherwise returns a successful result with a {@code null} content. | |
| 95 | - * So, a successful result with a non-null content indicates, that the respective credentialStatus is set. | |
| 96 | - */ | |
| 97 | - private Result<String> getStatusInternal(StatusListStatus status) { | |
| 98 | - var index = status.getStatusListIndex(); | |
| 99 | - var slCredUrl = status.getStatusListCredential(); | |
| 100 | - var credential = cache.get(slCredUrl); | |
| 101 | - var slCred = StatusList2021Credential.parse(credential); | |
| 102 | - | |
| 103 | - // check that the "statusPurpose" values match | |
| 104 | - var purpose = status.getStatusListPurpose(); | |
| 105 | - var slCredPurpose = slCred.statusPurpose(); | |
| 106 | - if (!purpose.equalsIgnoreCase(slCredPurpose)) { | |
| 107 | - return Result.failure("Credential's statusPurpose value must match the status list's purpose: '%s' != '%s'".formatted(purpose, slCredPurpose)); | |
| 108 | - } | |
| 109 | - | |
| 110 | - var bitStringResult = BitString.Parser.newInstance().parse(slCred.encodedList()); | |
| 111 | - | |
| 112 | - if (bitStringResult.failed()) { | |
| 113 | - return bitStringResult.mapTo(); | |
| 114 | - } | |
| 115 | - var bitString = bitStringResult.getContent(); | |
| 116 | - | |
| 117 | - // check that the value at index in the bitset is "1" | |
| 118 | - if (bitString.get(index)) { | |
| 119 | - return success(purpose); | |
| 120 | - } | |
| 121 | - return success(null); | |
| 122 | - } | |
| 123 | - | |
| 124 | - private VerifiableCredential downloadStatusListCredential(String credentialUrl) { | |
| 125 | - try { | |
| 126 | - return objectMapper.readValue(URI.create(credentialUrl).toURL(), VerifiableCredential.class); | |
| 127 | - } catch (IOException e) { | |
| 128 | - throw new RuntimeException(e); | |
| 129 | - } | |
| 130 | - } | |
| 131 | -} |
modified extensions/common/iam/verifiable-credentials/src/main/java/org/eclipse/edc/iam/verifiablecredentials/VerifiableCredentialValidationServiceImpl.java +5 −5
| @@ -18,9 +18,9 @@ | ||
| 18 | 18 | import org.eclipse.edc.iam.verifiablecredentials.rules.HasValidSubjectIds; |
| 19 | 19 | import org.eclipse.edc.iam.verifiablecredentials.rules.IsInValidityPeriod; |
| 20 | 20 | import org.eclipse.edc.iam.verifiablecredentials.rules.IsNotRevoked; |
| 21 | -import org.eclipse.edc.iam.verifiablecredentials.spi.RevocationListService; | |
| 22 | 21 | import org.eclipse.edc.iam.verifiablecredentials.spi.VerifiableCredentialValidationService; |
| 23 | 22 | import org.eclipse.edc.iam.verifiablecredentials.spi.model.Issuer; |
| 23 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.RevocationServiceRegistry; | |
| 24 | 24 | import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiableCredential; |
| 25 | 25 | import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiablePresentationContainer; |
| 26 | 26 | import org.eclipse.edc.iam.verifiablecredentials.spi.validation.CredentialValidationRule; |
| @@ -39,13 +39,13 @@ | ||
| 39 | 39 | public class VerifiableCredentialValidationServiceImpl implements VerifiableCredentialValidationService { |
| 40 | 40 | private final PresentationVerifier presentationVerifier; |
| 41 | 41 | private final TrustedIssuerRegistry trustedIssuerRegistry; |
| 42 | - private final RevocationListService revocationListService; | |
| 42 | + private final RevocationServiceRegistry revocationServiceRegistry; | |
| 43 | 43 | private final Clock clock; |
| 44 | 44 | |
| 45 | - public VerifiableCredentialValidationServiceImpl(PresentationVerifier presentationVerifier, TrustedIssuerRegistry trustedIssuerRegistry, RevocationListService revocationListService, Clock clock) { | |
| 45 | + public VerifiableCredentialValidationServiceImpl(PresentationVerifier presentationVerifier, TrustedIssuerRegistry trustedIssuerRegistry, RevocationServiceRegistry revocationServiceRegistry, Clock clock) { | |
| 46 | 46 | this.presentationVerifier = presentationVerifier; |
| 47 | 47 | this.trustedIssuerRegistry = trustedIssuerRegistry; |
| 48 | - this.revocationListService = revocationListService; | |
| 48 | + this.revocationServiceRegistry = revocationServiceRegistry; | |
| 49 | 49 | this.clock = clock; |
| 50 | 50 | } |
| 51 | 51 | |
| @@ -67,7 +67,7 @@ | ||
| 67 | 67 | var filters = new ArrayList<>(List.of( |
| 68 | 68 | new IsInValidityPeriod(clock), |
| 69 | 69 | new HasValidSubjectIds(presentationHolder), |
| 70 | - new IsNotRevoked(revocationListService), | |
| 70 | + new IsNotRevoked(revocationServiceRegistry), | |
| 71 | 71 | new HasValidIssuer(getTrustedIssuerIds()))); |
| 72 | 72 | |
| 73 | 73 | filters.addAll(additionalRules); |
added extensions/common/iam/verifiable-credentials/src/main/java/org/eclipse/edc/iam/verifiablecredentials/revocation/BaseRevocationListService.java +159 −0
| @@ -0,0 +1,159 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.revocation; | |
| 16 | + | |
| 17 | +import com.fasterxml.jackson.databind.DeserializationFeature; | |
| 18 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
| 19 | +import org.eclipse.edc.iam.verifiablecredentials.spi.RevocationListService; | |
| 20 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 21 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiableCredential; | |
| 22 | +import org.eclipse.edc.spi.EdcException; | |
| 23 | +import org.eclipse.edc.spi.result.AbstractResult; | |
| 24 | +import org.eclipse.edc.spi.result.Result; | |
| 25 | +import org.eclipse.edc.util.collection.Cache; | |
| 26 | + | |
| 27 | +import java.io.IOException; | |
| 28 | +import java.net.URI; | |
| 29 | +import java.time.Instant; | |
| 30 | +import java.util.Map; | |
| 31 | +import java.util.stream.Collectors; | |
| 32 | + | |
| 33 | +import static org.eclipse.edc.spi.result.Result.success; | |
| 34 | + | |
| 35 | +/** | |
| 36 | + * Service to check if a particular {@link VerifiableCredential} is "valid", where "validity" is defined as not revoked and not suspended nor having | |
| 37 | + * any other status. Credentials that don't have a {@code credentialStatus} object are deemed "valid" as well. | |
| 38 | + * <p> | |
| 39 | + * To achieve that, the {@link VerifiableCredential#getCredentialStatus()} object is inspected and checked against the status list credential referenced therein. | |
| 40 | + * <p> | |
| 41 | + * To limit traffic on the actual StatusList credential, it is cached in a thread-safe {@link Map}, and only re-downloaded if the cache is expired. | |
| 42 | + * <p> | |
| 43 | + * Currently, StatusList2021 and BitStringStatusList are supported. | |
| 44 | + */ | |
| 45 | +public abstract class BaseRevocationListService<C extends VerifiableCredential, S> implements RevocationListService { | |
| 46 | + private final Cache<String, C> statusListCredentialCache; | |
| 47 | + private final Class<C> credentialClass; | |
| 48 | + private final ObjectMapper objectMapper; | |
| 49 | + | |
| 50 | + protected BaseRevocationListService(ObjectMapper mapper, long cacheValidity, Class<C> credentialClass) { | |
| 51 | + this.objectMapper = mapper.copy() | |
| 52 | + .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) // technically, credential subjects and credential status can be objects AND Arrays | |
| 53 | + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // let's make sure this is disabled, because the "@context" would cause problems | |
| 54 | + statusListCredentialCache = new Cache<>(this::downloadStatusListCredential, cacheValidity); | |
| 55 | + this.credentialClass = credentialClass; | |
| 56 | + } | |
| 57 | + | |
| 58 | + @Override | |
| 59 | + public Result<Void> checkValidity(CredentialStatus credential) { | |
| 60 | + var credentialStatus = getCredentialStatus(credential); | |
| 61 | + var credentialIndex = getStatusIndex(credentialStatus); | |
| 62 | + return preliminaryChecks(credentialStatus) | |
| 63 | + .compose(v -> validateStatusPurpose(credentialStatus)) | |
| 64 | + .compose(v -> getStatusEntryValue(credentialStatus)) | |
| 65 | + .compose(status -> status != null ? | |
| 66 | + Result.failure("Credential status is '%s', status at index %d is '1'".formatted(status, credentialIndex)) : | |
| 67 | + Result.success()); | |
| 68 | + } | |
| 69 | + | |
| 70 | + @Override | |
| 71 | + public Result<String> getStatusPurpose(VerifiableCredential credential) { | |
| 72 | + if (credential.getCredentialStatus().isEmpty()) { | |
| 73 | + return success(null); | |
| 74 | + } | |
| 75 | + | |
| 76 | + var res = credential.getCredentialStatus().stream() | |
| 77 | + .map(this::getCredentialStatus) | |
| 78 | + .map(this::getStatusEntryValue) | |
| 79 | + .collect(Collectors.groupingBy(AbstractResult::succeeded)); | |
| 80 | + | |
| 81 | + if (res.containsKey(false)) { //if any failed | |
| 82 | + return Result.failure(res.get(false).stream().map(AbstractResult::getFailureDetail).toList()); | |
| 83 | + } | |
| 84 | + | |
| 85 | + var list = res.get(true).stream() | |
| 86 | + .filter(r -> r.getContent() != null) | |
| 87 | + .map(AbstractResult::getContent).toList(); | |
| 88 | + | |
| 89 | + return list.isEmpty() ? success(null) : success(String.join(", ", list)); | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * Hook to perform some preliminary checks before the actual status list validation is performed. Use this method to | |
| 94 | + * perform length checks, shape validation, etc. | |
| 95 | + * | |
| 96 | + * @param credentialStatus the credentialStatus object of the VC (not the StatusList credential!) | |
| 97 | + * @return A {@link Result} indicating the outcome of the preliminary checks. | |
| 98 | + */ | |
| 99 | + protected Result<Void> preliminaryChecks(S credentialStatus) { | |
| 100 | + return Result.success(); | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * Gets a statuslist credential from the cache, of if it's not there yet, downloads it. | |
| 105 | + * | |
| 106 | + * @param credentialUrl the URL from where to download the cred | |
| 107 | + * @return the VerifiableCredential | |
| 108 | + * @throws EdcException if it could not be downloaded | |
| 109 | + */ | |
| 110 | + protected C getCredential(String credentialUrl) { | |
| 111 | + var credential = statusListCredentialCache.get(credentialUrl); | |
| 112 | + // credential is cached, but expired -> download again | |
| 113 | + if (credential != null && credential.getExpirationDate() != null && credential.getExpirationDate().isBefore(Instant.now())) { | |
| 114 | + statusListCredentialCache.evict(credentialUrl); | |
| 115 | + } | |
| 116 | + return statusListCredentialCache.get(credentialUrl); | |
| 117 | + } | |
| 118 | + | |
| 119 | + /** | |
| 120 | + * Obtains the status purpose for a particular credentialStatus entry if it is set, otherwise returns a successful result with a {@code null} content. | |
| 121 | + * So, a successful result with a non-null content indicates, that the respective credentialStatus is set. | |
| 122 | + * | |
| 123 | + * @param credentialStatus the credentialStatus object of the VC (not the StatusList credential!) | |
| 124 | + */ | |
| 125 | + protected abstract Result<String> getStatusEntryValue(S credentialStatus); | |
| 126 | + | |
| 127 | + /** | |
| 128 | + * Validates, that the statusPurpose of the credentialStatus is equal to the one found in the StatusList Credential | |
| 129 | + * | |
| 130 | + * @param credentialStatus the credentialStatus object of the VC (not the StatusList credential!) | |
| 131 | + */ | |
| 132 | + protected abstract Result<Void> validateStatusPurpose(S credentialStatus); | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Gets the {@code statusIndex} entry of the VC's credentialStatus object. | |
| 136 | + * | |
| 137 | + * @param credentialStatus the credentialStatus object of the VC (not the StatusList credential!) | |
| 138 | + * @return the statusIndex entry | |
| 139 | + */ | |
| 140 | + protected abstract int getStatusIndex(S credentialStatus); | |
| 141 | + | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * Converts the generic {@link CredentialStatus} into a specific credentialStatus that is used by the concrete status list | |
| 145 | + * implementation. | |
| 146 | + * | |
| 147 | + * @param credentialStatus The generic {@link CredentialStatus} | |
| 148 | + * @return A specific credential status object | |
| 149 | + */ | |
| 150 | + protected abstract S getCredentialStatus(CredentialStatus credentialStatus); | |
| 151 | + | |
| 152 | + private C downloadStatusListCredential(String credentialUrl) { | |
| 153 | + try { | |
| 154 | + return objectMapper.readValue(URI.create(credentialUrl).toURL(), credentialClass); | |
| 155 | + } catch (IOException e) { | |
| 156 | + throw new EdcException(e); | |
| 157 | + } | |
| 158 | + } | |
| 159 | +} |
added extensions/common/iam/verifiable-credentials/src/main/java/org/eclipse/edc/iam/verifiablecredentials/revocation/RevocationServiceRegistryImpl.java +57 −0
| @@ -0,0 +1,57 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.revocation; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.iam.verifiablecredentials.spi.RevocationListService; | |
| 18 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 19 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.RevocationServiceRegistry; | |
| 20 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiableCredential; | |
| 21 | +import org.eclipse.edc.spi.monitor.Monitor; | |
| 22 | +import org.eclipse.edc.spi.result.Result; | |
| 23 | + | |
| 24 | +import java.util.HashMap; | |
| 25 | +import java.util.Map; | |
| 26 | + | |
| 27 | +public class RevocationServiceRegistryImpl implements RevocationServiceRegistry { | |
| 28 | + private final Map<String, RevocationListService> entries = new HashMap<>(); | |
| 29 | + private final Monitor monitor; | |
| 30 | + | |
| 31 | + public RevocationServiceRegistryImpl(Monitor monitor) { | |
| 32 | + this.monitor = monitor; | |
| 33 | + } | |
| 34 | + | |
| 35 | + @Override | |
| 36 | + public void addService(String statusListType, RevocationListService service) { | |
| 37 | + entries.put(statusListType, service); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + public Result<Void> checkValidity(VerifiableCredential credential) { | |
| 42 | + return credential.getCredentialStatus() | |
| 43 | + .stream() | |
| 44 | + .map(this::checkRevocation) | |
| 45 | + .reduce(Result::merge) | |
| 46 | + .orElse(Result.success()); | |
| 47 | + } | |
| 48 | + | |
| 49 | + private Result<Void> checkRevocation(CredentialStatus credentialStatus) { | |
| 50 | + var service = entries.get(credentialStatus.type()); | |
| 51 | + if (service == null) { | |
| 52 | + monitor.warning("No revocation service registered for type '%s', will not check revocation.".formatted(credentialStatus.type())); | |
| 53 | + return Result.success(); | |
| 54 | + } | |
| 55 | + return service.checkValidity(credentialStatus); | |
| 56 | + } | |
| 57 | +} |
added extensions/common/iam/verifiable-credentials/src/main/java/org/eclipse/edc/iam/verifiablecredentials/revocation/bitstring/BitstringStatusListRevocationService.java +114 −0
| @@ -0,0 +1,114 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.revocation.bitstring; | |
| 16 | + | |
| 17 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
| 18 | +import org.eclipse.edc.iam.verifiablecredentials.revocation.BaseRevocationListService; | |
| 19 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 20 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.BitString; | |
| 21 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListCredential; | |
| 22 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus; | |
| 23 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.StatusMessage; | |
| 24 | +import org.eclipse.edc.spi.result.Result; | |
| 25 | + | |
| 26 | +import java.util.Base64; | |
| 27 | + | |
| 28 | +import static org.eclipse.edc.spi.result.Result.success; | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * StatusList revocation service implementing the <a href="https://www.w3.org/TR/vc-bitstring-status-list/">BitStringStatusList</a> | |
| 32 | + * specification. | |
| 33 | + */ | |
| 34 | +public class BitstringStatusListRevocationService extends BaseRevocationListService<BitstringStatusListCredential, BitstringStatusListStatus> { | |
| 35 | + | |
| 36 | + public BitstringStatusListRevocationService(ObjectMapper mapper, long cacheValidity) { | |
| 37 | + super(mapper, cacheValidity, BitstringStatusListCredential.class); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + protected Result<Void> preliminaryChecks(BitstringStatusListStatus credentialStatus) { | |
| 42 | + var statusSize = credentialStatus.getStatusSize(); | |
| 43 | + if (statusSize != 1) { //todo: support more statusSize entries in the future | |
| 44 | + return Result.failure("Unsupported statusSize: currently only statusSize = 1 is supported. The VC contained statusSize = %d".formatted(statusSize)); | |
| 45 | + } | |
| 46 | + return success(); | |
| 47 | + } | |
| 48 | + | |
| 49 | + @Override | |
| 50 | + protected Result<String> getStatusEntryValue(BitstringStatusListStatus credentialStatus) { | |
| 51 | + var bitStringCredential = getCredential(credentialStatus.getStatusListCredential()); | |
| 52 | + | |
| 53 | + var bitString = bitStringCredential.encodedList(); | |
| 54 | + var decoder = Base64.getDecoder(); | |
| 55 | + if (bitString.charAt(0) == 'u') { // base64 url | |
| 56 | + decoder = Base64.getUrlDecoder(); | |
| 57 | + bitString = bitString.substring(1); //chop off header | |
| 58 | + } else if (bitString.charAt(0) == 'z') { //base58btc | |
| 59 | + return Result.failure("The encoded list is using the Base58-BTC alphabet ('z' multibase header), which is not supported."); | |
| 60 | + } | |
| 61 | + | |
| 62 | + var compressedBitstring = BitString.Parser.newInstance().decoder(decoder).parse(bitString); | |
| 63 | + if (compressedBitstring.failed()) { | |
| 64 | + return compressedBitstring.mapEmpty(); | |
| 65 | + } | |
| 66 | + var bitstring = compressedBitstring.getContent(); | |
| 67 | + | |
| 68 | + // todo: check that encodedList / statusSize == minimumLength (defaults to 131_072 = encodedList minimum length in bits), | |
| 69 | + // otherwise raise error | |
| 70 | + // todo: how to determine minimumLength? via config? | |
| 71 | + | |
| 72 | + var statusFlag = bitstring.get(credentialStatus.getStatusListIndex()); | |
| 73 | + | |
| 74 | + var statusPurpose = credentialStatus.getStatusListPurpose(); | |
| 75 | + // if the purpose is "message", we need to check the statusMessage object for the actual string | |
| 76 | + if (statusPurpose.equalsIgnoreCase("message")) { | |
| 77 | + var statusString = statusFlag ? "0x1" : "0x0"; //todo: change this when statusSize > 1 is supported | |
| 78 | + statusPurpose = credentialStatus.getStatusMessage().stream().filter(sm -> sm.status().equals(statusString)).map(StatusMessage::message).findAny().orElse(statusPurpose); | |
| 79 | + | |
| 80 | + return success(statusPurpose); | |
| 81 | + } else if (statusFlag) { | |
| 82 | + // currently, this supports only a statusSize of 1 | |
| 83 | + return success(statusPurpose); | |
| 84 | + } | |
| 85 | + | |
| 86 | + return success(null); | |
| 87 | + } | |
| 88 | + | |
| 89 | + @Override | |
| 90 | + protected Result<Void> validateStatusPurpose(BitstringStatusListStatus credentialStatus) { | |
| 91 | + var statusPurpose = credentialStatus.getStatusListPurpose(); | |
| 92 | + | |
| 93 | + var credentialUrl = credentialStatus.getStatusListCredential(); | |
| 94 | + var statusListCredential = getCredential(credentialUrl); | |
| 95 | + var credentialStatusPurpose = statusListCredential.statusPurpose(); | |
| 96 | + | |
| 97 | + if (!statusPurpose.equalsIgnoreCase(credentialStatusPurpose)) { | |
| 98 | + return Result.failure("Credential's statusPurpose value must match the statusPurpose of the Bitstring Credential: '%s' != '%s'".formatted(statusPurpose, credentialStatusPurpose)); | |
| 99 | + } | |
| 100 | + | |
| 101 | + return success(); | |
| 102 | + } | |
| 103 | + | |
| 104 | + @Override | |
| 105 | + protected int getStatusIndex(BitstringStatusListStatus credentialStatus) { | |
| 106 | + return credentialStatus.getStatusListIndex(); | |
| 107 | + } | |
| 108 | + | |
| 109 | + @Override | |
| 110 | + protected BitstringStatusListStatus getCredentialStatus(CredentialStatus credentialStatus) { | |
| 111 | + return BitstringStatusListStatus.from(credentialStatus); | |
| 112 | + } | |
| 113 | + | |
| 114 | +} |
added extensions/common/iam/verifiable-credentials/src/main/java/org/eclipse/edc/iam/verifiablecredentials/revocation/statuslist2021/StatusList2021RevocationService.java +82 −0
| @@ -0,0 +1,82 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.revocation.statuslist2021; | |
| 16 | + | |
| 17 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
| 18 | +import org.eclipse.edc.iam.verifiablecredentials.revocation.BaseRevocationListService; | |
| 19 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 20 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.BitString; | |
| 21 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021.StatusList2021Credential; | |
| 22 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021.StatusList2021Status; | |
| 23 | +import org.eclipse.edc.spi.result.Result; | |
| 24 | + | |
| 25 | +import static org.eclipse.edc.spi.result.Result.success; | |
| 26 | + | |
| 27 | + | |
| 28 | +/** | |
| 29 | + * StatusList revocation service implementing the <a href="https://w3c.github.io/cg-reports/credentials/CG-FINAL-vc-status-list-2021-20230102/">StatusList2021</a> | |
| 30 | + * specification. | |
| 31 | + */ | |
| 32 | +public class StatusList2021RevocationService extends BaseRevocationListService<StatusList2021Credential, StatusList2021Status> { | |
| 33 | + | |
| 34 | + public StatusList2021RevocationService(ObjectMapper objectMapper, long cacheValidity) { | |
| 35 | + super(objectMapper, cacheValidity, StatusList2021Credential.class); | |
| 36 | + } | |
| 37 | + | |
| 38 | + @Override | |
| 39 | + protected StatusList2021Status getCredentialStatus(CredentialStatus credentialStatus) { | |
| 40 | + return StatusList2021Status.from(credentialStatus); | |
| 41 | + } | |
| 42 | + | |
| 43 | + @Override | |
| 44 | + protected Result<String> getStatusEntryValue(StatusList2021Status credentialStatus) { | |
| 45 | + var index = credentialStatus.getStatusListIndex(); | |
| 46 | + var slCredUrl = credentialStatus.getStatusListCredential(); | |
| 47 | + var credential = getCredential(slCredUrl); | |
| 48 | + | |
| 49 | + | |
| 50 | + var bitStringResult = BitString.Parser.newInstance().parse(credential.encodedList()); | |
| 51 | + | |
| 52 | + if (bitStringResult.failed()) { | |
| 53 | + return bitStringResult.mapEmpty(); | |
| 54 | + } | |
| 55 | + var bitString = bitStringResult.getContent(); | |
| 56 | + | |
| 57 | + // check that the value at index in the bitset is "1" | |
| 58 | + if (bitString.get(index)) { | |
| 59 | + return success(credentialStatus.getStatusListPurpose()); | |
| 60 | + } | |
| 61 | + return success(null); | |
| 62 | + } | |
| 63 | + | |
| 64 | + @Override | |
| 65 | + protected Result<Void> validateStatusPurpose(StatusList2021Status credentialStatus) { | |
| 66 | + var slCred = getCredential(credentialStatus.getStatusListCredential()); | |
| 67 | + | |
| 68 | + // check that the "statusPurpose" values match | |
| 69 | + var purpose = credentialStatus.getStatusListPurpose(); | |
| 70 | + var slCredPurpose = slCred.statusPurpose(); | |
| 71 | + if (!purpose.equalsIgnoreCase(slCredPurpose)) { | |
| 72 | + return Result.failure("Credential's statusPurpose value must match the status list's purpose: '%s' != '%s'".formatted(purpose, slCredPurpose)); | |
| 73 | + } | |
| 74 | + | |
| 75 | + return success(); | |
| 76 | + } | |
| 77 | + | |
| 78 | + @Override | |
| 79 | + protected int getStatusIndex(StatusList2021Status credentialStatus) { | |
| 80 | + return credentialStatus.getStatusListIndex(); | |
| 81 | + } | |
| 82 | +} |
modified extensions/common/iam/verifiable-credentials/src/main/java/org/eclipse/edc/iam/verifiablecredentials/rules/IsNotRevoked.java +5 −11
| @@ -14,13 +14,11 @@ | ||
| 14 | 14 | |
| 15 | 15 | package org.eclipse.edc.iam.verifiablecredentials.rules; |
| 16 | 16 | |
| 17 | -import org.eclipse.edc.iam.verifiablecredentials.spi.RevocationListService; | |
| 17 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.RevocationServiceRegistry; | |
| 18 | 18 | import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiableCredential; |
| 19 | 19 | import org.eclipse.edc.iam.verifiablecredentials.spi.validation.CredentialValidationRule; |
| 20 | 20 | import org.eclipse.edc.spi.result.Result; |
| 21 | 21 | |
| 22 | -import static org.eclipse.edc.spi.result.Result.success; | |
| 23 | - | |
| 24 | 22 | /** |
| 25 | 23 | * This class represents a rule that checks if a given VerifiableCredential is revoked based on a BitStringStatusList/StatusList2021 credential. |
| 26 | 24 | * A credential is regarded as "not revoked" if: |
| @@ -34,18 +32,14 @@ | ||
| 34 | 32 | */ |
| 35 | 33 | public class IsNotRevoked implements CredentialValidationRule { |
| 36 | 34 | |
| 37 | - private final RevocationListService revocationListService; | |
| 35 | + private final RevocationServiceRegistry revocationServiceRegistry; | |
| 38 | 36 | |
| 39 | - public IsNotRevoked(RevocationListService revocationListService) { | |
| 40 | - this.revocationListService = revocationListService; | |
| 37 | + public IsNotRevoked(RevocationServiceRegistry revocationServiceRegistry) { | |
| 38 | + this.revocationServiceRegistry = revocationServiceRegistry; | |
| 41 | 39 | } |
| 42 | 40 | |
| 43 | 41 | @Override |
| 44 | 42 | public Result<Void> apply(VerifiableCredential credential) { |
| 45 | - if (credential.getCredentialStatus().isEmpty()) { | |
| 46 | - return success(); | |
| 47 | - } | |
| 48 | - | |
| 49 | - return revocationListService.checkValidity(credential); | |
| 43 | + return revocationServiceRegistry.checkValidity(credential); | |
| 50 | 44 | } |
| 51 | 45 | } |
added extensions/common/iam/verifiable-credentials/src/main/resources/META-INF/services/org.eclipse.edc.spi.system.ServiceExtension +15 −0
| @@ -0,0 +1,15 @@ | ||
| 1 | +# | |
| 2 | +# Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | +# | |
| 4 | +# This program and the accompanying materials are made available under the | |
| 5 | +# terms of the Apache License, Version 2.0 which is available at | |
| 6 | +# https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | +# | |
| 8 | +# SPDX-License-Identifier: Apache-2.0 | |
| 9 | +# | |
| 10 | +# Contributors: | |
| 11 | +# Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | +# | |
| 13 | +# | |
| 14 | + | |
| 15 | +org.eclipse.edc.iam.verifiablecredentials.RevocationServiceRegistryExtension | |
| No newline at the end of the file |
added extensions/common/iam/verifiable-credentials/src/test/java/org/eclipse/edc/iam/verifiablecredentials/RevocationServiceRegistryImplTest.java +95 −0
| @@ -0,0 +1,95 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.iam.verifiablecredentials.revocation.RevocationServiceRegistryImpl; | |
| 18 | +import org.eclipse.edc.iam.verifiablecredentials.spi.RevocationListService; | |
| 19 | +import org.eclipse.edc.iam.verifiablecredentials.spi.TestFunctions; | |
| 20 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 21 | +import org.eclipse.edc.spi.result.Result; | |
| 22 | +import org.junit.jupiter.api.Test; | |
| 23 | + | |
| 24 | +import java.util.Map; | |
| 25 | + | |
| 26 | +import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; | |
| 27 | +import static org.mockito.ArgumentMatchers.any; | |
| 28 | +import static org.mockito.Mockito.mock; | |
| 29 | +import static org.mockito.Mockito.when; | |
| 30 | + | |
| 31 | +class RevocationServiceRegistryImplTest { | |
| 32 | + | |
| 33 | + private final RevocationServiceRegistryImpl registry = new RevocationServiceRegistryImpl(mock()); | |
| 34 | + | |
| 35 | + @Test | |
| 36 | + void checkValidity() { | |
| 37 | + var mockService = mock(RevocationListService.class); | |
| 38 | + registry.addService("test-type", mockService); | |
| 39 | + when(mockService.checkValidity(any(CredentialStatus.class))).thenReturn(Result.success()); | |
| 40 | + | |
| 41 | + var cred = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "test-type", Map.of())).build(); | |
| 42 | + assertThat(registry.checkValidity(cred)).isSucceeded(); | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Test | |
| 46 | + void checkValidity_whenNoCredentialStatus() { | |
| 47 | + var mockService = mock(RevocationListService.class); | |
| 48 | + registry.addService("test-type", mockService); | |
| 49 | + when(mockService.checkValidity(any(CredentialStatus.class))).thenReturn(Result.success()); | |
| 50 | + | |
| 51 | + var cred = TestFunctions.createCredentialBuilder().build(); | |
| 52 | + assertThat(registry.checkValidity(cred)).isSucceeded(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Test | |
| 56 | + void checkValidity_noServiceFound_shouldReturnSuccess() { | |
| 57 | + var mockService = mock(RevocationListService.class); | |
| 58 | + registry.addService("test-type", mockService); | |
| 59 | + when(mockService.checkValidity(any(CredentialStatus.class))).thenReturn(Result.success()); | |
| 60 | + | |
| 61 | + var cred = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "not-exist", Map.of())).build(); | |
| 62 | + assertThat(registry.checkValidity(cred)).isSucceeded(); | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Test | |
| 66 | + void checkValidity_oneInvalid_shouldReturnFailure() { | |
| 67 | + var mockService = mock(RevocationListService.class); | |
| 68 | + registry.addService("test-type", mockService); | |
| 69 | + when(mockService.checkValidity(any(CredentialStatus.class))) | |
| 70 | + .thenReturn(Result.success()) | |
| 71 | + .thenReturn(Result.failure("test failure")); | |
| 72 | + | |
| 73 | + var cred = TestFunctions.createCredentialBuilder() | |
| 74 | + .credentialStatus(new CredentialStatus("test-id", "test-type", Map.of())) | |
| 75 | + .credentialStatus(new CredentialStatus("test-id", "test-type", Map.of())) | |
| 76 | + .build(); | |
| 77 | + assertThat(registry.checkValidity(cred)).isFailed() | |
| 78 | + .detail().isEqualTo("test failure"); | |
| 79 | + } | |
| 80 | + | |
| 81 | + @Test | |
| 82 | + void checkValidity_allInvalid_shouldReturnFailure() { | |
| 83 | + var mockService = mock(RevocationListService.class); | |
| 84 | + registry.addService("test-type", mockService); | |
| 85 | + when(mockService.checkValidity(any(CredentialStatus.class))) | |
| 86 | + .thenReturn(Result.failure("test failure")); | |
| 87 | + | |
| 88 | + var cred = TestFunctions.createCredentialBuilder() | |
| 89 | + .credentialStatus(new CredentialStatus("test-id", "test-type", Map.of())) | |
| 90 | + .credentialStatus(new CredentialStatus("test-id", "test-type", Map.of())) | |
| 91 | + .build(); | |
| 92 | + assertThat(registry.checkValidity(cred)).isFailed() | |
| 93 | + .detail().contains("test failure"); | |
| 94 | + } | |
| 95 | +} | |
| No newline at the end of the file |
deleted extensions/common/iam/verifiable-credentials/src/test/java/org/eclipse/edc/iam/verifiablecredentials/StatusList2021RevocationServiceTest.java +0 −202
| @@ -1,202 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | - * | |
| 4 | - * This program and the accompanying materials are made available under the | |
| 5 | - * terms of the Apache License, Version 2.0 which is available at | |
| 6 | - * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | - * | |
| 8 | - * SPDX-License-Identifier: Apache-2.0 | |
| 9 | - * | |
| 10 | - * Contributors: | |
| 11 | - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | - * | |
| 13 | - */ | |
| 14 | - | |
| 15 | -package org.eclipse.edc.iam.verifiablecredentials; | |
| 16 | - | |
| 17 | -import com.fasterxml.jackson.databind.ObjectMapper; | |
| 18 | -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
| 19 | -import org.eclipse.edc.iam.verifiablecredentials.spi.TestFunctions; | |
| 20 | -import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 21 | -import org.junit.jupiter.api.AfterEach; | |
| 22 | -import org.junit.jupiter.api.BeforeEach; | |
| 23 | -import org.junit.jupiter.api.Named; | |
| 24 | -import org.junit.jupiter.api.Test; | |
| 25 | -import org.junit.jupiter.api.extension.ExtensionContext; | |
| 26 | -import org.junit.jupiter.params.ParameterizedTest; | |
| 27 | -import org.junit.jupiter.params.provider.Arguments; | |
| 28 | -import org.junit.jupiter.params.provider.ArgumentsProvider; | |
| 29 | -import org.junit.jupiter.params.provider.ArgumentsSource; | |
| 30 | -import org.mockserver.integration.ClientAndServer; | |
| 31 | -import org.mockserver.model.HttpResponse; | |
| 32 | -import org.mockserver.verify.VerificationTimes; | |
| 33 | - | |
| 34 | -import java.util.Map; | |
| 35 | -import java.util.stream.Stream; | |
| 36 | - | |
| 37 | -import static org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist.StatusList2021Credential.STATUS_LIST_CREDENTIAL; | |
| 38 | -import static org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist.StatusList2021Credential.STATUS_LIST_INDEX; | |
| 39 | -import static org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist.StatusList2021Credential.STATUS_LIST_PURPOSE; | |
| 40 | -import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; | |
| 41 | -import static org.eclipse.edc.util.io.Ports.getFreePort; | |
| 42 | -import static org.mockserver.model.HttpRequest.request; | |
| 43 | - | |
| 44 | -class StatusList2021RevocationServiceTest { | |
| 45 | - private static final int NOT_REVOKED_INDEX = 1; | |
| 46 | - private static final int REVOKED_INDEX = 2; | |
| 47 | - private final StatusList2021RevocationService revocationService = new StatusList2021RevocationService(new ObjectMapper().registerModule(new JavaTimeModule()), | |
| 48 | - 5 * 60 * 1000); | |
| 49 | - private ClientAndServer clientAndServer; | |
| 50 | - | |
| 51 | - @BeforeEach | |
| 52 | - void setup() { | |
| 53 | - clientAndServer = ClientAndServer.startClientAndServer("localhost", getFreePort()); | |
| 54 | - clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 55 | - .respond(HttpResponse.response().withStatusCode(200).withBody(TestData.STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_INTERMEDIATE)); | |
| 56 | - } | |
| 57 | - | |
| 58 | - @AfterEach | |
| 59 | - void tearDown() { | |
| 60 | - clientAndServer.stop(); | |
| 61 | - } | |
| 62 | - | |
| 63 | - @ParameterizedTest | |
| 64 | - @ArgumentsSource(ArraySubjectProvider.class) | |
| 65 | - void checkRevocation_whenSubjectIsArray(String testData) { | |
| 66 | - clientAndServer.reset(); | |
| 67 | - clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 68 | - .respond(HttpResponse.response().withStatusCode(200).withBody(testData)); | |
| 69 | - var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021", | |
| 70 | - Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 71 | - STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 72 | - STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 73 | - .build(); | |
| 74 | - assertThat(revocationService.checkValidity(credential)).isSucceeded(); | |
| 75 | - } | |
| 76 | - | |
| 77 | - @Test | |
| 78 | - void checkRevocation_whenNotCached_valid() { | |
| 79 | - var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021", | |
| 80 | - Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 81 | - STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 82 | - STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 83 | - .build(); | |
| 84 | - assertThat(revocationService.checkValidity(credential)).isSucceeded(); | |
| 85 | - } | |
| 86 | - | |
| 87 | - @Test | |
| 88 | - void checkRevocation_whenNotCached_credentialPurposeMismatch() { | |
| 89 | - var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021", | |
| 90 | - Map.of(STATUS_LIST_PURPOSE, "suspension", | |
| 91 | - STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 92 | - STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 93 | - .build(); | |
| 94 | - assertThat(revocationService.checkValidity(credential)).isFailed() | |
| 95 | - .detail().startsWith("Credential's statusPurpose value must match the status list's purpose:"); | |
| 96 | - } | |
| 97 | - | |
| 98 | - @Test | |
| 99 | - void checkRevocation_whenNotCached_invalid() { | |
| 100 | - var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021", | |
| 101 | - Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 102 | - STATUS_LIST_INDEX, REVOKED_INDEX, | |
| 103 | - STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 104 | - .build(); | |
| 105 | - assertThat(revocationService.checkValidity(credential)).isFailed() | |
| 106 | - .detail().isEqualTo("Credential status is 'revocation', status at index %d is '1'".formatted(REVOKED_INDEX)); | |
| 107 | - } | |
| 108 | - | |
| 109 | - @Test | |
| 110 | - void checkRevocation_whenCached_valid() { | |
| 111 | - var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021Entry", | |
| 112 | - Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 113 | - STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 114 | - STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 115 | - .build(); | |
| 116 | - assertThat(revocationService.checkValidity(credential)).isSucceeded(); | |
| 117 | - assertThat(revocationService.checkValidity(credential)).isSucceeded(); | |
| 118 | - clientAndServer.verify(request(), VerificationTimes.exactly(1)); | |
| 119 | - } | |
| 120 | - | |
| 121 | - @ParameterizedTest | |
| 122 | - @ArgumentsSource(SingleSubjectProvider.class) | |
| 123 | - void getStatusPurposes_whenSingleCredentialStatusRevoked(String testData) { | |
| 124 | - clientAndServer.reset(); | |
| 125 | - clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 126 | - .respond(HttpResponse.response().withStatusCode(200).withBody(testData)); | |
| 127 | - var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021", | |
| 128 | - Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 129 | - STATUS_LIST_INDEX, REVOKED_INDEX, | |
| 130 | - STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 131 | - .build(); | |
| 132 | - assertThat(revocationService.getStatusPurpose(credential)).isSucceeded() | |
| 133 | - .isEqualTo("revocation"); | |
| 134 | - } | |
| 135 | - | |
| 136 | - @ParameterizedTest | |
| 137 | - @ArgumentsSource(ArraySubjectProvider.class) | |
| 138 | - void getStatusPurposes_whenMultipleCredentialStatusRevoked(String testData) { | |
| 139 | - clientAndServer.reset(); | |
| 140 | - clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 141 | - .respond(HttpResponse.response().withStatusCode(200).withBody(testData)); | |
| 142 | - var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021", | |
| 143 | - Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 144 | - STATUS_LIST_INDEX, REVOKED_INDEX, | |
| 145 | - STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 146 | - .build(); | |
| 147 | - assertThat(revocationService.getStatusPurpose(credential)).isSucceeded() | |
| 148 | - .isEqualTo("revocation"); | |
| 149 | - } | |
| 150 | - | |
| 151 | - @ParameterizedTest | |
| 152 | - @ArgumentsSource(SingleSubjectProvider.class) | |
| 153 | - void getStatusPurpose_whenCredentialStatusNotActive(String testData) { | |
| 154 | - clientAndServer.reset(); | |
| 155 | - clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 156 | - .respond(HttpResponse.response().withStatusCode(200).withBody(testData)); | |
| 157 | - var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021", | |
| 158 | - Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 159 | - STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 160 | - STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 161 | - .build(); | |
| 162 | - assertThat(revocationService.getStatusPurpose(credential)).isSucceeded() | |
| 163 | - .isNull(); | |
| 164 | - } | |
| 165 | - | |
| 166 | - @ParameterizedTest | |
| 167 | - @ArgumentsSource(SingleSubjectProvider.class) | |
| 168 | - void getStatusPurpose_whenNoCredentialStatus(String testData) { | |
| 169 | - clientAndServer.reset(); | |
| 170 | - clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 171 | - .respond(HttpResponse.response().withStatusCode(200).withBody(testData)); | |
| 172 | - var credential = TestFunctions.createCredentialBuilder().build(); | |
| 173 | - assertThat(revocationService.getStatusPurpose(credential)) | |
| 174 | - .isNotNull() | |
| 175 | - .isSucceeded(); | |
| 176 | - } | |
| 177 | - | |
| 178 | - | |
| 179 | - private static class SingleSubjectProvider implements ArgumentsProvider { | |
| 180 | - @Override | |
| 181 | - public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) { | |
| 182 | - return Stream.of( | |
| 183 | - Arguments.of(Named.of("VC (intermediate)", TestData.STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_INTERMEDIATE)), | |
| 184 | - Arguments.of(Named.of("VC 1.1", TestData.STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_1_0)), | |
| 185 | - Arguments.of(Named.of("VC 2.0", TestData.STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_2_0)) | |
| 186 | - | |
| 187 | - ); | |
| 188 | - } | |
| 189 | - } | |
| 190 | - | |
| 191 | - private static class ArraySubjectProvider implements ArgumentsProvider { | |
| 192 | - @Override | |
| 193 | - public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) { | |
| 194 | - return Stream.of( | |
| 195 | - Arguments.of(Named.of("VC 1.1", TestData.STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY_1_0)), | |
| 196 | - Arguments.of(Named.of("VC (intermediate)", TestData.STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY_INTERMEDIATE)), | |
| 197 | - Arguments.of(Named.of("VC 2.0", TestData.STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY_2_0)) | |
| 198 | - | |
| 199 | - ); | |
| 200 | - } | |
| 201 | - } | |
| 202 | -} | |
| No newline at the end of the file |
modified extensions/common/iam/verifiable-credentials/src/test/java/org/eclipse/edc/iam/verifiablecredentials/TestData.java +170 −116
| @@ -15,125 +15,179 @@ | ||
| 15 | 15 | package org.eclipse.edc.iam.verifiablecredentials; |
| 16 | 16 | |
| 17 | 17 | public class TestData { |
| 18 | - // test data taken from https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#example-example-statuslist2021credential-0 | |
| 19 | - public static final String STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY_INTERMEDIATE = """ | |
| 20 | - { | |
| 21 | - "@context": [ | |
| 22 | - "https://www.w3.org/2018/credentials/v1", | |
| 23 | - "https://w3id.org/vc/status-list/2021/v1" | |
| 24 | - ], | |
| 25 | - "id": "https://example.com/credentials/status/3", | |
| 26 | - "type": ["VerifiableCredential", "StatusList2021Credential"], | |
| 27 | - "issuer": "did:example:12345", | |
| 28 | - "issued": "2021-04-05T14:27:40Z", | |
| 29 | - "credentialSubject": [ | |
| 30 | - { | |
| 31 | - "id": "https://example.com/status/3#list", | |
| 32 | - "type": "StatusList2021", | |
| 33 | - "https://w3id.org/vc/status-list#statusPurpose": "revocation", | |
| 34 | - "https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA" | |
| 35 | - } | |
| 36 | - ] | |
| 37 | - } | |
| 38 | - """; | |
| 39 | 18 | |
| 40 | - public static final String STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_INTERMEDIATE = """ | |
| 41 | - { | |
| 42 | - "@context": [ | |
| 43 | - "https://www.w3.org/2018/credentials/v1", | |
| 44 | - "https://w3id.org/vc/status-list/2021/v1" | |
| 45 | - ], | |
| 46 | - "id": "https://example.com/credentials/status/3", | |
| 47 | - "type": ["VerifiableCredential", "StatusList2021Credential"], | |
| 48 | - "issuer": "did:example:12345", | |
| 49 | - "issued": "2021-04-05T14:27:40Z", | |
| 50 | - "credentialSubject": { | |
| 51 | - "id": "https://example.com/status/3#list", | |
| 52 | - "type": "StatusList2021", | |
| 53 | - "https://w3id.org/vc/status-list#statusPurpose": "revocation", | |
| 54 | - "https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA" | |
| 55 | - } | |
| 56 | - } | |
| 57 | - """; | |
| 19 | + public static class BitstringStatusList { | |
| 58 | 20 | |
| 59 | - public static final String STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY_2_0 = """ | |
| 60 | - { | |
| 61 | - "@context": [ | |
| 62 | - "https://www.w3.org/2018/credentials/v1", | |
| 63 | - "https://w3id.org/vc/status-list/2021/v1" | |
| 64 | - ], | |
| 65 | - "id": "https://example.com/credentials/status/3", | |
| 66 | - "type": ["VerifiableCredential", "StatusList2021Credential"], | |
| 67 | - "issuer": "did:example:12345", | |
| 68 | - "validFrom": "2021-04-05T14:27:40Z", | |
| 69 | - "credentialSubject": [ | |
| 70 | - { | |
| 71 | - "id": "https://example.com/status/3#list", | |
| 72 | - "type": "StatusList2021", | |
| 73 | - "https://w3id.org/vc/status-list#statusPurpose": "revocation", | |
| 74 | - "https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA" | |
| 75 | - } | |
| 76 | - ] | |
| 77 | - } | |
| 78 | - """; | |
| 21 | + public static final String BITSTRING_STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_TEMPLATE = """ | |
| 22 | + { | |
| 23 | + "@context": [ | |
| 24 | + "https://www.w3.org/ns/credentials/v2" | |
| 25 | + ], | |
| 26 | + "id": "https://example.com/credentials/status/3", | |
| 27 | + "type": ["VerifiableCredential", "BitstringStatusListCredential"], | |
| 28 | + "issuer": "did:example:12345", | |
| 29 | + "validFrom": "2021-04-05T14:27:40Z", | |
| 30 | + "credentialSubject": { | |
| 31 | + "id": "https://example.com/status/3#list", | |
| 32 | + "type": "BitstringStatusList", | |
| 33 | + "statusPurpose": "revocation", | |
| 34 | + "encodedList": "%s" | |
| 35 | + } | |
| 36 | + } | |
| 37 | + """; | |
| 38 | + public static final String BITSTRING_STATUS_LIST_CREDENTIAL_ARRAY_SUBJECT_TEMPLATE = """ | |
| 39 | + { | |
| 40 | + "@context": [ | |
| 41 | + "https://www.w3.org/ns/credentials/v2" | |
| 42 | + ], | |
| 43 | + "id": "https://example.com/credentials/status/3", | |
| 44 | + "type": ["VerifiableCredential", "BitstringStatusListCredential"], | |
| 45 | + "issuer": "did:example:12345", | |
| 46 | + "validFrom": "2021-04-05T14:27:40Z", | |
| 47 | + "credentialSubject": [{ | |
| 48 | + "id": "https://example.com/status/3#list", | |
| 49 | + "type": "BitstringStatusList", | |
| 50 | + "statusPurpose": "revocation", | |
| 51 | + "encodedList": "%s" | |
| 52 | + }] | |
| 53 | + } | |
| 54 | + """; | |
| 79 | 55 | |
| 80 | - public static final String STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_2_0 = """ | |
| 81 | - { | |
| 82 | - "@context": [ | |
| 83 | - "https://www.w3.org/2018/credentials/v1", | |
| 84 | - "https://w3id.org/vc/status-list/2021/v1" | |
| 85 | - ], | |
| 86 | - "id": "https://example.com/credentials/status/3", | |
| 87 | - "type": ["VerifiableCredential", "StatusList2021Credential"], | |
| 88 | - "issuer": "did:example:12345", | |
| 89 | - "validFrom": "2021-04-05T14:27:40Z", | |
| 90 | - "credentialSubject": { | |
| 91 | - "id": "https://example.com/status/3#list", | |
| 92 | - "type": "StatusList2021", | |
| 93 | - "https://w3id.org/vc/status-list#statusPurpose": "revocation", | |
| 94 | - "https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA" | |
| 95 | - } | |
| 96 | - } | |
| 97 | - """; | |
| 56 | + public static final String BITSTRING_STATUS_LIST_CREDENTIAL_PURPOSE_TEMPLATE = """ | |
| 57 | + { | |
| 58 | + "@context": [ | |
| 59 | + "https://www.w3.org/ns/credentials/v2" | |
| 60 | + ], | |
| 61 | + "id": "https://example.com/credentials/status/3", | |
| 62 | + "type": ["VerifiableCredential", "BitstringStatusListCredential"], | |
| 63 | + "issuer": "did:example:12345", | |
| 64 | + "validFrom": "2021-04-05T14:27:40Z", | |
| 65 | + "credentialSubject": [{ | |
| 66 | + "id": "https://example.com/status/3#list", | |
| 67 | + "type": "BitstringStatusList", | |
| 68 | + "statusPurpose": "%s", | |
| 69 | + "encodedList": "%s" | |
| 70 | + }] | |
| 71 | + } | |
| 72 | + """; | |
| 73 | + } | |
| 98 | 74 | |
| 75 | + public static class StatusList2021 { | |
| 99 | 76 | |
| 100 | - public static final String STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY_1_0 = """ | |
| 101 | - { | |
| 102 | - "@context": [ | |
| 103 | - "https://www.w3.org/2018/credentials/v1", | |
| 104 | - "https://w3id.org/vc/status-list/2021/v1" | |
| 105 | - ], | |
| 106 | - "id": "https://example.com/credentials/status/3", | |
| 107 | - "type": ["VerifiableCredential", "StatusList2021Credential"], | |
| 108 | - "issuer": "did:example:12345", | |
| 109 | - "issuanceDate": "2021-04-05T14:27:40Z", | |
| 110 | - "credentialSubject": [ | |
| 111 | - { | |
| 112 | - "id": "https://example.com/status/3#list", | |
| 113 | - "type": "StatusList2021", | |
| 114 | - "https://w3id.org/vc/status-list#statusPurpose": "revocation", | |
| 115 | - "https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA" | |
| 116 | - } | |
| 117 | - ] | |
| 118 | - } | |
| 119 | - """; | |
| 120 | - | |
| 121 | - public static final String STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_1_0 = """ | |
| 122 | - { | |
| 123 | - "@context": [ | |
| 124 | - "https://www.w3.org/2018/credentials/v1", | |
| 125 | - "https://w3id.org/vc/status-list/2021/v1" | |
| 126 | - ], | |
| 127 | - "id": "https://example.com/credentials/status/3", | |
| 128 | - "type": ["VerifiableCredential", "StatusList2021Credential"], | |
| 129 | - "issuer": "did:example:12345", | |
| 130 | - "issuanceDate": "2021-04-05T14:27:40Z", | |
| 131 | - "credentialSubject": { | |
| 132 | - "id": "https://example.com/status/3#list", | |
| 133 | - "type": "StatusList2021", | |
| 134 | - "https://w3id.org/vc/status-list#statusPurpose": "revocation", | |
| 135 | - "https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA" | |
| 136 | - } | |
| 137 | - } | |
| 138 | - """; | |
| 77 | + // test data taken from https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#example-example-statuslist2021credential-0 | |
| 78 | + public static final String STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY_INTERMEDIATE = """ | |
| 79 | + { | |
| 80 | + "@context": [ | |
| 81 | + "https://www.w3.org/2018/credentials/v1", | |
| 82 | + "https://w3id.org/vc/status-list/2021/v1" | |
| 83 | + ], | |
| 84 | + "id": "https://example.com/credentials/status/3", | |
| 85 | + "type": ["VerifiableCredential", "StatusList2021Credential"], | |
| 86 | + "issuer": "did:example:12345", | |
| 87 | + "issued": "2021-04-05T14:27:40Z", | |
| 88 | + "credentialSubject": [ | |
| 89 | + { | |
| 90 | + "id": "https://example.com/status/3#list", | |
| 91 | + "type": "StatusList2021", | |
| 92 | + "https://w3id.org/vc/status-list#statusPurpose": "revocation", | |
| 93 | + "https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA" | |
| 94 | + } | |
| 95 | + ] | |
| 96 | + } | |
| 97 | + """; | |
| 98 | + public static final String STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_INTERMEDIATE = """ | |
| 99 | + { | |
| 100 | + "@context": [ | |
| 101 | + "https://www.w3.org/2018/credentials/v1", | |
| 102 | + "https://w3id.org/vc/status-list/2021/v1" | |
| 103 | + ], | |
| 104 | + "id": "https://example.com/credentials/status/3", | |
| 105 | + "type": ["VerifiableCredential", "StatusList2021Credential"], | |
| 106 | + "issuer": "did:example:12345", | |
| 107 | + "issued": "2021-04-05T14:27:40Z", | |
| 108 | + "credentialSubject": { | |
| 109 | + "id": "https://example.com/status/3#list", | |
| 110 | + "type": "StatusList2021", | |
| 111 | + "https://w3id.org/vc/status-list#statusPurpose": "revocation", | |
| 112 | + "https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA" | |
| 113 | + } | |
| 114 | + } | |
| 115 | + """; | |
| 116 | + public static final String STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY_2_0 = """ | |
| 117 | + { | |
| 118 | + "@context": [ | |
| 119 | + "https://www.w3.org/2018/credentials/v1", | |
| 120 | + "https://w3id.org/vc/status-list/2021/v1" | |
| 121 | + ], | |
| 122 | + "id": "https://example.com/credentials/status/3", | |
| 123 | + "type": ["VerifiableCredential", "StatusList2021Credential"], | |
| 124 | + "issuer": "did:example:12345", | |
| 125 | + "validFrom": "2021-04-05T14:27:40Z", | |
| 126 | + "credentialSubject": [ | |
| 127 | + { | |
| 128 | + "id": "https://example.com/status/3#list", | |
| 129 | + "type": "StatusList2021", | |
| 130 | + "https://w3id.org/vc/status-list#statusPurpose": "revocation", | |
| 131 | + "https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA" | |
| 132 | + } | |
| 133 | + ] | |
| 134 | + } | |
| 135 | + """; | |
| 136 | + public static final String STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_2_0 = """ | |
| 137 | + { | |
| 138 | + "@context": [ | |
| 139 | + "https://www.w3.org/2018/credentials/v1", | |
| 140 | + "https://w3id.org/vc/status-list/2021/v1" | |
| 141 | + ], | |
| 142 | + "id": "https://example.com/credentials/status/3", | |
| 143 | + "type": ["VerifiableCredential", "StatusList2021Credential"], | |
| 144 | + "issuer": "did:example:12345", | |
| 145 | + "validFrom": "2021-04-05T14:27:40Z", | |
| 146 | + "credentialSubject": { | |
| 147 | + "id": "https://example.com/status/3#list", | |
| 148 | + "type": "StatusList2021", | |
| 149 | + "https://w3id.org/vc/status-list#statusPurpose": "revocation", | |
| 150 | + "https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA" | |
| 151 | + } | |
| 152 | + } | |
| 153 | + """; | |
| 154 | + public static final String STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY_1_0 = """ | |
| 155 | + { | |
| 156 | + "@context": [ | |
| 157 | + "https://www.w3.org/2018/credentials/v1", | |
| 158 | + "https://w3id.org/vc/status-list/2021/v1" | |
| 159 | + ], | |
| 160 | + "id": "https://example.com/credentials/status/3", | |
| 161 | + "type": ["VerifiableCredential", "StatusList2021Credential"], | |
| 162 | + "issuer": "did:example:12345", | |
| 163 | + "issuanceDate": "2021-04-05T14:27:40Z", | |
| 164 | + "credentialSubject": [ | |
| 165 | + { | |
| 166 | + "id": "https://example.com/status/3#list", | |
| 167 | + "type": "StatusList2021", | |
| 168 | + "https://w3id.org/vc/status-list#statusPurpose": "revocation", | |
| 169 | + "https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA" | |
| 170 | + } | |
| 171 | + ] | |
| 172 | + } | |
| 173 | + """; | |
| 174 | + public static final String STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_1_0 = """ | |
| 175 | + { | |
| 176 | + "@context": [ | |
| 177 | + "https://www.w3.org/2018/credentials/v1", | |
| 178 | + "https://w3id.org/vc/status-list/2021/v1" | |
| 179 | + ], | |
| 180 | + "id": "https://example.com/credentials/status/3", | |
| 181 | + "type": ["VerifiableCredential", "StatusList2021Credential"], | |
| 182 | + "issuer": "did:example:12345", | |
| 183 | + "issuanceDate": "2021-04-05T14:27:40Z", | |
| 184 | + "credentialSubject": { | |
| 185 | + "id": "https://example.com/status/3#list", | |
| 186 | + "type": "StatusList2021", | |
| 187 | + "https://w3id.org/vc/status-list#statusPurpose": "revocation", | |
| 188 | + "https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA" | |
| 189 | + } | |
| 190 | + } | |
| 191 | + """; | |
| 192 | + } | |
| 139 | 193 | } |
modified extensions/common/iam/verifiable-credentials/src/test/java/org/eclipse/edc/iam/verifiablecredentials/VerifiableCredentialValidationServiceImplTest.java +10 −5
| @@ -14,15 +14,16 @@ | ||
| 14 | 14 | |
| 15 | 15 | package org.eclipse.edc.iam.verifiablecredentials; |
| 16 | 16 | |
| 17 | -import org.eclipse.edc.iam.verifiablecredentials.spi.RevocationListService; | |
| 18 | 17 | import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialFormat; |
| 19 | 18 | import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; |
| 20 | 19 | import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialSubject; |
| 21 | 20 | import org.eclipse.edc.iam.verifiablecredentials.spi.model.Issuer; |
| 21 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.RevocationServiceRegistry; | |
| 22 | 22 | import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiablePresentationContainer; |
| 23 | 23 | import org.eclipse.edc.iam.verifiablecredentials.spi.validation.PresentationVerifier; |
| 24 | 24 | import org.eclipse.edc.iam.verifiablecredentials.spi.validation.TrustedIssuerRegistry; |
| 25 | 25 | import org.eclipse.edc.spi.result.Result; |
| 26 | +import org.junit.jupiter.api.BeforeEach; | |
| 26 | 27 | import org.junit.jupiter.api.Test; |
| 27 | 28 | |
| 28 | 29 | import java.time.Clock; |
| @@ -47,9 +48,13 @@ | ||
| 47 | 48 | public static final String CONSUMER_DID = "did:web:consumer"; |
| 48 | 49 | private final PresentationVerifier verifierMock = mock(); |
| 49 | 50 | private final TrustedIssuerRegistry trustedIssuerRegistryMock = mock(); |
| 50 | - private final RevocationListService revocationListServiceMock = mock(); | |
| 51 | - | |
| 52 | - private final VerifiableCredentialValidationServiceImpl service = new VerifiableCredentialValidationServiceImpl(verifierMock, trustedIssuerRegistryMock, revocationListServiceMock, Clock.systemUTC()); | |
| 51 | + private final RevocationServiceRegistry revocationServiceRegistry = mock(); | |
| 52 | + private final VerifiableCredentialValidationServiceImpl service = new VerifiableCredentialValidationServiceImpl(verifierMock, trustedIssuerRegistryMock, revocationServiceRegistry, Clock.systemUTC()); | |
| 53 | + | |
| 54 | + @BeforeEach | |
| 55 | + void setUp() { | |
| 56 | + when(revocationServiceRegistry.checkValidity(any())).thenReturn(Result.success()); | |
| 57 | + } | |
| 53 | 58 | |
| 54 | 59 | @Test |
| 55 | 60 | void cryptographicError() { |
| @@ -220,7 +225,7 @@ | ||
| 220 | 225 | var vpContainer = new VerifiablePresentationContainer("test-vp", CredentialFormat.JSON_LD, presentation); |
| 221 | 226 | when(verifierMock.verifyPresentation(any())).thenReturn(success()); |
| 222 | 227 | when(trustedIssuerRegistryMock.getTrustedIssuers()).thenReturn(Set.of(TRUSTED_ISSUER)); |
| 223 | - when(revocationListServiceMock.checkValidity(any())).thenReturn(Result.failure("invalid")); | |
| 228 | + when(revocationServiceRegistry.checkValidity(any())).thenReturn(Result.failure("invalid")); | |
| 224 | 229 | |
| 225 | 230 | var result = service.validate(List.of(vpContainer)); |
| 226 | 231 | assertThat(result).isFailed() |
added extensions/common/iam/verifiable-credentials/src/test/java/org/eclipse/edc/iam/verifiablecredentials/revocation/bitstring/BitstringStatusListRevocationServiceTest.java +305 −0
| @@ -0,0 +1,305 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.revocation.bitstring; | |
| 16 | + | |
| 17 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
| 18 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
| 19 | +import org.eclipse.edc.iam.verifiablecredentials.TestData; | |
| 20 | +import org.eclipse.edc.iam.verifiablecredentials.spi.TestFunctions; | |
| 21 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 22 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.BitString; | |
| 23 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus; | |
| 24 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.StatusMessage; | |
| 25 | +import org.junit.jupiter.api.AfterEach; | |
| 26 | +import org.junit.jupiter.api.BeforeEach; | |
| 27 | +import org.junit.jupiter.api.Nested; | |
| 28 | +import org.junit.jupiter.api.Test; | |
| 29 | +import org.mockserver.integration.ClientAndServer; | |
| 30 | +import org.mockserver.model.HttpResponse; | |
| 31 | +import org.mockserver.verify.VerificationTimes; | |
| 32 | + | |
| 33 | +import java.util.Base64; | |
| 34 | +import java.util.List; | |
| 35 | +import java.util.Map; | |
| 36 | + | |
| 37 | +import static org.eclipse.edc.iam.verifiablecredentials.TestData.BitstringStatusList.BITSTRING_STATUS_LIST_CREDENTIAL_ARRAY_SUBJECT_TEMPLATE; | |
| 38 | +import static org.eclipse.edc.iam.verifiablecredentials.TestData.BitstringStatusList.BITSTRING_STATUS_LIST_CREDENTIAL_PURPOSE_TEMPLATE; | |
| 39 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListCredential.BITSTRING_STATUSLIST_CREDENTIAL; | |
| 40 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus.STATUS_LIST_CREDENTIAL; | |
| 41 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus.STATUS_LIST_INDEX; | |
| 42 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus.STATUS_LIST_MESSAGES; | |
| 43 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus.STATUS_LIST_PURPOSE; | |
| 44 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus.STATUS_LIST_SIZE; | |
| 45 | +import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; | |
| 46 | +import static org.eclipse.edc.util.io.Ports.getFreePort; | |
| 47 | +import static org.mockserver.model.HttpRequest.request; | |
| 48 | + | |
| 49 | +class BitstringStatusListRevocationServiceTest { | |
| 50 | + | |
| 51 | + private static final int REVOKED_INDEX = 10; | |
| 52 | + private static final int NOT_REVOKED_INDEX = 15; | |
| 53 | + | |
| 54 | + private final BitstringStatusListRevocationService revocationService = new BitstringStatusListRevocationService(new ObjectMapper().registerModule(new JavaTimeModule()), | |
| 55 | + 5 * 60 * 1000); | |
| 56 | + private ClientAndServer clientAndServer; | |
| 57 | + | |
| 58 | + @BeforeEach | |
| 59 | + void setUp() { | |
| 60 | + clientAndServer = ClientAndServer.startClientAndServer("localhost", getFreePort()); | |
| 61 | + var bitstring = generateBitstring(); | |
| 62 | + var bitstringCredential = TestData.BitstringStatusList.BITSTRING_STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_TEMPLATE.formatted(bitstring); | |
| 63 | + clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 64 | + .respond(HttpResponse.response().withStatusCode(200).withBody(bitstringCredential)); | |
| 65 | + } | |
| 66 | + | |
| 67 | + @AfterEach | |
| 68 | + void tearDown() { | |
| 69 | + clientAndServer.stop(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + private String generateBitstring() { | |
| 73 | + return generateBitstring(0, 0); | |
| 74 | + } | |
| 75 | + | |
| 76 | + private String generateBitstring(int index, long value) { | |
| 77 | + var bitstring = BitString.Builder.newInstance().size(1024 * 16).build(); | |
| 78 | + bitstring.set(index, value != 0); | |
| 79 | + | |
| 80 | + return "u" + BitString.Writer.newInstance().encoder(Base64.getUrlEncoder().withoutPadding()).write(bitstring).getContent(); | |
| 81 | + } | |
| 82 | + | |
| 83 | + @Nested | |
| 84 | + public class CheckValidity { | |
| 85 | + @Test | |
| 86 | + void checkValidity_revoked_notCached() { | |
| 87 | + var bitstring = generateBitstring(REVOKED_INDEX, (byte) 0x2); | |
| 88 | + var bitstringCredential = TestData.BitstringStatusList.BITSTRING_STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_TEMPLATE.formatted(bitstring); | |
| 89 | + clientAndServer.reset() | |
| 90 | + .when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 91 | + .respond(HttpResponse.response().withStatusCode(200).withBody(bitstringCredential)); | |
| 92 | + | |
| 93 | + var credential = new CredentialStatus("test-id", BITSTRING_STATUSLIST_CREDENTIAL, | |
| 94 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 95 | + STATUS_LIST_INDEX, REVOKED_INDEX, | |
| 96 | + STATUS_LIST_SIZE, 1, | |
| 97 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))); | |
| 98 | + assertThat(revocationService.checkValidity(credential)).isFailed() | |
| 99 | + .detail().isEqualTo("Credential status is 'revocation', status at index 10 is '1'"); | |
| 100 | + } | |
| 101 | + | |
| 102 | + @Test | |
| 103 | + void checkValidity_revoked_whenCached() { | |
| 104 | + var bitstring = generateBitstring(REVOKED_INDEX, 1); | |
| 105 | + var bitstringCredential = TestData.BitstringStatusList.BITSTRING_STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_TEMPLATE.formatted(bitstring); | |
| 106 | + clientAndServer.reset() | |
| 107 | + .when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 108 | + .respond(HttpResponse.response().withStatusCode(200).withBody(bitstringCredential)); | |
| 109 | + | |
| 110 | + var credential = new CredentialStatus("test-id", BITSTRING_STATUSLIST_CREDENTIAL, | |
| 111 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 112 | + STATUS_LIST_INDEX, REVOKED_INDEX, | |
| 113 | + STATUS_LIST_SIZE, 1, | |
| 114 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))); | |
| 115 | + assertThat(revocationService.checkValidity(credential)).isFailed() | |
| 116 | + .detail().isEqualTo("Credential status is 'revocation', status at index 10 is '1'"); | |
| 117 | + clientAndServer.verify(request(), VerificationTimes.exactly(1)); | |
| 118 | + } | |
| 119 | + | |
| 120 | + @Test | |
| 121 | + void checkValidity_notRevoked_notCached() { | |
| 122 | + var credential = new CredentialStatus("test-id", BITSTRING_STATUSLIST_CREDENTIAL, | |
| 123 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 124 | + STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 125 | + STATUS_LIST_SIZE, 1, | |
| 126 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))); | |
| 127 | + assertThat(revocationService.checkValidity(credential)).isSucceeded(); | |
| 128 | + } | |
| 129 | + | |
| 130 | + @Test | |
| 131 | + void checkValidity_notRevoked_whenCached() { | |
| 132 | + var credential = new CredentialStatus("test-id", BITSTRING_STATUSLIST_CREDENTIAL, | |
| 133 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 134 | + STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 135 | + STATUS_LIST_SIZE, 1, | |
| 136 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))); | |
| 137 | + assertThat(revocationService.checkValidity(credential)).isSucceeded(); | |
| 138 | + clientAndServer.verify(request(), VerificationTimes.exactly(1)); | |
| 139 | + } | |
| 140 | + | |
| 141 | + @Test | |
| 142 | + void checkValidity_credentialPurposeMismatch_notCached() { | |
| 143 | + var credential = new CredentialStatus("test-id", BITSTRING_STATUSLIST_CREDENTIAL, | |
| 144 | + Map.of(STATUS_LIST_PURPOSE, "suspension", | |
| 145 | + STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 146 | + STATUS_LIST_SIZE, 1, | |
| 147 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))); | |
| 148 | + assertThat(revocationService.checkValidity(credential)).isFailed() | |
| 149 | + .detail().startsWith("Credential's statusPurpose value must match the statusPurpose of the Bitstring Credential:"); | |
| 150 | + } | |
| 151 | + | |
| 152 | + @Test | |
| 153 | + void checkValidity_whenSubjectIsArray_notRevoked() { | |
| 154 | + var bitstring = generateBitstring(); | |
| 155 | + var testData = BITSTRING_STATUS_LIST_CREDENTIAL_ARRAY_SUBJECT_TEMPLATE.formatted(bitstring); | |
| 156 | + clientAndServer.reset(); | |
| 157 | + clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 158 | + .respond(HttpResponse.response().withStatusCode(200).withBody(testData)); | |
| 159 | + | |
| 160 | + var credential = new CredentialStatus("test-id", BITSTRING_STATUSLIST_CREDENTIAL, | |
| 161 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 162 | + STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 163 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))); | |
| 164 | + assertThat(revocationService.checkValidity(credential)).isSucceeded(); | |
| 165 | + } | |
| 166 | + | |
| 167 | + @Test | |
| 168 | + void checkValidity_whenSubjectIsArray_revoked() { | |
| 169 | + var bitstring = generateBitstring(REVOKED_INDEX, 1); | |
| 170 | + var testData = BITSTRING_STATUS_LIST_CREDENTIAL_ARRAY_SUBJECT_TEMPLATE.formatted(bitstring); | |
| 171 | + clientAndServer.reset(); | |
| 172 | + clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 173 | + .respond(HttpResponse.response().withStatusCode(200).withBody(testData)); | |
| 174 | + | |
| 175 | + var credential = new CredentialStatus("test-id", BITSTRING_STATUSLIST_CREDENTIAL, | |
| 176 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 177 | + STATUS_LIST_INDEX, REVOKED_INDEX, | |
| 178 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))); | |
| 179 | + assertThat(revocationService.checkValidity(credential)).isFailed() | |
| 180 | + .detail().isEqualTo("Credential status is 'revocation', status at index 10 is '1'"); | |
| 181 | + } | |
| 182 | + } | |
| 183 | + | |
| 184 | + @Nested | |
| 185 | + public class GetStatusPurpose { | |
| 186 | + | |
| 187 | + @Test | |
| 188 | + void getStatusPurpose_singleStatusSet() { | |
| 189 | + clientAndServer.reset(); | |
| 190 | + clientAndServer | |
| 191 | + .when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 192 | + .respond(HttpResponse.response().withStatusCode(200).withBody(BITSTRING_STATUS_LIST_CREDENTIAL_PURPOSE_TEMPLATE.formatted("revocation", generateBitstring(REVOKED_INDEX, 1)))); | |
| 193 | + var credential = TestFunctions.createCredentialBuilder() | |
| 194 | + .credentialStatus(new CredentialStatus("test-id", BitstringStatusListStatus.TYPE, | |
| 195 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 196 | + STATUS_LIST_INDEX, REVOKED_INDEX, | |
| 197 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 198 | + .build(); | |
| 199 | + assertThat(revocationService.getStatusPurpose(credential)).isSucceeded() | |
| 200 | + .isEqualTo("revocation"); | |
| 201 | + } | |
| 202 | + | |
| 203 | + @Test | |
| 204 | + void getStatusPurpose_singleStatusSet_message() { | |
| 205 | + clientAndServer.reset(); | |
| 206 | + clientAndServer | |
| 207 | + .when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 208 | + .respond(HttpResponse.response().withStatusCode(200).withBody(BITSTRING_STATUS_LIST_CREDENTIAL_PURPOSE_TEMPLATE.formatted("message", generateBitstring(REVOKED_INDEX, 1)))); | |
| 209 | + var credential = TestFunctions.createCredentialBuilder() | |
| 210 | + .credentialStatus(new CredentialStatus("test-id", BitstringStatusListStatus.TYPE, | |
| 211 | + Map.of(STATUS_LIST_PURPOSE, "message", | |
| 212 | + STATUS_LIST_INDEX, REVOKED_INDEX, | |
| 213 | + STATUS_LIST_SIZE, 1, | |
| 214 | + STATUS_LIST_MESSAGES, List.of(new StatusMessage("0x0", "accepted"), new StatusMessage("0x1", "rejected")), | |
| 215 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 216 | + .build(); | |
| 217 | + assertThat(revocationService.getStatusPurpose(credential)).isSucceeded() | |
| 218 | + .isEqualTo("rejected"); | |
| 219 | + } | |
| 220 | + | |
| 221 | + @Test | |
| 222 | + void getStatusPurpose_singleStatusNotSet_message() { | |
| 223 | + clientAndServer.reset(); | |
| 224 | + clientAndServer | |
| 225 | + .when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 226 | + .respond(HttpResponse.response().withStatusCode(200).withBody(BITSTRING_STATUS_LIST_CREDENTIAL_PURPOSE_TEMPLATE.formatted("message", generateBitstring()))); | |
| 227 | + var credential = TestFunctions.createCredentialBuilder() | |
| 228 | + .credentialStatus(new CredentialStatus("test-id", BitstringStatusListStatus.TYPE, | |
| 229 | + Map.of(STATUS_LIST_PURPOSE, "message", | |
| 230 | + STATUS_LIST_INDEX, 69, | |
| 231 | + STATUS_LIST_SIZE, 1, | |
| 232 | + STATUS_LIST_MESSAGES, List.of(new StatusMessage("0x0", "accepted"), new StatusMessage("0x1", "rejected")), | |
| 233 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 234 | + .build(); | |
| 235 | + assertThat(revocationService.getStatusPurpose(credential)).isSucceeded() | |
| 236 | + .isEqualTo("accepted"); | |
| 237 | + } | |
| 238 | + | |
| 239 | + | |
| 240 | + @Test | |
| 241 | + void getStatusPurpose_singleStatus_notSet() { | |
| 242 | + clientAndServer.reset(); | |
| 243 | + clientAndServer | |
| 244 | + .when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 245 | + .respond(HttpResponse.response().withStatusCode(200).withBody(BITSTRING_STATUS_LIST_CREDENTIAL_PURPOSE_TEMPLATE.formatted("revocation", generateBitstring(REVOKED_INDEX, 1)))); | |
| 246 | + var credential = TestFunctions.createCredentialBuilder() | |
| 247 | + .credentialStatus(new CredentialStatus("test-id", BitstringStatusListStatus.TYPE, | |
| 248 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 249 | + STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 250 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 251 | + .build(); | |
| 252 | + assertThat(revocationService.getStatusPurpose(credential)).isSucceeded() | |
| 253 | + .isNull(); | |
| 254 | + } | |
| 255 | + | |
| 256 | + @Test | |
| 257 | + void getStatusPurpose_multipleStatus_onlyOneSet() { | |
| 258 | + clientAndServer.reset(); | |
| 259 | + clientAndServer | |
| 260 | + .when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 261 | + .respond(HttpResponse.response().withStatusCode(200).withBody(BITSTRING_STATUS_LIST_CREDENTIAL_PURPOSE_TEMPLATE.formatted("revocation", generateBitstring(REVOKED_INDEX, 1)))); | |
| 262 | + clientAndServer | |
| 263 | + .when(request().withMethod("GET").withPath("/credentials/status/4")) | |
| 264 | + .respond(HttpResponse.response().withStatusCode(200).withBody(BITSTRING_STATUS_LIST_CREDENTIAL_PURPOSE_TEMPLATE.formatted("suspension", generateBitstring()))); | |
| 265 | + | |
| 266 | + var credential = TestFunctions.createCredentialBuilder() | |
| 267 | + .credentialStatus(new CredentialStatus("test-id", BitstringStatusListStatus.TYPE, | |
| 268 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 269 | + STATUS_LIST_INDEX, REVOKED_INDEX, | |
| 270 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 271 | + .credentialStatus(new CredentialStatus("test-id", BitstringStatusListStatus.TYPE, | |
| 272 | + Map.of(STATUS_LIST_PURPOSE, "suspension", | |
| 273 | + STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 274 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/4".formatted(clientAndServer.getPort())))) | |
| 275 | + .build(); | |
| 276 | + assertThat(revocationService.getStatusPurpose(credential)).isSucceeded() | |
| 277 | + .isEqualTo("revocation"); | |
| 278 | + } | |
| 279 | + | |
| 280 | + @Test | |
| 281 | + void getStatusPurpose_multipleCredentialStatus() { | |
| 282 | + | |
| 283 | + clientAndServer.reset(); | |
| 284 | + clientAndServer | |
| 285 | + .when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 286 | + .respond(HttpResponse.response().withStatusCode(200).withBody(BITSTRING_STATUS_LIST_CREDENTIAL_PURPOSE_TEMPLATE.formatted("revocation", generateBitstring(42, 1)))); | |
| 287 | + clientAndServer | |
| 288 | + .when(request().withMethod("GET").withPath("/credentials/status/4")) | |
| 289 | + .respond(HttpResponse.response().withStatusCode(200).withBody(BITSTRING_STATUS_LIST_CREDENTIAL_PURPOSE_TEMPLATE.formatted("suspension", generateBitstring(69, 1)))); | |
| 290 | + | |
| 291 | + var credential = TestFunctions.createCredentialBuilder() | |
| 292 | + .credentialStatus(new CredentialStatus("test-id", BitstringStatusListStatus.TYPE, | |
| 293 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 294 | + STATUS_LIST_INDEX, 42, | |
| 295 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 296 | + .credentialStatus(new CredentialStatus("test-id", BitstringStatusListStatus.TYPE, | |
| 297 | + Map.of(STATUS_LIST_PURPOSE, "suspension", | |
| 298 | + STATUS_LIST_INDEX, 69, | |
| 299 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/4".formatted(clientAndServer.getPort())))) | |
| 300 | + .build(); | |
| 301 | + assertThat(revocationService.getStatusPurpose(credential)).isSucceeded() | |
| 302 | + .isEqualTo("revocation, suspension"); | |
| 303 | + } | |
| 304 | + } | |
| 305 | +} | |
| No newline at the end of the file |
added extensions/common/iam/verifiable-credentials/src/test/java/org/eclipse/edc/iam/verifiablecredentials/revocation/statuslist/StatusList2021RevocationServiceTest.java +199 −0
| @@ -0,0 +1,199 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.revocation.statuslist; | |
| 16 | + | |
| 17 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
| 18 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
| 19 | +import org.eclipse.edc.iam.verifiablecredentials.TestData; | |
| 20 | +import org.eclipse.edc.iam.verifiablecredentials.revocation.statuslist2021.StatusList2021RevocationService; | |
| 21 | +import org.eclipse.edc.iam.verifiablecredentials.spi.TestFunctions; | |
| 22 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 23 | +import org.junit.jupiter.api.AfterEach; | |
| 24 | +import org.junit.jupiter.api.BeforeEach; | |
| 25 | +import org.junit.jupiter.api.Named; | |
| 26 | +import org.junit.jupiter.api.Test; | |
| 27 | +import org.junit.jupiter.api.extension.ExtensionContext; | |
| 28 | +import org.junit.jupiter.params.ParameterizedTest; | |
| 29 | +import org.junit.jupiter.params.provider.Arguments; | |
| 30 | +import org.junit.jupiter.params.provider.ArgumentsProvider; | |
| 31 | +import org.junit.jupiter.params.provider.ArgumentsSource; | |
| 32 | +import org.mockserver.integration.ClientAndServer; | |
| 33 | +import org.mockserver.model.HttpResponse; | |
| 34 | +import org.mockserver.verify.VerificationTimes; | |
| 35 | + | |
| 36 | +import java.util.Map; | |
| 37 | +import java.util.stream.Stream; | |
| 38 | + | |
| 39 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021.StatusList2021Status.STATUS_LIST_CREDENTIAL; | |
| 40 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021.StatusList2021Status.STATUS_LIST_INDEX; | |
| 41 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021.StatusList2021Status.STATUS_LIST_PURPOSE; | |
| 42 | +import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; | |
| 43 | +import static org.eclipse.edc.util.io.Ports.getFreePort; | |
| 44 | +import static org.mockserver.model.HttpRequest.request; | |
| 45 | + | |
| 46 | +class StatusList2021RevocationServiceTest { | |
| 47 | + private static final int NOT_REVOKED_INDEX = 1; | |
| 48 | + private static final int REVOKED_INDEX = 2; | |
| 49 | + private final StatusList2021RevocationService revocationService = new StatusList2021RevocationService(new ObjectMapper().registerModule(new JavaTimeModule()), | |
| 50 | + 5 * 60 * 1000); | |
| 51 | + private ClientAndServer clientAndServer; | |
| 52 | + | |
| 53 | + @BeforeEach | |
| 54 | + void setup() { | |
| 55 | + clientAndServer = ClientAndServer.startClientAndServer("localhost", getFreePort()); | |
| 56 | + clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 57 | + .respond(HttpResponse.response().withStatusCode(200).withBody(TestData.StatusList2021.STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_INTERMEDIATE)); | |
| 58 | + } | |
| 59 | + | |
| 60 | + @AfterEach | |
| 61 | + void tearDown() { | |
| 62 | + clientAndServer.stop(); | |
| 63 | + } | |
| 64 | + | |
| 65 | + @ParameterizedTest | |
| 66 | + @ArgumentsSource(ArraySubjectProvider.class) | |
| 67 | + void checkRevocation_whenSubjectIsArray(String testData) { | |
| 68 | + clientAndServer.reset(); | |
| 69 | + clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 70 | + .respond(HttpResponse.response().withStatusCode(200).withBody(testData)); | |
| 71 | + var credential = new CredentialStatus("test-id", "StatusList2021", | |
| 72 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 73 | + STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 74 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))); | |
| 75 | + assertThat(revocationService.checkValidity(credential)).isSucceeded(); | |
| 76 | + } | |
| 77 | + | |
| 78 | + @Test | |
| 79 | + void checkRevocation_whenNotCached_valid() { | |
| 80 | + var credential = new CredentialStatus("test-id", "StatusList2021", | |
| 81 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 82 | + STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 83 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))); | |
| 84 | + assertThat(revocationService.checkValidity(credential)).isSucceeded(); | |
| 85 | + } | |
| 86 | + | |
| 87 | + @Test | |
| 88 | + void checkRevocation_whenNotCached_credentialPurposeMismatch() { | |
| 89 | + var credential = new CredentialStatus("test-id", "StatusList2021", | |
| 90 | + Map.of(STATUS_LIST_PURPOSE, "suspension", | |
| 91 | + STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 92 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))); | |
| 93 | + assertThat(revocationService.checkValidity(credential)).isFailed() | |
| 94 | + .detail().startsWith("Credential's statusPurpose value must match the status list's purpose:"); | |
| 95 | + } | |
| 96 | + | |
| 97 | + @Test | |
| 98 | + void checkRevocation_whenNotCached_invalid() { | |
| 99 | + var credential = new CredentialStatus("test-id", "StatusList2021", | |
| 100 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 101 | + STATUS_LIST_INDEX, REVOKED_INDEX, | |
| 102 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))); | |
| 103 | + assertThat(revocationService.checkValidity(credential)).isFailed() | |
| 104 | + .detail().isEqualTo("Credential status is 'revocation', status at index %d is '1'".formatted(REVOKED_INDEX)); | |
| 105 | + } | |
| 106 | + | |
| 107 | + @Test | |
| 108 | + void checkRevocation_whenCached_valid() { | |
| 109 | + var credential = new CredentialStatus("test-id", "StatusList2021Entry", | |
| 110 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 111 | + STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 112 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))); | |
| 113 | + assertThat(revocationService.checkValidity(credential)).isSucceeded(); | |
| 114 | + assertThat(revocationService.checkValidity(credential)).isSucceeded(); | |
| 115 | + clientAndServer.verify(request(), VerificationTimes.exactly(1)); | |
| 116 | + } | |
| 117 | + | |
| 118 | + @ParameterizedTest | |
| 119 | + @ArgumentsSource(SingleSubjectProvider.class) | |
| 120 | + void getStatusPurposes_whenSingleCredentialStatusRevoked(String testData) { | |
| 121 | + clientAndServer.reset(); | |
| 122 | + clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 123 | + .respond(HttpResponse.response().withStatusCode(200).withBody(testData)); | |
| 124 | + var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021", | |
| 125 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 126 | + STATUS_LIST_INDEX, REVOKED_INDEX, | |
| 127 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 128 | + .build(); | |
| 129 | + assertThat(revocationService.getStatusPurpose(credential)).isSucceeded() | |
| 130 | + .isEqualTo("revocation"); | |
| 131 | + } | |
| 132 | + | |
| 133 | + @ParameterizedTest | |
| 134 | + @ArgumentsSource(ArraySubjectProvider.class) | |
| 135 | + void getStatusPurposes_whenMultipleCredentialStatusRevoked(String testData) { | |
| 136 | + clientAndServer.reset(); | |
| 137 | + clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 138 | + .respond(HttpResponse.response().withStatusCode(200).withBody(testData)); | |
| 139 | + var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021", | |
| 140 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 141 | + STATUS_LIST_INDEX, REVOKED_INDEX, | |
| 142 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 143 | + .build(); | |
| 144 | + assertThat(revocationService.getStatusPurpose(credential)).isSucceeded() | |
| 145 | + .isEqualTo("revocation"); | |
| 146 | + } | |
| 147 | + | |
| 148 | + @ParameterizedTest | |
| 149 | + @ArgumentsSource(SingleSubjectProvider.class) | |
| 150 | + void getStatusPurpose_whenCredentialStatusNotActive(String testData) { | |
| 151 | + clientAndServer.reset(); | |
| 152 | + clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 153 | + .respond(HttpResponse.response().withStatusCode(200).withBody(testData)); | |
| 154 | + var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021", | |
| 155 | + Map.of(STATUS_LIST_PURPOSE, "revocation", | |
| 156 | + STATUS_LIST_INDEX, NOT_REVOKED_INDEX, | |
| 157 | + STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort())))) | |
| 158 | + .build(); | |
| 159 | + assertThat(revocationService.getStatusPurpose(credential)).isSucceeded() | |
| 160 | + .isNull(); | |
| 161 | + } | |
| 162 | + | |
| 163 | + @ParameterizedTest | |
| 164 | + @ArgumentsSource(SingleSubjectProvider.class) | |
| 165 | + void getStatusPurpose_whenNoCredentialStatus(String testData) { | |
| 166 | + clientAndServer.reset(); | |
| 167 | + clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3")) | |
| 168 | + .respond(HttpResponse.response().withStatusCode(200).withBody(testData)); | |
| 169 | + var credential = TestFunctions.createCredentialBuilder().build(); | |
| 170 | + assertThat(revocationService.getStatusPurpose(credential)) | |
| 171 | + .isNotNull() | |
| 172 | + .isSucceeded(); | |
| 173 | + } | |
| 174 | + | |
| 175 | + | |
| 176 | + private static class SingleSubjectProvider implements ArgumentsProvider { | |
| 177 | + @Override | |
| 178 | + public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) { | |
| 179 | + return Stream.of( | |
| 180 | + Arguments.of(Named.of("VC (intermediate)", TestData.StatusList2021.STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_INTERMEDIATE)), | |
| 181 | + Arguments.of(Named.of("VC 1.1", TestData.StatusList2021.STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_1_0)), | |
| 182 | + Arguments.of(Named.of("VC 2.0", TestData.StatusList2021.STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT_2_0)) | |
| 183 | + | |
| 184 | + ); | |
| 185 | + } | |
| 186 | + } | |
| 187 | + | |
| 188 | + private static class ArraySubjectProvider implements ArgumentsProvider { | |
| 189 | + @Override | |
| 190 | + public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) { | |
| 191 | + return Stream.of( | |
| 192 | + Arguments.of(Named.of("VC 1.1", TestData.StatusList2021.STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY_1_0)), | |
| 193 | + Arguments.of(Named.of("VC (intermediate)", TestData.StatusList2021.STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY_INTERMEDIATE)), | |
| 194 | + Arguments.of(Named.of("VC 2.0", TestData.StatusList2021.STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY_2_0)) | |
| 195 | + | |
| 196 | + ); | |
| 197 | + } | |
| 198 | + } | |
| 199 | +} | |
| No newline at the end of the file |
modified spi/common/verifiable-credentials-spi/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/RevocationListService.java +6 −5
| @@ -14,11 +14,12 @@ | ||
| 14 | 14 | |
| 15 | 15 | package org.eclipse.edc.iam.verifiablecredentials.spi; |
| 16 | 16 | |
| 17 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 17 | 18 | import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiableCredential; |
| 18 | 19 | import org.eclipse.edc.spi.result.Result; |
| 19 | 20 | |
| 20 | 21 | /** |
| 21 | - * Checks a {@link VerifiableCredential} for revocation. Implementors should maintain an internal cache to limit remote calls | |
| 22 | + * Checks a {@link CredentialStatus} for revocation. Implementors should maintain an internal cache to limit remote calls | |
| 22 | 23 | * to the status list credential. |
| 23 | 24 | * <ul> |
| 24 | 25 | * <li>StatusList2021: the cache expiry can be configured</li> |
| @@ -30,13 +31,13 @@ | ||
| 30 | 31 | */ |
| 31 | 32 | public interface RevocationListService { |
| 32 | 33 | /** |
| 33 | - * Check the "validity" of a credential, where validity is understood as not-revoked and not-suspended. Credentials that don't have | |
| 34 | - * a {@code credentialStatus} object are deemed valid. If the {@code credentialStatus} object is invalid, the credential is deemed invalid. | |
| 34 | + * Check the "validity" of a credential status, where validity is understood as not-revoked and not-suspended. Credentials that don't have | |
| 35 | + * a {@code credentialStatus} object are deemed valid. If the {@code credentialStatus} object is structurally invalid, the credential is deemed invalid. | |
| 35 | 36 | */ |
| 36 | - Result<Void> checkValidity(VerifiableCredential credential); | |
| 37 | + Result<Void> checkValidity(CredentialStatus credential); | |
| 37 | 38 | |
| 38 | 39 | /** |
| 39 | - * Determines the status of a credential. If a {@code credentialStatus} object exists, the service will determin the "status purpose". It can be: | |
| 40 | + * Determines the status of a credential. If a {@code credentialStatus} object exists, the service will determine the "status purpose". It can be: | |
| 40 | 41 | * <ul> |
| 41 | 42 | * <li>null: {@code credentialStatus} object not present, or status purpose is present but the status credential's encoded bitstring resolves a "0" at the status index. |
| 42 | 43 | * i.e. the credential is "not revoked" and "not suspended". </li> |
modified spi/common/verifiable-credentials-spi/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/VcConstants.java +3 −0
| @@ -30,4 +30,7 @@ | ||
| 30 | 30 | String VC_PREFIX_V2 = "https://www.w3.org/ns/credentials/v2"; |
| 31 | 31 | String STATUSLIST_2021_URL = "https://w3id.org/vc/status-list/2021/v1"; |
| 32 | 32 | String STATUSLIST_2021_PREFIX = "https://w3id.org/vc/status-list#"; |
| 33 | + | |
| 34 | + String BITSTRING_STATUS_LIST_URL = VC_PREFIX_V2; | |
| 35 | + String BITSTRING_STATUS_LIST_PREFIX = "https://www.w3.org/ns/credentials/status#"; | |
| 33 | 36 | } |
added spi/common/verifiable-credentials-spi/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/RevocationServiceRegistry.java +42 −0
| @@ -0,0 +1,42 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.spi.model; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.iam.verifiablecredentials.spi.RevocationListService; | |
| 18 | +import org.eclipse.edc.spi.result.Result; | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * Registry that contains specific revocation status checker services per type. | |
| 22 | + */ | |
| 23 | +public interface RevocationServiceRegistry { | |
| 24 | + /** | |
| 25 | + * Register a {@link RevocationListService} per status type | |
| 26 | + * | |
| 27 | + * @param statusListType the {@code CredentialStatus.type} value, for example {@code "BitstringStatusListEntry"}. | |
| 28 | + * @param service a {@link RevocationListService} that specifically checks a particular revocation list, for example | |
| 29 | + * {@code BitstringStatusListRevocationService} | |
| 30 | + */ | |
| 31 | + void addService(String statusListType, RevocationListService service); | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Checks the revocation status of a {@link VerifiableCredential}. If no {@link RevocationListService} was registered for a particular | |
| 35 | + * type, the implementation must return {@link Result#success()}. If the credential does not contain any credentialStatus, | |
| 36 | + * the result is {@link Result#success()} as well. | |
| 37 | + * | |
| 38 | + * @param credential The VC | |
| 39 | + * @return {@link Result#success()} if the VC does not contain a {@link CredentialStatus}, or if all {@link CredentialStatus} objects are valid (= not revoked, not suspended), {@link Result#failure(String)} otherwise. | |
| 40 | + */ | |
| 41 | + Result<Void> checkValidity(VerifiableCredential credential); | |
| 42 | +} |
added spi/common/verifiable-credentials-spi/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/revocation/BitString.java +201 −0
| @@ -0,0 +1,201 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.spi.result.Result; | |
| 18 | + | |
| 19 | +import java.io.ByteArrayInputStream; | |
| 20 | +import java.io.ByteArrayOutputStream; | |
| 21 | +import java.io.IOException; | |
| 22 | +import java.util.Base64; | |
| 23 | +import java.util.zip.GZIPInputStream; | |
| 24 | +import java.util.zip.GZIPOutputStream; | |
| 25 | + | |
| 26 | +/** | |
| 27 | + * Representation of <a href="https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#bitstring-encoding">StatusList2021Credential#bitstring</a> | |
| 28 | + */ | |
| 29 | +public class BitString { | |
| 30 | + | |
| 31 | + private final boolean leftToRightIndexing; | |
| 32 | + private final byte[] bits; | |
| 33 | + private final int bitsPerByte = 8; | |
| 34 | + | |
| 35 | + private BitString(byte[] bits, boolean leftToRightIndexing) { | |
| 36 | + this.bits = bits; | |
| 37 | + this.leftToRightIndexing = leftToRightIndexing; | |
| 38 | + } | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * Checks if the bit at the input index is `1`. The input index should be in the | |
| 42 | + * bound (0-bitstring_length - 1). The default bit order is left to right, which means | |
| 43 | + * that for a byte 00000001, the bit value of that first (zeroth) index is `0` and | |
| 44 | + * the last index (seventh) is `1` | |
| 45 | + * | |
| 46 | + * @param idx The bit index to check | |
| 47 | + * @return True if `1`, false otherwise | |
| 48 | + */ | |
| 49 | + public boolean get(int idx) { | |
| 50 | + | |
| 51 | + if (idx < 0 || idx >= length()) { | |
| 52 | + throw new IllegalArgumentException("Index out of range 0-%s".formatted(length())); | |
| 53 | + } | |
| 54 | + var byteIdx = idx / bitsPerByte; | |
| 55 | + var shift = bitPosition(idx); | |
| 56 | + return (bits[byteIdx] & (1L << shift)) != 0; | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Set the bit at idx to either `1` or `0` depending on the boolean in input | |
| 61 | + * | |
| 62 | + * @param idx The index to change | |
| 63 | + * @param status true or false if it's revoked or not | |
| 64 | + */ | |
| 65 | + public void set(int idx, boolean status) { | |
| 66 | + if (idx < 0 || idx >= length()) { | |
| 67 | + throw new IllegalArgumentException("Index out of range 0-%s".formatted(length())); | |
| 68 | + } | |
| 69 | + var byteIdx = idx / bitsPerByte; | |
| 70 | + var shift = bitPosition(idx); | |
| 71 | + | |
| 72 | + if (status) { | |
| 73 | + bits[byteIdx] |= (byte) (1L << shift); | |
| 74 | + } else { | |
| 75 | + bits[byteIdx] &= (byte) ~(1L << shift); | |
| 76 | + } | |
| 77 | + } | |
| 78 | + | |
| 79 | + public int length() { | |
| 80 | + return bits.length * bitsPerByte; | |
| 81 | + } | |
| 82 | + | |
| 83 | + private int bitPosition(int idx) { | |
| 84 | + var bitIdx = idx % bitsPerByte; | |
| 85 | + return leftToRightIndexing ? (7 - bitIdx) : bitIdx; | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Parser configuration for {@link BitString} | |
| 90 | + */ | |
| 91 | + public static final class Builder { | |
| 92 | + | |
| 93 | + private boolean leftToRightIndexing = true; | |
| 94 | + private int size = 16 * 1024 * 8; | |
| 95 | + | |
| 96 | + private Builder() { | |
| 97 | + } | |
| 98 | + | |
| 99 | + public static Builder newInstance() { | |
| 100 | + return new Builder(); | |
| 101 | + } | |
| 102 | + | |
| 103 | + | |
| 104 | + public Builder leftToRightIndexing(boolean leftToRightIndexing) { | |
| 105 | + this.leftToRightIndexing = leftToRightIndexing; | |
| 106 | + return this; | |
| 107 | + } | |
| 108 | + | |
| 109 | + public Builder size(int size) { | |
| 110 | + this.size = size; | |
| 111 | + return this; | |
| 112 | + } | |
| 113 | + | |
| 114 | + public BitString build() { | |
| 115 | + if (size % 8 != 0) { | |
| 116 | + throw new IllegalArgumentException("BitString size should be multiple of 8"); | |
| 117 | + } | |
| 118 | + var bits = new byte[size / 8]; | |
| 119 | + return new BitString(bits, leftToRightIndexing); | |
| 120 | + } | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Parser configuration for {@link BitString} | |
| 125 | + */ | |
| 126 | + public static final class Parser { | |
| 127 | + private boolean leftToRightIndexing = true; | |
| 128 | + private Base64.Decoder decoder = Base64.getDecoder(); | |
| 129 | + | |
| 130 | + private Parser() { | |
| 131 | + } | |
| 132 | + | |
| 133 | + public static Parser newInstance() { | |
| 134 | + return new Parser(); | |
| 135 | + } | |
| 136 | + | |
| 137 | + public Parser leftToRightIndexing(boolean leftToRightIndexing) { | |
| 138 | + this.leftToRightIndexing = leftToRightIndexing; | |
| 139 | + return this; | |
| 140 | + } | |
| 141 | + | |
| 142 | + public Parser decoder(Base64.Decoder decoder) { | |
| 143 | + this.decoder = decoder; | |
| 144 | + return this; | |
| 145 | + } | |
| 146 | + | |
| 147 | + public Result<BitString> parse(String encodedList) { | |
| 148 | + return Result.ofThrowable(() -> decoder.decode(encodedList)) | |
| 149 | + .compose(this::decompress) | |
| 150 | + .map(bytes -> new BitString(bytes, leftToRightIndexing)); | |
| 151 | + } | |
| 152 | + | |
| 153 | + private Result<byte[]> decompress(byte[] bytes) { | |
| 154 | + try (var inputStream = new GZIPInputStream(new ByteArrayInputStream(bytes))) { | |
| 155 | + try (var outputStream = new ByteArrayOutputStream()) { | |
| 156 | + inputStream.transferTo(outputStream); | |
| 157 | + return Result.success(outputStream.toByteArray()); | |
| 158 | + } | |
| 159 | + } catch (IOException e) { | |
| 160 | + return Result.failure("Failed to ungzip encoded list: %s".formatted(e.getMessage())); | |
| 161 | + } | |
| 162 | + } | |
| 163 | + } | |
| 164 | + | |
| 165 | + /** | |
| 166 | + * Writer configuration for {@link BitString} | |
| 167 | + */ | |
| 168 | + public static final class Writer { | |
| 169 | + private Base64.Encoder encoder = Base64.getEncoder(); | |
| 170 | + | |
| 171 | + private Writer() { | |
| 172 | + } | |
| 173 | + | |
| 174 | + public static Writer newInstance() { | |
| 175 | + return new Writer(); | |
| 176 | + } | |
| 177 | + | |
| 178 | + public Writer encoder(Base64.Encoder encoder) { | |
| 179 | + this.encoder = encoder; | |
| 180 | + return this; | |
| 181 | + } | |
| 182 | + | |
| 183 | + public Result<String> write(BitString bitString) { | |
| 184 | + return compress(bitString.bits) | |
| 185 | + .compose(compressed -> Result.ofThrowable(() -> encoder.encodeToString(compressed))); | |
| 186 | + | |
| 187 | + } | |
| 188 | + | |
| 189 | + private Result<byte[]> compress(byte[] bytes) { | |
| 190 | + try (var outputStream = new ByteArrayOutputStream()) { | |
| 191 | + try (var zipStream = new GZIPOutputStream(outputStream)) { | |
| 192 | + zipStream.write(bytes); | |
| 193 | + zipStream.close(); | |
| 194 | + return Result.success(outputStream.toByteArray()); | |
| 195 | + } | |
| 196 | + } catch (IOException e) { | |
| 197 | + return Result.failure("Failed to gzip the input bytes: %s".formatted(e.getMessage())); | |
| 198 | + } | |
| 199 | + } | |
| 200 | + } | |
| 201 | +} |
added spi/common/verifiable-credentials-spi/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/revocation/bitstringstatuslist/BitstringStatusListCredential.java +103 −0
| @@ -0,0 +1,103 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiableCredential; | |
| 18 | + | |
| 19 | +import java.time.Duration; | |
| 20 | +import java.time.Instant; | |
| 21 | + | |
| 22 | +import static java.util.Optional.ofNullable; | |
| 23 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.VcConstants.BITSTRING_STATUS_LIST_PREFIX; | |
| 24 | + | |
| 25 | +public class BitstringStatusListCredential extends VerifiableCredential { | |
| 26 | + public static final String BITSTRING_STATUSLIST_CREDENTIAL = "BitstringStatusListCredential"; | |
| 27 | + public static final String BITSTRING_ENCODED_LIST_LITERAL = "encodedList"; | |
| 28 | + public static final String BITSTRING_TTL_LITERAL = "ttl"; | |
| 29 | + public static final String STATUS_LIST_ENCODED_LIST = BITSTRING_STATUS_LIST_PREFIX + BITSTRING_ENCODED_LIST_LITERAL; | |
| 30 | + public static final String STATUS_LIST_TTL = BITSTRING_STATUS_LIST_PREFIX + BITSTRING_TTL_LITERAL; | |
| 31 | + | |
| 32 | + public String encodedList() { | |
| 33 | + return (String) credentialSubject.get(0).getClaim(BITSTRING_STATUS_LIST_PREFIX, BITSTRING_ENCODED_LIST_LITERAL); | |
| 34 | + } | |
| 35 | + | |
| 36 | + public String statusPurpose() { | |
| 37 | + return (String) credentialSubject.get(0).getClaim(BITSTRING_STATUS_LIST_PREFIX, BitstringStatusListStatus.BITSTRING_STATUS_LIST_PURPOSE_LITERAL); | |
| 38 | + } | |
| 39 | + | |
| 40 | + public Instant validFrom() { | |
| 41 | + return getIssuanceDate(); | |
| 42 | + } | |
| 43 | + | |
| 44 | + public Instant validUntil() { | |
| 45 | + return getExpirationDate(); | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * The "time-to-live" in milliseconds | |
| 50 | + * | |
| 51 | + * @see <a href="https://www.w3.org/TR/vc-bitstring-status-list/#bitstringstatuslistcredential">W3C BitStringStatusListCredential</a> | |
| 52 | + * @deprecated the "ttl" field is in conflict with the {@link BitstringStatusListCredential#validUntil()} field and may get removed in the future. | |
| 53 | + */ | |
| 54 | + @Deprecated | |
| 55 | + public Duration ttl() { | |
| 56 | + var ttl = ofNullable(credentialSubject.get(0).getClaim(BITSTRING_STATUS_LIST_PREFIX, BITSTRING_TTL_LITERAL)) | |
| 57 | + .map(String::valueOf) | |
| 58 | + .map(Long::parseLong) | |
| 59 | + .orElse(300_000L); | |
| 60 | + | |
| 61 | + return Duration.ofMillis(ttl); | |
| 62 | + } | |
| 63 | + | |
| 64 | + public static class Builder extends VerifiableCredential.Builder<BitstringStatusListCredential, BitstringStatusListCredential.Builder> { | |
| 65 | + protected Builder(BitstringStatusListCredential credential) { | |
| 66 | + super(credential); | |
| 67 | + } | |
| 68 | + | |
| 69 | + public static BitstringStatusListCredential.Builder newInstance() { | |
| 70 | + return new BitstringStatusListCredential.Builder(new BitstringStatusListCredential()); | |
| 71 | + } | |
| 72 | + | |
| 73 | + @Override | |
| 74 | + public BitstringStatusListCredential build() { | |
| 75 | + super.build(); | |
| 76 | + if (!instance.type.contains(BITSTRING_STATUSLIST_CREDENTIAL)) { | |
| 77 | + throw new IllegalArgumentException("Only '%s' type is supported, but encountered: %s".formatted(BITSTRING_STATUSLIST_CREDENTIAL, instance.type)); | |
| 78 | + } | |
| 79 | + | |
| 80 | + if (instance.credentialSubject == null || instance.credentialSubject.isEmpty()) { | |
| 81 | + throw new IllegalArgumentException("Status list credentials must contain a 'credentialSubject' property."); | |
| 82 | + } | |
| 83 | + if (instance.credentialSubject.size() != 1) { | |
| 84 | + throw new IllegalArgumentException("Status list credentials must contain exactly 1 'credentialSubject' object, but found %d".formatted(instance.credentialSubject.size())); | |
| 85 | + } | |
| 86 | + | |
| 87 | + // check mandatory fields of the credentialSubject object | |
| 88 | + var subject = instance.credentialSubject.get(0); | |
| 89 | + if (subject.getClaim(BITSTRING_STATUS_LIST_PREFIX, BITSTRING_ENCODED_LIST_LITERAL) == null) { | |
| 90 | + throw new IllegalArgumentException("Status list credentials must contain a 'credentialSubject.encodedList' field."); | |
| 91 | + } | |
| 92 | + if (subject.getClaim(BITSTRING_STATUS_LIST_PREFIX, BitstringStatusListStatus.BITSTRING_STATUS_LIST_PURPOSE_LITERAL) == null) { | |
| 93 | + throw new IllegalArgumentException("Status list credentials must contain a 'credentialSubject.statusPurpose' field."); | |
| 94 | + } | |
| 95 | + return instance; | |
| 96 | + } | |
| 97 | + | |
| 98 | + @Override | |
| 99 | + protected BitstringStatusListCredential.Builder self() { | |
| 100 | + return this; | |
| 101 | + } | |
| 102 | + } | |
| 103 | +} |
added spi/common/verifiable-credentials-spi/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/revocation/bitstringstatuslist/BitstringStatusListStatus.java +145 −0
| @@ -0,0 +1,145 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 18 | + | |
| 19 | +import java.util.ArrayList; | |
| 20 | +import java.util.Collections; | |
| 21 | +import java.util.List; | |
| 22 | +import java.util.Map; | |
| 23 | + | |
| 24 | +import static java.util.Optional.ofNullable; | |
| 25 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.VcConstants.BITSTRING_STATUS_LIST_PREFIX; | |
| 26 | + | |
| 27 | +public class BitstringStatusListStatus { | |
| 28 | + | |
| 29 | + public static final String TYPE = "BitstringStatusListEntry"; | |
| 30 | + public static final String BITSTRING_STATUS_LIST_CREDENTIAL_LITERAL = "statusListCredential"; | |
| 31 | + public static final String STATUS_LIST_CREDENTIAL = BITSTRING_STATUS_LIST_PREFIX + BITSTRING_STATUS_LIST_CREDENTIAL_LITERAL; | |
| 32 | + public static final String BITSTRING_STATUS_LIST_INDEX_LITERAL = "statusListIndex"; | |
| 33 | + public static final String STATUS_LIST_INDEX = BITSTRING_STATUS_LIST_PREFIX + BITSTRING_STATUS_LIST_INDEX_LITERAL; | |
| 34 | + public static final String BITSTRING_STATUS_LIST_PURPOSE_LITERAL = "statusPurpose"; | |
| 35 | + public static final String STATUS_LIST_PURPOSE = BITSTRING_STATUS_LIST_PREFIX + BITSTRING_STATUS_LIST_PURPOSE_LITERAL; | |
| 36 | + public static final String BITSTRING_STATUS_LIST_SIZE_LITERAL = "statusSize"; | |
| 37 | + public static final String STATUS_LIST_SIZE = BITSTRING_STATUS_LIST_PREFIX + BITSTRING_STATUS_LIST_SIZE_LITERAL; | |
| 38 | + public static final String BITSTRING_STATUS_LIST_MESSAGE_LITERAL = "statusMessage"; | |
| 39 | + public static final String STATUS_LIST_MESSAGES = BITSTRING_STATUS_LIST_PREFIX + BITSTRING_STATUS_LIST_MESSAGE_LITERAL; | |
| 40 | + public static final String BITSTRING_STATUS_LIST_REFERENCE_LITERAL = "statusReference"; | |
| 41 | + public static final String STATUS_LIST_REFERENCE = BITSTRING_STATUS_LIST_PREFIX + BITSTRING_STATUS_LIST_REFERENCE_LITERAL; | |
| 42 | + private List<String> statusReference = new ArrayList<>(); | |
| 43 | + private String statusListPurpose; | |
| 44 | + private int statusListIndex; | |
| 45 | + private String statusListCredential; | |
| 46 | + private int statusSize; | |
| 47 | + private List<StatusMessage> statusMessage; | |
| 48 | + | |
| 49 | + private BitstringStatusListStatus() { | |
| 50 | + | |
| 51 | + } | |
| 52 | + | |
| 53 | + public static BitstringStatusListStatus from(CredentialStatus status) { | |
| 54 | + var instance = new BitstringStatusListStatus(); | |
| 55 | + | |
| 56 | + instance.statusListCredential = ofNullable(getId(status)) | |
| 57 | + .map(Object::toString) | |
| 58 | + .orElseThrow(() -> new IllegalArgumentException(missingProperty(STATUS_LIST_CREDENTIAL))); | |
| 59 | + | |
| 60 | + instance.statusListIndex = ofNullable(status.getProperty(BITSTRING_STATUS_LIST_PREFIX, BITSTRING_STATUS_LIST_INDEX_LITERAL)) | |
| 61 | + .map(Object::toString) | |
| 62 | + .map(Integer::parseInt) | |
| 63 | + .orElseThrow(() -> new IllegalArgumentException(missingProperty(STATUS_LIST_INDEX))); | |
| 64 | + | |
| 65 | + instance.statusListPurpose = ofNullable(status.getProperty(BITSTRING_STATUS_LIST_PREFIX, BITSTRING_STATUS_LIST_PURPOSE_LITERAL)) | |
| 66 | + .map(Object::toString) | |
| 67 | + .orElseThrow(() -> new IllegalArgumentException(missingProperty(STATUS_LIST_PURPOSE))); | |
| 68 | + | |
| 69 | + var size = ofNullable(status.getProperty(BITSTRING_STATUS_LIST_PREFIX, STATUS_LIST_SIZE)) | |
| 70 | + .map(Object::toString) | |
| 71 | + .map(Integer::parseInt); | |
| 72 | + var statusMessages = status.getProperty(BITSTRING_STATUS_LIST_PREFIX, STATUS_LIST_MESSAGES); | |
| 73 | + | |
| 74 | + if (size.isEmpty() && statusMessages != null) { | |
| 75 | + throw new IllegalArgumentException("statusSize must be specified and > 1 if statusMessage is present."); | |
| 76 | + } | |
| 77 | + instance.statusSize = size.orElse(1); | |
| 78 | + | |
| 79 | + if (instance.statusSize <= 0) { | |
| 80 | + throw new IllegalArgumentException("If present, statusSize must be a positive integer > 0 but was '%d'.".formatted(instance.statusSize)); | |
| 81 | + } | |
| 82 | + | |
| 83 | + instance.statusMessage = extractStatusMessage(statusMessages, instance.statusSize); | |
| 84 | + | |
| 85 | + //noinspection unchecked | |
| 86 | + instance.statusReference = ofNullable(status.getProperty(BITSTRING_STATUS_LIST_PREFIX, STATUS_LIST_REFERENCE)) | |
| 87 | + .map(obj -> (List<String>) obj) | |
| 88 | + .orElse(Collections.emptyList()); | |
| 89 | + | |
| 90 | + return instance; | |
| 91 | + | |
| 92 | + } | |
| 93 | + | |
| 94 | + private static List<StatusMessage> extractStatusMessage(Object statusMessageObject, int statusSize) { | |
| 95 | + if (statusMessageObject == null) return List.of(); | |
| 96 | + //noinspection unchecked | |
| 97 | + var msg = (List<StatusMessage>) (statusMessageObject); | |
| 98 | + | |
| 99 | + // explicitly check this, because statusSize = 1 and an empty message array would be acceptable | |
| 100 | + if (statusSize > 1 && msg.isEmpty()) { | |
| 101 | + throw new IllegalArgumentException("If statusSize is > 1, statusMessage must be present."); | |
| 102 | + } | |
| 103 | + if (Math.pow(2, statusSize) != msg.size()) { | |
| 104 | + throw new IllegalArgumentException("If present, statusSize (in bits) must be equal to the number of possible statusMessage entries, e.g. statusSize = 3, statusMessage = 8 entries."); | |
| 105 | + } | |
| 106 | + | |
| 107 | + return msg; | |
| 108 | + } | |
| 109 | + | |
| 110 | + private static Object getId(CredentialStatus status) { | |
| 111 | + var credentialId = status.getProperty(BITSTRING_STATUS_LIST_PREFIX, BITSTRING_STATUS_LIST_CREDENTIAL_LITERAL); | |
| 112 | + if (credentialId instanceof Map<?, ?> map) { | |
| 113 | + return map.get("@id"); | |
| 114 | + } | |
| 115 | + return credentialId; | |
| 116 | + } | |
| 117 | + | |
| 118 | + private static String missingProperty(String property) { | |
| 119 | + return "A BitstringStatusList credential must have a credentialStatus object with the '%s' property".formatted(property); | |
| 120 | + } | |
| 121 | + | |
| 122 | + public String getStatusListCredential() { | |
| 123 | + return statusListCredential; | |
| 124 | + } | |
| 125 | + | |
| 126 | + public int getStatusListIndex() { | |
| 127 | + return statusListIndex; | |
| 128 | + } | |
| 129 | + | |
| 130 | + public String getStatusListPurpose() { | |
| 131 | + return statusListPurpose; | |
| 132 | + } | |
| 133 | + | |
| 134 | + public List<StatusMessage> getStatusMessage() { | |
| 135 | + return statusMessage; | |
| 136 | + } | |
| 137 | + | |
| 138 | + public List<String> getStatusReference() { | |
| 139 | + return statusReference; | |
| 140 | + } | |
| 141 | + | |
| 142 | + public int getStatusSize() { | |
| 143 | + return statusSize; | |
| 144 | + } | |
| 145 | +} |
added spi/common/verifiable-credentials-spi/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/revocation/bitstringstatuslist/StatusMessage.java +23 −0
| @@ -0,0 +1,23 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist; | |
| 16 | + | |
| 17 | +public record StatusMessage(String status, String message) { | |
| 18 | + public StatusMessage { | |
| 19 | + if (!status.startsWith("0x")) { | |
| 20 | + throw new IllegalArgumentException("All statusMessage status fields MUST start with \"0x\""); | |
| 21 | + } | |
| 22 | + } | |
| 23 | +} |
added spi/common/verifiable-credentials-spi/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/revocation/statuslist2021/StatusList2021Credential.java +84 −0
| @@ -0,0 +1,84 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiableCredential; | |
| 18 | + | |
| 19 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.VcConstants.STATUSLIST_2021_PREFIX; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * Represents a special {@link VerifiableCredential}, specifically a <a href="https://www.w3.org/TR/2023/WD-vc-status-list-20230427/">W3C StatusList2021</a> credential. | |
| 23 | + * That means that the shape of the {@link VerifiableCredential#getCredentialSubject()} is not arbitrary anymore, but must contain specific items. | |
| 24 | + * <p> | |
| 25 | + * Every StatusList2021 credential is a valid {@link VerifiableCredential}. | |
| 26 | + */ | |
| 27 | +public class StatusList2021Credential extends VerifiableCredential { | |
| 28 | + public static final String STATUSLIST_2021_CREDENTIAL = "StatusList2021Credential"; | |
| 29 | + public static final String STATUS_LIST_ENCODED_LIST_LITERAL = "encodedList"; | |
| 30 | + public static final String STATUS_LIST_ENCODED_LIST = STATUSLIST_2021_PREFIX + STATUS_LIST_ENCODED_LIST_LITERAL; | |
| 31 | + | |
| 32 | + private StatusList2021Credential() { | |
| 33 | + } | |
| 34 | + | |
| 35 | + public String encodedList() { | |
| 36 | + return (String) credentialSubject.get(0).getClaim(STATUSLIST_2021_PREFIX, STATUS_LIST_ENCODED_LIST_LITERAL); | |
| 37 | + } | |
| 38 | + | |
| 39 | + public String statusPurpose() { | |
| 40 | + return (String) credentialSubject.get(0).getClaim(STATUSLIST_2021_PREFIX, StatusList2021Status.STATUS_LIST_PURPOSE_LITERAL); | |
| 41 | + } | |
| 42 | + | |
| 43 | + public static class Builder extends VerifiableCredential.Builder<StatusList2021Credential, Builder> { | |
| 44 | + protected Builder(StatusList2021Credential credential) { | |
| 45 | + super(credential); | |
| 46 | + } | |
| 47 | + | |
| 48 | + public static Builder newInstance() { | |
| 49 | + return new Builder(new StatusList2021Credential()); | |
| 50 | + } | |
| 51 | + | |
| 52 | + @Override | |
| 53 | + public StatusList2021Credential build() { | |
| 54 | + super.build(); | |
| 55 | + if (!instance.type.contains(STATUSLIST_2021_CREDENTIAL)) { | |
| 56 | + throw new IllegalArgumentException("Only %s is supported, but encountered: %s".formatted(STATUSLIST_2021_CREDENTIAL, instance.type)); | |
| 57 | + } | |
| 58 | + | |
| 59 | + if (instance.credentialSubject == null || instance.credentialSubject.isEmpty()) { | |
| 60 | + throw new IllegalArgumentException("Status list credentials must contain a 'credentialSubject' property."); | |
| 61 | + } | |
| 62 | + if (instance.credentialSubject.size() != 1) { | |
| 63 | + throw new IllegalArgumentException("Status list credentials must contain exactly 1 'credentialSubject' object, but found %d".formatted(instance.credentialSubject.size())); | |
| 64 | + } | |
| 65 | + | |
| 66 | + // check mandatory fields of the credentialSubject object | |
| 67 | + var subject = instance.credentialSubject.get(0); | |
| 68 | + if (subject.getClaim(STATUSLIST_2021_PREFIX, STATUS_LIST_ENCODED_LIST_LITERAL) == null) { | |
| 69 | + throw new IllegalArgumentException("Status list credentials must contain a 'credentialSubject.encodedList' field."); | |
| 70 | + } | |
| 71 | + if (subject.getClaim(STATUSLIST_2021_PREFIX, StatusList2021Status.STATUS_LIST_PURPOSE_LITERAL) == null) { | |
| 72 | + throw new IllegalArgumentException("Status list credentials must contain a 'credentialSubject.statusPurpose' field."); | |
| 73 | + } | |
| 74 | + return instance; | |
| 75 | + } | |
| 76 | + | |
| 77 | + @Override | |
| 78 | + protected Builder self() { | |
| 79 | + return this; | |
| 80 | + } | |
| 81 | + } | |
| 82 | + | |
| 83 | + | |
| 84 | +} |
added spi/common/verifiable-credentials-spi/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/revocation/statuslist2021/StatusList2021Status.java +86 −0
| @@ -0,0 +1,86 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 18 | + | |
| 19 | +import java.util.Map; | |
| 20 | + | |
| 21 | +import static java.util.Optional.ofNullable; | |
| 22 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.VcConstants.STATUSLIST_2021_PREFIX; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * Specialized {@code credentialStatus}, that contains information mandated by the StatusList2021 standard. | |
| 26 | + */ | |
| 27 | +public class StatusList2021Status { | |
| 28 | + | |
| 29 | + public static final String TYPE = "StatusList2021Entry"; | |
| 30 | + public static final String STATUS_LIST_CREDENTIAL_LITERAL = "statusListCredential"; | |
| 31 | + public static final String STATUS_LIST_CREDENTIAL = STATUSLIST_2021_PREFIX + STATUS_LIST_CREDENTIAL_LITERAL; | |
| 32 | + public static final String STATUS_LIST_INDEX_LITERAL = "statusListIndex"; | |
| 33 | + public static final String STATUS_LIST_INDEX = STATUSLIST_2021_PREFIX + STATUS_LIST_INDEX_LITERAL; | |
| 34 | + public static final String STATUS_LIST_PURPOSE_LITERAL = "statusPurpose"; | |
| 35 | + public static final String STATUS_LIST_PURPOSE = STATUSLIST_2021_PREFIX + STATUS_LIST_PURPOSE_LITERAL; | |
| 36 | + private String statusListPurpose; | |
| 37 | + private int statusListIndex; | |
| 38 | + private String statusListCredential; | |
| 39 | + | |
| 40 | + private StatusList2021Status() { | |
| 41 | + } | |
| 42 | + | |
| 43 | + public static StatusList2021Status from(CredentialStatus status) { | |
| 44 | + var instance = new StatusList2021Status(); | |
| 45 | + instance.statusListCredential = ofNullable(getId(status)) | |
| 46 | + .map(Object::toString) | |
| 47 | + .orElseThrow(() -> new IllegalArgumentException(missingProperty(STATUS_LIST_CREDENTIAL))); | |
| 48 | + | |
| 49 | + instance.statusListIndex = ofNullable(status.getProperty(STATUSLIST_2021_PREFIX, STATUS_LIST_INDEX_LITERAL)) | |
| 50 | + .map(Object::toString) | |
| 51 | + .map(Integer::parseInt) | |
| 52 | + .orElseThrow(() -> new IllegalArgumentException(missingProperty(STATUS_LIST_INDEX))); | |
| 53 | + | |
| 54 | + instance.statusListPurpose = ofNullable(status.getProperty(STATUSLIST_2021_PREFIX, STATUS_LIST_PURPOSE_LITERAL)) | |
| 55 | + .map(Object::toString) | |
| 56 | + .orElseThrow(() -> new IllegalArgumentException(missingProperty(STATUS_LIST_PURPOSE))); | |
| 57 | + | |
| 58 | + return instance; | |
| 59 | + } | |
| 60 | + | |
| 61 | + private static Object getId(CredentialStatus status) { | |
| 62 | + var credentialId = status.getProperty(STATUSLIST_2021_PREFIX, STATUS_LIST_CREDENTIAL_LITERAL); | |
| 63 | + if (credentialId instanceof Map<?, ?> map) { | |
| 64 | + return map.get("@id"); | |
| 65 | + } | |
| 66 | + return credentialId; | |
| 67 | + } | |
| 68 | + | |
| 69 | + private static String missingProperty(String property) { | |
| 70 | + return "A StatusList2021 credential must have a credentialStatus object with the '%s' property".formatted(property); | |
| 71 | + } | |
| 72 | + | |
| 73 | + public String getStatusListPurpose() { | |
| 74 | + return statusListPurpose; | |
| 75 | + } | |
| 76 | + | |
| 77 | + public int getStatusListIndex() { | |
| 78 | + return statusListIndex; | |
| 79 | + } | |
| 80 | + | |
| 81 | + public String getStatusListCredential() { | |
| 82 | + return statusListCredential; | |
| 83 | + } | |
| 84 | + | |
| 85 | + | |
| 86 | +} |
deleted spi/common/verifiable-credentials-spi/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/statuslist/BitString.java +0 −201
| @@ -1,201 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | - * | |
| 4 | - * This program and the accompanying materials are made available under the | |
| 5 | - * terms of the Apache License, Version 2.0 which is available at | |
| 6 | - * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | - * | |
| 8 | - * SPDX-License-Identifier: Apache-2.0 | |
| 9 | - * | |
| 10 | - * Contributors: | |
| 11 | - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | - * | |
| 13 | - */ | |
| 14 | - | |
| 15 | -package org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist; | |
| 16 | - | |
| 17 | -import org.eclipse.edc.spi.result.Result; | |
| 18 | - | |
| 19 | -import java.io.ByteArrayInputStream; | |
| 20 | -import java.io.ByteArrayOutputStream; | |
| 21 | -import java.io.IOException; | |
| 22 | -import java.util.Base64; | |
| 23 | -import java.util.zip.GZIPInputStream; | |
| 24 | -import java.util.zip.GZIPOutputStream; | |
| 25 | - | |
| 26 | -/** | |
| 27 | - * Representation of <a href="https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#bitstring-encoding">StatusList2021Credential#bitstring</a> | |
| 28 | - */ | |
| 29 | -public class BitString { | |
| 30 | - | |
| 31 | - private final boolean leftToRightIndexing; | |
| 32 | - private final byte[] bits; | |
| 33 | - private final int bitsPerByte = 8; | |
| 34 | - | |
| 35 | - private BitString(byte[] bits, boolean leftToRightIndexing) { | |
| 36 | - this.bits = bits; | |
| 37 | - this.leftToRightIndexing = leftToRightIndexing; | |
| 38 | - } | |
| 39 | - | |
| 40 | - /** | |
| 41 | - * Checks if the bit at the input index is `1`. The input index should be in the | |
| 42 | - * bound (0-bitstring_length - 1). The default bit order is left to right, which means | |
| 43 | - * that for a byte 00000001, the bit value of that first (zeroth) index is `0` and | |
| 44 | - * the last index (seventh) is `1` | |
| 45 | - * | |
| 46 | - * @param idx The bit index to check | |
| 47 | - * @return True if `1`, false otherwise | |
| 48 | - */ | |
| 49 | - public boolean get(int idx) { | |
| 50 | - | |
| 51 | - if (idx < 0 || idx >= length()) { | |
| 52 | - throw new IllegalArgumentException("Index out of range 0-%s".formatted(length())); | |
| 53 | - } | |
| 54 | - var byteIdx = idx / bitsPerByte; | |
| 55 | - var shift = bitPosition(idx); | |
| 56 | - return (bits[byteIdx] & (1L << shift)) != 0; | |
| 57 | - } | |
| 58 | - | |
| 59 | - /** | |
| 60 | - * Set the bit at idx to either `1` or `0` depending on the boolean in input | |
| 61 | - * | |
| 62 | - * @param idx The index to change | |
| 63 | - * @param status true or false if it's revoked or not | |
| 64 | - */ | |
| 65 | - public void set(int idx, boolean status) { | |
| 66 | - if (idx < 0 || idx >= length()) { | |
| 67 | - throw new IllegalArgumentException("Index out of range 0-%s".formatted(length())); | |
| 68 | - } | |
| 69 | - var byteIdx = idx / bitsPerByte; | |
| 70 | - var shift = bitPosition(idx); | |
| 71 | - | |
| 72 | - if (status) { | |
| 73 | - bits[byteIdx] |= (byte) (1L << shift); | |
| 74 | - } else { | |
| 75 | - bits[byteIdx] &= (byte) ~(1L << shift); | |
| 76 | - } | |
| 77 | - } | |
| 78 | - | |
| 79 | - public int length() { | |
| 80 | - return bits.length * bitsPerByte; | |
| 81 | - } | |
| 82 | - | |
| 83 | - private int bitPosition(int idx) { | |
| 84 | - var bitIdx = idx % bitsPerByte; | |
| 85 | - return leftToRightIndexing ? (7 - bitIdx) : bitIdx; | |
| 86 | - } | |
| 87 | - | |
| 88 | - /** | |
| 89 | - * Parser configuration for {@link BitString} | |
| 90 | - */ | |
| 91 | - public static final class Builder { | |
| 92 | - | |
| 93 | - private boolean leftToRightIndexing = true; | |
| 94 | - private int size = 16 * 1024 * 8; | |
| 95 | - | |
| 96 | - private Builder() { | |
| 97 | - } | |
| 98 | - | |
| 99 | - public static Builder newInstance() { | |
| 100 | - return new Builder(); | |
| 101 | - } | |
| 102 | - | |
| 103 | - | |
| 104 | - public Builder leftToRightIndexing(boolean leftToRightIndexing) { | |
| 105 | - this.leftToRightIndexing = leftToRightIndexing; | |
| 106 | - return this; | |
| 107 | - } | |
| 108 | - | |
| 109 | - public Builder size(int size) { | |
| 110 | - this.size = size; | |
| 111 | - return this; | |
| 112 | - } | |
| 113 | - | |
| 114 | - public BitString build() { | |
| 115 | - if (size % 8 != 0) { | |
| 116 | - throw new IllegalArgumentException("BitString size should be multiple of 8"); | |
| 117 | - } | |
| 118 | - var bits = new byte[size / 8]; | |
| 119 | - return new BitString(bits, leftToRightIndexing); | |
| 120 | - } | |
| 121 | - } | |
| 122 | - | |
| 123 | - /** | |
| 124 | - * Parser configuration for {@link BitString} | |
| 125 | - */ | |
| 126 | - public static final class Parser { | |
| 127 | - private boolean leftToRightIndexing = true; | |
| 128 | - private Base64.Decoder decoder = Base64.getDecoder(); | |
| 129 | - | |
| 130 | - private Parser() { | |
| 131 | - } | |
| 132 | - | |
| 133 | - public static Parser newInstance() { | |
| 134 | - return new Parser(); | |
| 135 | - } | |
| 136 | - | |
| 137 | - public Parser leftToRightIndexing(boolean leftToRightIndexing) { | |
| 138 | - this.leftToRightIndexing = leftToRightIndexing; | |
| 139 | - return this; | |
| 140 | - } | |
| 141 | - | |
| 142 | - public Parser decoder(Base64.Decoder decoder) { | |
| 143 | - this.decoder = decoder; | |
| 144 | - return this; | |
| 145 | - } | |
| 146 | - | |
| 147 | - public Result<BitString> parse(String encodedList) { | |
| 148 | - return Result.ofThrowable(() -> decoder.decode(encodedList)) | |
| 149 | - .compose(this::decompress) | |
| 150 | - .map(bytes -> new BitString(bytes, leftToRightIndexing)); | |
| 151 | - } | |
| 152 | - | |
| 153 | - private Result<byte[]> decompress(byte[] bytes) { | |
| 154 | - try (var inputStream = new GZIPInputStream(new ByteArrayInputStream(bytes))) { | |
| 155 | - try (var outputStream = new ByteArrayOutputStream()) { | |
| 156 | - inputStream.transferTo(outputStream); | |
| 157 | - return Result.success(outputStream.toByteArray()); | |
| 158 | - } | |
| 159 | - } catch (IOException e) { | |
| 160 | - return Result.failure("Failed to ungzip encoded list: %s".formatted(e.getMessage())); | |
| 161 | - } | |
| 162 | - } | |
| 163 | - } | |
| 164 | - | |
| 165 | - /** | |
| 166 | - * Writer configuration for {@link BitString} | |
| 167 | - */ | |
| 168 | - public static final class Writer { | |
| 169 | - private Base64.Encoder encoder = Base64.getEncoder(); | |
| 170 | - | |
| 171 | - private Writer() { | |
| 172 | - } | |
| 173 | - | |
| 174 | - public static Writer newInstance() { | |
| 175 | - return new Writer(); | |
| 176 | - } | |
| 177 | - | |
| 178 | - public Writer encoder(Base64.Encoder encoder) { | |
| 179 | - this.encoder = encoder; | |
| 180 | - return this; | |
| 181 | - } | |
| 182 | - | |
| 183 | - public Result<String> write(BitString bitString) { | |
| 184 | - return compress(bitString.bits) | |
| 185 | - .compose(compressed -> Result.ofThrowable(() -> encoder.encodeToString(compressed))); | |
| 186 | - | |
| 187 | - } | |
| 188 | - | |
| 189 | - private Result<byte[]> compress(byte[] bytes) { | |
| 190 | - try (var outputStream = new ByteArrayOutputStream()) { | |
| 191 | - try (var zipStream = new GZIPOutputStream(outputStream)) { | |
| 192 | - zipStream.write(bytes); | |
| 193 | - zipStream.close(); | |
| 194 | - return Result.success(outputStream.toByteArray()); | |
| 195 | - } | |
| 196 | - } catch (IOException e) { | |
| 197 | - return Result.failure("Failed to gzip the input bytes: %s".formatted(e.getMessage())); | |
| 198 | - } | |
| 199 | - } | |
| 200 | - } | |
| 201 | -} |
deleted spi/common/verifiable-credentials-spi/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/statuslist/StatusList2021Credential.java +0 −106
| @@ -1,106 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | - * | |
| 4 | - * This program and the accompanying materials are made available under the | |
| 5 | - * terms of the Apache License, Version 2.0 which is available at | |
| 6 | - * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | - * | |
| 8 | - * SPDX-License-Identifier: Apache-2.0 | |
| 9 | - * | |
| 10 | - * Contributors: | |
| 11 | - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | - * | |
| 13 | - */ | |
| 14 | - | |
| 15 | -package org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist; | |
| 16 | - | |
| 17 | -import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiableCredential; | |
| 18 | - | |
| 19 | -import static org.eclipse.edc.iam.verifiablecredentials.spi.VcConstants.STATUSLIST_2021_PREFIX; | |
| 20 | - | |
| 21 | -/** | |
| 22 | - * Represents a special {@link VerifiableCredential}, specifically a <a href="https://www.w3.org/TR/2023/WD-vc-status-list-20230427/">W3C StatusList2021</a> credential. | |
| 23 | - * That means that the shape of the {@link VerifiableCredential#getCredentialSubject()} is not arbitrary anymore, but must contain specific items. | |
| 24 | - * <p> | |
| 25 | - * Since every StatusList2021 credential is a valid {@link VerifiableCredential}, way to construct them is via the static factory method | |
| 26 | - * {@link StatusList2021Credential#parse(VerifiableCredential)}, which will throw a {@link IllegalArgumentException} if the shape is not correct. | |
| 27 | - */ | |
| 28 | -public class StatusList2021Credential extends VerifiableCredential { | |
| 29 | - public static final String STATUSLIST_2021_TYPE = "StatusList2021"; | |
| 30 | - public static final String STATUSLIST_2021_CREDENTIAL = STATUSLIST_2021_TYPE + "Credential"; | |
| 31 | - public static final String STATUS_LIST_ENCODED_LIST_LITERAL = "encodedList"; | |
| 32 | - public static final String STATUS_LIST_ENCODED_LIST = STATUSLIST_2021_PREFIX + STATUS_LIST_ENCODED_LIST_LITERAL; | |
| 33 | - public static final String STATUS_LIST_CREDENTIAL_LITERAL = "statusListCredential"; | |
| 34 | - public static final String STATUS_LIST_CREDENTIAL = STATUSLIST_2021_PREFIX + STATUS_LIST_CREDENTIAL_LITERAL; | |
| 35 | - public static final String STATUS_LIST_INDEX_LITERAL = "statusListIndex"; | |
| 36 | - public static final String STATUS_LIST_INDEX = STATUSLIST_2021_PREFIX + STATUS_LIST_INDEX_LITERAL; | |
| 37 | - public static final String STATUS_LIST_PURPOSE_LITERAL = "statusPurpose"; | |
| 38 | - public static final String STATUS_LIST_PURPOSE = STATUSLIST_2021_PREFIX + STATUS_LIST_PURPOSE_LITERAL; | |
| 39 | - | |
| 40 | - private StatusList2021Credential() { | |
| 41 | - } | |
| 42 | - | |
| 43 | - public static StatusList2021Credential parse(VerifiableCredential rawCredential) { | |
| 44 | - return StatusList2021Credential.Builder.newInstance() | |
| 45 | - .credentialStatus(rawCredential.getCredentialStatus()) | |
| 46 | - .id(rawCredential.getId()) | |
| 47 | - .credentialSubjects(rawCredential.getCredentialSubject()) | |
| 48 | - .name(rawCredential.getName()) | |
| 49 | - .types(rawCredential.getType()) | |
| 50 | - .description(rawCredential.getDescription()) | |
| 51 | - .issuanceDate(rawCredential.getIssuanceDate()) | |
| 52 | - .issuer(rawCredential.getIssuer()) | |
| 53 | - .expirationDate(rawCredential.getExpirationDate()) | |
| 54 | - .build(); | |
| 55 | - } | |
| 56 | - | |
| 57 | - public String encodedList() { | |
| 58 | - return (String) credentialSubject.get(0).getClaim(STATUSLIST_2021_PREFIX, STATUS_LIST_ENCODED_LIST_LITERAL); | |
| 59 | - } | |
| 60 | - | |
| 61 | - public String statusPurpose() { | |
| 62 | - return (String) credentialSubject.get(0).getClaim(STATUSLIST_2021_PREFIX, STATUS_LIST_PURPOSE_LITERAL); | |
| 63 | - } | |
| 64 | - | |
| 65 | - public static class Builder extends VerifiableCredential.Builder<StatusList2021Credential, Builder> { | |
| 66 | - protected Builder(StatusList2021Credential credential) { | |
| 67 | - super(credential); | |
| 68 | - } | |
| 69 | - | |
| 70 | - public static Builder newInstance() { | |
| 71 | - return new Builder(new StatusList2021Credential()); | |
| 72 | - } | |
| 73 | - | |
| 74 | - @Override | |
| 75 | - public StatusList2021Credential build() { | |
| 76 | - super.build(); | |
| 77 | - if (!instance.type.contains(STATUSLIST_2021_CREDENTIAL)) { | |
| 78 | - throw new IllegalArgumentException("Only %s is supported, but encountered: %s".formatted(STATUSLIST_2021_CREDENTIAL, instance.type)); | |
| 79 | - } | |
| 80 | - | |
| 81 | - if (instance.credentialSubject == null || instance.credentialSubject.isEmpty()) { | |
| 82 | - throw new IllegalArgumentException("Status list credentials must contain a 'credentialSubject' property."); | |
| 83 | - } | |
| 84 | - if (instance.credentialSubject.size() != 1) { | |
| 85 | - throw new IllegalArgumentException("Status list credentials must contain exactly 1 'credentialSubject' object, but found %d".formatted(instance.credentialSubject.size())); | |
| 86 | - } | |
| 87 | - | |
| 88 | - // check mandatory fields of the credentialSubject object | |
| 89 | - var subject = instance.credentialSubject.get(0); | |
| 90 | - if (subject.getClaim(STATUSLIST_2021_PREFIX, STATUS_LIST_ENCODED_LIST_LITERAL) == null) { | |
| 91 | - throw new IllegalArgumentException("Status list credentials must contain a 'credentialSubject.encodedList' field."); | |
| 92 | - } | |
| 93 | - if (subject.getClaim(STATUSLIST_2021_PREFIX, STATUS_LIST_PURPOSE_LITERAL) == null) { | |
| 94 | - throw new IllegalArgumentException("Status list credentials must contain a 'credentialSubject.statusPurpose' field."); | |
| 95 | - } | |
| 96 | - return instance; | |
| 97 | - } | |
| 98 | - | |
| 99 | - @Override | |
| 100 | - protected Builder self() { | |
| 101 | - return this; | |
| 102 | - } | |
| 103 | - } | |
| 104 | - | |
| 105 | - | |
| 106 | -} |
deleted spi/common/verifiable-credentials-spi/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/statuslist/StatusListStatus.java +0 −79
| @@ -1,79 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | - * | |
| 4 | - * This program and the accompanying materials are made available under the | |
| 5 | - * terms of the Apache License, Version 2.0 which is available at | |
| 6 | - * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | - * | |
| 8 | - * SPDX-License-Identifier: Apache-2.0 | |
| 9 | - * | |
| 10 | - * Contributors: | |
| 11 | - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | - * | |
| 13 | - */ | |
| 14 | - | |
| 15 | -package org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist; | |
| 16 | - | |
| 17 | -import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 18 | - | |
| 19 | -import java.util.Map; | |
| 20 | - | |
| 21 | -import static java.util.Optional.ofNullable; | |
| 22 | -import static org.eclipse.edc.iam.verifiablecredentials.spi.VcConstants.STATUSLIST_2021_PREFIX; | |
| 23 | - | |
| 24 | -/** | |
| 25 | - * Specialized {@code credentialStatus}, that contains information mandated by the StatusList2021 standard. | |
| 26 | - */ | |
| 27 | -public class StatusListStatus { | |
| 28 | - | |
| 29 | - private String statusListPurpose; | |
| 30 | - private int statusListIndex; | |
| 31 | - private String statusListCredential; | |
| 32 | - | |
| 33 | - private StatusListStatus() { | |
| 34 | - } | |
| 35 | - | |
| 36 | - public static StatusListStatus parse(CredentialStatus status) { | |
| 37 | - var instance = new StatusListStatus(); | |
| 38 | - instance.statusListCredential = ofNullable(getId(status)) | |
| 39 | - .map(Object::toString) | |
| 40 | - .orElseThrow(() -> new IllegalArgumentException(missingProperty(StatusList2021Credential.STATUS_LIST_CREDENTIAL))); | |
| 41 | - | |
| 42 | - instance.statusListIndex = ofNullable(status.getProperty(STATUSLIST_2021_PREFIX, StatusList2021Credential.STATUS_LIST_INDEX_LITERAL)) | |
| 43 | - .map(Object::toString) | |
| 44 | - .map(Integer::parseInt) | |
| 45 | - .orElseThrow(() -> new IllegalArgumentException(missingProperty(StatusList2021Credential.STATUS_LIST_INDEX))); | |
| 46 | - | |
| 47 | - instance.statusListPurpose = ofNullable(status.getProperty(STATUSLIST_2021_PREFIX, StatusList2021Credential.STATUS_LIST_PURPOSE_LITERAL)) | |
| 48 | - .map(Object::toString) | |
| 49 | - .orElseThrow(() -> new IllegalArgumentException(missingProperty(StatusList2021Credential.STATUS_LIST_PURPOSE))); | |
| 50 | - | |
| 51 | - return instance; | |
| 52 | - } | |
| 53 | - | |
| 54 | - private static Object getId(CredentialStatus status) { | |
| 55 | - var credentialId = status.getProperty(STATUSLIST_2021_PREFIX, StatusList2021Credential.STATUS_LIST_CREDENTIAL_LITERAL); | |
| 56 | - if (credentialId instanceof Map<?, ?> map) { | |
| 57 | - return map.get("@id"); | |
| 58 | - } | |
| 59 | - return credentialId; | |
| 60 | - } | |
| 61 | - | |
| 62 | - private static String missingProperty(String property) { | |
| 63 | - return "A StatusList2021 credential must have a credentialStatus object with the '%s' property".formatted(property); | |
| 64 | - } | |
| 65 | - | |
| 66 | - public String getStatusListPurpose() { | |
| 67 | - return statusListPurpose; | |
| 68 | - } | |
| 69 | - | |
| 70 | - public int getStatusListIndex() { | |
| 71 | - return statusListIndex; | |
| 72 | - } | |
| 73 | - | |
| 74 | - public String getStatusListCredential() { | |
| 75 | - return statusListCredential; | |
| 76 | - } | |
| 77 | - | |
| 78 | - | |
| 79 | -} |
added spi/common/verifiable-credentials-spi/src/test/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/revocation/bitstringstatuslist/BitstringStatusListCredentialTest.java +119 −0
| @@ -0,0 +1,119 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist; | |
| 16 | + | |
| 17 | +import org.assertj.core.api.ThrowableAssert; | |
| 18 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialSubject; | |
| 19 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.Issuer; | |
| 20 | +import org.junit.jupiter.api.Test; | |
| 21 | + | |
| 22 | +import java.time.Instant; | |
| 23 | +import java.util.List; | |
| 24 | +import java.util.Map; | |
| 25 | + | |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 27 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; | |
| 28 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListCredential.STATUS_LIST_ENCODED_LIST; | |
| 29 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListCredential.STATUS_LIST_TTL; | |
| 30 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus.STATUS_LIST_PURPOSE; | |
| 31 | + | |
| 32 | +class BitstringStatusListCredentialTest { | |
| 33 | + @Test | |
| 34 | + void parseBitStringStatusList() { | |
| 35 | + // values taken from https://www.w3.org/TR/vc-bitstring-status-list/#example-example-bitstringstatuslistcredential | |
| 36 | + //noinspection unchecked | |
| 37 | + var credential = BitstringStatusListCredential.Builder.newInstance() | |
| 38 | + .id("https://example.com/credentials/status/3") | |
| 39 | + .types(List.of("VerifiableCredential", "BitstringStatusListCredential")) | |
| 40 | + .issuer(new Issuer("did:example:12345", Map.of())) | |
| 41 | + .issuanceDate(Instant.parse("2021-04-05T14:27:40Z")) | |
| 42 | + .credentialSubject(CredentialSubject.Builder.newInstance() | |
| 43 | + .id("https://example.com/status/3#list") | |
| 44 | + .claim("type", "BitstringStatusList") | |
| 45 | + .claim(STATUS_LIST_PURPOSE, "revocation") | |
| 46 | + .claim(STATUS_LIST_ENCODED_LIST, "uH4sIAAAAAAAAA-3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA") | |
| 47 | + .claim(STATUS_LIST_TTL, 123_456) | |
| 48 | + .build()) | |
| 49 | + .build(); | |
| 50 | + | |
| 51 | + | |
| 52 | + assertThat(credential.encodedList()).isNotNull(); | |
| 53 | + assertThat(credential.statusPurpose()).isEqualTo("revocation"); | |
| 54 | + assertThat(credential.validFrom()).isEqualTo(Instant.parse("2021-04-05T14:27:40Z")); | |
| 55 | + assertThat(credential.validUntil()).isNull(); | |
| 56 | + assertThat(credential.ttl()).hasMillis(123_456); | |
| 57 | + } | |
| 58 | + | |
| 59 | + @Test | |
| 60 | + void parse_useDefaultValue_ifTtlNotPresent() { | |
| 61 | + // values taken from https://www.w3.org/TR/vc-bitstring-status-list/#example-example-bitstringstatuslistcredential | |
| 62 | + //noinspection unchecked | |
| 63 | + var credential = BitstringStatusListCredential.Builder.newInstance() | |
| 64 | + .id("https://example.com/credentials/status/3") | |
| 65 | + .types(List.of("VerifiableCredential", "BitstringStatusListCredential")) | |
| 66 | + .issuer(new Issuer("did:example:12345", Map.of())) | |
| 67 | + .issuanceDate(Instant.parse("2021-04-05T14:27:40Z")) | |
| 68 | + .credentialSubject(CredentialSubject.Builder.newInstance() | |
| 69 | + .id("https://example.com/status/3#list") | |
| 70 | + .claim("type", "BitstringStatusList") | |
| 71 | + .claim(STATUS_LIST_PURPOSE, "revocation") | |
| 72 | + .claim(STATUS_LIST_ENCODED_LIST, "uH4sIAAAAAAAAA-3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA") | |
| 73 | + .build()) | |
| 74 | + .build(); | |
| 75 | + | |
| 76 | + | |
| 77 | + assertThat(credential.ttl()).hasMillis(300_000); | |
| 78 | + } | |
| 79 | + | |
| 80 | + @Test | |
| 81 | + void parse_noPurpose_expectException() { | |
| 82 | + ThrowableAssert.ThrowingCallable action = () -> BitstringStatusListCredential.Builder.newInstance() | |
| 83 | + .id("https://example.com/credentials/status/3") | |
| 84 | + .types(List.of("VerifiableCredential", "BitstringStatusListCredential")) | |
| 85 | + .issuer(new Issuer("did:example:12345", Map.of())) | |
| 86 | + .issuanceDate(Instant.parse("2021-04-05T14:27:40Z")) | |
| 87 | + .credentialSubject(CredentialSubject.Builder.newInstance() | |
| 88 | + .id("https://example.com/status/3#list") | |
| 89 | + .claim("type", "BitstringStatusList") | |
| 90 | + // .claim(STATUS_LIST_PURPOSE, "revocation") | |
| 91 | + .claim(STATUS_LIST_ENCODED_LIST, "uH4sIAAAAAAAAA-3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA") | |
| 92 | + .claim(STATUS_LIST_TTL, 123_456) | |
| 93 | + .build()) | |
| 94 | + .build(); | |
| 95 | + | |
| 96 | + assertThatThrownBy(action).isInstanceOf(IllegalArgumentException.class); | |
| 97 | + } | |
| 98 | + | |
| 99 | + @Test | |
| 100 | + void parse_noEncodedList_expectException() { | |
| 101 | + //noinspection unchecked | |
| 102 | + ThrowableAssert.ThrowingCallable action = () -> BitstringStatusListCredential.Builder.newInstance() | |
| 103 | + .id("https://example.com/credentials/status/3") | |
| 104 | + .types(List.of("VerifiableCredential", "BitstringStatusListCredential")) | |
| 105 | + .issuer(new Issuer("did:example:12345", Map.of())) | |
| 106 | + .issuanceDate(Instant.parse("2021-04-05T14:27:40Z")) | |
| 107 | + .credentialSubject(CredentialSubject.Builder.newInstance() | |
| 108 | + .id("https://example.com/status/3#list") | |
| 109 | + .claim("type", "BitstringStatusList") | |
| 110 | + .claim(STATUS_LIST_PURPOSE, "revocation") | |
| 111 | + //.claim(STATUS_LIST_ENCODED_LIST, "uH4sIAAAAAAAAA-3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA") | |
| 112 | + .claim(STATUS_LIST_TTL, 123_456) | |
| 113 | + .build()) | |
| 114 | + .build(); | |
| 115 | + | |
| 116 | + assertThatThrownBy(action).isInstanceOf(IllegalArgumentException.class); | |
| 117 | + } | |
| 118 | + | |
| 119 | +} | |
| No newline at the end of the file |
added spi/common/verifiable-credentials-spi/src/test/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/revocation/bitstringstatuslist/BitstringStatusListStatusTest.java +210 −0
| @@ -0,0 +1,210 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist; | |
| 16 | + | |
| 17 | + | |
| 18 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 19 | +import org.junit.jupiter.api.Test; | |
| 20 | +import org.junit.jupiter.params.ParameterizedTest; | |
| 21 | +import org.junit.jupiter.params.provider.ValueSource; | |
| 22 | + | |
| 23 | +import java.util.List; | |
| 24 | +import java.util.Map; | |
| 25 | + | |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 27 | +import static org.assertj.core.api.Assertions.assertThatNoException; | |
| 28 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; | |
| 29 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus.STATUS_LIST_CREDENTIAL; | |
| 30 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus.STATUS_LIST_INDEX; | |
| 31 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus.STATUS_LIST_MESSAGES; | |
| 32 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus.STATUS_LIST_PURPOSE; | |
| 33 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus.STATUS_LIST_REFERENCE; | |
| 34 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus.STATUS_LIST_SIZE; | |
| 35 | + | |
| 36 | +class BitstringStatusListStatusTest { | |
| 37 | + | |
| 38 | + @Test | |
| 39 | + void parse_fullySpecified() { | |
| 40 | + var props = Map.of( | |
| 41 | + STATUS_LIST_PURPOSE, "revocation", | |
| 42 | + STATUS_LIST_INDEX, "237", | |
| 43 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3", | |
| 44 | + STATUS_LIST_MESSAGES, | |
| 45 | + List.of(new StatusMessage("0x0", "foo"), | |
| 46 | + new StatusMessage("0x1", "bar"), | |
| 47 | + new StatusMessage("0x2", "qooz"), | |
| 48 | + new StatusMessage("0x3", "qaz")), | |
| 49 | + STATUS_LIST_SIZE, "2", | |
| 50 | + STATUS_LIST_REFERENCE, List.of("ref1", "ref2") | |
| 51 | + ); | |
| 52 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", BitstringStatusListStatus.TYPE, props); | |
| 53 | + | |
| 54 | + var parsed = BitstringStatusListStatus.from(credentialStatus); | |
| 55 | + assertThat(parsed.getStatusListCredential()).isEqualTo("https://example.com/credentials/status/3"); | |
| 56 | + assertThat(parsed.getStatusListIndex()).isEqualTo(237); | |
| 57 | + assertThat(parsed.getStatusListPurpose()).isEqualTo("revocation"); | |
| 58 | + assertThat(parsed.getStatusMessage()).hasSize(4); | |
| 59 | + assertThat(parsed.getStatusReference()).hasSize(2); | |
| 60 | + } | |
| 61 | + | |
| 62 | + @Test | |
| 63 | + void parse_sparselyPopulated() { | |
| 64 | + var props = Map.of( | |
| 65 | + STATUS_LIST_PURPOSE, "revocation", | |
| 66 | + STATUS_LIST_INDEX, "237", | |
| 67 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3", | |
| 68 | + STATUS_LIST_MESSAGES, | |
| 69 | + List.of(new StatusMessage("0x0", "foo"), | |
| 70 | + new StatusMessage("0x1", "bar")), | |
| 71 | + STATUS_LIST_SIZE, "1" | |
| 72 | + ); | |
| 73 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", BitstringStatusListStatus.TYPE, props); | |
| 74 | + | |
| 75 | + var parsed = BitstringStatusListStatus.from(credentialStatus); | |
| 76 | + assertThat(parsed.getStatusListCredential()).isEqualTo("https://example.com/credentials/status/3"); | |
| 77 | + assertThat(parsed.getStatusListIndex()).isEqualTo(237); | |
| 78 | + assertThat(parsed.getStatusListPurpose()).isEqualTo("revocation"); | |
| 79 | + assertThat(parsed.getStatusMessage()).hasSize(2); | |
| 80 | + } | |
| 81 | + | |
| 82 | + @Test | |
| 83 | + void parse_missingPurpose() { | |
| 84 | + Map<String, Object> props = Map.of( | |
| 85 | + //"statusPurpose", "revocation", | |
| 86 | + STATUS_LIST_INDEX, "237", | |
| 87 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3" | |
| 88 | + ); | |
| 89 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", BitstringStatusListStatus.TYPE, props); | |
| 90 | + assertThatThrownBy(() -> BitstringStatusListStatus.from(credentialStatus)) | |
| 91 | + .isInstanceOf(IllegalArgumentException.class) | |
| 92 | + .hasMessageContaining("statusPurpose"); | |
| 93 | + } | |
| 94 | + | |
| 95 | + @Test | |
| 96 | + void parse_missingIndex() { | |
| 97 | + Map<String, Object> props = Map.of( | |
| 98 | + STATUS_LIST_PURPOSE, "revocation", | |
| 99 | + //"statusListIndex", "237", | |
| 100 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3" | |
| 101 | + ); | |
| 102 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", BitstringStatusListStatus.TYPE, props); | |
| 103 | + assertThatThrownBy(() -> BitstringStatusListStatus.from(credentialStatus)).isInstanceOf(IllegalArgumentException.class) | |
| 104 | + .hasMessageContaining("statusListIndex"); | |
| 105 | + } | |
| 106 | + | |
| 107 | + @Test | |
| 108 | + void parse_missingCredential() { | |
| 109 | + Map<String, Object> props = Map.of( | |
| 110 | + STATUS_LIST_PURPOSE, "revocation", | |
| 111 | + STATUS_LIST_INDEX, "237" | |
| 112 | + // "statusListCredential", "https://example.com/credentials/status/3" | |
| 113 | + ); | |
| 114 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", BitstringStatusListStatus.TYPE, props); | |
| 115 | + assertThatThrownBy(() -> BitstringStatusListStatus.from(credentialStatus)).isInstanceOf(IllegalArgumentException.class) | |
| 116 | + .hasMessageContaining("statusListCredential"); | |
| 117 | + } | |
| 118 | + | |
| 119 | + @Test | |
| 120 | + void parse_statusSize_doesNotMatch_statusMessageLength_expectException() { | |
| 121 | + var props = Map.of( | |
| 122 | + STATUS_LIST_PURPOSE, "revocation", | |
| 123 | + STATUS_LIST_INDEX, "237", | |
| 124 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3", | |
| 125 | + STATUS_LIST_MESSAGES, | |
| 126 | + List.of(new StatusMessage("0x0", "foo"), | |
| 127 | + new StatusMessage("0x1", "bar"), | |
| 128 | + new StatusMessage("0x2", "qooz"), | |
| 129 | + new StatusMessage("0x3", "qaz")), | |
| 130 | + STATUS_LIST_SIZE, "3" // would expect 8 statusMessage entries | |
| 131 | + ); | |
| 132 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", BitstringStatusListStatus.TYPE, props); | |
| 133 | + | |
| 134 | + assertThatThrownBy(() -> BitstringStatusListStatus.from(credentialStatus)).isInstanceOf(IllegalArgumentException.class) | |
| 135 | + .hasMessageContaining("If present, statusSize (in bits) must be equal to the number of possible statusMessage entries"); | |
| 136 | + } | |
| 137 | + | |
| 138 | + @Test | |
| 139 | + void parse_statusSizeNotSpecified_statusMessageSpecified_expectException() { | |
| 140 | + var props = Map.of( | |
| 141 | + STATUS_LIST_PURPOSE, "revocation", | |
| 142 | + STATUS_LIST_INDEX, "237", | |
| 143 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3", | |
| 144 | + STATUS_LIST_MESSAGES, | |
| 145 | + List.of(new StatusMessage("0x0", "foo"), | |
| 146 | + new StatusMessage("0x1", "bar"), | |
| 147 | + new StatusMessage("0x2", "qooz"), | |
| 148 | + new StatusMessage("0x3", "qaz")) | |
| 149 | + //STATUS_LIST_SIZE, "3" | |
| 150 | + ); | |
| 151 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", BitstringStatusListStatus.TYPE, props); | |
| 152 | + | |
| 153 | + assertThatThrownBy(() -> BitstringStatusListStatus.from(credentialStatus)).isInstanceOf(IllegalArgumentException.class) | |
| 154 | + .hasMessage("statusSize must be specified and > 1 if statusMessage is present."); | |
| 155 | + } | |
| 156 | + | |
| 157 | + | |
| 158 | + @Test | |
| 159 | + void parse_statusSizeSpecified_statusMessageNotSpecified_succeeds() { | |
| 160 | + Map<String, Object> props = Map.of( | |
| 161 | + STATUS_LIST_PURPOSE, "revocation", | |
| 162 | + STATUS_LIST_INDEX, "237", | |
| 163 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3", | |
| 164 | + STATUS_LIST_SIZE, "1" | |
| 165 | + ); | |
| 166 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", BitstringStatusListStatus.TYPE, props); | |
| 167 | + | |
| 168 | + assertThatNoException().isThrownBy(() -> BitstringStatusListStatus.from(credentialStatus)); | |
| 169 | + } | |
| 170 | + | |
| 171 | + @ParameterizedTest(name = "statusSize (bits): {0}") | |
| 172 | + @ValueSource(ints = { 1, 2, 10, 1000, Integer.MAX_VALUE }) | |
| 173 | + void parse_statusSizeInvalid_statusMessageNotSpecified_succeeds(int statusSize) { | |
| 174 | + Map<String, Object> props = Map.of( | |
| 175 | + STATUS_LIST_PURPOSE, "revocation", | |
| 176 | + STATUS_LIST_INDEX, "237", | |
| 177 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3", | |
| 178 | + STATUS_LIST_SIZE, statusSize | |
| 179 | + ); | |
| 180 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", BitstringStatusListStatus.TYPE, props); | |
| 181 | + assertThatNoException().isThrownBy(() -> BitstringStatusListStatus.from(credentialStatus)); | |
| 182 | + } | |
| 183 | + | |
| 184 | + @Test | |
| 185 | + void parse_statusSizeNotSpecified_statusMessageNotSpecified_shouldSucceed() { | |
| 186 | + Map<String, Object> props = Map.of( | |
| 187 | + STATUS_LIST_PURPOSE, "revocation", | |
| 188 | + STATUS_LIST_INDEX, "237", | |
| 189 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3" | |
| 190 | + ); | |
| 191 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", BitstringStatusListStatus.TYPE, props); | |
| 192 | + | |
| 193 | + assertThatNoException().isThrownBy(() -> BitstringStatusListStatus.from(credentialStatus)); | |
| 194 | + } | |
| 195 | + | |
| 196 | + @ParameterizedTest(name = "Invalid statusSize {0}") | |
| 197 | + @ValueSource(ints = { 0, -1, -10 }) | |
| 198 | + void parse_statusSizeNegative_expectException(Object size) { | |
| 199 | + Map<String, Object> props = Map.of( | |
| 200 | + STATUS_LIST_PURPOSE, "revocation", | |
| 201 | + STATUS_LIST_INDEX, "237", | |
| 202 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3", | |
| 203 | + STATUS_LIST_SIZE, size | |
| 204 | + ); | |
| 205 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", BitstringStatusListStatus.TYPE, props); | |
| 206 | + | |
| 207 | + assertThatThrownBy(() -> BitstringStatusListStatus.from(credentialStatus)).isInstanceOf(IllegalArgumentException.class) | |
| 208 | + .hasMessage("If present, statusSize must be a positive integer > 0 but was '%s'.".formatted(size)); | |
| 209 | + } | |
| 210 | +} | |
| No newline at the end of the file |
added spi/common/verifiable-credentials-spi/src/test/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/revocation/statuslist2021/BitStringTest.java +192 −0
| @@ -0,0 +1,192 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.BitString; | |
| 18 | +import org.junit.jupiter.api.Nested; | |
| 19 | +import org.junit.jupiter.api.Test; | |
| 20 | +import org.junit.jupiter.api.extension.ExtensionContext; | |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; | |
| 22 | +import org.junit.jupiter.params.provider.Arguments; | |
| 23 | +import org.junit.jupiter.params.provider.ArgumentsProvider; | |
| 24 | +import org.junit.jupiter.params.provider.ArgumentsSource; | |
| 25 | + | |
| 26 | +import java.io.ByteArrayInputStream; | |
| 27 | +import java.io.ByteArrayOutputStream; | |
| 28 | +import java.io.IOException; | |
| 29 | +import java.util.Arrays; | |
| 30 | +import java.util.Base64; | |
| 31 | +import java.util.stream.Stream; | |
| 32 | +import java.util.zip.GZIPInputStream; | |
| 33 | + | |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 35 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; | |
| 36 | + | |
| 37 | +public class BitStringTest { | |
| 38 | + | |
| 39 | + private static Base64.Decoder getDecoder(Format format) { | |
| 40 | + return switch (format) { | |
| 41 | + case Base64 -> Base64.getDecoder(); | |
| 42 | + case Base64Url -> Base64.getUrlDecoder(); | |
| 43 | + }; | |
| 44 | + } | |
| 45 | + | |
| 46 | + private static Base64.Encoder getEncoder(Format format) { | |
| 47 | + return switch (format) { | |
| 48 | + case Base64 -> Base64.getEncoder(); | |
| 49 | + case Base64Url -> Base64.getUrlEncoder(); | |
| 50 | + }; | |
| 51 | + } | |
| 52 | + | |
| 53 | + @Test | |
| 54 | + void get() { | |
| 55 | + | |
| 56 | + var bitString = BitString.Builder.newInstance().build(); | |
| 57 | + | |
| 58 | + assertThat(bitString.get(0)).isFalse(); | |
| 59 | + assertThat(bitString.get(50_000)).isFalse(); | |
| 60 | + | |
| 61 | + bitString.set(0, true); | |
| 62 | + bitString.set(50_000, true); | |
| 63 | + | |
| 64 | + assertThat(bitString.get(0)).isTrue(); | |
| 65 | + assertThat(bitString.get(50_000)).isTrue(); | |
| 66 | + | |
| 67 | + bitString.set(0, false); | |
| 68 | + bitString.set(50_000, false); | |
| 69 | + | |
| 70 | + assertThat(bitString.get(0)).isFalse(); | |
| 71 | + assertThat(bitString.get(50_000)).isFalse(); | |
| 72 | + } | |
| 73 | + | |
| 74 | + @Test | |
| 75 | + void get_whenOutOfBound() { | |
| 76 | + | |
| 77 | + var bitString = BitString.Builder.newInstance().build(); | |
| 78 | + | |
| 79 | + assertThatThrownBy(() -> bitString.get(200_000)).isInstanceOf(IllegalArgumentException.class); | |
| 80 | + assertThatThrownBy(() -> bitString.get(-10)).isInstanceOf(IllegalArgumentException.class); | |
| 81 | + } | |
| 82 | + | |
| 83 | + @Test | |
| 84 | + void set_whenOutOfBound() { | |
| 85 | + | |
| 86 | + var bitString = BitString.Builder.newInstance().build(); | |
| 87 | + | |
| 88 | + assertThatThrownBy(() -> bitString.set(200_000, true)).isInstanceOf(IllegalArgumentException.class); | |
| 89 | + assertThatThrownBy(() -> bitString.set(-10, true)).isInstanceOf(IllegalArgumentException.class); | |
| 90 | + } | |
| 91 | + | |
| 92 | + @Test | |
| 93 | + void build_invalidSize() { | |
| 94 | + assertThatThrownBy(() -> BitString.Builder.newInstance().size(10).build()).isInstanceOf(IllegalArgumentException.class); | |
| 95 | + } | |
| 96 | + | |
| 97 | + enum Format { | |
| 98 | + Base64, | |
| 99 | + Base64Url | |
| 100 | + } | |
| 101 | + | |
| 102 | + private static class ValidEncodedListProvider implements ArgumentsProvider { | |
| 103 | + | |
| 104 | + @Override | |
| 105 | + public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | |
| 106 | + return Stream.of( | |
| 107 | + // Base64 decoder | |
| 108 | + Arguments.of("H4sIAAAAAAAAA+3BMQEAAADCoPVPbQsvoAAAAAAAAAAAAAAAAP4GcwM92tQwAAA=", 100_000, new int[]{}, Format.Base64, true), | |
| 109 | + Arguments.of("H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA", 131072, new int[]{ 0, 2 }, Format.Base64, true), | |
| 110 | + Arguments.of("H4sIAAAAAAAAA+3OMQ0AAAgDsElHOh72EJJWQRMAAAAAAIDWXAcAAAAAAIDHFvRitn7UMAAA", 100_000, new int[]{ 50_000 }, Format.Base64, true), | |
| 111 | + Arguments.of("H4sIAAAAAAAAA+3BIQEAAAACIP1/2hkWoAEAAAAAAAAAAAAAAAAAAADeBjn7xTYAQAAA", 131072, new int[]{ 7 }, Format.Base64, true), | |
| 112 | + // Base64 URL decoder | |
| 113 | + Arguments.of("H4sIAAAAAAAAA-3BMQEAAADCoPVPbQsvoAAAAAAAAAAAAAAAAP4GcwM92tQwAAA", 100_000, new int[]{}, Format.Base64Url, true), | |
| 114 | + Arguments.of("H4sIAAAAAAAAA-3BIQEAAAACIP-vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA", 131072, new int[]{ 0, 2 }, Format.Base64Url, true), | |
| 115 | + Arguments.of("H4sIAAAAAAAAA-3OMQ0AAAgDsElHOh72EJJWQRMAAAAAAIDWXAcAAAAAAIDHFvRitn7UMAAA", 100_000, new int[]{ 50_000 }, Format.Base64Url, true), | |
| 116 | + Arguments.of("H4sIAAAAAAAAA-3BIQEAAAACIP1_2hkWoAEAAAAAAAAAAAAAAAAAAADeBjn7xTYAQAAA", 131072, new int[]{ 7 }, Format.Base64Url, true), | |
| 117 | + | |
| 118 | + // Left to right = false | |
| 119 | + Arguments.of("H4sIAAAAAAAA_-3AIQEAAAACIIv_LzvDAg0AAAAAAAAAAAAAAAAAAADwNgZXEi0AQAAA", 131072, new int[]{ 0, 2 }, Format.Base64Url, false) | |
| 120 | + ); | |
| 121 | + } | |
| 122 | + } | |
| 123 | + | |
| 124 | + @Nested | |
| 125 | + class Parse { | |
| 126 | + | |
| 127 | + @ParameterizedTest | |
| 128 | + @ArgumentsSource(ValidEncodedListProvider.class) | |
| 129 | + void parse(String list, int size, int[] revoked, Format format, boolean leftToRightIndexing) { | |
| 130 | + | |
| 131 | + var result = BitString.Parser.newInstance().decoder(getDecoder(format)).leftToRightIndexing(leftToRightIndexing).parse(list); | |
| 132 | + | |
| 133 | + assertThat(result.succeeded()).isTrue(); | |
| 134 | + assertThat(result.getContent()).satisfies(bitString -> { | |
| 135 | + assertThat(bitString.length()).isEqualTo(size); | |
| 136 | + Arrays.stream(revoked).forEach((idx) -> assertThat(bitString.get(idx)).isTrue()); | |
| 137 | + }); | |
| 138 | + } | |
| 139 | + | |
| 140 | + @Test | |
| 141 | + void parse_invalidBas64() { | |
| 142 | + | |
| 143 | + var result = BitString.Parser.newInstance().parse("invalid-"); | |
| 144 | + assertThat(result.failed()).isTrue(); | |
| 145 | + assertThat(result.getFailureDetail()).contains("Illegal base64 character"); | |
| 146 | + } | |
| 147 | + | |
| 148 | + @Test | |
| 149 | + void parse_invalidGzip() { | |
| 150 | + | |
| 151 | + var result = BitString.Parser.newInstance().parse("invalid/gzip"); | |
| 152 | + assertThat(result.failed()).isTrue(); | |
| 153 | + assertThat(result.getFailureDetail()).contains("Failed to ungzip encoded list: Not in GZIP format"); | |
| 154 | + } | |
| 155 | + } | |
| 156 | + | |
| 157 | + @Nested | |
| 158 | + class Write { | |
| 159 | + | |
| 160 | + @ParameterizedTest | |
| 161 | + @ArgumentsSource(ValidEncodedListProvider.class) | |
| 162 | + void write(String list, int size, int[] revoked, Format format, boolean leftToRightIndexing) { | |
| 163 | + | |
| 164 | + var bitString = BitString.Builder.newInstance().size(size).leftToRightIndexing(leftToRightIndexing).build(); | |
| 165 | + assertThat(bitString.length()).isEqualTo(size); | |
| 166 | + | |
| 167 | + Arrays.stream(revoked).forEach((idx) -> bitString.set(idx, true)); | |
| 168 | + Arrays.stream(revoked).forEach((idx) -> assertThat(bitString.get(idx)).isTrue()); | |
| 169 | + | |
| 170 | + var result = BitString.Writer.newInstance().encoder(getEncoder(format)).write(bitString); | |
| 171 | + | |
| 172 | + var decoder = getDecoder(format); | |
| 173 | + assertThat(result.succeeded()).isTrue(); | |
| 174 | + assertThat(decode(list, decoder)).isEqualTo(decode(result.getContent(), decoder)); | |
| 175 | + } | |
| 176 | + | |
| 177 | + private byte[] decode(String list, Base64.Decoder decoder) { | |
| 178 | + return decompress(decoder.decode(list)); | |
| 179 | + } | |
| 180 | + | |
| 181 | + private byte[] decompress(byte[] bytes) { | |
| 182 | + try (var inputStream = new GZIPInputStream(new ByteArrayInputStream(bytes))) { | |
| 183 | + try (var outputStream = new ByteArrayOutputStream()) { | |
| 184 | + inputStream.transferTo(outputStream); | |
| 185 | + return outputStream.toByteArray(); | |
| 186 | + } | |
| 187 | + } catch (IOException e) { | |
| 188 | + throw new RuntimeException(e); | |
| 189 | + } | |
| 190 | + } | |
| 191 | + } | |
| 192 | +} |
added spi/common/verifiable-credentials-spi/src/test/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/revocation/statuslist2021/StatusList2021CredentialTest.java +93 −0
| @@ -0,0 +1,93 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021; | |
| 16 | + | |
| 17 | +import org.assertj.core.api.ThrowableAssert; | |
| 18 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialSubject; | |
| 19 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.Issuer; | |
| 20 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListCredential; | |
| 21 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.bitstringstatuslist.BitstringStatusListStatus; | |
| 22 | +import org.junit.jupiter.api.Test; | |
| 23 | + | |
| 24 | +import java.time.Instant; | |
| 25 | +import java.util.List; | |
| 26 | +import java.util.Map; | |
| 27 | + | |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 29 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; | |
| 30 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021.StatusList2021Credential.STATUS_LIST_ENCODED_LIST; | |
| 31 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021.StatusList2021Status.STATUS_LIST_PURPOSE; | |
| 32 | + | |
| 33 | +public class StatusList2021CredentialTest { | |
| 34 | + | |
| 35 | + @Test | |
| 36 | + void parseStatusList2021() { | |
| 37 | + // values taken from https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#statuslist2021credential | |
| 38 | + //noinspection unchecked | |
| 39 | + var statusListCred = StatusList2021Credential.Builder.newInstance() | |
| 40 | + .id("https://example.com/credentials/status/3") | |
| 41 | + .types(List.of("VerifiableCredential", "StatusList2021Credential")) | |
| 42 | + .issuer(new Issuer("did:example:12345", Map.of())) | |
| 43 | + .issuanceDate(Instant.parse("2021-04-05T14:27:40Z")) | |
| 44 | + .credentialSubject(CredentialSubject.Builder.newInstance() | |
| 45 | + .id("https://example.com/status/3#list") | |
| 46 | + .claim("type", "StatusList2021") | |
| 47 | + .claim(STATUS_LIST_PURPOSE, "revocation") | |
| 48 | + .claim(STATUS_LIST_ENCODED_LIST, "H4sIAAAAAAAAA-3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA") | |
| 49 | + .build()) | |
| 50 | + .build(); | |
| 51 | + | |
| 52 | + | |
| 53 | + assertThat(statusListCred.encodedList()).isNotNull(); | |
| 54 | + assertThat(statusListCred.statusPurpose()).isEqualTo("revocation"); | |
| 55 | + } | |
| 56 | + | |
| 57 | + @Test | |
| 58 | + void parse_noPurpose_expectException() { | |
| 59 | + ThrowableAssert.ThrowingCallable action = () -> StatusList2021Credential.Builder.newInstance() | |
| 60 | + .id("https://example.com/credentials/status/3") | |
| 61 | + .types(List.of("VerifiableCredential", "StatusList2021Credential")) | |
| 62 | + .issuer(new Issuer("did:example:12345", Map.of())) | |
| 63 | + .issuanceDate(Instant.parse("2021-04-05T14:27:40Z")) | |
| 64 | + .credentialSubject(CredentialSubject.Builder.newInstance() | |
| 65 | + .id("https://example.com/status/3#list") | |
| 66 | + .claim("type", "BitstringStatusList") | |
| 67 | + // .claim(STATUS_LIST_PURPOSE, "revocation") | |
| 68 | + .claim(BitstringStatusListCredential.STATUS_LIST_ENCODED_LIST, "uH4sIAAAAAAAAA-3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA") | |
| 69 | + .build()) | |
| 70 | + .build(); | |
| 71 | + | |
| 72 | + assertThatThrownBy(action).isInstanceOf(IllegalArgumentException.class); | |
| 73 | + } | |
| 74 | + | |
| 75 | + @Test | |
| 76 | + void parse_noEncodedList_expectException() { | |
| 77 | + ThrowableAssert.ThrowingCallable action = () -> StatusList2021Credential.Builder.newInstance() | |
| 78 | + .id("https://example.com/credentials/status/3") | |
| 79 | + .types(List.of("VerifiableCredential", "StatusList2021Credential")) | |
| 80 | + .issuer(new Issuer("did:example:12345", Map.of())) | |
| 81 | + .issuanceDate(Instant.parse("2021-04-05T14:27:40Z")) | |
| 82 | + .credentialSubject(CredentialSubject.Builder.newInstance() | |
| 83 | + .id("https://example.com/status/3#list") | |
| 84 | + .claim("type", "BitstringStatusList") | |
| 85 | + .claim(BitstringStatusListStatus.STATUS_LIST_PURPOSE, "revocation") | |
| 86 | + //.claim(STATUS_LIST_ENCODED_LIST, "uH4sIAAAAAAAAA-3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA") | |
| 87 | + .build()) | |
| 88 | + .build(); | |
| 89 | + | |
| 90 | + assertThatThrownBy(action).isInstanceOf(IllegalArgumentException.class); | |
| 91 | + } | |
| 92 | + | |
| 93 | +} |
added spi/common/verifiable-credentials-spi/src/test/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/revocation/statuslist2021/StatusList2021StatusTest.java +80 −0
| @@ -0,0 +1,80 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 18 | +import org.junit.jupiter.api.Test; | |
| 19 | + | |
| 20 | +import java.util.Map; | |
| 21 | + | |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 23 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; | |
| 24 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021.StatusList2021Status.STATUS_LIST_CREDENTIAL; | |
| 25 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021.StatusList2021Status.STATUS_LIST_INDEX; | |
| 26 | +import static org.eclipse.edc.iam.verifiablecredentials.spi.model.revocation.statuslist2021.StatusList2021Status.STATUS_LIST_PURPOSE; | |
| 27 | + | |
| 28 | +class StatusList2021StatusTest { | |
| 29 | + | |
| 30 | + @Test | |
| 31 | + void verifyStatusList2021() { | |
| 32 | + Map<String, Object> props = Map.of( | |
| 33 | + STATUS_LIST_PURPOSE, "revocation", | |
| 34 | + STATUS_LIST_INDEX, "237", | |
| 35 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3" | |
| 36 | + ); | |
| 37 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", "StatusList2021Entry", props); | |
| 38 | + | |
| 39 | + var parsed = StatusList2021Status.from(credentialStatus); | |
| 40 | + assertThat(parsed.getStatusListCredential()).isEqualTo("https://example.com/credentials/status/3"); | |
| 41 | + assertThat(parsed.getStatusListIndex()).isEqualTo(237); | |
| 42 | + assertThat(parsed.getStatusListPurpose()).isEqualTo("revocation"); | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Test | |
| 46 | + void verifyMissingPurpose() { | |
| 47 | + Map<String, Object> props = Map.of( | |
| 48 | + //"statusPurpose", "revocation", | |
| 49 | + STATUS_LIST_INDEX, "237", | |
| 50 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3" | |
| 51 | + ); | |
| 52 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", "StatusList2021Entry", props); | |
| 53 | + assertThatThrownBy(() -> StatusList2021Status.from(credentialStatus)).isInstanceOf(IllegalArgumentException.class) | |
| 54 | + .hasMessageContaining("statusPurpose"); | |
| 55 | + } | |
| 56 | + | |
| 57 | + @Test | |
| 58 | + void verifyMissingIndex() { | |
| 59 | + Map<String, Object> props = Map.of( | |
| 60 | + STATUS_LIST_PURPOSE, "revocation", | |
| 61 | + //"statusListIndex", "237", | |
| 62 | + STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3" | |
| 63 | + ); | |
| 64 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", "StatusList2021Entry", props); | |
| 65 | + assertThatThrownBy(() -> StatusList2021Status.from(credentialStatus)).isInstanceOf(IllegalArgumentException.class) | |
| 66 | + .hasMessageContaining("statusListIndex"); | |
| 67 | + } | |
| 68 | + | |
| 69 | + @Test | |
| 70 | + void verifyMissingCredential() { | |
| 71 | + Map<String, Object> props = Map.of( | |
| 72 | + STATUS_LIST_PURPOSE, "revocation", | |
| 73 | + STATUS_LIST_INDEX, "237" | |
| 74 | + // "statusListCredential", "https://example.com/credentials/status/3" | |
| 75 | + ); | |
| 76 | + var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", "StatusList2021Entry", props); | |
| 77 | + assertThatThrownBy(() -> StatusList2021Status.from(credentialStatus)).isInstanceOf(IllegalArgumentException.class) | |
| 78 | + .hasMessageContaining("statusListCredential"); | |
| 79 | + } | |
| 80 | +} | |
| No newline at the end of the file |
deleted spi/common/verifiable-credentials-spi/src/test/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/statuslist/BitStringTest.java +0 −191
| @@ -1,191 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | - * | |
| 4 | - * This program and the accompanying materials are made available under the | |
| 5 | - * terms of the Apache License, Version 2.0 which is available at | |
| 6 | - * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | - * | |
| 8 | - * SPDX-License-Identifier: Apache-2.0 | |
| 9 | - * | |
| 10 | - * Contributors: | |
| 11 | - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | - * | |
| 13 | - */ | |
| 14 | - | |
| 15 | -package org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist; | |
| 16 | - | |
| 17 | -import org.junit.jupiter.api.Nested; | |
| 18 | -import org.junit.jupiter.api.Test; | |
| 19 | -import org.junit.jupiter.api.extension.ExtensionContext; | |
| 20 | -import org.junit.jupiter.params.ParameterizedTest; | |
| 21 | -import org.junit.jupiter.params.provider.Arguments; | |
| 22 | -import org.junit.jupiter.params.provider.ArgumentsProvider; | |
| 23 | -import org.junit.jupiter.params.provider.ArgumentsSource; | |
| 24 | - | |
| 25 | -import java.io.ByteArrayInputStream; | |
| 26 | -import java.io.ByteArrayOutputStream; | |
| 27 | -import java.io.IOException; | |
| 28 | -import java.util.Arrays; | |
| 29 | -import java.util.Base64; | |
| 30 | -import java.util.stream.Stream; | |
| 31 | -import java.util.zip.GZIPInputStream; | |
| 32 | - | |
| 33 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 34 | -import static org.assertj.core.api.Assertions.assertThatThrownBy; | |
| 35 | - | |
| 36 | -public class BitStringTest { | |
| 37 | - | |
| 38 | - private static Base64.Decoder getDecoder(Format format) { | |
| 39 | - return switch (format) { | |
| 40 | - case Base64 -> Base64.getDecoder(); | |
| 41 | - case Base64Url -> Base64.getUrlDecoder(); | |
| 42 | - }; | |
| 43 | - } | |
| 44 | - | |
| 45 | - private static Base64.Encoder getEncoder(Format format) { | |
| 46 | - return switch (format) { | |
| 47 | - case Base64 -> Base64.getEncoder(); | |
| 48 | - case Base64Url -> Base64.getUrlEncoder(); | |
| 49 | - }; | |
| 50 | - } | |
| 51 | - | |
| 52 | - @Test | |
| 53 | - void get() { | |
| 54 | - | |
| 55 | - var bitString = BitString.Builder.newInstance().build(); | |
| 56 | - | |
| 57 | - assertThat(bitString.get(0)).isFalse(); | |
| 58 | - assertThat(bitString.get(50_000)).isFalse(); | |
| 59 | - | |
| 60 | - bitString.set(0, true); | |
| 61 | - bitString.set(50_000, true); | |
| 62 | - | |
| 63 | - assertThat(bitString.get(0)).isTrue(); | |
| 64 | - assertThat(bitString.get(50_000)).isTrue(); | |
| 65 | - | |
| 66 | - bitString.set(0, false); | |
| 67 | - bitString.set(50_000, false); | |
| 68 | - | |
| 69 | - assertThat(bitString.get(0)).isFalse(); | |
| 70 | - assertThat(bitString.get(50_000)).isFalse(); | |
| 71 | - } | |
| 72 | - | |
| 73 | - @Test | |
| 74 | - void get_whenOutOfBound() { | |
| 75 | - | |
| 76 | - var bitString = BitString.Builder.newInstance().build(); | |
| 77 | - | |
| 78 | - assertThatThrownBy(() -> bitString.get(200_000)).isInstanceOf(IllegalArgumentException.class); | |
| 79 | - assertThatThrownBy(() -> bitString.get(-10)).isInstanceOf(IllegalArgumentException.class); | |
| 80 | - } | |
| 81 | - | |
| 82 | - @Test | |
| 83 | - void set_whenOutOfBound() { | |
| 84 | - | |
| 85 | - var bitString = BitString.Builder.newInstance().build(); | |
| 86 | - | |
| 87 | - assertThatThrownBy(() -> bitString.set(200_000, true)).isInstanceOf(IllegalArgumentException.class); | |
| 88 | - assertThatThrownBy(() -> bitString.set(-10, true)).isInstanceOf(IllegalArgumentException.class); | |
| 89 | - } | |
| 90 | - | |
| 91 | - @Test | |
| 92 | - void build_invalidSize() { | |
| 93 | - assertThatThrownBy(() -> BitString.Builder.newInstance().size(10).build()).isInstanceOf(IllegalArgumentException.class); | |
| 94 | - } | |
| 95 | - | |
| 96 | - enum Format { | |
| 97 | - Base64, | |
| 98 | - Base64Url | |
| 99 | - } | |
| 100 | - | |
| 101 | - private static class ValidEncodedListProvider implements ArgumentsProvider { | |
| 102 | - | |
| 103 | - @Override | |
| 104 | - public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | |
| 105 | - return Stream.of( | |
| 106 | - // Base64 decoder | |
| 107 | - Arguments.of("H4sIAAAAAAAAA+3BMQEAAADCoPVPbQsvoAAAAAAAAAAAAAAAAP4GcwM92tQwAAA=", 100_000, new int[]{}, Format.Base64, true), | |
| 108 | - Arguments.of("H4sIAAAAAAAAA+3BIQEAAAACIP+vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA", 131072, new int[]{ 0, 2 }, Format.Base64, true), | |
| 109 | - Arguments.of("H4sIAAAAAAAAA+3OMQ0AAAgDsElHOh72EJJWQRMAAAAAAIDWXAcAAAAAAIDHFvRitn7UMAAA", 100_000, new int[]{ 50_000 }, Format.Base64, true), | |
| 110 | - Arguments.of("H4sIAAAAAAAAA+3BIQEAAAACIP1/2hkWoAEAAAAAAAAAAAAAAAAAAADeBjn7xTYAQAAA", 131072, new int[]{ 7 }, Format.Base64, true), | |
| 111 | - // Base64 URL decoder | |
| 112 | - Arguments.of("H4sIAAAAAAAAA-3BMQEAAADCoPVPbQsvoAAAAAAAAAAAAAAAAP4GcwM92tQwAAA", 100_000, new int[]{}, Format.Base64Url, true), | |
| 113 | - Arguments.of("H4sIAAAAAAAAA-3BIQEAAAACIP-vcKozLEADAAAAAAAAAAAAAAAAAAAAvA0cOP65AEAAAA", 131072, new int[]{ 0, 2 }, Format.Base64Url, true), | |
| 114 | - Arguments.of("H4sIAAAAAAAAA-3OMQ0AAAgDsElHOh72EJJWQRMAAAAAAIDWXAcAAAAAAIDHFvRitn7UMAAA", 100_000, new int[]{ 50_000 }, Format.Base64Url, true), | |
| 115 | - Arguments.of("H4sIAAAAAAAAA-3BIQEAAAACIP1_2hkWoAEAAAAAAAAAAAAAAAAAAADeBjn7xTYAQAAA", 131072, new int[]{ 7 }, Format.Base64Url, true), | |
| 116 | - | |
| 117 | - // Left to right = false | |
| 118 | - Arguments.of("H4sIAAAAAAAA_-3AIQEAAAACIIv_LzvDAg0AAAAAAAAAAAAAAAAAAADwNgZXEi0AQAAA", 131072, new int[]{ 0, 2 }, Format.Base64Url, false) | |
| 119 | - ); | |
| 120 | - } | |
| 121 | - } | |
| 122 | - | |
| 123 | - @Nested | |
| 124 | - class Parse { | |
| 125 | - | |
| 126 | - @ParameterizedTest | |
| 127 | - @ArgumentsSource(ValidEncodedListProvider.class) | |
| 128 | - void parse(String list, int size, int[] revoked, Format format, boolean leftToRightIndexing) { | |
| 129 | - | |
| 130 | - var result = BitString.Parser.newInstance().decoder(getDecoder(format)).leftToRightIndexing(leftToRightIndexing).parse(list); | |
| 131 | - | |
| 132 | - assertThat(result.succeeded()).isTrue(); | |
| 133 | - assertThat(result.getContent()).satisfies(bitString -> { | |
| 134 | - assertThat(bitString.length()).isEqualTo(size); | |
| 135 | - Arrays.stream(revoked).forEach((idx) -> assertThat(bitString.get(idx)).isTrue()); | |
| 136 | - }); | |
| 137 | - } | |
| 138 | - | |
| 139 | - @Test | |
| 140 | - void parse_invalidBas64() { | |
| 141 | - | |
| 142 | - var result = BitString.Parser.newInstance().parse("invalid-"); | |
| 143 | - assertThat(result.failed()).isTrue(); | |
| 144 | - assertThat(result.getFailureDetail()).contains("Illegal base64 character"); | |
| 145 | - } | |
| 146 | - | |
| 147 | - @Test | |
| 148 | - void parse_invalidGzip() { | |
| 149 | - | |
| 150 | - var result = BitString.Parser.newInstance().parse("invalid/gzip"); | |
| 151 | - assertThat(result.failed()).isTrue(); | |
| 152 | - assertThat(result.getFailureDetail()).contains("Failed to ungzip encoded list: Not in GZIP format"); | |
| 153 | - } | |
| 154 | - } | |
| 155 | - | |
| 156 | - @Nested | |
| 157 | - class Write { | |
| 158 | - | |
| 159 | - @ParameterizedTest | |
| 160 | - @ArgumentsSource(ValidEncodedListProvider.class) | |
| 161 | - void write(String list, int size, int[] revoked, Format format, boolean leftToRightIndexing) { | |
| 162 | - | |
| 163 | - var bitString = BitString.Builder.newInstance().size(size).leftToRightIndexing(leftToRightIndexing).build(); | |
| 164 | - assertThat(bitString.length()).isEqualTo(size); | |
| 165 | - | |
| 166 | - Arrays.stream(revoked).forEach((idx) -> bitString.set(idx, true)); | |
| 167 | - Arrays.stream(revoked).forEach((idx) -> assertThat(bitString.get(idx)).isTrue()); | |
| 168 | - | |
| 169 | - var result = BitString.Writer.newInstance().encoder(getEncoder(format)).write(bitString); | |
| 170 | - | |
| 171 | - var decoder = getDecoder(format); | |
| 172 | - assertThat(result.succeeded()).isTrue(); | |
| 173 | - assertThat(decode(list, decoder)).isEqualTo(decode(result.getContent(), decoder)); | |
| 174 | - } | |
| 175 | - | |
| 176 | - private byte[] decode(String list, Base64.Decoder decoder) { | |
| 177 | - return decompress(decoder.decode(list)); | |
| 178 | - } | |
| 179 | - | |
| 180 | - private byte[] decompress(byte[] bytes) { | |
| 181 | - try (var inputStream = new GZIPInputStream(new ByteArrayInputStream(bytes))) { | |
| 182 | - try (var outputStream = new ByteArrayOutputStream()) { | |
| 183 | - inputStream.transferTo(outputStream); | |
| 184 | - return outputStream.toByteArray(); | |
| 185 | - } | |
| 186 | - } catch (IOException e) { | |
| 187 | - throw new RuntimeException(e); | |
| 188 | - } | |
| 189 | - } | |
| 190 | - } | |
| 191 | -} |
deleted spi/common/verifiable-credentials-spi/src/test/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/statuslist/StatusList2021CredentialTest.java +0 −75
| @@ -1,75 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | - * | |
| 4 | - * This program and the accompanying materials are made available under the | |
| 5 | - * terms of the Apache License, Version 2.0 which is available at | |
| 6 | - * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | - * | |
| 8 | - * SPDX-License-Identifier: Apache-2.0 | |
| 9 | - * | |
| 10 | - * Contributors: | |
| 11 | - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | - * | |
| 13 | - */ | |
| 14 | - | |
| 15 | -package org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist; | |
| 16 | - | |
| 17 | -import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialSubject; | |
| 18 | -import org.eclipse.edc.iam.verifiablecredentials.spi.model.Issuer; | |
| 19 | -import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiableCredential; | |
| 20 | -import org.junit.jupiter.api.Test; | |
| 21 | - | |
| 22 | -import java.time.Instant; | |
| 23 | -import java.util.List; | |
| 24 | -import java.util.Map; | |
| 25 | - | |
| 26 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 27 | -import static org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist.StatusList2021Credential.STATUS_LIST_ENCODED_LIST; | |
| 28 | -import static org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist.StatusList2021Credential.STATUS_LIST_PURPOSE; | |
| 29 | - | |
| 30 | -public class StatusList2021CredentialTest { | |
| 31 | - | |
| 32 | - @Test | |
| 33 | - void parseStatusList2021() { | |
| 34 | - // values taken from https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#statuslist2021credential | |
| 35 | - var credential = VerifiableCredential.Builder.newInstance() | |
| 36 | - .id("https://example.com/credentials/status/3") | |
| 37 | - .types(List.of("VerifiableCredential", "StatusList2021Credential")) | |
| 38 | - .issuer(new Issuer("did:example:12345", Map.of())) | |
| 39 | - .issuanceDate(Instant.parse("2021-04-05T14:27:40Z")) | |
| 40 | - .credentialSubject(CredentialSubject.Builder.newInstance() | |
| 41 | - .id("https://example.com/status/3#list") | |
| 42 | - .claim("type", "StatusList2021") | |
| 43 | - .claim(STATUS_LIST_PURPOSE, "revocation") | |
| 44 | - .claim(STATUS_LIST_ENCODED_LIST, "H4sIAAAAAAAAA-3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA") | |
| 45 | - .build()) | |
| 46 | - .build(); | |
| 47 | - | |
| 48 | - var statusListCred = StatusList2021Credential.parse(credential); | |
| 49 | - | |
| 50 | - assertThat(statusListCred.encodedList()).isNotNull(); | |
| 51 | - assertThat(statusListCred.statusPurpose()).isEqualTo("revocation"); | |
| 52 | - } | |
| 53 | - | |
| 54 | - @Test | |
| 55 | - void parseBitStringStatusList() { | |
| 56 | - // values taken from https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#statuslist2021credential | |
| 57 | - var credential = VerifiableCredential.Builder.newInstance() | |
| 58 | - .id("https://example.com/credentials/status/3") | |
| 59 | - .types(List.of("VerifiableCredential", "StatusList2021Credential")) | |
| 60 | - .issuer(new Issuer("did:example:12345", Map.of())) | |
| 61 | - .issuanceDate(Instant.parse("2021-04-05T14:27:40Z")) | |
| 62 | - .credentialSubject(CredentialSubject.Builder.newInstance() | |
| 63 | - .id("https://example.com/status/3#list") | |
| 64 | - .claim("type", "StatusList2021") | |
| 65 | - .claim(STATUS_LIST_PURPOSE, "revocation") | |
| 66 | - .claim(STATUS_LIST_ENCODED_LIST, "H4sIAAAAAAAAA-3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA") | |
| 67 | - .build()) | |
| 68 | - .build(); | |
| 69 | - | |
| 70 | - var statusListCred = StatusList2021Credential.parse(credential); | |
| 71 | - | |
| 72 | - assertThat(statusListCred.encodedList()).isNotNull(); | |
| 73 | - assertThat(statusListCred.statusPurpose()).isEqualTo("revocation"); | |
| 74 | - } | |
| 75 | -} |
deleted spi/common/verifiable-credentials-spi/src/test/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/statuslist/StatusListStatusTest.java +0 −80
| @@ -1,80 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | - * | |
| 4 | - * This program and the accompanying materials are made available under the | |
| 5 | - * terms of the Apache License, Version 2.0 which is available at | |
| 6 | - * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | - * | |
| 8 | - * SPDX-License-Identifier: Apache-2.0 | |
| 9 | - * | |
| 10 | - * Contributors: | |
| 11 | - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | - * | |
| 13 | - */ | |
| 14 | - | |
| 15 | -package org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist; | |
| 16 | - | |
| 17 | -import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus; | |
| 18 | -import org.junit.jupiter.api.Test; | |
| 19 | - | |
| 20 | -import java.util.Map; | |
| 21 | - | |
| 22 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 23 | -import static org.assertj.core.api.Assertions.assertThatThrownBy; | |
| 24 | -import static org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist.StatusList2021Credential.STATUS_LIST_CREDENTIAL; | |
| 25 | -import static org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist.StatusList2021Credential.STATUS_LIST_INDEX; | |
| 26 | -import static org.eclipse.edc.iam.verifiablecredentials.spi.model.statuslist.StatusList2021Credential.STATUS_LIST_PURPOSE; | |
| 27 | - | |
| 28 | -class StatusListStatusTest { | |
| 29 | - | |
| 30 | - @Test | |
| 31 | - void verifyStatusList2021() { | |
| 32 | - Map<String, Object> props = Map.of( | |
| 33 | - STATUS_LIST_PURPOSE, "revocation", | |
| 34 | - STATUS_LIST_INDEX, "237", | |
| 35 | - STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3" | |
| 36 | - ); | |
| 37 | - var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", "StatusList2021Entry", props); | |
| 38 | - | |
| 39 | - var parsed = StatusListStatus.parse(credentialStatus); | |
| 40 | - assertThat(parsed.getStatusListCredential()).isEqualTo("https://example.com/credentials/status/3"); | |
| 41 | - assertThat(parsed.getStatusListIndex()).isEqualTo(237); | |
| 42 | - assertThat(parsed.getStatusListPurpose()).isEqualTo("revocation"); | |
| 43 | - } | |
| 44 | - | |
| 45 | - @Test | |
| 46 | - void verifyMissingPurpose() { | |
| 47 | - Map<String, Object> props = Map.of( | |
| 48 | - //"statusPurpose", "revocation", | |
| 49 | - STATUS_LIST_INDEX, "237", | |
| 50 | - STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3" | |
| 51 | - ); | |
| 52 | - var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", "StatusList2021Entry", props); | |
| 53 | - assertThatThrownBy(() -> StatusListStatus.parse(credentialStatus)).isInstanceOf(IllegalArgumentException.class) | |
| 54 | - .hasMessageContaining("statusPurpose"); | |
| 55 | - } | |
| 56 | - | |
| 57 | - @Test | |
| 58 | - void verifyMissingIndex() { | |
| 59 | - Map<String, Object> props = Map.of( | |
| 60 | - STATUS_LIST_PURPOSE, "revocation", | |
| 61 | - //"statusListIndex", "237", | |
| 62 | - STATUS_LIST_CREDENTIAL, "https://example.com/credentials/status/3" | |
| 63 | - ); | |
| 64 | - var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", "StatusList2021Entry", props); | |
| 65 | - assertThatThrownBy(() -> StatusListStatus.parse(credentialStatus)).isInstanceOf(IllegalArgumentException.class) | |
| 66 | - .hasMessageContaining("statusListIndex"); | |
| 67 | - } | |
| 68 | - | |
| 69 | - @Test | |
| 70 | - void verifyMissingCredential() { | |
| 71 | - Map<String, Object> props = Map.of( | |
| 72 | - STATUS_LIST_PURPOSE, "revocation", | |
| 73 | - STATUS_LIST_INDEX, "237" | |
| 74 | - // "statusListCredential", "https://example.com/credentials/status/3" | |
| 75 | - ); | |
| 76 | - var credentialStatus = new CredentialStatus("https://example.com/credentials/status/3#94567", "StatusList2021Entry", props); | |
| 77 | - assertThatThrownBy(() -> StatusListStatus.parse(credentialStatus)).isInstanceOf(IllegalArgumentException.class) | |
| 78 | - .hasMessageContaining("statusListCredential"); | |
| 79 | - } | |
| 80 | -} | |
| No newline at the end of the file |