-
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.
feat: implement local DidPublisher (#198)
- Loading branch information
1 parent
3b92ab0
commit b67982c
Showing
21 changed files
with
992 additions
and
35 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
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
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)) | ||
} |
110 changes: 110 additions & 0 deletions
110
...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,110 @@ | ||
/* | ||
* 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.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.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; | ||
|
||
public DidWebController(Monitor monitor, DidResourceStore didResourceStore, DidWebParser didWebParser) { | ||
this.monitor = monitor; | ||
this.didResourceStore = didResourceStore; | ||
this.didWebParser = didWebParser; | ||
} | ||
|
||
@GET | ||
public DidDocument getDidDocument(@Context ContainerRequestContext context) { | ||
|
||
var httpUrl = context.getUriInfo().getAbsolutePath(); | ||
|
||
var charset = extractCharset(context.getHeaderString(CONTENT_TYPE)); | ||
String did; | ||
did = didWebParser.parse(httpUrl, charset); | ||
|
||
|
||
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(this::parseCharsetFromContentType) | ||
.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; | ||
} | ||
} |
Oops, something went wrong.