|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Metaform Systems Inc. |
| 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 | + * Metaform Systems Inc. - initial API and implementation |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +package org.eclipse.edc.identityhub.api.credentialoffer; |
| 16 | + |
| 17 | +import jakarta.json.JsonObject; |
| 18 | +import jakarta.ws.rs.Consumes; |
| 19 | +import jakarta.ws.rs.HeaderParam; |
| 20 | +import jakarta.ws.rs.POST; |
| 21 | +import jakarta.ws.rs.Path; |
| 22 | +import jakarta.ws.rs.PathParam; |
| 23 | +import jakarta.ws.rs.Produces; |
| 24 | +import jakarta.ws.rs.core.Response; |
| 25 | +import org.eclipse.edc.identityhub.protocols.dcp.spi.DcpIssuerTokenVerifier; |
| 26 | +import org.eclipse.edc.identityhub.protocols.dcp.spi.model.CredentialOfferMessage; |
| 27 | +import org.eclipse.edc.identityhub.spi.participantcontext.ParticipantContextService; |
| 28 | +import org.eclipse.edc.spi.monitor.Monitor; |
| 29 | +import org.eclipse.edc.transform.spi.TypeTransformerRegistry; |
| 30 | +import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry; |
| 31 | +import org.eclipse.edc.web.spi.exception.AuthenticationFailedException; |
| 32 | +import org.eclipse.edc.web.spi.exception.InvalidRequestException; |
| 33 | +import org.eclipse.edc.web.spi.exception.NotAuthorizedException; |
| 34 | +import org.eclipse.edc.web.spi.exception.ValidationFailureException; |
| 35 | + |
| 36 | +import static jakarta.ws.rs.core.HttpHeaders.AUTHORIZATION; |
| 37 | +import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; |
| 38 | +import static org.eclipse.edc.iam.identitytrust.spi.DcpConstants.DSPACE_DCP_NAMESPACE_V_1_0; |
| 39 | +import static org.eclipse.edc.identityhub.protocols.dcp.spi.DcpConstants.DCP_SCOPE_V_1_0; |
| 40 | +import static org.eclipse.edc.identityhub.spi.participantcontext.ParticipantContextId.onEncoded; |
| 41 | + |
| 42 | +@Consumes(APPLICATION_JSON) |
| 43 | +@Produces(APPLICATION_JSON) |
| 44 | +@Path("/v1/participants/{participantContextId}/offers") |
| 45 | +public class CredentialOfferApiController implements CredentialOfferApi { |
| 46 | + |
| 47 | + private final JsonObjectValidatorRegistry validatorRegistry; |
| 48 | + private final TypeTransformerRegistry transformerRegistry; |
| 49 | + private final Monitor monitor; |
| 50 | + private final DcpIssuerTokenVerifier issuerTokenVerifier; |
| 51 | + private final ParticipantContextService participantContextService; |
| 52 | + |
| 53 | + public CredentialOfferApiController(JsonObjectValidatorRegistry validatorRegistry, |
| 54 | + TypeTransformerRegistry transformerRegistry, |
| 55 | + Monitor monitor, |
| 56 | + DcpIssuerTokenVerifier issuerTokenVerifier, |
| 57 | + ParticipantContextService participantContextService) { |
| 58 | + this.validatorRegistry = validatorRegistry; |
| 59 | + this.transformerRegistry = transformerRegistry; |
| 60 | + this.monitor = monitor; |
| 61 | + this.issuerTokenVerifier = issuerTokenVerifier; |
| 62 | + this.participantContextService = participantContextService; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + @POST |
| 67 | + @Override |
| 68 | + public Response offerCredential(@PathParam("participantContextId") String participantContextId, JsonObject credentialOfferMessage, @HeaderParam(AUTHORIZATION) String authHeader) { |
| 69 | + if (credentialOfferMessage == null) { |
| 70 | + throw new InvalidRequestException("Request body is null"); |
| 71 | + } |
| 72 | + if (authHeader == null) { |
| 73 | + throw new AuthenticationFailedException("Authorization header missing"); |
| 74 | + } |
| 75 | + var authToken = authHeader.replace("Bearer", "").trim(); |
| 76 | + validatorRegistry.validate(DSPACE_DCP_NAMESPACE_V_1_0.toIri(CredentialOfferMessage.CREDENTIAL_OFFER_MESSAGE_TERM), credentialOfferMessage).orElseThrow(ValidationFailureException::new); |
| 77 | + var protocolRegistry = transformerRegistry.forContext(DCP_SCOPE_V_1_0); |
| 78 | + |
| 79 | + participantContextId = onEncoded(participantContextId).orElseThrow(InvalidRequestException::new); |
| 80 | + |
| 81 | + var offerMessage = protocolRegistry.forContext(DCP_SCOPE_V_1_0).transform(credentialOfferMessage, CredentialOfferMessage.class).orElseThrow(InvalidRequestException::new); |
| 82 | + |
| 83 | + var participantContext = participantContextService.getParticipantContext(participantContextId) |
| 84 | + .orElseThrow((f) -> new AuthenticationFailedException("Invalid participant")); |
| 85 | + |
| 86 | + // validate Issuer's SI token |
| 87 | + issuerTokenVerifier.verify(participantContext, authToken) |
| 88 | + .orElseThrow(f -> new NotAuthorizedException("ID token verification failed: %s".formatted(f.getFailureDetail()))); |
| 89 | + |
| 90 | + |
| 91 | + //todo: process credential offer message |
| 92 | + monitor.warning("Credential offer was received but processing it is not yet implemented"); |
| 93 | + return Response.ok().build(); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments