Repository navigation
25940 support credentials issuance through oid4vci #27931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mposolda
merged 35 commits into
keycloak:main
from
wistefan:25940-support-credentials-issuance-through-oid4vci
Apr 22, 2024
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
7537291
Implement the oid4vci endpoints
wistefan 491c22c
Implement the oid4vci endpoints
wistefan 91b4f51
implement oid4vci endpoint
wistefan 6ba49cd
implement tests
wistefan f0a09e4
implement tests
wistefan 56ed471
add documentation and update tests
wistefan cba5370
add documentation and update tests
wistefan 4f662b3
code quality improvements
wistefan 7d8ba5c
fix doc
wistefan 63e3421
update pojos, make collections immutable
wistefan 61130c6
fix mapper initialization
wistefan da73e5b
more review cleanups
wistefan c5d99cb
update doc reference
wistefan cb50832
make it immutable
wistefan b97de0f
fix merge conflicts
wistefan d1820e1
Implement the oid4vci endpoints
wistefan d4428a8
Implement the oid4vci endpoints
wistefan 5f32992
implement oid4vci endpoint
wistefan 9b62a61
implement tests
wistefan 035a83b
implement tests
wistefan d2d3708
add documentation and update tests
wistefan 2275871
add documentation and update tests
wistefan b82e13c
update pojos, make collections immutable
wistefan d7b1c64
fix mapper initialization
wistefan 9a2040c
fix merge conflicts
wistefan 08fbfab
fix test
wistefan f8467c2
fix proof related values
wistefan a85a2fe
fix dependency issues
wistefan 7beca5e
fix mapper instantiation, add comments
wistefan 3372d3c
restart test after feature activation
wistefan a65798d
minor improvements
wistefan bbdb87f
fix header doc, use ClientSessionContext, code cleanups
wistefan 940c4af
update error types
wistefan 42f5903
cleanups
wistefan 0d88edc
more cleanups
wistefan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
169 changes: 169 additions & 0 deletions
169
services/src/main/java/org/keycloak/protocol/oid4vc/OID4VCClientRegistrationProvider.java
This file contains hidden or 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,169 @@ | ||
| /* | ||
| * Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.protocol.oid4vc; | ||
|
|
||
| import jakarta.ws.rs.Consumes; | ||
| import jakarta.ws.rs.DELETE; | ||
| import jakarta.ws.rs.POST; | ||
| import jakarta.ws.rs.PUT; | ||
| import jakarta.ws.rs.Path; | ||
| import jakarta.ws.rs.PathParam; | ||
| import jakarta.ws.rs.Produces; | ||
| import jakarta.ws.rs.core.MediaType; | ||
| import jakarta.ws.rs.core.Response; | ||
| import org.jboss.logging.Logger; | ||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.protocol.oid4vc.model.OID4VCClient; | ||
| import org.keycloak.protocol.oid4vc.model.SupportedCredentialConfiguration; | ||
| import org.keycloak.representations.idm.ClientRepresentation; | ||
| import org.keycloak.services.ErrorResponseException; | ||
| import org.keycloak.services.clientregistration.AbstractClientRegistrationProvider; | ||
| import org.keycloak.services.clientregistration.DefaultClientRegistrationContext; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Provides the client-registration functionality for OID4VC-clients. | ||
| * | ||
| * @author <a href="https://github.com/wistefan">Stefan Wiedemann</a> | ||
| */ | ||
| public class OID4VCClientRegistrationProvider extends AbstractClientRegistrationProvider { | ||
|
|
||
| private static final Logger LOGGER = Logger.getLogger(OID4VCClientRegistrationProvider.class); | ||
|
|
||
| private static final String VC_KEY = "vc"; | ||
|
|
||
| public OID4VCClientRegistrationProvider(KeycloakSession session) { | ||
| super(session); | ||
| } | ||
|
|
||
| @POST | ||
| @Consumes(MediaType.APPLICATION_JSON) | ||
| @Produces(MediaType.APPLICATION_JSON) | ||
| public Response createOID4VCClient(OID4VCClient client) { | ||
| ClientRepresentation clientRepresentation = toClientRepresentation(client); | ||
| validate(clientRepresentation); | ||
|
|
||
| ClientRepresentation cr = create( | ||
| new DefaultClientRegistrationContext(session, clientRepresentation, this)); | ||
| URI uri = session.getContext().getUri().getAbsolutePathBuilder().path(cr.getClientId()).build(); | ||
| return Response.created(uri).entity(cr).build(); | ||
| } | ||
|
|
||
| @PUT | ||
| @Path("{clientId}") | ||
| @Consumes(MediaType.APPLICATION_JSON) | ||
| @Produces(MediaType.APPLICATION_JSON) | ||
| public Response updateOID4VCClient(@PathParam("clientId") String clientDid, OID4VCClient client) { | ||
| client.setClientDid(clientDid); | ||
| ClientRepresentation clientRepresentation = toClientRepresentation(client); | ||
| validate(clientRepresentation); | ||
| clientRepresentation = update(clientDid, | ||
| new DefaultClientRegistrationContext(session, clientRepresentation, this)); | ||
| return Response.ok(clientRepresentation).build(); | ||
| } | ||
|
|
||
| @DELETE | ||
| @Path("{clientId}") | ||
| public Response deleteOID4VCClient(@PathParam("clientId") String clientDid) { | ||
| delete(clientDid); | ||
| return Response.noContent().build(); | ||
| } | ||
|
|
||
| /** | ||
| * Validates the clientRepresentation to fulfill the requirement of an OID4VC client | ||
| */ | ||
| public static void validate(ClientRepresentation client) { | ||
| String did = client.getClientId(); | ||
| if (did == null) { | ||
| throw new ErrorResponseException("no_did", "A client did needs to be configured for OID4VC clients", | ||
| Response.Status.BAD_REQUEST); | ||
| } | ||
| if (!did.startsWith("did:")) { | ||
| throw new ErrorResponseException("invalid_did", "The client id is not a did.", | ||
| Response.Status.BAD_REQUEST); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Translate an incoming {@link OID4VCClient} into a keycloak native {@link ClientRepresentation}. | ||
| * | ||
| * @param oid4VCClient pojo, containing the oid4vc client parameters | ||
| * @return a clientRepresentation | ||
| */ | ||
| protected static ClientRepresentation toClientRepresentation(OID4VCClient oid4VCClient) { | ||
| ClientRepresentation clientRepresentation = new ClientRepresentation(); | ||
| clientRepresentation.setProtocol(OID4VCLoginProtocolFactory.PROTOCOL_ID); | ||
|
|
||
| clientRepresentation.setId(Optional.ofNullable(oid4VCClient.getId()).orElse(UUID.randomUUID().toString())); | ||
| clientRepresentation.setClientId(oid4VCClient.getClientDid()); | ||
| // only add non-null parameters | ||
| Optional.ofNullable(oid4VCClient.getDescription()).ifPresent(clientRepresentation::setDescription); | ||
| Optional.ofNullable(oid4VCClient.getName()).ifPresent(clientRepresentation::setName); | ||
|
|
||
|
|
||
| Map<String, String> clientAttributes = oid4VCClient.getSupportedVCTypes() | ||
| .stream() | ||
| .map(SupportedCredentialConfiguration::toDotNotation) | ||
| .flatMap(dotNotated -> dotNotated.entrySet().stream()) | ||
| .collect(Collectors.toMap(entry -> VC_KEY + "." + entry.getKey(), Map.Entry::getValue, (e1, e2) -> e1)); | ||
|
|
||
| if (!clientAttributes.isEmpty()) { | ||
| clientRepresentation.setAttributes(clientAttributes); | ||
| } | ||
|
|
||
|
|
||
| LOGGER.debugf("Generated client representation {}.", clientRepresentation); | ||
| return clientRepresentation; | ||
| } | ||
|
|
||
| public static OID4VCClient fromClientAttributes(String clientId, Map<String, String> clientAttributes) { | ||
|
|
||
| OID4VCClient oid4VCClient = new OID4VCClient() | ||
| .setClientDid(clientId); | ||
|
|
||
| Set<String> supportedCredentialIds = new HashSet<>(); | ||
| Map<String, String> attributes = new HashMap<>(); | ||
| clientAttributes | ||
| .entrySet() | ||
| .forEach(entry -> { | ||
| if (!entry.getKey().startsWith(VC_KEY)) { | ||
| return; | ||
| } | ||
| String key = entry.getKey().substring((VC_KEY + ".").length()); | ||
| supportedCredentialIds.add(key.split("\\.")[0]); | ||
| attributes.put(key, entry.getValue()); | ||
| }); | ||
|
|
||
|
|
||
| List<SupportedCredentialConfiguration> supportedCredentialConfigurations = supportedCredentialIds | ||
| .stream() | ||
| .map(id -> SupportedCredentialConfiguration.fromDotNotation(id, attributes)) | ||
| .toList(); | ||
|
|
||
| return oid4VCClient.setSupportedVCTypes(supportedCredentialConfigurations); | ||
| } | ||
| } | ||
68 changes: 68 additions & 0 deletions
68
...s/src/main/java/org/keycloak/protocol/oid4vc/OID4VCClientRegistrationProviderFactory.java
This file contains hidden or 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,68 @@ | ||
| /* | ||
| * Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.protocol.oid4vc; | ||
|
|
||
| import org.keycloak.Config; | ||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.KeycloakSessionFactory; | ||
| import org.keycloak.provider.ProviderConfigProperty; | ||
| import org.keycloak.services.clientregistration.ClientRegistrationProvider; | ||
| import org.keycloak.services.clientregistration.ClientRegistrationProviderFactory; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Implementation of the {@link ClientRegistrationProviderFactory} to integrate the OID4VC protocols with | ||
| * Keycloak's client-registration. | ||
| * <p> | ||
| * {@see https://openid.net/specs/openid-4-verifiable-credential-issuance-1_0.html} | ||
| * | ||
| * @author <a href="https://github.com/wistefan">Stefan Wiedemann</a> | ||
| */ | ||
| public class OID4VCClientRegistrationProviderFactory implements ClientRegistrationProviderFactory, OID4VCEnvironmentProviderFactory { | ||
|
|
||
| @Override | ||
| public ClientRegistrationProvider create(KeycloakSession session) { | ||
| return new OID4VCClientRegistrationProvider(session); | ||
| } | ||
|
|
||
| @Override | ||
| public void init(Config.Scope config) { | ||
| // no config required | ||
| } | ||
|
|
||
| @Override | ||
| public void postInit(KeycloakSessionFactory factory) { | ||
| // nothing to do post init | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| // no resources to close | ||
| } | ||
|
|
||
| @Override | ||
| public String getId() { | ||
| return OID4VCLoginProtocolFactory.PROTOCOL_ID; | ||
| } | ||
|
|
||
| @Override | ||
| public List<ProviderConfigProperty> getConfigMetadata() { | ||
| return List.of(); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
services/src/main/java/org/keycloak/protocol/oid4vc/OID4VCEnvironmentProviderFactory.java
This file contains hidden or 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,38 @@ | ||
| /* | ||
| * Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.protocol.oid4vc; | ||
|
|
||
| import org.keycloak.Config; | ||
| import org.keycloak.common.Profile; | ||
| import org.keycloak.provider.EnvironmentDependentProviderFactory; | ||
|
|
||
| /** | ||
| * Interface for all OID4VC related provider factories, to ensure usage of the same feature flag. | ||
| */ | ||
| public interface OID4VCEnvironmentProviderFactory extends EnvironmentDependentProviderFactory { | ||
|
|
||
| @Override | ||
| default boolean isSupported(Config.Scope config) { | ||
| return Profile.isFeatureEnabled(Profile.Feature.OID4VC_VCI); | ||
| } | ||
|
|
||
| @Override | ||
| default boolean isSupported() { | ||
| return Profile.isFeatureEnabled(Profile.Feature.OID4VC_VCI); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you tell me why OID4VCI specific client
OID4VCClient is needed? Compared with a normal clientOIDCClientRepresentation,OID4VCClient' adds the following field:/** * Comma-separated list of supported credentials types */ private List<SupportedCredentialConfiguration> supportedVCTypes;If so, adding it onto
OIDCClientRepresentationis also an option.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I´m not sure if I fully understand. Beside the supportedVCTypes, it also adds the did of the client. Its used to provide the ability to create credentials focused for clients supporting OID4VC standards, not neccessarily OIDC. Mixing both might be confusing, especially if one only supports one of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the clarification.