-
Notifications
You must be signed in to change notification settings - Fork 8.8k
[OID4VP] Introduce SD-JWT User Attribute / Session mappers #51632
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
Open
dominikschlosser
wants to merge
1
commit into
keycloak:main
Choose a base branch
from
dominikschlosser:vp-mappers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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
134 changes: 134 additions & 0 deletions
134
services/src/main/java/org/keycloak/broker/oid4vp/mappers/AbstractOID4VPClaimMapper.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,134 @@ | ||
| /* | ||
| * Copyright 2026 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.broker.oid4vp.mappers; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.keycloak.Config; | ||
| import org.keycloak.broker.oid4vp.OID4VPIdentityProvider; | ||
| import org.keycloak.broker.oid4vp.OID4VPIdentityProviderFactory; | ||
| import org.keycloak.broker.provider.AbstractIdentityProviderMapper; | ||
| import org.keycloak.broker.provider.BrokeredIdentityContext; | ||
| import org.keycloak.common.Profile; | ||
| import org.keycloak.models.IdentityProviderMapperModel; | ||
| import org.keycloak.models.IdentityProviderSyncMode; | ||
| import org.keycloak.provider.EnvironmentDependentProviderFactory; | ||
| import org.keycloak.provider.ProviderConfigProperty; | ||
| import org.keycloak.util.JsonSerialization; | ||
| import org.keycloak.utils.StringUtil; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import org.jboss.logging.Logger; | ||
|
|
||
| /** | ||
| * Base for OID4VP identity provider mappers that read a claim of the verified credential | ||
| * presentation, addressed by a {@link ClaimPath} over the claims JSON. Subclasses decide where the | ||
| * resolved values go. Not tied to a credential format: any format whose verification yields claims | ||
| * JSON under {@link OID4VPIdentityProvider#CREDENTIAL_CLAIMS} can build on it. | ||
| */ | ||
| public abstract class AbstractOID4VPClaimMapper extends AbstractIdentityProviderMapper | ||
| implements EnvironmentDependentProviderFactory { | ||
|
|
||
| protected static final Logger logger = Logger.getLogger(AbstractOID4VPClaimMapper.class); | ||
|
|
||
| public static final String CLAIM = "claim"; | ||
|
|
||
| private static final String[] COMPATIBLE_PROVIDERS = {OID4VPIdentityProviderFactory.PROVIDER_ID}; | ||
|
|
||
| protected static ProviderConfigProperty claimProperty() { | ||
| ProviderConfigProperty property = new ProviderConfigProperty(); | ||
| property.setName(CLAIM); | ||
| property.setLabel("Claim"); | ||
| property.setHelpText("Path of the claim in the presented credential. Use dot notation for nested claims, " | ||
| + "i.e. 'address.locality', [] to select all array elements, i.e. 'nationalities[]', and [0] to select the first element of the presented array. " | ||
| + "To use dot (.) literally, escape it with backslash (\\.)"); | ||
| property.setType(ProviderConfigProperty.STRING_TYPE); | ||
| return property; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getCompatibleProviders() { | ||
| return COMPATIBLE_PROVIDERS; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supportsSyncMode(IdentityProviderSyncMode syncMode) { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSupported(Config.Scope config) { | ||
| return Profile.isFeatureEnabled(Profile.Feature.OID4VC_VP); | ||
| } | ||
|
|
||
| protected JsonNode credentialClaims(BrokeredIdentityContext context) { | ||
| Object claims = context.getContextData().get(OID4VPIdentityProvider.CREDENTIAL_CLAIMS); | ||
| if (claims == null) { | ||
| return null; | ||
| } | ||
| if (claims instanceof JsonNode node) { | ||
| return node; | ||
| } | ||
| // The brokered context may have been serialized into the authentication session in | ||
| // between, for example while the first broker login shows the review profile page. | ||
| return JsonSerialization.mapper.valueToTree(claims); | ||
| } | ||
|
|
||
| // Null when the claim is absent or the mapper is misconfigured. | ||
| protected List<String> claimValues(IdentityProviderMapperModel mapperModel, BrokeredIdentityContext context) { | ||
| String claimPath = mapperModel.getConfig().get(CLAIM); | ||
| if (StringUtil.isBlank(claimPath)) { | ||
| logger.warnf("No claim configured for mapper %s", mapperModel.getName()); | ||
| return null; | ||
| } | ||
| ClaimPath path = ClaimPath.parse(claimPath.trim()); | ||
| if (path == null) { | ||
| logger.warnf("Invalid claim path '%s' in mapper %s", claimPath, mapperModel.getName()); | ||
| return null; | ||
| } | ||
| List<JsonNode> matches = path.select(credentialClaims(context)); | ||
| if (matches.isEmpty()) { | ||
| return null; | ||
| } | ||
| Iterable<JsonNode> selected = matches.size() == 1 && matches.get(0).isArray() | ||
| ? matches.get(0) | ||
| : matches; | ||
| // The values end up in the brokered context, whose serialization restores lists by their | ||
| // concrete class, so they must stay plain ArrayLists. | ||
| List<String> values = new ArrayList<>(); | ||
| for (JsonNode node : selected) { | ||
| if (!node.isNull()) { | ||
| values.add(value(node)); | ||
| } | ||
| } | ||
| return values; | ||
| } | ||
|
|
||
| protected String value(JsonNode node) { | ||
| if (node.isValueNode()) { | ||
| return node.asText(); | ||
| } | ||
| try { | ||
| return JsonSerialization.writeValueAsString(node); | ||
| } catch (IOException e) { | ||
| throw new IllegalStateException("Failed to serialize the claim value", e); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.