From c3fdeaab173a6023f84d0fc038e07fd95f35490f Mon Sep 17 00:00:00 2001 From: Thomas Diesler Date: Wed, 25 Feb 2026 07:19:44 +0100 Subject: [PATCH] [OID4VCI] Add support for CredentialScopeRepresentation Signed-off-by: Thomas Diesler --- .../models/oid4vci/CredentialScopeModel.java | 192 ++++++------ .../oid4vc/OID4VCLoginProtocolFactory.java | 56 ++-- .../oid4vc/issuance/OID4VCIssuerEndpoint.java | 3 +- .../issuance/mappers/OID4VCContextMapper.java | 2 +- .../mappers/OID4VCGeneratedIdMapper.java | 2 +- .../OID4VCIssuedAtTimeClaimMapper.java | 2 +- .../oid4vc/issuance/mappers/OID4VCMapper.java | 2 +- .../issuance/mappers/OID4VCTypeMapper.java | 2 +- .../oid4vc/model/CredentialBuildConfig.java | 6 +- .../oid4vc/model/CredentialRequest.java | 4 +- .../model/CredentialScopeRepresentation.java | 293 ++++++++++++++++++ .../protocol/oid4vc/utils/OID4VCUtil.java | 4 +- .../admin/client/ClientScopeTestOid4Vci.java | 34 +- 13 files changed, 451 insertions(+), 151 deletions(-) create mode 100644 services/src/main/java/org/keycloak/protocol/oid4vc/model/CredentialScopeRepresentation.java diff --git a/server-spi-private/src/main/java/org/keycloak/models/oid4vci/CredentialScopeModel.java b/server-spi-private/src/main/java/org/keycloak/models/oid4vci/CredentialScopeModel.java index 19683d65205..4eacc9d9f97 100644 --- a/server-spi-private/src/main/java/org/keycloak/models/oid4vci/CredentialScopeModel.java +++ b/server-spi-private/src/main/java/org/keycloak/models/oid4vci/CredentialScopeModel.java @@ -26,7 +26,6 @@ import java.util.stream.Stream; import org.keycloak.VCFormat; -import org.keycloak.constants.OID4VCIConstants; import org.keycloak.models.ClientScopeModel; import org.keycloak.models.ProtocolMapperModel; import org.keycloak.models.RealmModel; @@ -42,50 +41,50 @@ */ public class CredentialScopeModel implements ClientScopeModel { - - public static final String SD_JWT_VISIBLE_CLAIMS_DEFAULT = "id,iat,nbf,exp,jti"; - public static final int SD_JWT_DECOYS_DEFAULT = 10; - public static final String FORMAT_DEFAULT = VCFormat.SD_JWT_VC; - public static final String HASH_ALGORITHM_DEFAULT = "sha-256"; - public static final String TOKEN_TYPE_DEFAULT = "JWS"; - public static final int EXPIRY_IN_SECONDS_DEFAULT = 31536000; // 1 year public static final String CRYPTOGRAPHIC_BINDING_METHODS_DEFAULT = "jwk"; + public static final String VC_BUILD_CONFIG_HASH_ALGORITHM_DEFAULT = "SHA-256"; + public static final String VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS_DEFAULT = "id,iat,nbf,exp,jti"; + public static final String VC_BUILD_CONFIG_TOKEN_JWS_TYPE_DEFAULT = "JWS"; + public static final Integer VC_EXPIRY_IN_SECONDS_DEFAULT = 31536000; // 1 year + public static final String VC_FORMAT_DEFAULT = VCFormat.SD_JWT_VC; + public static final Integer VC_SD_JWT_NUMBER_OF_DECOYS_DEFAULT = 10; + /** * the credential configuration id as provided in the metadata endpoint */ - public static final String CONFIGURATION_ID = "vc.credential_configuration_id"; - public static final String CREDENTIAL_IDENTIFIER = "vc.credential_identifier"; - public static final String FORMAT = "vc.format"; - public static final String EXPIRY_IN_SECONDS = "vc.expiry_in_seconds"; - public static final String ISSUER_DID = "vc.issuer_did"; + public static final String VC_CONFIGURATION_ID = "vc.credential_configuration_id"; + public static final String VC_IDENTIFIER = "vc.credential_identifier"; + public static final String VC_FORMAT = "vc.format"; + public static final String VC_EXPIRY_IN_SECONDS = "vc.expiry_in_seconds"; + public static final String VC_ISSUER_DID = "vc.issuer_did"; public static final String VCT = "vc.verifiable_credential_type"; /** * the value that is added into the "types"-attribute of a verifiable credential */ - public static final String TYPES = "vc.supported_credential_types"; + public static final String VC_SUPPORTED_TYPES = "vc.supported_credential_types"; /** * the value that is entered into the "@contexts"-attribute of a verifiable credential */ - public static final String CONTEXTS = "vc.credential_contexts"; + public static final String VC_CONTEXTS = "vc.credential_contexts"; /** * The credential signature algorithm. If it is not configured, then the realm active key is used to sign the verifiable credential */ - public static final String SIGNING_ALG = "vc.credential_signing_alg"; + public static final String VC_SIGNING_ALG = "vc.credential_signing_alg"; /** * if the credential is only meant for specific cryptographic binding algorithms the global default list can be * overridden here. The global default list is retrieved from the available keys in the realm. */ - public static final String CRYPTOGRAPHIC_BINDING_METHODS = "vc.cryptographic_binding_methods_supported"; + public static final String VC_CRYPTOGRAPHIC_BINDING_METHODS = "vc.cryptographic_binding_methods_supported"; /** * an optional configuration that can be used to select a specific key for signing the credential */ - public static final String SIGNING_KEY_ID = "vc.signing_key_id"; + public static final String VC_SIGNING_KEY_ID = "vc.signing_key_id"; /** * an optional attribute for the metadata endpoint @@ -95,28 +94,28 @@ public class CredentialScopeModel implements ClientScopeModel { /** * this attribute holds a customizable value for the number of decoys to use in a SD-JWT credential */ - public static final String SD_JWT_NUMBER_OF_DECOYS = "vc.sd_jwt.number_of_decoys"; + public static final String VC_SD_JWT_NUMBER_OF_DECOYS = "vc.sd_jwt.number_of_decoys"; /** * an optional attribute that tells us which attributes should be added into the SD-JWT body. */ - public static final String SD_JWT_VISIBLE_CLAIMS = "vc.credential_build_config.sd_jwt.visible_claims"; + public static final String VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS = "vc.credential_build_config.sd_jwt.visible_claims"; /** * an optional configuration that can be used to select a specific hash algorithm */ - public static final String HASH_ALGORITHM = "vc.credential_build_config.hash_algorithm"; + public static final String VC_BUILD_CONFIG_HASH_ALGORITHM = "vc.credential_build_config.hash_algorithm"; /** * this attribute holds the 'typ' value that will be added into the JWS header of the credential. */ - public static final String TOKEN_JWS_TYPE = "vc.credential_build_config.token_jws_type"; + public static final String VC_BUILD_CONFIG_TOKEN_JWS_TYPE = "vc.credential_build_config.token_jws_type"; /** * this configuration property can be used to enforce specific claims to be included in the metadata, if they would * normally not and vice versa */ - public static final String INCLUDE_IN_METADATA = "vc.include_in_metadata"; + public static final String VC_INCLUDE_IN_METADATA = "vc.include_in_metadata"; /** * OPTIONAL. Object that describes the requirement for key attestations as described in Appendix D, which the @@ -125,18 +124,30 @@ public class CredentialScopeModel implements ClientScopeModel { * key_storage and user_authentication parameters are absent, the key_attestations_required parameter may be empty, * indicating a key attestation is needed without additional constraints. */ - public static final String KEY_ATTESTATION_REQUIRED = "vc.key_attestations_required"; + public static final String VC_KEY_ATTESTATION_REQUIRED = "vc.key_attestations_required"; /** * OPTIONAL. A non-empty array defining values specified in Appendix D.2 accepted by the Credential Issuer. */ - public static final String KEY_ATTESTATION_REQUIRED_KEY_STORAGE = "vc.key_attestations_required.key_storage"; + public static final String VC_KEY_ATTESTATION_REQUIRED_KEY_STORAGE = "vc.key_attestations_required.key_storage"; /** * OPTIONAL. A non-empty array defining values specified in Appendix D.2 accepted by the Credential Issuer. */ - public static final String KEY_ATTESTATION_REQUIRED_USER_AUTH = "vc.key_attestations_required.user_authentication"; - + public static final String VC_KEY_ATTESTATION_REQUIRED_USER_AUTH = "vc.key_attestations_required.user_authentication"; + + // [TODO] Remove deprecated constants used in the old testsuite + @Deprecated public static final String CONFIGURATION_ID = VC_CONFIGURATION_ID; + @Deprecated public static final String CREDENTIAL_IDENTIFIER = VC_IDENTIFIER; + @Deprecated public static final String CONTEXTS = VC_CONTEXTS; + @Deprecated public static final String EXPIRY_IN_SECONDS = VC_EXPIRY_IN_SECONDS; + @Deprecated public static final Integer EXPIRY_IN_SECONDS_DEFAULT = VC_EXPIRY_IN_SECONDS_DEFAULT; + @Deprecated public static final String FORMAT = VC_FORMAT; + @Deprecated public static final String ISSUER_DID = VC_ISSUER_DID; + @Deprecated public static final String KEY_ATTESTATION_REQUIRED = VC_KEY_ATTESTATION_REQUIRED; + @Deprecated public static final String KEY_ATTESTATION_REQUIRED_KEY_STORAGE = VC_KEY_ATTESTATION_REQUIRED_KEY_STORAGE; + @Deprecated public static final String KEY_ATTESTATION_REQUIRED_USER_AUTH = VC_KEY_ATTESTATION_REQUIRED_USER_AUTH; + @Deprecated public static final String TYPES = VC_SUPPORTED_TYPES; /** * the actual object that is represented by this scope @@ -149,11 +160,11 @@ public CredentialScopeModel(ClientScopeModel clientScope) { } public String getIssuerDid() { - return clientScope.getAttribute(ISSUER_DID); + return clientScope.getAttribute(VC_ISSUER_DID); } public void setIssuerDid(String issuerDid) { - clientScope.setAttribute(ISSUER_DID, issuerDid); + clientScope.setAttribute(VC_ISSUER_DID, issuerDid); } public String getScope() { @@ -161,152 +172,149 @@ public String getScope() { } public String getCredentialConfigurationId() { - return Optional.ofNullable(clientScope.getAttribute(CONFIGURATION_ID)).orElse(getName()); + return clientScope.getAttribute(VC_CONFIGURATION_ID); } public void setCredentialConfigurationId(String credentialConfigurationId) { - clientScope.setAttribute(CONFIGURATION_ID, Optional.ofNullable(credentialConfigurationId).orElse(getName())); + clientScope.setAttribute(VC_CONFIGURATION_ID, credentialConfigurationId); } public String getCredentialIdentifier() { - return Optional.ofNullable(clientScope.getAttribute(CREDENTIAL_IDENTIFIER)).orElse(getName()); + return clientScope.getAttribute(VC_IDENTIFIER); } public void setCredentialIdentifier(String credentialIdentifier) { - clientScope.setAttribute(CREDENTIAL_IDENTIFIER, Optional.ofNullable(credentialIdentifier).orElse(getName())); + clientScope.setAttribute(VC_IDENTIFIER, credentialIdentifier); } public String getFormat() { - return Optional.ofNullable(clientScope.getAttribute(FORMAT)).orElse(FORMAT_DEFAULT); + return Optional.ofNullable(clientScope.getAttribute(VC_FORMAT)).orElse(VC_FORMAT_DEFAULT); } public void setFormat(String credentialFormat) { - clientScope.setAttribute(FORMAT, Optional.ofNullable(credentialFormat).orElse(FORMAT_DEFAULT)); + clientScope.setAttribute(VC_FORMAT, credentialFormat); } public Integer getExpiryInSeconds() { - return Optional.ofNullable(clientScope.getAttribute(EXPIRY_IN_SECONDS)).map(Integer::parseInt) - .orElse(EXPIRY_IN_SECONDS_DEFAULT); + return Optional.ofNullable(clientScope.getAttribute(VC_EXPIRY_IN_SECONDS)) + .map(Integer::parseInt) + .orElse(VC_EXPIRY_IN_SECONDS_DEFAULT); } public void setExpiryInSeconds(Integer expiryInSeconds) { - clientScope.setAttribute(EXPIRY_IN_SECONDS, - Optional.ofNullable(expiryInSeconds).map(String::valueOf) - .orElse(String.valueOf(EXPIRY_IN_SECONDS_DEFAULT))); + clientScope.setAttribute(VC_EXPIRY_IN_SECONDS, String.valueOf(expiryInSeconds)); } - public int getSdJwtNumberOfDecoys() { - return Optional.ofNullable(clientScope.getAttribute(SD_JWT_NUMBER_OF_DECOYS)).map(Integer::parseInt) - .orElse(SD_JWT_DECOYS_DEFAULT); + public Integer getSdJwtNumberOfDecoys() { + return Optional.ofNullable(clientScope.getAttribute(VC_SD_JWT_NUMBER_OF_DECOYS)) + .map(Integer::parseInt) + .orElse(VC_SD_JWT_NUMBER_OF_DECOYS_DEFAULT); } public void setSdJwtNumberOfDecoys(Integer sdJwtNumberOfDecoys) { - clientScope.setAttribute(SD_JWT_NUMBER_OF_DECOYS, - Optional.ofNullable(sdJwtNumberOfDecoys).map(String::valueOf) - .orElse(String.valueOf(SD_JWT_DECOYS_DEFAULT))); + clientScope.setAttribute(VC_SD_JWT_NUMBER_OF_DECOYS, String.valueOf(sdJwtNumberOfDecoys)); } public String getVct() { - return Optional.ofNullable(clientScope.getAttribute(VCT)).orElse(getName()); + return clientScope.getAttribute(VCT); } public void setVct(String vct) { - clientScope.setAttribute(VCT, Optional.ofNullable(vct).orElse(getName())); + clientScope.setAttribute(VCT, vct); } - public String getTokenJwsType() { - return Optional.ofNullable(clientScope.getAttribute(TOKEN_JWS_TYPE)).orElse(TOKEN_TYPE_DEFAULT); + public String getBuildConfigTokenJwsType() { + return Optional.ofNullable(clientScope.getAttribute(VC_BUILD_CONFIG_TOKEN_JWS_TYPE)) + .orElse(VC_BUILD_CONFIG_TOKEN_JWS_TYPE_DEFAULT); } - public void setTokenJwsType(String tokenJwsType) { - clientScope.setAttribute(TOKEN_JWS_TYPE, Optional.ofNullable(tokenJwsType).orElse(TOKEN_TYPE_DEFAULT)); + public void setBuildConfigTokenJwsType(String tokenJwsType) { + clientScope.setAttribute(VC_BUILD_CONFIG_TOKEN_JWS_TYPE, tokenJwsType); } public String getSigningKeyId() { - return clientScope.getAttribute(SIGNING_KEY_ID); + return clientScope.getAttribute(VC_SIGNING_KEY_ID); } public void setSigningKeyId(String signingKeyId) { - clientScope.setAttribute(SIGNING_KEY_ID, signingKeyId); + clientScope.setAttribute(VC_SIGNING_KEY_ID, signingKeyId); } - public String getHashAlgorithm() { - return Optional.ofNullable(clientScope.getAttribute(HASH_ALGORITHM)).orElse(HASH_ALGORITHM_DEFAULT); + public String getBuildConfigHashAlgorithm() { + return Optional.ofNullable(clientScope.getAttribute(VC_BUILD_CONFIG_HASH_ALGORITHM)) + .orElse(VC_BUILD_CONFIG_HASH_ALGORITHM_DEFAULT); } - public void setHashAlgorithm(String hashAlgorithm) { - clientScope.setAttribute(HASH_ALGORITHM, hashAlgorithm); + public void setBuildConfigHashAlgorithm(String hashAlgorithm) { + clientScope.setAttribute(VC_BUILD_CONFIG_HASH_ALGORITHM, hashAlgorithm); } public List getSupportedCredentialTypes() { - return Optional.ofNullable(clientScope.getAttribute(TYPES)) + return Optional.ofNullable(clientScope.getAttribute(VC_SUPPORTED_TYPES)) .map(s -> s.split(",")) .map(Arrays::asList) .orElse(Collections.singletonList(getName())); } public void setSupportedCredentialTypes(String supportedCredentialTypes) { - clientScope.setAttribute(TYPES, Optional.ofNullable(supportedCredentialTypes).orElse(getName())); + clientScope.setAttribute(VC_SUPPORTED_TYPES, supportedCredentialTypes); } public void setSupportedCredentialTypes(List supportedCredentialTypes) { - clientScope.setAttribute(TYPES, String.join(",", supportedCredentialTypes)); + clientScope.setAttribute(VC_SUPPORTED_TYPES, String.join(",", supportedCredentialTypes)); } public List getVcContexts() { - return Optional.ofNullable(clientScope.getAttribute(CONTEXTS)) + return Optional.ofNullable(clientScope.getAttribute(VC_CONTEXTS)) .map(s -> s.split(",")) .map(Arrays::asList) .orElse(Collections.singletonList(getName())); } public void setVcContexts(String vcContexts) { - clientScope.setAttribute(CONTEXTS, Optional.ofNullable(vcContexts).orElse(getName())); + clientScope.setAttribute(VC_CONTEXTS, vcContexts); } public void setVcContexts(List vcContexts) { - clientScope.setAttribute(CONTEXTS, String.join(",", vcContexts)); + clientScope.setAttribute(VC_CONTEXTS, String.join(",", vcContexts)); } public String getSigningAlg() { - return clientScope.getAttribute(SIGNING_ALG); + return clientScope.getAttribute(VC_SIGNING_ALG); } public void setSigningAlg(String signingAlg) { - clientScope.setAttribute(SIGNING_ALG, signingAlg); + clientScope.setAttribute(VC_SIGNING_ALG, signingAlg); } public List getCryptographicBindingMethods() { - return Optional.ofNullable(clientScope.getAttribute(CRYPTOGRAPHIC_BINDING_METHODS)) + return Optional.ofNullable(clientScope.getAttribute(VC_CRYPTOGRAPHIC_BINDING_METHODS)) .map(s -> s.split(",")) .map(Arrays::asList) .orElse(Collections.emptyList()); } public void setCryptographicBindingMethods(String cryptographicBindingMethods) { - clientScope.setAttribute(CRYPTOGRAPHIC_BINDING_METHODS, cryptographicBindingMethods); + clientScope.setAttribute(VC_CRYPTOGRAPHIC_BINDING_METHODS, cryptographicBindingMethods); } public void setCryptographicBindingMethods(List cryptographicBindingMethods) { - clientScope.setAttribute(CRYPTOGRAPHIC_BINDING_METHODS, - String.join(",", cryptographicBindingMethods)); + clientScope.setAttribute(VC_CRYPTOGRAPHIC_BINDING_METHODS, String.join(",", cryptographicBindingMethods)); } - public List getSdJwtVisibleClaims() { - return Optional.ofNullable(clientScope.getAttribute(SD_JWT_VISIBLE_CLAIMS)) + public List getBuildConfigSdJwtVisibleClaims() { + return Optional.ofNullable(clientScope.getAttribute(VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS)) .map(s -> s.split(",")) .map(Arrays::asList) - .orElse(List.of(SD_JWT_VISIBLE_CLAIMS_DEFAULT.split(","))); + .orElse(List.of(VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS_DEFAULT.split(","))); } - public void setSdJwtVisibleClaims(String sdJwtVisibleClaims) { - clientScope.setAttribute(SD_JWT_VISIBLE_CLAIMS, Optional.ofNullable(sdJwtVisibleClaims) - .orElse(SD_JWT_VISIBLE_CLAIMS_DEFAULT)); + public void setBuildConfigSdJwtVisibleClaims(String sdJwtVisibleClaims) { + clientScope.setAttribute(VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS, sdJwtVisibleClaims); } - public void setSdJwtVisibleClaims(List sdJwtVisibleClaims) { - clientScope.setAttribute(SD_JWT_VISIBLE_CLAIMS, - String.join(",", sdJwtVisibleClaims)); + public void setBuildConfigSdJwtVisibleClaims(List sdJwtVisibleClaims) { + clientScope.setAttribute(VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS, String.join(",", sdJwtVisibleClaims)); } public String getVcDisplay() { @@ -318,17 +326,15 @@ public void setVcDisplay(String vcDisplay) { } public boolean isKeyAttestationRequired() { - return Optional.ofNullable(clientScope.getAttribute(KEY_ATTESTATION_REQUIRED)) - .map(Boolean::parseBoolean) - .orElse(false); + return Boolean.parseBoolean(getAttribute(VC_KEY_ATTESTATION_REQUIRED)); } public void setKeyAttestationRequired(boolean keyAttestationRequired) { - clientScope.setAttribute(KEY_ATTESTATION_REQUIRED, String.valueOf(keyAttestationRequired)); + clientScope.setAttribute(VC_KEY_ATTESTATION_REQUIRED, String.valueOf(keyAttestationRequired)); } public List getRequiredKeyAttestationKeyStorage() { - return Optional.ofNullable(clientScope.getAttribute(KEY_ATTESTATION_REQUIRED_KEY_STORAGE)) + return Optional.ofNullable(clientScope.getAttribute(VC_KEY_ATTESTATION_REQUIRED_KEY_STORAGE)) .map(s -> Arrays.asList(s.split(","))) // it is important to return null here instead of an empty list: // If both key_storage and user_authentication parameters are absent, the @@ -338,12 +344,12 @@ public List getRequiredKeyAttestationKeyStorage() { } public void setRequiredKeyAttestationKeyStorage(List keyStorage) { - clientScope.setAttribute(KEY_ATTESTATION_REQUIRED_KEY_STORAGE, - Optional.ofNullable(keyStorage).map(list -> String.join(",")).orElse(null)); + clientScope.setAttribute(VC_KEY_ATTESTATION_REQUIRED_KEY_STORAGE, Optional.ofNullable(keyStorage) + .map(list -> String.join(",")).orElse(null)); } public List getRequiredKeyAttestationUserAuthentication() { - return Optional.ofNullable(clientScope.getAttribute(KEY_ATTESTATION_REQUIRED_USER_AUTH)) + return Optional.ofNullable(clientScope.getAttribute(VC_KEY_ATTESTATION_REQUIRED_USER_AUTH)) .map(s -> Arrays.asList(s.split(","))) // it is important to return null here instead of an empty list: // If both key_storage and user_authentication parameters are absent, the @@ -352,9 +358,9 @@ public List getRequiredKeyAttestationUserAuthentication() { .orElse(null); } - public void getRequiredKeyAttestationUserAuthentication(List userAuthentication) { - clientScope.setAttribute(KEY_ATTESTATION_REQUIRED_USER_AUTH, - Optional.ofNullable(userAuthentication).map(list -> String.join(",")).orElse(null)); + public void setRequiredKeyAttestationUserAuthentication(List userAuthentication) { + clientScope.setAttribute(VC_KEY_ATTESTATION_REQUIRED_USER_AUTH, Optional.ofNullable(userAuthentication) + .map(list -> String.join(",")).orElse(null)); } @Override @@ -473,9 +479,9 @@ public String getDynamicScopeRegexp() { } public Stream getOid4vcProtocolMappersStream() { - return clientScope.getProtocolMappersStream().filter(pm -> { - return OID4VCIConstants.OID4VC_PROTOCOL.equals(pm.getProtocol()); - }).map(Oid4vcProtocolMapperModel::new); + return clientScope.getProtocolMappersStream() + .filter(pm -> OID4VC_PROTOCOL.equals(pm.getProtocol())) + .map(Oid4vcProtocolMapperModel::new); } @Override diff --git a/services/src/main/java/org/keycloak/protocol/oid4vc/OID4VCLoginProtocolFactory.java b/services/src/main/java/org/keycloak/protocol/oid4vc/OID4VCLoginProtocolFactory.java index b0ad989f05b..07c51826e7a 100644 --- a/services/src/main/java/org/keycloak/protocol/oid4vc/OID4VCLoginProtocolFactory.java +++ b/services/src/main/java/org/keycloak/protocol/oid4vc/OID4VCLoginProtocolFactory.java @@ -53,24 +53,24 @@ import static org.keycloak.VCFormat.SD_JWT_VC; import static org.keycloak.constants.OID4VCIConstants.OID4VC_PROTOCOL; import static org.keycloak.models.ClientScopeModel.INCLUDE_IN_TOKEN_SCOPE; -import static org.keycloak.models.oid4vci.CredentialScopeModel.CONFIGURATION_ID; -import static org.keycloak.models.oid4vci.CredentialScopeModel.CONTEXTS; -import static org.keycloak.models.oid4vci.CredentialScopeModel.CRYPTOGRAPHIC_BINDING_METHODS; import static org.keycloak.models.oid4vci.CredentialScopeModel.CRYPTOGRAPHIC_BINDING_METHODS_DEFAULT; -import static org.keycloak.models.oid4vci.CredentialScopeModel.EXPIRY_IN_SECONDS; -import static org.keycloak.models.oid4vci.CredentialScopeModel.EXPIRY_IN_SECONDS_DEFAULT; -import static org.keycloak.models.oid4vci.CredentialScopeModel.FORMAT; -import static org.keycloak.models.oid4vci.CredentialScopeModel.HASH_ALGORITHM; -import static org.keycloak.models.oid4vci.CredentialScopeModel.HASH_ALGORITHM_DEFAULT; -import static org.keycloak.models.oid4vci.CredentialScopeModel.INCLUDE_IN_METADATA; -import static org.keycloak.models.oid4vci.CredentialScopeModel.SD_JWT_DECOYS_DEFAULT; -import static org.keycloak.models.oid4vci.CredentialScopeModel.SD_JWT_NUMBER_OF_DECOYS; -import static org.keycloak.models.oid4vci.CredentialScopeModel.SD_JWT_VISIBLE_CLAIMS; -import static org.keycloak.models.oid4vci.CredentialScopeModel.SD_JWT_VISIBLE_CLAIMS_DEFAULT; -import static org.keycloak.models.oid4vci.CredentialScopeModel.TOKEN_JWS_TYPE; -import static org.keycloak.models.oid4vci.CredentialScopeModel.TOKEN_TYPE_DEFAULT; -import static org.keycloak.models.oid4vci.CredentialScopeModel.TYPES; import static org.keycloak.models.oid4vci.CredentialScopeModel.VCT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_BUILD_CONFIG_HASH_ALGORITHM; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_BUILD_CONFIG_HASH_ALGORITHM_DEFAULT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS_DEFAULT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_BUILD_CONFIG_TOKEN_JWS_TYPE; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_BUILD_CONFIG_TOKEN_JWS_TYPE_DEFAULT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_CONFIGURATION_ID; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_CONTEXTS; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_CRYPTOGRAPHIC_BINDING_METHODS; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_EXPIRY_IN_SECONDS; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_EXPIRY_IN_SECONDS_DEFAULT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_FORMAT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_INCLUDE_IN_METADATA; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_SD_JWT_NUMBER_OF_DECOYS; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_SD_JWT_NUMBER_OF_DECOYS_DEFAULT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_SUPPORTED_TYPES; /** * Factory for creating all OID4VC related endpoints and the default mappers. @@ -156,8 +156,8 @@ public void setupClientDefaults(ClientRepresentation rep, ClientModel newClient) public void addClientScopeDefaults(ClientScopeRepresentation clientScope) { String scopeName = clientScope.getName(); - clientScope.getAttributes().computeIfAbsent(FORMAT, k -> VCFormat.getFromScope(scopeName)); - String format = clientScope.getAttributes().get(FORMAT); + clientScope.getAttributes().computeIfAbsent(VC_FORMAT, k -> VCFormat.getFromScope(scopeName)); + String format = clientScope.getAttributes().get(VC_FORMAT); int idx = scopeName.lastIndexOf(VCFormat.getScopeSuffix(format)); String credentialType = idx > 0 ? scopeName.substring(0, idx) : scopeName; @@ -167,19 +167,19 @@ public void addClientScopeDefaults(ClientScopeRepresentation clientScope) { // clientScope.getAttributes().computeIfAbsent(ISSUER_DID, k -> ) clientScope.getAttributes().putIfAbsent(INCLUDE_IN_TOKEN_SCOPE, "true"); - clientScope.getAttributes().putIfAbsent(INCLUDE_IN_METADATA, "true"); - clientScope.getAttributes().putIfAbsent(CONFIGURATION_ID, scopeName); - clientScope.getAttributes().putIfAbsent(TYPES, credentialType); - clientScope.getAttributes().putIfAbsent(CONTEXTS, credentialType); + clientScope.getAttributes().putIfAbsent(VC_INCLUDE_IN_METADATA, "true"); + clientScope.getAttributes().putIfAbsent(VC_CONFIGURATION_ID, scopeName); + clientScope.getAttributes().putIfAbsent(VC_SUPPORTED_TYPES, credentialType); + clientScope.getAttributes().putIfAbsent(VC_CONTEXTS, credentialType); clientScope.getAttributes().putIfAbsent(VCT, credentialType); - clientScope.getAttributes().putIfAbsent(CRYPTOGRAPHIC_BINDING_METHODS, CRYPTOGRAPHIC_BINDING_METHODS_DEFAULT); - clientScope.getAttributes().putIfAbsent(HASH_ALGORITHM, HASH_ALGORITHM_DEFAULT); - clientScope.getAttributes().putIfAbsent(TOKEN_JWS_TYPE, TOKEN_TYPE_DEFAULT); - clientScope.getAttributes().putIfAbsent(EXPIRY_IN_SECONDS, String.valueOf(EXPIRY_IN_SECONDS_DEFAULT)); + clientScope.getAttributes().putIfAbsent(VC_CRYPTOGRAPHIC_BINDING_METHODS, CRYPTOGRAPHIC_BINDING_METHODS_DEFAULT); + clientScope.getAttributes().putIfAbsent(VC_BUILD_CONFIG_HASH_ALGORITHM, VC_BUILD_CONFIG_HASH_ALGORITHM_DEFAULT); + clientScope.getAttributes().putIfAbsent(VC_BUILD_CONFIG_TOKEN_JWS_TYPE, VC_BUILD_CONFIG_TOKEN_JWS_TYPE_DEFAULT); + clientScope.getAttributes().putIfAbsent(VC_EXPIRY_IN_SECONDS, String.valueOf(VC_EXPIRY_IN_SECONDS_DEFAULT)); if (SD_JWT_VC.equals(format)) { - clientScope.getAttributes().putIfAbsent(SD_JWT_NUMBER_OF_DECOYS, String.valueOf(SD_JWT_DECOYS_DEFAULT)); - clientScope.getAttributes().putIfAbsent(SD_JWT_VISIBLE_CLAIMS, SD_JWT_VISIBLE_CLAIMS_DEFAULT); + clientScope.getAttributes().putIfAbsent(VC_SD_JWT_NUMBER_OF_DECOYS, String.valueOf(VC_SD_JWT_NUMBER_OF_DECOYS_DEFAULT)); + clientScope.getAttributes().putIfAbsent(VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS, VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS_DEFAULT); } } diff --git a/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/OID4VCIssuerEndpoint.java b/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/OID4VCIssuerEndpoint.java index f0ec9261e02..80a30545052 100644 --- a/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/OID4VCIssuerEndpoint.java +++ b/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/OID4VCIssuerEndpoint.java @@ -142,6 +142,7 @@ import static org.keycloak.OID4VCConstants.OPENID_CREDENTIAL; import static org.keycloak.constants.OID4VCIConstants.CREDENTIAL_OFFER_CREATE; import static org.keycloak.constants.OID4VCIConstants.OID4VC_PROTOCOL; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_CONFIGURATION_ID; import static org.keycloak.protocol.oid4vc.model.ErrorType.INVALID_CREDENTIAL_OFFER_REQUEST; import static org.keycloak.protocol.oid4vc.model.ErrorType.INVALID_CREDENTIAL_REQUEST; import static org.keycloak.protocol.oid4vc.model.ErrorType.UNKNOWN_CREDENTIAL_CONFIGURATION; @@ -470,7 +471,7 @@ public Response createCredentialOffer( // ClientScopeModel credentialClientScope = session.clientScopes() .getClientScopesByProtocol(realmModel, OID4VC_PROTOCOL) - .filter(it -> credConfigId.equals(it.getAttribute(CredentialScopeModel.CONFIGURATION_ID))) + .filter(it -> credConfigId.equals(it.getAttribute(VC_CONFIGURATION_ID))) .findFirst() .orElse(null); if (credentialClientScope == null) { diff --git a/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCContextMapper.java b/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCContextMapper.java index 74bd073c857..8ae215be675 100644 --- a/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCContextMapper.java +++ b/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCContextMapper.java @@ -64,7 +64,7 @@ protected List getIndividualConfigProperties() { */ @Override public boolean includeInMetadata() { - return Optional.ofNullable(mapperModel.getConfig().get(CredentialScopeModel.INCLUDE_IN_METADATA)) + return Optional.ofNullable(mapperModel.getConfig().get(CredentialScopeModel.VC_INCLUDE_IN_METADATA)) .map(Boolean::parseBoolean) .orElse(false); } diff --git a/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCGeneratedIdMapper.java b/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCGeneratedIdMapper.java index 2abe99f11cc..b5f5de11340 100644 --- a/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCGeneratedIdMapper.java +++ b/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCGeneratedIdMapper.java @@ -64,7 +64,7 @@ protected List getIndividualConfigProperties() { */ @Override public boolean includeInMetadata() { - return Optional.ofNullable(mapperModel.getConfig().get(CredentialScopeModel.INCLUDE_IN_METADATA)) + return Optional.ofNullable(mapperModel.getConfig().get(CredentialScopeModel.VC_INCLUDE_IN_METADATA)) .map(Boolean::parseBoolean) .orElse(false); } diff --git a/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCIssuedAtTimeClaimMapper.java b/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCIssuedAtTimeClaimMapper.java index d679c6711f8..f5eef3222a4 100644 --- a/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCIssuedAtTimeClaimMapper.java +++ b/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCIssuedAtTimeClaimMapper.java @@ -101,7 +101,7 @@ protected List getIndividualConfigProperties() { */ @Override public boolean includeInMetadata() { - return Optional.ofNullable(mapperModel.getConfig().get(CredentialScopeModel.INCLUDE_IN_METADATA)) + return Optional.ofNullable(mapperModel.getConfig().get(CredentialScopeModel.VC_INCLUDE_IN_METADATA)) .map(Boolean::parseBoolean) .orElse(false); } diff --git a/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCMapper.java b/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCMapper.java index 25400f17c93..41daefdae67 100644 --- a/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCMapper.java +++ b/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCMapper.java @@ -99,7 +99,7 @@ public OID4VCMapper setMapperModel(ProtocolMapperModel mapperModel, String forma * attributes from the metadata */ public boolean includeInMetadata() { - return Optional.ofNullable(mapperModel.getConfig().get(CredentialScopeModel.INCLUDE_IN_METADATA)) + return Optional.ofNullable(mapperModel.getConfig().get(CredentialScopeModel.VC_INCLUDE_IN_METADATA)) .map(Boolean::parseBoolean) .orElse(true); } diff --git a/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCTypeMapper.java b/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCTypeMapper.java index 83682bac177..af8cd1773b9 100644 --- a/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCTypeMapper.java +++ b/services/src/main/java/org/keycloak/protocol/oid4vc/issuance/mappers/OID4VCTypeMapper.java @@ -63,7 +63,7 @@ protected List getIndividualConfigProperties() { */ @Override public boolean includeInMetadata() { - return Optional.ofNullable(mapperModel.getConfig().get(CredentialScopeModel.INCLUDE_IN_METADATA)) + return Optional.ofNullable(mapperModel.getConfig().get(CredentialScopeModel.VC_INCLUDE_IN_METADATA)) .map(Boolean::parseBoolean) .orElse(false); } diff --git a/services/src/main/java/org/keycloak/protocol/oid4vc/model/CredentialBuildConfig.java b/services/src/main/java/org/keycloak/protocol/oid4vc/model/CredentialBuildConfig.java index 5b46b1afbf6..1d3d76567b8 100644 --- a/services/src/main/java/org/keycloak/protocol/oid4vc/model/CredentialBuildConfig.java +++ b/services/src/main/java/org/keycloak/protocol/oid4vc/model/CredentialBuildConfig.java @@ -98,12 +98,12 @@ public static CredentialBuildConfig parse(KeycloakSession keycloakSession, return new CredentialBuildConfig().setCredentialIssuer(credentialIssuer) .setCredentialConfigId(credentialConfiguration.getId()) .setCredentialType(credentialModel.getVct()) - .setTokenJwsType(credentialModel.getTokenJwsType()) + .setTokenJwsType(credentialModel.getBuildConfigTokenJwsType()) .setNumberOfDecoys(credentialModel.getSdJwtNumberOfDecoys()) .setSigningKeyId(credentialModel.getSigningKeyId()) .setSigningAlgorithm(signingAlg) - .setHashAlgorithm(credentialModel.getHashAlgorithm()) - .setSdJwtVisibleClaims(credentialModel.getSdJwtVisibleClaims()); + .setHashAlgorithm(credentialModel.getBuildConfigHashAlgorithm()) + .setSdJwtVisibleClaims(credentialModel.getBuildConfigSdJwtVisibleClaims()); } public String getCredentialIssuer() { diff --git a/services/src/main/java/org/keycloak/protocol/oid4vc/model/CredentialRequest.java b/services/src/main/java/org/keycloak/protocol/oid4vc/model/CredentialRequest.java index c424b9f02b0..9000edd510d 100644 --- a/services/src/main/java/org/keycloak/protocol/oid4vc/model/CredentialRequest.java +++ b/services/src/main/java/org/keycloak/protocol/oid4vc/model/CredentialRequest.java @@ -134,9 +134,9 @@ public Optional findCredentialScope(KeycloakSession keyclo Map searchAttributeMap = Optional.ofNullable(credentialConfigurationId) .map(credentialIdentifier -> { - return Map.of(CredentialScopeModel.CONFIGURATION_ID, credentialConfigurationId); + return Map.of(CredentialScopeModel.VC_CONFIGURATION_ID, credentialConfigurationId); }).orElseGet(() -> { - return Map.of(CredentialScopeModel.CREDENTIAL_IDENTIFIER, credentialIdentifier); + return Map.of(CredentialScopeModel.VC_IDENTIFIER, credentialIdentifier); }); RealmModel currentRealm = keycloakSession.getContext().getRealm(); diff --git a/services/src/main/java/org/keycloak/protocol/oid4vc/model/CredentialScopeRepresentation.java b/services/src/main/java/org/keycloak/protocol/oid4vc/model/CredentialScopeRepresentation.java new file mode 100644 index 00000000000..50c3030cc82 --- /dev/null +++ b/services/src/main/java/org/keycloak/protocol/oid4vc/model/CredentialScopeRepresentation.java @@ -0,0 +1,293 @@ +package org.keycloak.protocol.oid4vc.model; + +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Optional; + +import org.keycloak.constants.OID4VCIConstants; +import org.keycloak.representations.idm.ClientScopeRepresentation; + +import static org.keycloak.models.ClientScopeModel.INCLUDE_IN_TOKEN_SCOPE; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VCT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_BUILD_CONFIG_HASH_ALGORITHM; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_BUILD_CONFIG_HASH_ALGORITHM_DEFAULT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS_DEFAULT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_BUILD_CONFIG_TOKEN_JWS_TYPE; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_BUILD_CONFIG_TOKEN_JWS_TYPE_DEFAULT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_CONFIGURATION_ID; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_CONTEXTS; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_CRYPTOGRAPHIC_BINDING_METHODS; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_DISPLAY; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_EXPIRY_IN_SECONDS; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_EXPIRY_IN_SECONDS_DEFAULT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_FORMAT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_FORMAT_DEFAULT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_IDENTIFIER; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_ISSUER_DID; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_KEY_ATTESTATION_REQUIRED; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_KEY_ATTESTATION_REQUIRED_KEY_STORAGE; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_KEY_ATTESTATION_REQUIRED_USER_AUTH; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_SD_JWT_NUMBER_OF_DECOYS; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_SD_JWT_NUMBER_OF_DECOYS_DEFAULT; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_SIGNING_ALG; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_SIGNING_KEY_ID; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_SUPPORTED_TYPES; + +/** + * An extension of {@link ClientScopeRepresentation} which and adds additional functionality for OID4VCI + * + * @author Thomas Diesler + */ +public class CredentialScopeRepresentation extends ClientScopeRepresentation { + + public CredentialScopeRepresentation(String name) { + this.name = name; + this.protocol = OID4VCIConstants.OID4VC_PROTOCOL; + setBuildConfigHashAlgorithm(VC_BUILD_CONFIG_HASH_ALGORITHM_DEFAULT); + setBuildConfigSdJwtVisibleClaims(VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS_DEFAULT); + setBuildConfigTokenJwsType(VC_BUILD_CONFIG_TOKEN_JWS_TYPE_DEFAULT); + setSdJwtNumberOfDecoys(VC_SD_JWT_NUMBER_OF_DECOYS_DEFAULT); + setExpiryInSeconds(VC_EXPIRY_IN_SECONDS_DEFAULT); + setFormat(VC_FORMAT_DEFAULT); + } + + public CredentialScopeRepresentation(ClientScopeRepresentation clientScope) { + this.id = clientScope.getId(); + this.name = clientScope.getName(); + this.description = clientScope.getDescription(); + this.protocol = clientScope.getProtocol(); + this.attributes = clientScope.getAttributes(); + this.protocolMappers = clientScope.getProtocolMappers(); + } + + public boolean getIncludeInTokenScope() { + return Boolean.parseBoolean(getAttribute(INCLUDE_IN_TOKEN_SCOPE)); + } + + public CredentialScopeRepresentation setIncludeInTokenScope(boolean includeInScope) { + return setAttribute(INCLUDE_IN_TOKEN_SCOPE, String.valueOf(includeInScope)); + } + + public String getIssuerDid() { + return getAttribute(VC_ISSUER_DID); + } + + public CredentialScopeRepresentation setIssuerDid(String issuerDid) { + return setAttribute(VC_ISSUER_DID, issuerDid); + } + + public String getCredentialConfigurationId() { + return getAttribute(VC_CONFIGURATION_ID); + } + + public CredentialScopeRepresentation setCredentialConfigurationId(String credentialConfigurationId) { + return setAttribute(VC_CONFIGURATION_ID, credentialConfigurationId); + } + + public String getCredentialIdentifier() { + return getAttribute(VC_IDENTIFIER); + } + + public CredentialScopeRepresentation setCredentialIdentifier(String credentialIdentifier) { + return setAttribute(VC_IDENTIFIER, credentialIdentifier); + } + + public String getFormat() { + return getAttribute(VC_FORMAT); + } + + public CredentialScopeRepresentation setFormat(String credentialFormat) { + return setAttribute(VC_FORMAT, credentialFormat); + } + + public Integer getExpiryInSeconds() { + return Optional.ofNullable(getAttribute(VC_EXPIRY_IN_SECONDS)) + .map(Integer::parseInt) + .orElse(null); + } + + public CredentialScopeRepresentation setExpiryInSeconds(Integer expiryInSeconds) { + return setAttribute(VC_EXPIRY_IN_SECONDS, Optional.ofNullable(expiryInSeconds) + .map(String::valueOf) + .orElse(null)); + } + + public Integer getSdJwtNumberOfDecoys() { + return Optional.ofNullable(getAttribute(VC_SD_JWT_NUMBER_OF_DECOYS)) + .map(Integer::parseInt) + .orElse(null); + } + + public CredentialScopeRepresentation setSdJwtNumberOfDecoys(Integer sdJwtNumberOfDecoys) { + return setAttribute(VC_SD_JWT_NUMBER_OF_DECOYS, Optional.ofNullable(sdJwtNumberOfDecoys) + .map(String::valueOf) + .orElse(null)); + } + + public String getVct() { + return getAttribute(VCT); + } + + public CredentialScopeRepresentation setVct(String vct) { + return setAttribute(VCT, vct); + } + + public String getBuildConfigTokenJwsType() { + return getAttribute(VC_BUILD_CONFIG_TOKEN_JWS_TYPE); + } + + public CredentialScopeRepresentation setBuildConfigTokenJwsType(String tokenJwsType) { + return setAttribute(VC_BUILD_CONFIG_TOKEN_JWS_TYPE, tokenJwsType); + } + + public String getSigningKeyId() { + return getAttribute(VC_SIGNING_KEY_ID); + } + + public CredentialScopeRepresentation setSigningKeyId(String signingKeyId) { + return setAttribute(VC_SIGNING_KEY_ID, signingKeyId); + } + + public String getBuildConfigHashAlgorithm() { + return getAttribute(VC_BUILD_CONFIG_HASH_ALGORITHM); + } + + public CredentialScopeRepresentation setBuildConfigHashAlgorithm(String hashAlgorithm) { + return setAttribute(VC_BUILD_CONFIG_HASH_ALGORITHM, hashAlgorithm); + } + + public List getSupportedCredentialTypes() { + return Optional.ofNullable(getAttribute(VC_SUPPORTED_TYPES)) + .map(s -> s.split(",")) + .map(Arrays::asList) + .orElse(Collections.emptyList()); + } + + public CredentialScopeRepresentation setSupportedCredentialTypes(String supportedCredentialTypes) { + return setAttribute(VC_SUPPORTED_TYPES, supportedCredentialTypes); + } + + public CredentialScopeRepresentation setSupportedCredentialTypes(List supportedCredentialTypes) { + return setAttribute(VC_SUPPORTED_TYPES, String.join(",", supportedCredentialTypes)); + } + + public List getVcContexts() { + return Optional.ofNullable(getAttribute(VC_CONTEXTS)) + .map(s -> s.split(",")) + .map(Arrays::asList) + .orElse(Collections.emptyList()); + } + + public CredentialScopeRepresentation setVcContexts(String vcContexts) { + return setAttribute(VC_CONTEXTS, vcContexts); + } + + public CredentialScopeRepresentation setVcContexts(List vcContexts) { + return setAttribute(VC_CONTEXTS, String.join(",", vcContexts)); + } + + public String getSigningAlg() { + return getAttribute(VC_SIGNING_ALG); + } + + public CredentialScopeRepresentation setSigningAlg(String signingAlg) { + return setAttribute(VC_SIGNING_ALG, signingAlg); + } + + public List getCryptographicBindingMethods() { + return Optional.ofNullable(getAttribute(VC_CRYPTOGRAPHIC_BINDING_METHODS)) + .map(s -> s.split(",")) + .map(Arrays::asList) + .orElse(Collections.emptyList()); + } + + public CredentialScopeRepresentation setCryptographicBindingMethods(String cryptographicBindingMethods) { + return setAttribute(VC_CRYPTOGRAPHIC_BINDING_METHODS, cryptographicBindingMethods); + } + + public CredentialScopeRepresentation setCryptographicBindingMethods(List cryptographicBindingMethods) { + return setAttribute(VC_CRYPTOGRAPHIC_BINDING_METHODS, String.join(",", cryptographicBindingMethods)); + } + + public List getBuildConfigSdJwtVisibleClaims() { + return Optional.ofNullable(getAttribute(VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS)) + .map(s -> s.split(",")) + .map(Arrays::asList) + .orElse(Collections.emptyList()); + } + + public CredentialScopeRepresentation setBuildConfigSdJwtVisibleClaims(String sdJwtVisibleClaims) { + return setAttribute(VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS, sdJwtVisibleClaims); + } + + public CredentialScopeRepresentation setBuildConfigSdJwtVisibleClaims(List sdJwtVisibleClaims) { + return setAttribute(VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS, String.join(",", sdJwtVisibleClaims)); + } + + public String getDisplay() { + return getAttribute(VC_DISPLAY); + } + + public CredentialScopeRepresentation setDisplay(String vcDisplay) { + return setAttribute(VC_DISPLAY, vcDisplay); + } + + public boolean isKeyAttestationRequired() { + return Boolean.parseBoolean(getAttribute(VC_KEY_ATTESTATION_REQUIRED)); + } + + public CredentialScopeRepresentation setKeyAttestationRequired(boolean keyAttestationRequired) { + return setAttribute(VC_KEY_ATTESTATION_REQUIRED, String.valueOf(keyAttestationRequired)); + } + + public List getRequiredKeyAttestationKeyStorage() { + return Optional.ofNullable(getAttribute(VC_KEY_ATTESTATION_REQUIRED_KEY_STORAGE)) + .map(s -> Arrays.asList(s.split(","))) + // it is important to return null here instead of an empty list: + // If both key_storage and user_authentication parameters are absent, the + // key_attestations_required parameter may be empty, indicating a key attestation is needed + // without additional constraints. Meaning we must not add empty objects to the metadata endpoint + .orElse(null); + } + + public CredentialScopeRepresentation setRequiredKeyAttestationKeyStorage(List keyStorage) { + return setAttribute(VC_KEY_ATTESTATION_REQUIRED_KEY_STORAGE, Optional.ofNullable(keyStorage) + .map(list -> String.join(",")).orElse(null)); + } + + public List getRequiredKeyAttestationUserAuthentication() { + return Optional.ofNullable(getAttribute(VC_KEY_ATTESTATION_REQUIRED_USER_AUTH)) + .map(s -> Arrays.asList(s.split(","))) + // it is important to return null here instead of an empty list: + // If both key_storage and user_authentication parameters are absent, the + // key_attestations_required parameter may be empty, indicating a key attestation is needed + // without additional constraints. Meaning we must not add empty objects to the metadata endpoint + .orElse(null); + } + + public CredentialScopeRepresentation setRequiredKeyAttestationUserAuthentication(List userAuthentication) { + return setAttribute(VC_KEY_ATTESTATION_REQUIRED_USER_AUTH, Optional.ofNullable(userAuthentication) + .map(list -> String.join(",")).orElse(null)); + } + + // Private --------------------------------------------------------------------------------------------------------- + + private String getAttribute(String key) { + return attributes != null ? attributes.get(key) : null; + } + + private CredentialScopeRepresentation setAttribute(String key, String value) { + if (attributes == null) { + attributes = new LinkedHashMap<>(); + } + if (value != null) { + attributes.put(key, value); + } else { + attributes.remove(key); + } + return this; + } +} diff --git a/services/src/main/java/org/keycloak/protocol/oid4vc/utils/OID4VCUtil.java b/services/src/main/java/org/keycloak/protocol/oid4vc/utils/OID4VCUtil.java index 63e42ef4622..ae403866d14 100644 --- a/services/src/main/java/org/keycloak/protocol/oid4vc/utils/OID4VCUtil.java +++ b/services/src/main/java/org/keycloak/protocol/oid4vc/utils/OID4VCUtil.java @@ -5,12 +5,12 @@ import org.keycloak.models.ClientScopeModel; import org.keycloak.models.KeycloakSession; import org.keycloak.models.RealmModel; -import org.keycloak.models.oid4vci.CredentialScopeModel; import org.keycloak.utils.StringUtil; import org.jboss.logging.Logger; import static org.keycloak.constants.OID4VCIConstants.OID4VC_PROTOCOL; +import static org.keycloak.models.oid4vci.CredentialScopeModel.VC_CONFIGURATION_ID; public class OID4VCUtil { @@ -34,7 +34,7 @@ public static ClientScopeModel getClientScopeByCredentialConfigId(KeycloakSessio List clientScopes = session.clientScopes() .getClientScopesByProtocol(realmModel, OID4VC_PROTOCOL) - .filter(it -> credentialConfigId.equals(it.getAttribute(CredentialScopeModel.CONFIGURATION_ID))) + .filter(it -> credentialConfigId.equals(it.getAttribute(VC_CONFIGURATION_ID))) .toList(); if (clientScopes.size() > 1) { List clientScopeNames = clientScopes.stream() diff --git a/tests/base/src/test/java/org/keycloak/tests/admin/client/ClientScopeTestOid4Vci.java b/tests/base/src/test/java/org/keycloak/tests/admin/client/ClientScopeTestOid4Vci.java index 68da940bd70..7b723f2d23b 100644 --- a/tests/base/src/test/java/org/keycloak/tests/admin/client/ClientScopeTestOid4Vci.java +++ b/tests/base/src/test/java/org/keycloak/tests/admin/client/ClientScopeTestOid4Vci.java @@ -69,28 +69,28 @@ public void testDefaultOid4VciClientScopeAttributes() { ClientScopeRepresentation createdClientScope = clientScopes().get(clientScopeId).toRepresentation(); Assertions.assertNotNull(createdClientScope); - Assertions.assertEquals(CredentialScopeModel.SD_JWT_VISIBLE_CLAIMS_DEFAULT, - createdClientScope.getAttributes().get(CredentialScopeModel.SD_JWT_VISIBLE_CLAIMS)); - Assertions.assertEquals(String.valueOf(CredentialScopeModel.SD_JWT_DECOYS_DEFAULT), - createdClientScope.getAttributes().get(CredentialScopeModel.SD_JWT_NUMBER_OF_DECOYS)); - Assertions.assertEquals(CredentialScopeModel.FORMAT_DEFAULT, - createdClientScope.getAttributes().get(CredentialScopeModel.FORMAT)); - Assertions.assertEquals(CredentialScopeModel.HASH_ALGORITHM_DEFAULT, - createdClientScope.getAttributes().get(CredentialScopeModel.HASH_ALGORITHM)); - Assertions.assertEquals(CredentialScopeModel.TOKEN_TYPE_DEFAULT, - createdClientScope.getAttributes().get(CredentialScopeModel.TOKEN_JWS_TYPE)); - Assertions.assertEquals(String.valueOf(CredentialScopeModel.EXPIRY_IN_SECONDS_DEFAULT), - createdClientScope.getAttributes().get(CredentialScopeModel.EXPIRY_IN_SECONDS)); + Assertions.assertEquals(CredentialScopeModel.VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS_DEFAULT, + createdClientScope.getAttributes().get(CredentialScopeModel.VC_BUILD_CONFIG_SD_JWT_VISIBLE_CLAIMS)); + Assertions.assertEquals(String.valueOf(CredentialScopeModel.VC_SD_JWT_NUMBER_OF_DECOYS_DEFAULT), + createdClientScope.getAttributes().get(CredentialScopeModel.VC_SD_JWT_NUMBER_OF_DECOYS)); + Assertions.assertEquals(CredentialScopeModel.VC_FORMAT_DEFAULT, + createdClientScope.getAttributes().get(CredentialScopeModel.VC_FORMAT)); + Assertions.assertEquals(CredentialScopeModel.VC_BUILD_CONFIG_HASH_ALGORITHM_DEFAULT, + createdClientScope.getAttributes().get(CredentialScopeModel.VC_BUILD_CONFIG_HASH_ALGORITHM)); + Assertions.assertEquals(CredentialScopeModel.VC_BUILD_CONFIG_TOKEN_JWS_TYPE_DEFAULT, + createdClientScope.getAttributes().get(CredentialScopeModel.VC_BUILD_CONFIG_TOKEN_JWS_TYPE)); + Assertions.assertEquals(String.valueOf(CredentialScopeModel.VC_EXPIRY_IN_SECONDS_DEFAULT), + createdClientScope.getAttributes().get(CredentialScopeModel.VC_EXPIRY_IN_SECONDS)); Assertions.assertEquals(CredentialScopeModel.CRYPTOGRAPHIC_BINDING_METHODS_DEFAULT, - createdClientScope.getAttributes().get(CredentialScopeModel.CRYPTOGRAPHIC_BINDING_METHODS)); + createdClientScope.getAttributes().get(CredentialScopeModel.VC_CRYPTOGRAPHIC_BINDING_METHODS)); Assertions.assertEquals(clientScope.getName(), - createdClientScope.getAttributes().get(CredentialScopeModel.CONFIGURATION_ID)); + createdClientScope.getAttributes().get(CredentialScopeModel.VC_CONFIGURATION_ID)); Assertions.assertEquals(clientScope.getName(), - createdClientScope.getAttributes().get(CredentialScopeModel.CREDENTIAL_IDENTIFIER)); + createdClientScope.getAttributes().get(CredentialScopeModel.VC_IDENTIFIER)); Assertions.assertEquals(clientScope.getName(), - createdClientScope.getAttributes().get(CredentialScopeModel.TYPES)); + createdClientScope.getAttributes().get(CredentialScopeModel.VC_SUPPORTED_TYPES)); Assertions.assertEquals(clientScope.getName(), - createdClientScope.getAttributes().get(CredentialScopeModel.CONTEXTS)); + createdClientScope.getAttributes().get(CredentialScopeModel.VC_CONTEXTS)); Assertions.assertEquals(clientScope.getName(), createdClientScope.getAttributes().get(CredentialScopeModel.VCT)); // Note: ISSUER_DID is intentionally not set by default, as there's no sensible default