-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f4d06e
commit 12bf948
Showing
20 changed files
with
966 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...b-did/src/main/java/org/eclipse/edc/identityhub/did/DidDocumentPublisherRegistryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2023 Metaform Systems, Inc. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Metaform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub.did; | ||
|
||
import org.eclipse.edc.identithub.did.spi.DidDocumentPublisher; | ||
import org.eclipse.edc.identithub.did.spi.DidDocumentPublisherRegistry; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* In-mem variant of the publisher registry. | ||
*/ | ||
public class DidDocumentPublisherRegistryImpl implements DidDocumentPublisherRegistry { | ||
private final Map<String, DidDocumentPublisher> publishers = new HashMap<>(); | ||
|
||
|
||
@Override | ||
public void addPublisher(String didMethodName, DidDocumentPublisher publisher) { | ||
publishers.put(didMethodName, publisher); | ||
} | ||
|
||
@Override | ||
public DidDocumentPublisher getPublisher(String did) { | ||
return publishers.get(did); | ||
} | ||
|
||
@Override | ||
public boolean canPublish(String did) { | ||
return publishers.containsKey(did); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
extensions/did-publisher/local-did-publisher/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2023 Metaform Systems, Inc. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Metaform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
plugins { | ||
`java-library` | ||
`java-test-fixtures` | ||
`maven-publish` | ||
} | ||
|
||
val swagger: String by project | ||
|
||
dependencies { | ||
|
||
api(project(":spi:identity-hub-did-spi")) | ||
implementation(libs.jakarta.rsApi) | ||
implementation(libs.edc.spi.web) | ||
|
||
testImplementation(libs.edc.junit) | ||
testImplementation(libs.restAssured) | ||
testImplementation(testFixtures(libs.edc.core.jersey)) | ||
} |
116 changes: 116 additions & 0 deletions
116
...isher/src/main/java/org/eclipse/edc/identityhub/publisher/did/local/DidWebController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (c) 2023 Metaform Systems, Inc. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Metaform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub.publisher.did.local; | ||
|
||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.core.Context; | ||
import org.eclipse.edc.iam.did.spi.document.DidDocument; | ||
import org.eclipse.edc.identithub.did.spi.DidWebParser; | ||
import org.eclipse.edc.identithub.did.spi.model.DidResource; | ||
import org.eclipse.edc.identithub.did.spi.model.DidState; | ||
import org.eclipse.edc.identithub.did.spi.store.DidResourceStore; | ||
import org.eclipse.edc.spi.EdcException; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.spi.query.Criterion; | ||
import org.eclipse.edc.spi.query.QuerySpec; | ||
import org.eclipse.edc.web.spi.exception.InvalidRequestException; | ||
|
||
import java.net.MalformedURLException; | ||
import java.nio.charset.Charset; | ||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
|
||
import static jakarta.ws.rs.core.HttpHeaders.CONTENT_TYPE; | ||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
|
||
@Consumes(APPLICATION_JSON) | ||
@Produces(APPLICATION_JSON) | ||
@Path("{any:.*}") | ||
public class DidWebController { | ||
private static final Charset DEFAULT_CHARSET = Charset.defaultCharset(); | ||
private static final Pattern CHARSET_REGEX_PATTERN = Pattern.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"]*)"); | ||
private final Monitor monitor; | ||
private final DidResourceStore didResourceStore; | ||
private final DidWebParser didWebParser = new DidWebParser(); | ||
|
||
public DidWebController(Monitor monitor, DidResourceStore didResourceStore) { | ||
this.monitor = monitor; | ||
this.didResourceStore = didResourceStore; | ||
} | ||
|
||
@GET | ||
public DidDocument getDidDocument(@Context ContainerRequestContext context) { | ||
|
||
var httpUrl = context.getUriInfo().getAbsolutePath(); | ||
|
||
var charset = extractCharset(context.getHeaderString(CONTENT_TYPE)); | ||
String did; | ||
try { | ||
did = didWebParser.parse(httpUrl.toURL(), charset); | ||
} catch (MalformedURLException e) { | ||
monitor.warning("Error interpreting request URL", e); | ||
throw new EdcException("Error interpreting request URL", e); | ||
} | ||
|
||
|
||
var q = QuerySpec.Builder.newInstance() | ||
.filter(new Criterion("state", "=", DidState.PUBLISHED.code())) | ||
.filter(new Criterion("did", "=", did)) | ||
.build(); | ||
|
||
monitor.debug("Looking up '%s'".formatted(did)); | ||
var dids = didResourceStore.query(q) | ||
.stream() | ||
.map(DidResource::getDocument) | ||
.toList(); | ||
|
||
if (dids.size() > 1) { | ||
throw new InvalidRequestException("DID '%s' resolved more than one document".formatted(did)); | ||
} | ||
|
||
return dids.stream().findFirst().orElse(null); | ||
} | ||
|
||
private Charset extractCharset(String contentType) { | ||
return Optional.ofNullable(contentType) | ||
.map(ct -> parseCharsetFromContentType(contentType)) | ||
.orElse(DEFAULT_CHARSET); | ||
} | ||
|
||
/** | ||
* Parses the "charset" attribute from the Content-Type header. Returns the value of the charset attribute, | ||
* or {@link Charset#defaultCharset()} if the attribute was not present or represents an invalid charset | ||
* | ||
* @param contentType The Content-Type header | ||
* @return The value of the charset attribute or the default charset | ||
*/ | ||
private Charset parseCharsetFromContentType(String contentType) { | ||
var m = CHARSET_REGEX_PATTERN.matcher(contentType); | ||
if (m.find()) { | ||
var cs = m.group(1).trim().toUpperCase(); | ||
if (Charset.isSupported(cs)) { | ||
return Charset.forName(cs); | ||
} else { | ||
monitor.warning("Charset '%s' is not supported, defaulting to %s".formatted(cs, DEFAULT_CHARSET)); | ||
return DEFAULT_CHARSET; | ||
} | ||
} | ||
return DEFAULT_CHARSET; | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...sher/src/main/java/org/eclipse/edc/identityhub/publisher/did/local/LocalDidPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright (c) 2023 Metaform Systems, Inc. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Metaform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub.publisher.did.local; | ||
|
||
import org.eclipse.edc.identithub.did.spi.DidConstants; | ||
import org.eclipse.edc.identithub.did.spi.DidDocumentPublisher; | ||
import org.eclipse.edc.identithub.did.spi.model.DidResource; | ||
import org.eclipse.edc.identithub.did.spi.model.DidState; | ||
import org.eclipse.edc.identithub.did.spi.store.DidResourceStore; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.spi.query.Criterion; | ||
import org.eclipse.edc.spi.query.QuerySpec; | ||
import org.eclipse.edc.spi.result.Result; | ||
|
||
import java.util.Collection; | ||
|
||
import static org.eclipse.edc.identithub.did.spi.DidConstants.DID_WEB_METHOD_REGEX; | ||
import static org.eclipse.edc.spi.result.Result.failure; | ||
import static org.eclipse.edc.spi.result.Result.success; | ||
|
||
/** | ||
* A DID publisher that maintains "did:web" documents. All documents in the database ({@link DidResourceStore}) | ||
* where the {@link DidResource#getState()} == {@link DidState#PUBLISHED} are regarded as published and are made available | ||
* through an HTTP endpoint. | ||
*/ | ||
public class LocalDidPublisher implements DidDocumentPublisher { | ||
|
||
private final DidResourceStore didResourceStore; | ||
private final Monitor monitor; | ||
|
||
public LocalDidPublisher(DidResourceStore didResourceStore, Monitor monitor) { | ||
this.didResourceStore = didResourceStore; | ||
this.monitor = monitor; | ||
} | ||
|
||
@Override | ||
public boolean canHandle(String id) { | ||
return DID_WEB_METHOD_REGEX.matcher(id).matches(); | ||
} | ||
|
||
@Override | ||
public Result<Void> publish(String did) { | ||
var existingDocument = didResourceStore.findById(did); | ||
if (existingDocument == null) { | ||
return Result.failure("A DID Resource with the ID '%s' was not found.".formatted(did)); | ||
} | ||
|
||
if (isPublished(existingDocument)) { | ||
monitor.warning("DID '%s' is already published - this action will overwrite it.".formatted(did)); | ||
} | ||
|
||
existingDocument.transitionState(DidState.PUBLISHED); | ||
|
||
return didResourceStore.update(existingDocument) | ||
.map(v -> success()) | ||
.orElse(f -> failure(f.getFailureDetail())); | ||
} | ||
|
||
@Override | ||
public Result<Void> unpublish(String did) { | ||
var existingDocument = didResourceStore.findById(did); | ||
if (existingDocument == null) { | ||
return Result.failure("A DID Resource with the ID '%s' was not found.".formatted(did)); | ||
} | ||
|
||
if (!isPublished(existingDocument)) { | ||
monitor.info("Un-publish DID Resource '%s': not published -> NOOP.".formatted(did)); | ||
// do not return early, the state could be anything | ||
} | ||
|
||
existingDocument.transitionState(DidState.UNPUBLISHED); | ||
return didResourceStore.update(existingDocument) | ||
.map(v -> success()) | ||
.orElse(f -> failure(f.getFailureDetail())); | ||
|
||
} | ||
|
||
@Override | ||
public Collection<DidResource> getPublishedDocuments() { | ||
var q = QuerySpec.Builder.newInstance() | ||
.filter(new Criterion("state", "=", DidState.PUBLISHED.code())) | ||
.filter(new Criterion("did", "like", DidConstants.DID_WEB_METHOD + "%")) | ||
.build(); | ||
|
||
return didResourceStore.query(q); | ||
} | ||
|
||
private boolean isPublished(DidResource didResource) { | ||
return didResource.getState() == DidState.PUBLISHED.code(); | ||
} | ||
} |
Oops, something went wrong.