8000 [OID4VCI] Migrate CredentialsOffer to multiple grant types - Part2 by tdiesler · Pull Request #46947 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -40,10 +40,10 @@
import org.keycloak.protocol.oid4vc.model.ClaimsDescription;
import org.keycloak.protocol.oid4vc.model.CredentialsOffer;
import org.keycloak.protocol.oid4vc.model.OID4VCAuthorizationDetail;
import org.keycloak.protocol.oid4vc.model.PreAuthorizedCodeGrant;
import org.keycloak.protocol.oid4vc.model.SupportedCredentialConfiguration;
import org.keycloak.protocol.oid4vc.utils.ClaimsPathPointer;
import org.keycloak.protocol.oidc.grants.PreAuthorizedCodeGrantType;
import org.keycloak.protocol.oidc.grants.PreAuthorizedCodeGrantTypeFactory;
import org.keycloak.protocol.oidc.rar.AuthorizationDetailsProcessor;
import org.keycloak.protocol.oidc.rar.InvalidAuthorizationDetailsException;
import org.keycloak.representations.AuthorizationDetailsJSONRepresentation;
Expand Down Expand Up @@ -113,7 +113,7 @@ private void createOfferStateForAuthorizationCodeFlow(UserSessionModel userSessi
// Skip if we're in pre-authorized code flow (it already has an offer state that will be updated)
// Pre-authorized flow sets VC_ISSUANCE_FLOW note on the client session
String vcIssuanceFlow = clientSession.getNote(PreAuthorizedCodeGrantType.VC_ISSUANCE_FLOW);
if (vcIssuanceFlow != null && vcIssuanceFlow.equals(PreAuthorizedCodeGrantTypeFactory.GRANT_TYPE)) {
if (vcIssuanceFlow != null && vcIssuanceFlow.equals(PreAuthorizedCodeGrant.PRE_AUTH_GRANT_TYPE)) {
logger.debugf("Skipping offer state creation for pre-authorized code flow (offer state already exists and will be updated)");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,12 @@
import org.keycloak.protocol.oid4vc.model.NonceResponse;
import org.keycloak.protocol.oid4vc.model.OID4VCAuthorizationDetail;
import org.keycloak.protocol.oid4vc.model.OfferResponseType;
import org.keycloak.protocol.oid4vc.model.PreAuthorizedCode;
import org.keycloak.protocol.oid4vc.model.PreAuthorizedGrant;
import org.keycloak.protocol.oid4vc.model.PreAuthorizedCodeGrant;
import org.keycloak.protocol.oid4vc.model.Proofs;
import org.keycloak.protocol.oid4vc.model.SupportedCredentialConfiguration;
import org.keycloak.protocol.oid4vc.model.VerifiableCredential;
import org.keycloak.protocol.oid4vc.utils.ClaimsPathPointer;
import org.keycloak.protocol.oidc.grants.PreAuthorizedCodeGrantType;
import org.keycloak.protocol.oidc.grants.PreAuthorizedCodeGrantTypeFactory;
import org.keycloak.protocol.oidc.rar.AuthorizationDetailsProcessor;
import org.keycloak.representations.AccessToken;
import org.keycloak.representations.AuthorizationDetailsJSONRepresentation;
Expand Down Expand Up @@ -534,8 +532,7 @@ public Response createCredentialOffer(

if (preAuthorized) {
String code = "urn:oid4vci:code:" + SecretGenerator.getInstance().randomString(64);
credOffer.setGrants(new PreAuthorizedGrant().setPreAuthorizedCode(
new PreAuthorizedCode().setPreAuthorizedCode(code)));
credOffer.addGrant(new PreAuthorizedCodeGrant().setPreAuthorizedCode(code));
}

// Create the CredentialOfferState
Expand Down Expand Up @@ -695,7 +692,7 @@ private void checkScope(CredentialScopeModel requestedCredential) {
AuthenticatedClientSessionModel clientSession = getAuthenticatedClientSession();
String vcIssuanceFlow = clientSession.getNote(PreAuthorizedCodeGrantType.VC_ISSUANCE_FLOW);

if (vcIssuanceFlow == null || !vcIssuanceFlow.equals(PreAuthorizedCodeGrantTypeFactory.GRANT_TYPE)) {
if (vcIssuanceFlow == null || !vcIssuanceFlow.equals(PreAuthorizedCodeGrant.PRE_AUTH_GRANT_TYPE)) {
// Use getAuthResult() instead of bearerTokenAuthenticator.authenticate() directly
// This ensures we benefit from the cachedAuthResult caching that prevents DPoP proof reuse
AccessToken accessToken = getAuthResult().token();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.keycloak.common.util.Time;
import org.keycloak.protocol.oid4vc.model.CredentialsOffer;
import org.keycloak.protocol.oid4vc.model.OID4VCAuthorizationDetail;
import org.keycloak.protocol.oid4vc.model.PreAuthorizedCode;
import org.keycloak.protocol.oid4vc.model.PreAuthorizedGrant;
import org.keycloak.saml.RandomSecret;

import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down Expand Up @@ -55,9 +53,7 @@ public CredentialOfferState(CredentialsOffer credOffer, String clientId, String

@Transient
public Optional<String> getPreAuthorizedCode() {
return Optional.ofNullable(credentialsOffer.getGrants())
.map(PreAuthorizedGrant::getPreAuthorizedCode)
.map(PreAuthorizedCode::getPreAuthorizedCode);
return Optional.ofNullable(credentialsOffer.getPreAuthorizedCode());
}

public void generateTxCode() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 Red Hat, Inc. and/or its affiliates
* 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");
Expand All @@ -19,42 +19,50 @@

import java.util.Objects;

import org.keycloak.protocol.oidc.grants.PreAuthorizedCodeGrantTypeFactory;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Container for the pre-authorized code to be used in a Credential Offer
* Container for the authorization code grant to be used in a Credential Offer
* <p>
* {@see https://openid.net/specs/openid-4-verifiable-credential-issuance-1_0.html#name-credential-offer}
*
* @author <a href="https://github.com/wistefan">Stefan Wiedemann</a>
* @author <a href="mailto:tdiesler@ibm.com">Thomas Diesler</a>
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PreAuthorizedGrant {
public class AuthorizationCodeGrant implements CredentialOfferGrant {

public static final String AUTH_CODE_GRANT_TYPE = "authorization_code";
public static final String ISSUER_STATE = "issuer_state";

@Override
@JsonIgnore
public String getGrantType() {
return AUTH_CODE_GRANT_TYPE;
}

@JsonProperty(PreAuthorizedCodeGrantTypeFactory.GRANT_TYPE)
private PreAuthorizedCode preAuthorizedCode;
@JsonProperty(ISSUER_STATE)
private String issuerState;

public PreAuthorizedCode getPreAuthorizedCode() {
return preAuthorizedCode;
public String getIssuerState() {
return issuerState;
}
Comment on lines +45 to 50
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.

As per my review, this is the main point of this PR. Could you clarify how this will be leveraged (in a subsequent PR?) and what are security concerns if the wallet sends back an unexpected value? My apologies if that's asking you to repeat yourself.

@tdiesler tdiesler Mar 9, 2026 •
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.

Yes, this is part of PR #46950

Currently, the issuer state is simply the base64 encoded CredentialOfferState.Id

With so many "identifiers" floating around, it is perhaps useful to mention that this id is a server generated opaque id that identifies the internal CredentialOfferState. The offer state can now be retrieved using these three keys ...

  • CredentialOfferUri.Nonce (which is the id that the wallet uses to get the offer)
  • pre-authorized_code (which the token endpoint uses when a pre-auth code is redeemed)
  • CredentialOfferState.Id (which the authorization endpoint uses when given an issuer_state)

Also worth noting, that credential_identifier is no longer a key by which an offer state can be found. Instead it is (as intended by the spec) an identifier for a variation of claims that are available for a given credential_configuration_id. It is a property defined by the Issuer (i.e. vc.credential_identifier) on the client scope. It is no longer generated nor is it necessarily a UUID - I changed the test accordingly.

Currently, we only support a single value for this property - it should be multiple and there should be some mapping that results in different claims or claim values depending on credential_identifier

#46264

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 detailed context. It helps a lot to have CredentialOfferState.Id. I was even planning to add it for for JWT pre auth code PR I'm working on.

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.

UR welcome

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.

Currently, we only support a single value for this property - it should be multiple and there should be some mapping that results in different claims or claim values depending on credential_identifier
#46264

This can turn to be quite complicated, but depends on what exactly is in the scope for this "mapping support" . For me, the 1:1 mapping between credential_configuration_id and credential_identifier would be OK and it is sufficient to support just that for OID4VCI specification support as it is up to the Keycloak (Issuer) on what credential_identifiers it returns in the tokenResponse inside authorization_details . But support for "Authorization groups" of user profile would be likely OK . Added comment to the #46264 as I guess that is rather a place where to discuss the details of this?

< 10BC8 /div>

public PreAuthorizedGrant setPreAuthorizedCode(PreAuthorizedCode preAuthorizedCode) {
this.preAuthorizedCode = preAuthorizedCode;
public AuthorizationCodeGrant setIssuerState(String issuerState) {
this.issuerState = issuerState;
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PreAuthorizedGrant grant)) return false;
return Objects.equals(getPreAuthorizedCode(), grant.getPreAuthorizedCode());
if (!(o instanceof AuthorizationCodeGrant grant)) return false;
return Objects.equals(issuerState, grant.issuerState);
}

@Override
public int hashCode() {
return Objects.hash(getPreAuthorizedCode());
return Objects.hash(issuerState);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2026 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
Comment thread
tdiesler marked this conversation as resolved.
Resolved
*
* 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.model;


import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* Container for the grants to be used in a Credential Offer
* <p>
* {@see https://openid.net/specs/openid-4-verifiable-credential-issuance-1_0.html#name-credential-offer}
*
* @author <a href="mailto:tdiesler@ibm.com">Thomas Diesler</a>
*/
public interface CredentialOfferGrant {

@JsonIgnore
String getGrantType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.keycloak.protocol.oid4vc.model;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;

import static org.keycloak.protocol.oid4vc.model.AuthorizationCodeGrant.AUTH_CODE_GRANT_TYPE;
import static org.keycloak.protocol.oid4vc.model.PreAuthorizedCodeGrant.PRE_AUTH_GRANT_TYPE;

public final class CredentialOfferGrantsDeserializer extends JsonDeserializer<Map<String, CredentialOfferGrant>> {

@Override
public Map<String, CredentialOfferGrant> deserialize(JsonParser p, DeserializationContext ctx) throws IOException {

Map<String, CredentialOfferGrant> grants = new LinkedHashMap<>();
var mapper = (ObjectMapper) p.getCodec();
var node = mapper.readTree(p);

var fields = node.fieldNames();
while (fields.hasNext()) {
var grantType = fields.next();
var valueNode = node.get(grantType);

Class<? extends CredentialOfferGrant> target =
switch (grantType) {
case AUTH_CODE_GRANT_TYPE -> AuthorizationCodeGrant.class;
case PRE_AUTH_GRANT_TYPE -> PreAuthorizedCodeGrant.class;
default -> throw new InvalidFormatException(
p, "Unknown grant type key: " + grantType, grantType, CredentialOfferGrant.class);
};

CredentialOfferGrant grant = mapper.treeToValue(valueNode, target);
grants.put(grantType, grant);
}
return grants;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,24 @@

package org.keycloak.protocol.oid4vc.model;

import java.beans.Transient;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.keycloak.common.util.KeycloakUriBuilder;
import org.keycloak.util.JsonSerialization;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import static org.keycloak.OID4VCConstants.WELL_KNOWN_OPENID_CREDENTIAL_ISSUER;
import static org.keycloak.protocol.oid4vc.model.AuthorizationCodeGrant.AUTH_CODE_GRANT_TYPE;
import static org.keycloak.protocol.oid4vc.model.PreAuthorizedCodeGrant.PRE_AUTH_GRANT_TYPE;

/**
* Represents a CredentialsOffer according to the OID4VCI Spec
Expand All @@ -45,8 +52,9 @@ public class CredentialsOffer {
@JsonProperty("credential_configuration_ids")
private List<String> credentialConfigurationIds;

// current implementation only supports pre-authorized codes.
private PreAuthorizedGrant grants;
@JsonProperty("grants")
@JsonDeserialize(using = CredentialOfferGrantsDeserializer.class)
private Map<String, CredentialOfferGrant> grants = new HashMap<>();

public String getCredentialIssuer() {
return credentialIssuer;
Expand All @@ -57,7 +65,7 @@ public CredentialsOffer setCredentialIssuer(String credentialIssuer) {
return this;
}

@Transient
@JsonIgnore
public String getIssuerMetadataUrl() {
var metadataUrl = KeycloakUriBuilder
.fromUri(credentialIssuer)
Expand All @@ -83,24 +91,55 @@ public CredentialsOffer setCredentialConfigurationIds(List<String> credentialCon
return this;
}

public PreAuthorizedGrant getGrants() {
return grants;
public CredentialOfferGrant getGrant(String grantType) {
return grants.get(grantType);
}

public CredentialsOffer setGrants(PreAuthorizedGrant grants) {
this.grants = grants;
public CredentialsOffer addGrant(CredentialOfferGrant grant) {
grants.put(grant.getGrantType(), grant);
return this;
}

@JsonIgnore
public AuthorizationCodeGrant getAuthorizationCodeGrant() {
return (AuthorizationCodeGrant) grants.get(AUTH_CODE_GRANT_TYPE);
}

@JsonIgnore
public String getIssuerState() {
return Optional.ofNullable(getAuthorizationCodeGrant())
.map(AuthorizationCodeGrant::getIssuerState)
.orElse(null);
}

@JsonIgnore
public PreAuthorizedCodeGrant getPreAuthorizedGrant() {
return (PreAuthorizedCodeGrant) grants.get(PRE_AUTH_GRANT_TYPE);
}

@JsonIgnore
public String getPreAuthorizedCode() {
return Optional.ofNullable(getPreAuthorizedGrant())
.map(PreAuthorizedCodeGrant::getPreAuthorizedCode)
.orElse(null);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CredentialsOffer that)) return false;
return Objects.equals(getCredentialIssuer(), that.getCredentialIssuer()) && Objects.equals(getCredentialConfigurationIds(), that.getCredentialConfigurationIds()) && Objects.equals(getGrants(), that.getGrants());
boolean match = Objects.equals(credentialIssuer, that.credentialIssuer);
match &= Objects.equals(credentialConfigurationIds, that.credentialConfigurationIds);
match &= Objects.equals(grants, that.grants);
return match;
}

@Override
public int hashCode() {
return Objects.hash(getCredentialIssuer(), getCredentialConfigurationIds(), getGrants());
return Objects.hash(credentialIssuer, credentialConfigurationIds, grants);
}

public String toString() {
return JsonSerialization.valueAsString(this);
}
}
Loading
Loading
0