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 @@ -20,10 +20,10 @@
import org.keycloak.authorization.policy.evaluation.Realm;
import org.keycloak.models.ClientModel;
import org.keycloak.models.RealmModel;
import org.keycloak.common.util.Base64Url;

import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.UUID;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -51,8 +51,8 @@ public static IdentityBrokerState decoded(String state, String clientId, String
bb.putLong(clientDbUuid.getMostSignificantBits());
bb.putLong(clientDbUuid.getLeastSignificantBits());
byte[] clientUuidBytes = bb.array();
clientIdEncoded = Base64.getEncoder().encodeToString(clientUuidBytes).replace("=", "");
} catch (IllegalArgumentException e) {
clientIdEncoded = Base64Url.encode(clientUuidBytes);
} catch (RuntimeException e) {
// Ignore...the clientid in the database was not in UUID format. Just use as is.
}
}
Expand All @@ -73,7 +73,7 @@ public static IdentityBrokerState encoded(String encodedState, RealmModel realmM
try {
// If this decoding succeeds it was the result of the encoding of a UUID client.id - if it fails we interpret it as client.clientId
// in accordance to the method decoded above
byte[] decodedClientId = Base64.getDecoder().decode(clientId);
byte[] decodedClientId = Base64Url.decode(clientId);
ByteBuffer bb = ByteBuffer.wrap(decodedClientId);
long first = bb.getLong();
long second = bb.getLong();
Expand All @@ -83,7 +83,7 @@ public static IdentityBrokerState encoded(String encodedState, RealmModel realmM
if (clientModel != null) {
clientId = clientModel.getClientId();
}
} catch (IllegalArgumentException | BufferUnderflowException e) {
} catch (RuntimeException e) {
// Ignore...the clientid was not in encoded uuid format. Just use as it is.
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ public void testDecodedWithClientIdAnActualUuid() {
Assert.assertEquals("gNrGamIDGKpKSI9yOrcFzYTKoFGH779_WNCacAelkhk.vpISZLVDAc0.7UlEjBTPRx6oOgY9DcO8jA", encodedState.getEncoded());
}

@Test
public void testDecodedWithClientIdAnActualUuidBASE64UriFriendly() {

// Given
String state = "gNrGamIDGKpKSI9yOrcFzYTKoFGH779_WNCacAelkhk";
String clientId = "c5ac1ea7-6c28-4be1-b7cd-d63a1ba57f78";
String clientClientId = "http://i.am.an.url";
String tabId = "vpISZLVDAc0";

// When
IdentityBrokerState encodedState = IdentityBrokerState.decoded(state, clientId, clientClientId, tabId);

// Then
Assert.assertNotNull(encodedState);
Assert.assertEquals(clientClientId, encodedState.getClientId());
Assert.assertEquals(tabId, encodedState.getTabId());
Assert.assertEquals("gNrGamIDGKpKSI9yOrcFzYTKoFGH779_WNCacAelkhk.vpISZLVDAc0.xawep2woS-G3zdY6G6V_eA", encodedState.getEncoded());
}

@Test
public void testEncodedWithClientIdUUid() {
// Given
Expand Down