8000 25940 support credentials issuance through oid4vci by wistefan · Pull Request #27931 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
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 Mar 11, 2024
491c22c
Implement the oid4vci endpoints
wistefan Mar 11, 2024
91b4f51
implement oid4vci endpoint
wistefan Mar 13, 2024
6ba49cd
implement tests
wistefan Mar 14, 2024
f0a09e4
implement tests
wistefan Mar 14, 2024
56ed471
add documentation and update tests
wistefan Mar 15, 2024
cba5370
add documentation and update tests
wistefan Mar 15, 2024
4f662b3
code quality improvements
wistefan Mar 15, 2024
7d8ba5c
fix doc
wistefan Mar 15, 2024
63e3421
update pojos, make collections immutable
wistefan Mar 18, 2024
61130c6
fix mapper initialization
wistefan Mar 18, 2024
da73e5b
more review cleanups
wistefan Mar 18, 2024
c5d99cb
update doc reference
wistefan Mar 18, 2024
cb50832
make it immutable
wistefan Mar 19, 2024
b97de0f
fix merge conflicts
wistefan Mar 19, 2024
d1820e1
Implement the oid4vci endpoints
wistefan Mar 11, 2024
d4428a8
Implement the oid4vci endpoints
wistefan Mar 11, 2024
5f32992
implement oid4vci endpoint
wistefan Mar 13, 2024
9b62a61
implement tests
wistefan Mar 14, 2024
035a83b
implement tests
wistefan Mar 14, 2024
d2d3708
add documentation and update tests
wistefan Mar 15, 2024
2275871
add documentation and update tests
wistefan Mar 15, 2024
b82e13c
update pojos, make collections immutable
wistefan Mar 18, 2024
d7b1c64
fix mapper initialization
wistefan Mar 18, 2024
9a2040c
fix merge conflicts
wistefan Mar 19, 2024
08fbfab
fix test
wistefan Mar 19, 2024
f8467c2
fix proof related values
wistefan Mar 20, 2024
a85a2fe
fix dependency issues
wistefan Mar 22, 2024
7beca5e
fix mapper instantiation, add comments
wistefan Apr 8, 2024
3372d3c
restart test after feature activation
wistefan Apr 8, 2024
a65798d
minor improvements
wistefan Apr 17, 2024
bbdb87f
fix header doc, use ClientSessionContext, code cleanups
wistefan Apr 17, 2024
940c4af
update error types
wistefan Apr 19, 2024
42f5903
cleanups
wistefan Apr 19, 2024
0d88edc
more cleanups
wistefan Apr 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Copy Markdown
Contributor

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 client OIDCClientRepresentation, OID4VCClient' adds the following field:

/** * Comma-separated list of supported credentials types */ private List<SupportedCredentialConfiguration> supportedVCTypes;
If so, adding it onto OIDCClientRepresentation is also an option.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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.


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);
}
}
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();
}
}
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);
}
}
Loading
0