forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KEYCLOAK-18146 Search for clients by client attribute when doing saml…
… artifact resolution
- Loading branch information
Showing
26 changed files
with
364 additions
and
52 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...onnections/jpa/updater/liquibase/custom/JpaUpdate14_0_0_MigrateSamlArtifactAttribute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2020 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.connections.jpa.updater.liquibase.custom; | ||
|
||
import liquibase.exception.CustomChangeException; | ||
import liquibase.statement.core.InsertStatement; | ||
import liquibase.structure.core.Table; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString; | ||
|
||
public class JpaUpdate14_0_0_MigrateSamlArtifactAttribute extends CustomKeycloakTask { | ||
|
||
private static final String SAML_ARTIFACT_BINDING_IDENTIFIER = "saml.artifact.binding.identifier"; | ||
|
||
private final Map<String, String> clientIds = new HashMap<>(); | ||
|
||
@Override | ||
protected void generateStatementsImpl() throws CustomChangeException { | ||
extractClientsData("SELECT C.ID, C.CLIENT_ID FROM " + getTableName("CLIENT") + " C " + | ||
"LEFT JOIN " + getTableName("CLIENT_ATTRIBUTES") + " CA " + | ||
"ON C.ID = CA.CLIENT_ID AND CA.NAME='" + SAML_ARTIFACT_BINDING_IDENTIFIER + "' " + | ||
"WHERE C.PROTOCOL='saml' AND CA.NAME IS NULL"); | ||
|
||
for (Map.Entry<String, String> clientPair : clientIds.entrySet()) { | ||
String id = clientPair.getKey(); | ||
|
||
String clientId = clientPair.getValue(); | ||
String samlIdentifier = computeArtifactBindingIdentifierString(clientId); | ||
|
||
statements.add( | ||
new InsertStatement(null, null, database.correctObjectName("CLIENT_ATTRIBUTES", Table.class)) | ||
.addColumnValue("CLIENT_ID", id) | ||
.addColumnValue("NAME", SAML_ARTIFACT_BINDING_IDENTIFIER) | ||
.addColumnValue("VALUE", samlIdentifier) | ||
); | ||
} | ||
} | ||
|
||
private void extractClientsData(String sql) throws CustomChangeException { | ||
try (PreparedStatement statement = jdbcConnection.prepareStatement(sql); | ||
ResultSet rs = statement.executeQuery()) { | ||
|
||
while (rs.next()) { | ||
String id = rs.getString(1); | ||
String clientId = rs.getString(2); | ||
|
||
if (id == null || id.trim().isEmpty() | ||
|| clientId == null || clientId.trim().isEmpty()) { | ||
continue; | ||
} | ||
|
||
clientIds.put(id, clientId); | ||
} | ||
|
||
} catch (Exception e) { | ||
throw new CustomChangeException(getTaskId() + ": Exception when extracting data from previous version", e); | ||
} | ||
} | ||
|
||
@Override | ||
protected String getTaskId() { | ||
return "Migrate Saml attributes (14.0.0)"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
server-spi-private/src/main/java/org/keycloak/protocol/saml/util/ArtifactBindingUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.keycloak.protocol.saml.util; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Base64; | ||
|
||
public class ArtifactBindingUtils { | ||
public static String artifactToResolverProviderId(String artifact) { | ||
return byteArrayToResolverProviderId(Base64.getDecoder().decode(artifact)); | ||
} | ||
|
||
public static String byteArrayToResolverProviderId(byte[] ar) { | ||
return String.format("%02X%02X", ar[0], ar[1]); | ||
} | ||
|
||
/** | ||
* Computes identifier from the given String, for example, from entityId | ||
* | ||
* @param identifierFrom String that will be turned into an identifier | ||
* @return Base64 of SHA-1 hash of the identifierFrom | ||
*/ | ||
public static String computeArtifactBindingIdentifierString(String identifierFrom) { | ||
return Base64.getEncoder().encodeToString(computeArtifactBindingIdentifier(identifierFrom)); | ||
} | ||
|
||
/** | ||
* Turns byte representation of the identifier into readable String | ||
* | ||
* @param identifier byte representation of the identifier | ||
* @return Base64 of the identifier | ||
*/ | ||
public static String getArtifactBindingIdentifierString(byte[] identifier) { | ||
return Base64.getEncoder().encodeToString(identifier); | ||
} | ||
|
||
/** | ||
* Computes 20 bytes long byte identifier of the given string, for example, from entityId | ||
* | ||
* @param identifierFrom String that will be turned into an identifier | ||
* @return SHA-1 hash of the given identifierFrom | ||
*/ | ||
public static byte[] computeArtifactBindingIdentifier(String identifierFrom) { | ||
try { | ||
MessageDigest sha1Digester = MessageDigest.getInstance("SHA-1"); | ||
return sha1Digester.digest(identifierFrom.getBytes(StandardCharsets.UTF_8)); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException("JVM does not support required cryptography algorithms: SHA-1/SHA1PRNG.", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
services/src/main/java/org/keycloak/protocol/util/ArtifactBindingUtils.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.