Skip to content

Commit 6638b84

Browse files
committed
update for Keycloak 3.4.3, version now matches Keycloak version
1 parent 0ae1021 commit 6638b84

File tree

7 files changed

+14
-64
lines changed

7 files changed

+14
-64
lines changed

.travis.yml

-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ services:
1313
- docker
1414

1515
env:
16-
# - KEYCLOAK_VERSION=2.5.5.Final
17-
# - KEYCLOAK_VERSION=3.0.0.Final
18-
# - KEYCLOAK_VERSION=3.1.0.Final
19-
- KEYCLOAK_VERSION=3.2.1.Final
20-
- KEYCLOAK_VERSION=3.3.0.Final
21-
- KEYCLOAK_VERSION=3.4.0.Final
2216
- KEYCLOAK_VERSION=3.4.3.Final
2317

2418
before_install:

integrationTest/suite.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ get_ticket() {
1212
exit 1
1313
fi
1414

15-
login_url=${BASH_REMATCH[1]}
15+
login_url=${BASH_REMATCH[1]//&/&}
1616
redirect_response=$(curl --fail --silent -D - -b /tmp/cookies --data 'username=admin&password=admin' "$login_url")
1717
if [[ !($redirect_response =~ $ticket_pattern) ]] ; then
1818
echo "No service ticket found in response"

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222

2323
<groupId>org.keycloak</groupId>
2424
<artifactId>keycloak-protocol-cas</artifactId>
25-
<version>2.1.1-SNAPSHOT</version>
25+
<version>3.4.3</version>
2626
<name>Keycloak CAS Protocol</name>
2727
<description />
2828

2929
<properties>
30-
<keycloak.version>3.2.0.Final</keycloak.version>
30+
<keycloak.version>${project.version}.Final</keycloak.version>
3131
<jboss.logging.version>3.3.0.Final</jboss.logging.version>
3232
<jboss.logging.tools.version>2.0.1.Final</jboss.logging.tools.version>
3333
<junit.version>4.12</junit.version>

src/main/java/org/keycloak/protocol/cas/CASLoginProtocol.java

+1-12
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212
import org.keycloak.services.managers.ClientSessionCode;
1313
import org.keycloak.services.managers.ResourceAdminManager;
1414
import org.keycloak.sessions.AuthenticationSessionModel;
15-
import org.keycloak.sessions.CommonClientSessionModel;
1615

1716
import javax.ws.rs.core.HttpHeaders;
1817
import javax.ws.rs.core.Response;
1918
import javax.ws.rs.core.UriInfo;
2019
import java.io.IOException;
21-
import java.lang.reflect.Method;
2220
import java.net.URI;
2321

2422
public class CASLoginProtocol implements LoginProtocol {
@@ -93,16 +91,7 @@ public Response authenticated(UserSessionModel userSession, AuthenticatedClientS
9391
String service = clientSession.getRedirectUri();
9492
//TODO validate service
9593

96-
String code;
97-
try {
98-
// Keycloak >3.4 branch: Method getCode was renamed to getOrGenerateCode, CODE_TO_TOKEN was removed
99-
Method getOrGenerateCode = ClientSessionCode.class.getMethod("getOrGenerateCode");
100-
code = (String) getOrGenerateCode.invoke(accessCode);
101-
} catch (ReflectiveOperationException e) {
102-
// Keycloak <=3.3 branch
103-
accessCode.setAction(CommonClientSessionModel.Action.CODE_TO_TOKEN.name());
104-
code = accessCode.getCode();
105-
}
94+
String code = accessCode.getOrGenerateCode();
10695
KeycloakUriBuilder uriBuilder = KeycloakUriBuilder.fromUri(service);
10796
uriBuilder.queryParam(TICKET_RESPONSE_PARAM, SERVICE_TICKET_PREFIX + code);
10897

src/main/java/org/keycloak/protocol/cas/endpoints/AuthorizationEndpoint.java

+4-14
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@ public Response build() {
4242
checkRealm();
4343
checkClient(service);
4444

45-
AuthorizationEndpointChecks checks = getOrCreateAuthenticationSession(client, null);
46-
if (checks.response != null) {
47-
return checks.response;
48-
}
49-
50-
authenticationSession = checks.authSession;
45+
authenticationSession = createAuthenticationSession(client, null);
5146
updateAuthenticationSession();
5247

5348
// So back button doesn't work
@@ -64,7 +59,7 @@ public Response build() {
6459
private void checkClient(String service) {
6560
if (service == null) {
6661
event.error(Errors.INVALID_REQUEST);
67-
throw new ErrorPageException(session, Messages.MISSING_PARAMETER, CASLoginProtocol.SERVICE_PARAM);
62+
throw new ErrorPageException(session, Response.Status.BAD_REQUEST, Messages.MISSING_PARAMETER, CASLoginProtocol.SERVICE_PARAM);
6863
}
6964

7065
client = realm.getClients().stream()
@@ -73,12 +68,12 @@ private void checkClient(String service) {
7368
.findFirst().orElse(null);
7469
if (client == null) {
7570
event.error(Errors.CLIENT_NOT_FOUND);
76-
throw new ErrorPageException(session, Messages.CLIENT_NOT_FOUND);
71+
throw new ErrorPageException(session, Response.Status.BAD_REQUEST, Messages.CLIENT_NOT_FOUND);
7772
}
7873

7974
if (!client.isEnabled()) {
8075
event.error(Errors.CLIENT_DISABLED);
81-
throw new ErrorPageException(session, Messages.CLIENT_DISABLED);
76+
throw new ErrorPageException(session, Response.Status.BAD_REQUEST, Messages.CLIENT_DISABLED);
8277
}
8378

8479
redirectUri = RedirectUtils.verifyRedirectUri(uriInfo, service, realm, client);
@@ -94,9 +89,4 @@ private void updateAuthenticationSession() {
9489
authenticationSession.setRedirectUri(redirectUri);
9590
authenticationSession.setAction(AuthenticationSessionModel.Action.AUTHENTICATE.name());
9691
}
97-
98-
@Override
99-
protected boolean isNewRequest(AuthenticationSessionModel authSession, ClientModel clientFromRequest, String requestState) {
100-
return true;
101-
}
10292
}

src/main/java/org/keycloak/protocol/cas/endpoints/LogoutEndpoint.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public Response logout(@QueryParam(CASLoginProtocol.SERVICE_PARAM) String servic
6666
logger.debug("finishing CAS browser logout");
6767
return response;
6868
}
69-
return ErrorPage.error(session, Messages.FAILED_LOGOUT);
69+
return ErrorPage.error(session, null, Response.Status.BAD_REQUEST, Messages.FAILED_LOGOUT);
7070
}
7171

7272
private void checkClient(String service) {

src/main/java/org/keycloak/protocol/cas/endpoints/ValidateEndpoint.java

+5-28
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import javax.ws.rs.GET;
2020
import javax.ws.rs.core.*;
21-
import java.lang.reflect.Method;
2221

2322
public class ValidateEndpoint {
2423
protected static final Logger logger = Logger.getLogger(ValidateEndpoint.class);
@@ -137,46 +136,24 @@ private void checkTicket(String ticket, boolean requireReauth) {
137136
event.detail(Details.CODE_ID, parts[2]);
138137
}
139138

140-
ClientSessionCode.ParseResult<AuthenticatedClientSessionModel> parseResult;
141-
try {
142-
// Keycloak >3.4 branch: Parameter event was added to ClientSessionCode.parseResult
143-
Method parseResultMethod = ClientSessionCode.class.getMethod("parseResult",
144-
String.class, KeycloakSession.class, RealmModel.class, EventBuilder.class, Class.class);
145-
parseResult = (ClientSessionCode.ParseResult<AuthenticatedClientSessionModel>) parseResultMethod.invoke(
146-
null, code, session, realm, event, AuthenticatedClientSessionModel.class);
147-
} catch (ReflectiveOperationException e) {
148-
// Keycloak <=3.3 branch
149-
parseResult = ClientSessionCode.parseResult(code, session, realm, AuthenticatedClientSessionModel.class);
150-
}
139+
ClientSessionCode.ParseResult<AuthenticatedClientSessionModel> parseResult = ClientSessionCode.parseResult(code, null, session, realm, client, event, AuthenticatedClientSessionModel.class);
151140
if (parseResult.isAuthSessionNotFound() || parseResult.isIllegalHash()) {
152141
event.error(Errors.INVALID_CODE);
153142

154143
// Attempt to use same code twice should invalidate existing clientSession
155144
AuthenticatedClientSessionModel clientSession = parseResult.getClientSession();
156145
if (clientSession != null) {
157-
clientSession.setUserSession(null);
146+
clientSession.detachFromUserSession();
158147
}
159148

160149
throw new CASValidationException(CASErrorCode.INVALID_TICKET, "Code not valid", Response.Status.BAD_REQUEST);
161150
}
162151

163152
clientSession = parseResult.getClientSession();
164153

165-
try {
166-
// Keycloak >3.4 branch: Method isExpiredToken was added
167-
Method isExpiredToken = ClientSessionCode.ParseResult.class.getMethod("isExpiredToken");
168-
if ((Boolean) isExpiredToken.invoke(parseResult)) {
169-
event.error(Errors.EXPIRED_CODE);
170-
throw new CASValidationException(CASErrorCode.INVALID_TICKET, "Code is expired", Response.Status.BAD_REQUEST);
171-
}
172-
} catch (ReflectiveOperationException e) {
173-
// Keycloak <=3.3 branch
174-
if (!parseResult.getCode().isValid(AuthenticatedClientSessionModel.Action.CODE_TO_TOKEN.name(), ClientSessionCode.ActionType.CLIENT)) {
175-
event.error(Errors.INVALID_CODE);
176-
throw new CASValidationException(CASErrorCode.INVALID_TICKET, "Code is expired", Response.Status.BAD_REQUEST);
177-
}
178-
179-
parseResult.getCode().setAction(null);
154+
if (parseResult.isExpiredToken()) {
155+
event.error(Errors.EXPIRED_CODE);
156+
throw new CASValidationException(CASErrorCode.INVALID_TICKET, "Code is expired", Response.Status.BAD_REQUEST);
180157
}
181158

182159
clientSession.setNote(CASLoginProtocol.SESSION_SERVICE_TICKET, ticket);

0 commit comments

Comments
 (0)