8000 [OID4VCI] Migrate OID4VCIWellKnownProviderTest by tdiesler · Pull Request #46672 · 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
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.keycloak.protocol.oid4vc.issuance;

import java.util.List;
import java.util.Map;

import org.keycloak.protocol.oid4vc.model.ClaimsDescription;
import org.keycloak.protocol.oid4vc.model.OID4VCAuthorizationDetail;
import org.keycloak.representations.AuthorizationDetailsJSONRepresentation;
import org.keycloak.util.AuthorizationDetailsParser;

import static org.keycloak.OID4VCConstants.CREDENTIAL_CONFIGURATION_ID;
import static org.keycloak.OID4VCConstants.CREDENTIAL_IDENTIFIERS;
import static org.keycloak.protocol.oid4vc.model.ClaimsDescription.MANDATORY;
import static org.keycloak.protocol.oid4vc.model.ClaimsDescription.PATH;
import static org.keycloak.protocol.oid4vc.model.OID4VCAuthorizationDetail.CLAIMS;

public class OID4VCAuthorizationDetailsParser implements AuthorizationDetailsParser {

@Override
public <T extends AuthorizationDetailsJSONRepresentation> T asSubtype(AuthorizationDetailsJSONRepresentation authzDetail, Class<T> clazz) {
if (OID4VCAuthorizationDetail.class.equals(clazz)) {
if (authzDetail instanceof OID4VCAuthorizationDetail) {
return clazz.cast(authzDetail);
} else {
OID4VCAuthorizationDetail detail = new OID4VCAuthorizationDetail();
fillFields(authzDetail, detail);
return clazz.cast(detail);
}
} else {
throw new IllegalArgumentException("Authorization details '" + authzDetail + "' is unsupported to be parsed to '" + clazz + "'.");
}
}

private void fillFields(AuthorizationDetailsJSONRepresentation inDetail, OID4VCAuthorizationDetail outDetail) {
outDetail.setType(inDetail.getType());
outDetail.setLocations(inDetail.getLocations());
outDetail.setCredentialConfigurationId((String) inDetail.getCustomData().get(CREDENTIAL_CONFIGURATION_ID));
outDetail.setCredentialIdentifiers((List<String>) inDetail.getCustomData().get(CREDENTIAL_IDENTIFIERS));
outDetail.setClaims(parseClaims((List<Map>) inDetail.getCustomData().get(CLAIMS)));
}

private static List<ClaimsDescription> parseClaims(List<Map> genericClaims) {
if (genericClaims == null) {
return null;
}

return genericClaims.stream()
.map(claim -> {
List<Object> path = (List<Object>) claim.get(PATH);
Boolean mandatory = (Boolean) claim.get(MANDATORY);
return new ClaimsDescription(path, mandatory);
})
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,12 @@
import org.keycloak.protocol.oidc.rar.AuthorizationDetailsProcessor;
import org.keycloak.protocol.oidc.rar.InvalidAuthorizationDetailsException;
import org.keycloak.representations.AuthorizationDetailsJSONRepresentation;
import org.keycloak.util.AuthorizationDetailsParser;
import org.keycloak.util.JsonSerialization;

import org.jboss.logging.Logger;

import static org.keycloak.OID4VCConstants.CREDENTIAL_CONFIGURATION_ID;
import static org.keycloak.OID4VCConstants.CREDENTIAL_IDENTIFIERS;
import static org.keycloak.OID4VCConstants.OPENID_CREDENTIAL;
import static org.keycloak.models.Constants.AUTHORIZATION_DETAILS_RESPONSE;
import static org.keycloak.protocol.oid4vc.model.ClaimsDescription.MANDATORY;
import static org.keycloak.protocol.oid4vc.model.ClaimsDescription.PATH;
import static org.keycloak.protocol.oid4vc.model.OID4VCAuthorizationDetail.CLAIMS;

public class OID4VCAuthorizationDetailsProcessor implements AuthorizationDetailsProcessor<OID4VCAuthorizationDetail> {
private static final Logger logger = Logger.getLogger(OID4VCAuthorizationDetailsProcessor.class);
Expand Down Expand Up @@ -407,45 +401,4 @@ public void close() {
// No cleanup needed
}


public static class OID4VCAuthorizationDetailsParser implements AuthorizationDetailsParser {

@Override
public <T extends AuthorizationDetailsJSONRepresentation> T asSubtype(AuthorizationDetailsJSONRepresentation authzDetail, Class<T> clazz) {
if (OID4VCAuthorizationDetail.class.equals(clazz)) {
if (authzDetail instanceof OID4VCAuthorizationDetail) {
return clazz.cast(authzDetail);
} else {
OID4VCAuthorizationDetail detail = new OID4VCAuthorizationDetail();
fillFields(authzDetail, detail);
return clazz.cast(detail);
}
} else {
throw new IllegalArgumentException("Authorization details '" + authzDetail + "' is unsupported to be parsed to '" + clazz + "'.");
}
}

private void fillFields(AuthorizationDetailsJSONRepresentation inDetail, OID4VCAuthorizationDetail outDetail) {
outDetail.setType(inDetail.getType());
outDetail.setLocations(inDetail.getLocations());
outDetail.setCredentialConfigurationId((String) inDetail.getCustomData().get(CREDENTIAL_CONFIGURATION_ID));
outDetail.setCredentialIdentifiers((List<String>) inDetail.getCustomData().get(CREDENTIAL_IDENTIFIERS));
outDetail.setClaims(parseClaims((List<Map>) inDetail.getCustomData().get(CLAIMS)));
}

private static List<ClaimsDescription> parseClaims(List<Map> genericClaims) {
if (genericClaims == null) {
return null;
}

return genericClaims.stream()
.map(claim -> {
List<Object> path = (List<Object>) claim.get(PATH);
Boolean mandatory = (Boolean) claim.get(MANDATORY);
return new ClaimsDescription(path, mandatory);
})
.toList();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public OID4VCAuthorizationDetailsProcessor create(KeycloakSession session) {

@Override
public void init(Config.Scope config) {
AuthorizationDetailsParser.registerParser(OPENID_CREDENTIAL, new OID4VCAuthorizationDetailsProcessor.OID4VCAuthorizationDetailsParser());
AuthorizationDetailsParser.registerParser(OPENID_CREDENTIAL, new OID4VCAuthorizationDetailsParser());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class OID4VCAuthorizationDetailsProcessorTest {

@BeforeClass
public static void beforeClass() {
AuthorizationDetailsParser.registerParser(OPENID_CREDENTIAL, new OID4VCAuthorizationDetailsProcessor.OID4VCAuthorizationDetailsParser());
AuthorizationDetailsParser.registerParser(OPENID_CREDENTIAL, new OID4VCAuthorizationDetailsParser());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/*
* 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.tests.oid4vc;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.keycloak.admin.client.resource.ComponentsResource;
import org.keycloak.common.util.CertificateUtils;
import org.keycloak.common.util.KeyUtils;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.common.util.PemUtils;
import org.keycloak.common.util.SecretGenerator;
import org.keycloak.crypto.Algorithm;
import org.keycloak.crypto.KeyUse;
import org.keycloak.crypto.KeyWrapper;
import org.keycloak.keys.KeyProvider;
import org.keycloak.protocol.oid4vc.issuance.OID4VCIssuerWellKnownProvider;
import org.keycloak.protocol.oid4vc.model.CredentialIssuer;
import org.keycloak.protocol.oid4vc.model.CredentialResponseEncryptionMetadata;
import org.keycloak.representations.idm.ComponentRepresentation;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.annotations.TestSetup;
import org.keycloak.testframework.remote.runonserver.InjectRunOnServer;
import org.keycloak.testframework.remote.runonserver.RunOnServerClient;

import org.junit.jupiter.api.Test;

import static org.keycloak.common.crypto.CryptoConstants.A128KW;
import static org.keycloak.common.crypto.CryptoConstants.RSA_OAEP;
import static org.keycloak.jose.jwe.JWEConstants.RSA_OAEP_256;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@KeycloakIntegrationTest(config = OID4VCIssuerTestBase.VCTestServerConfig.class)
public class OID4VCIWellKnownProviderTest extends OID4VCIssuerTestBase {

@InjectRunOnServer
RunOnServerClient runOnServer;

boolean configuredAlready;

@TestSetup
public void configureTestRealm() {

// When named differently this method is called before OID4VCIssuerTestBase.configureTestRealm()
// When named the same name, it is called twice (i.e. does not override)
//
// [TODO] IntegrationTest sub class @TestSetup called before super class
// https://github.com/keycloak/keycloak/issues/46667

if (!configuredAlready) {
super.configureTestRealm();

ComponentsResource components = testRealm.admin().components();
components.add(getRsaKeyProvider(getRsaKey_Default())).close();
components.add(getRsaEncKeyProvider(RSA_OAEP_256, "enc-key-oaep256", 100)).close();
components.add(getAesKeyProvider(A128KW, "aes-enc", "ENC", "aes-generated")).close();
components.add(getAesKeyProvider(Algorithm.HS256, "aes-sig", "SIG", "hmac-generated")).close();

configuredAlready = true;
}
}

@Test
public void assertOnlyAsymmetricIncluded() {

// Server-side debugging: KC_TEST_SERVER = embedded
//
runOnServer.run(session -> {
CredentialIssuer credentialIssuer = new OID4VCIssuerWellKnownProvider(session).getIssuerMetadata();
CredentialResponseEncryptionMetadata credentialResponseEncryption = credentialIssuer.getCredentialResponseEncryption();
List<String> algValuesSupported = credentialResponseEncryption.getAlgValuesSupported();
assertEquals(2, algValuesSupported.size(), "Two asymmetric encryption are present.");
assertTrue(algValuesSupported.contains(RSA_OAEP), "The default algorithm for asymmetric encryption should be available as well.");
assertTrue(algValuesSupported.contains(RSA_OAEP_256), "The algorithm of the configured asymmetric encryption key should be provided.");
});
}

ComponentRepresentation getAesKeyProvider(String algorithm, String keyName, String keyUse, String providerId) {
// Generate a random AES key (default length: 256 bits)
byte[] secret = SecretGenerator.getInstance().randomBytes(32); // 32 bytes = 256 bits

String secretBase64 = Base64.getEncoder().encodeToString(secret);

ComponentRepresentation component = new ComponentRepresentation();
component.setProviderType(KeyProvider.class.getName());
component.setName(keyName);
component.setId(UUID.randomUUID().toString());
component.setProviderId(providerId);

component.setConfig(new MultivaluedHashMap<>(
Map.of(
"secret", List.of(secretBase64),
"active", List.of("true"),
"priority", List.of(String.valueOf(100)),
"enabled", List.of("true"),
"algorithm", List.of(algorithm),
"keyUse", List.of(keyUse) // encryption usage
)
));
return component;
}

ComponentRepresentation getRsaKeyProvider(KeyWrapper keyWrapper) {
ComponentRepresentation component = new ComponentRepresentation();
component.setProviderType(KeyProvider.class.getName());
component.setName("rsa-key-provider");
component.setId(UUID.randomUUID().toString());
component.setProviderId("rsa");

Certificate certificate = CertificateUtils.generateV1SelfSignedCertificate(
new KeyPair((PublicKey) keyWrapper.getPublicKey(), (PrivateKey) keyWrapper.getPrivateKey()), "TestKey");

component.setConfig(new MultivaluedHashMap<>(
Map.of(
"privateKey", List.of(PemUtils.encodeKey(keyWrapper.getPrivateKey())),
"certificate", List.of(PemUtils.encodeCertificate(certificate)),
"active", List.of("true"),
"priority", List.of("0"),
"enabled", List.of("true"),
"algorithm", List.of(keyWrapper.getAlgorithm()),
"keyUse", List.of(keyWrapper.getUse().name())
)
));
return component;
}

ComponentRepresentation getRsaEncKeyProvider(String algorithm, String keyName, int priority) {
ComponentRepresentation component = new ComponentRepresentation();
component.setProviderType(KeyProvider.class.getName());
component.setName(keyName);
component.setId(UUID.randomUUID().toString());
component.setProviderId("rsa");

KeyWrapper keyWrapper = getRsaKey(KeyUse.ENC, algorithm, keyName);
Certificate certificate = CertificateUtils.generateV1SelfSignedCertificate(
new KeyPair((PublicKey) keyWrapper.getPublicKey(), (PrivateKey) keyWrapper.getPrivateKey()), "TestKey");

component.setConfig(new MultivaluedHashMap<>(
Map.of(
"privateKey", List.of(PemUtils.encodeKey(keyWrapper.getPrivateKey())),
"certificate", List.of(PemUtils.encodeCertificate(certificate)),
"active", List.of("true"),
"priority", List.of(String.valueOf(priority)),
"enabled", List.of("true"),
"algorithm", List.of(algorithm),
"keyUse", List.of(KeyUse.ENC.name())
)
));
return component;
}

KeyWrapper getRsaKey_Default() {
return getRsaKey(KeyUse.SIG, "RS256", null);
}

KeyWrapper getRsaKey(KeyUse keyUse, String algorithm, String keyName) {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
var keyPair = kpg.generateKeyPair();
KeyWrapper kw = new KeyWrapper();
kw.setPrivateKey(keyPair.getPrivate());
kw.setPublicKey(keyPair.getPublic());
kw.setUse(keyUse);
kw.setKid(keyName != null ? keyName : KeyUtils.createKeyId(keyPair.getPublic()));
kw.setType("RSA");
kw.setAlgorithm(algorithm);
return kw;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
Loading
Loading
0