8000 [OID4VCI] Migrate OID4VCCredentialOfferMatrixTest by tdiesler · Pull Request #46608 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
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
3 changes: 3 additions & 0 deletions core/src/main/java/org/keycloak/OAuth2Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,7 @@ public interface OAuth2Constants {
String DPOP_JWT_HEADER_TYPE = "dpop+jwt";
String ALGS_ATTRIBUTE = "algs";

// OID4VCI - https://openid.net/specs/openid-4-verifiable-credential-issuance-1_0.html#section-5.1.3
// Used to pass the issuer_state value back to the Credential Issuer.
String ISSUER_STATE = "issuer_state";
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public String getScope() {
}

public String getCredentialConfigurationId() {
return clientScope.getAttribute(VC_CONFIGURATION_ID);
return Optional.ofNullable(clientScope.getAttribute(VC_CONFIGURATION_ID)).orElse(clientScope.getName());
}

public void setCredentialConfigurationId(String credentialConfigurationId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@
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;
Expand Down Expand Up @@ -543,8 +542,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 @@ -581,11 +579,10 @@ public Response createCredentialOffer(
.detail(Details.VERIFIABLE_CREDENTIAL_TARGET_USER_ID, targetUserId)
.success();

String redactedTxCode = !Strings.isEmpty(offerState.getTxCode()) ? "******" : null;
CredentialOfferURI credOfferURI = new CredentialOfferURI()
.setIssuer(credOffer.getCredentialIssuer() + "/protocol/" + OID4VC_PROTOCOL + "/" + CREDENTIAL_OFFER_PATH)
.setNonce(offerState.getNonce())
.setTxCode(redactedTxCode);
.setTxCode(offerState.getTxCode());
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.

Not sure why you keep adding this. It is entirely broken

@tdiesler tdiesler Feb 27, 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.

The TestResource is not available in the new test suite AFAIK. Perhaps we can agree that we want working tests - I'm using the above to achieve that under current conditions - its not by bad intention.

As soon as a better alternative is available we can use that.

I might want to add that this is only temporary - with credential policies (#46466) we will have this secure-by-default again (i.e. redacted).

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.

Please keep in mind that we can of course move changes from one PR (i.e. this one) to the next (i.e. #46385), but is that really worth it? The latter is the important one. This one contains the test infra borrowed from it.


// Respond with QR-Code as 'image/png'
if (responseType == OfferResponseType.QR) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
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.protocol.oid4vc.model.PreAuthorizedCodeGrant;
import org.keycloak.saml.RandomSecret;

import com.fasterxml.jackson.annotation.JsonInclude;

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

@JsonInclude(JsonInclude.Include.NON_NULL)
public class CredentialOfferState {

Expand All @@ -55,9 +56,9 @@ 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.getGrant(PRE_AUTH_GRANT_TYPE))
.map(it -> (PreAuthorizedCodeGrant) it)
.map(PreAuthorizedCodeGrant::getPreAuthorizedCode);
}

public void generateTxCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.keycloak.common.util.Time;
import org.keycloak.models.KeycloakSession;
import org.keycloak.protocol.oid4vc.model.OID4VCAuthorizationDetail;
import org.keycloak.util.JsonSerialization;

/**
Expand Down Expand Up @@ -69,11 +70,11 @@ public void putOfferState(KeycloakSession session, CredentialOfferState entry) {
entry.getPreAuthorizedCode().ifPresent(it -> {
session.singleUseObjects().put(it, lifespanSeconds, Map.of(ENTRY_KEY, entryJson));
});
Optional.ofNullable(entry.getAuthorizationDetails()).ifPresent(it -> {
it.getCredentialIdentifiers().forEach( cid -> {
session.singleUseObjects().put(cid, lifespanSeconds, Map.of(ENTRY_KEY, entryJson));
});
});
OID4VCAuthorizationDetail authDetails = entry.getAuthorizationDetails();
if (authDetails != null && authDetails.getCredentialIdentifiers() != null) {
authDetails.getCredentialIdentifiers().forEach( cid ->
session.singleUseObjects().put(cid, lifespanSeconds, Map.of(ENTRY_KEY, entryJson)));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.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,41 @@
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.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 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,12 +17,7 @@

package org.keycloak.protocol.oid4vc.model;

import java.util.Map;
import java.util.Optional;

import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.oid4vci.CredentialScopeModel;
import org.keycloak.util.JsonSerialization;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down Expand Up @@ -130,23 +125,6 @@ public CredentialRequest setCredentialResponseEncryption(CredentialResponseEncry
return this;
}

public Optional<CredentialScopeModel> findCredentialScope(KeycloakSession keycloakSession) {
Map<String, String> searchAttributeMap =
Optional.ofNullable(credentialConfigurationId)
.map(credentialIdentifier -> {
return Map.of(CredentialScopeModel.VC_CONFIGURATION_ID, credentialConfigurationId);
}).orElseGet(() -> {
return Map.of(CredentialScopeModel.VC_IDENTIFIER, credentialIdentifier);
});

RealmModel currentRealm = keycloakSession.getContext().getRealm();
final boolean useOrExpression = false;
return keycloakSession.clientScopes()
.getClientScopesByAttributes(currentRealm, searchAttributeMap, useOrExpression)
.map(CredentialScopeModel::new)
.findAny();
}

@Override
public String toString() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@

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.PreAuthorizedCodeGrant.PRE_AUTH_GRANT_TYPE;

/**
* Represents a CredentialsOffer according to the OID4VCI Spec
Expand All @@ -45,8 +51,9 @@ public class CredentialsOffer {
@JsonProperty("credential_configuration_ids")
private List<String> 1056E 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 +64,7 @@ public CredentialsOffer setCredentialIssuer(String credentialIssuer) {
return this;
}

@Transient
@JsonIgnore
public String getIssuerMetadataUrl() {
var metadataUrl = KeycloakUriBuilder
.fromUri(credentialIssuer)
Expand All @@ -83,24 +90,43 @@ 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 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