Skip to content

Commit

Permalink
feat: implement local DidPublisher
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Dec 14, 2023
1 parent 4f4d06e commit 12bf948
Show file tree
Hide file tree
Showing 20 changed files with 966 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,37 @@
import org.eclipse.edc.web.jersey.jsonld.ObjectMapperProvider;
import org.eclipse.edc.web.spi.WebService;

import static org.eclipse.edc.identityservice.api.PresentationApiExtension.NAME;
import static org.eclipse.edc.spi.CoreConstants.JSON_LD;

@Extension(value = "Presentation API Extension")
@Extension(value = NAME)
public class PresentationApiExtension implements ServiceExtension {

public static final String NAME = "Presentation API Extension";
public static final String RESOLUTION_SCOPE = "resolution-scope";
public static final String RESOLUTION_CONTEXT = "resolution";

@Inject
private TypeTransformerRegistry typeTransformer;

@Inject
private JsonObjectValidatorRegistry validatorRegistry;

@Inject
private WebService webService;

@Inject
private AccessTokenVerifier accessTokenVerifier;

@Inject
private CredentialQueryResolver credentialResolver;

@Inject
private VerifiablePresentationService verifiablePresentationService;

@Inject
private JsonLd jsonLd;

@Inject
private TypeManager typeManager;

@Override
public String name() {
return NAME;
}

@Override
public void initialize(ServiceExtensionContext context) {
// setup validator
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@

package org.eclipse.edc.identityhub.did;

import org.eclipse.edc.identithub.did.spi.DidDocumentPublisherRegistry;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
import org.eclipse.edc.spi.system.ServiceExtension;

import static org.eclipse.edc.identityhub.did.DidServicesExtension.NAME;

@Extension(value = NAME)
public class DidServicesExtension implements ServiceExtension {
public static final String NAME = "DID Service Extension";

@Override
public String name() {
return NAME;
}

@Provider
public DidDocumentPublisherRegistry createRegistry() {
return new DidDocumentPublisherRegistryImpl();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
public class DidDefaultServicesExtension implements ServiceExtension {
public static final String NAME = "DID Default Services Extension";

@Override
public String name() {
return NAME;
}

@Provider(isDefault = true)
public DidResourceStore createInMemoryDidResourceStore() {
return new InMemoryDidResourceStore();
Expand Down
32 changes: 32 additions & 0 deletions extensions/did-publisher/local-did-publisher/build.gradle.kts
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))
}
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;
}
}
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();
}
}
Loading

0 comments on commit 12bf948

Please sign in to comment.