mirror of https://github.com/jacekkow/keycloak-protocol-cas

0eba0a991c2c00deff05f8da67dcc6e34d8a8f7e..1c53e109b7be3f0278736087ac75d962bbc6cb67
2026-02-08 Aggelos Sachtouris
Fix CAS ticket format to support allowed character set
1c53e1 diff | tree
2026-02-10 github-actions
Update to Keycloak 26.5.3
e252cf diff | tree
2 files modified
33 ■■■■ changed files
pom.xml 6 ●●●● patch | view | raw | blame | history
src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java 27 ●●●● patch | view | raw | blame | history
pom.xml
@@ -22,7 +22,7 @@
    <groupId>org.keycloak</groupId>
    <artifactId>keycloak-protocol-cas</artifactId>
    <version>26.5.2</version>
    <version>26.5.3</version>
    <name>Keycloak CAS Protocol</name>
    <description />
@@ -31,7 +31,7 @@
        <apache.httpcomponents.version>4.5.14</apache.httpcomponents.version>
        <jboss.logging.version>3.6.1.Final</jboss.logging.version>
        <jboss.logging.tools.version>3.0.4.Final</jboss.logging.tools.version>
        <junit.version>6.0.2</junit.version>
        <junit.version>5.13.4</junit.version>
        <resteasy.version>6.2.12.Final</resteasy.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -39,7 +39,7 @@
        <maven.compiler.release>${java.version}</maven.compiler.release>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.outputTimestamp>1769244879</project.build.outputTimestamp>
        <project.build.outputTimestamp>1770716421</project.build.outputTimestamp>
    </properties>
    <dependencies>
src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java
@@ -18,7 +18,9 @@
import org.keycloak.services.managers.UserSessionCrossDCManager;
import org.keycloak.services.util.DefaultClientSessionContext;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.HexFormat;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@@ -32,6 +34,7 @@
public abstract class AbstractValidateEndpoint {
    protected final Logger logger = Logger.getLogger(getClass());
    private static final Pattern DOT = Pattern.compile("\\.");
    private static final Pattern SEPERATION_PATTERN = Pattern.compile("--");
    protected KeycloakSession session;
    protected RealmModel realm;
    protected EventBuilder event;
@@ -95,16 +98,23 @@
            throw new CASValidationException(CASErrorCode.INVALID_TICKET_SPEC, "Malformed service ticket", Response.Status.BAD_REQUEST);
        }
        boolean isReusable = ticket.startsWith(CASLoginProtocol.PROXY_GRANTING_TICKET_PREFIX);
        String ticketValue = ticket.substring(prefix.length());
        String[] parsed = DOT.split(ticket.substring(prefix.length()), 3);
        boolean isReusable = ticket.startsWith(CASLoginProtocol.PROXY_GRANTING_TICKET_PREFIX);
        // Check if the ticket is hex encoded or using old method
        String[] parsed;
        if (ticketValue.contains(".")) {
            parsed = DOT.split(ticketValue, 3);
        } else {
            parsed = SEPERATION_PATTERN.split(ticketValue, 3);
        }
        if (parsed.length != 3) {
            event.error(Errors.INVALID_CODE);
            throw new CASValidationException(CASErrorCode.INVALID_TICKET_SPEC, "Invalid format of the code", Response.Status.BAD_REQUEST);
        }
        String codeUUID = parsed[0];
        String userSessionId = parsed[1];
        String userSessionId = ticketValue.contains(".") ? parsed[1] : hexDecode(parsed[1]);
        String clientUUID = parsed[2];
        event.detail(Details.CODE_ID, userSessionId);
@@ -262,6 +272,15 @@
        UserSessionModel userSession = clientSession.getUserSession();
        OAuth2Code codeData = new OAuth2Code(key, Time.currentTime() + userSession.getRealm().getAccessCodeLifespan(), null, null, redirectUriParam, null, null, null, userSession.getId());
        session.singleUseObjects().put(prefix + key, clientSession.getUserSession().getRealm().getAccessCodeLifespan(), codeData.serializeCode());
        return prefix + key + "." + clientSession.getUserSession().getId() + "." + clientSession.getClient().getId();
        return prefix + key + "--" + hexEncode(clientSession.getUserSession().getId()) + "--" + clientSession.getClient().getId();
    }
    private String hexEncode(String value) {
        return HexFormat.of().formatHex(value.getBytes(StandardCharsets.US_ASCII));
    }
    private String hexDecode(String hex) {
        byte[] bytes = HexFormat.of().parseHex(hex);
        return new String(bytes, StandardCharsets.US_ASCII);
    }
}