From 3a804315cdead9711369ccb988be82e7930f8db2 Mon Sep 17 00:00:00 2001 From: Kristaps Berzinch Date: Tue, 17 Sep 2024 20:35:59 -0400 Subject: [PATCH 1/4] Store state in cookie instead of service URL --- .../broker/cas/CasIdentityProvider.java | 58 +++++++------------ .../keycloak/broker/cas/util/UrlHelper.java | 25 ++------ 2 files changed, 26 insertions(+), 57 deletions(-) diff --git a/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java b/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java index dc060e0..b7d12e7 100644 --- a/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java +++ b/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java @@ -1,6 +1,5 @@ package io.github.johnjcool.keycloak.broker.cas; -import static io.github.johnjcool.keycloak.broker.cas.util.UrlHelper.PROVIDER_PARAMETER_STATE; import static io.github.johnjcool.keycloak.broker.cas.util.UrlHelper.PROVIDER_PARAMETER_TICKET; import static io.github.johnjcool.keycloak.broker.cas.util.UrlHelper.createAuthenticationUrl; import static io.github.johnjcool.keycloak.broker.cas.util.UrlHelper.createLogoutUrl; @@ -8,15 +7,11 @@ import io.github.johnjcool.keycloak.broker.cas.model.ServiceResponse; import io.github.johnjcool.keycloak.broker.cas.model.Success; +import jakarta.ws.rs.CookieParam; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.QueryParam; -import jakarta.ws.rs.core.Context; -import jakarta.ws.rs.core.HttpHeaders; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; -import jakarta.ws.rs.core.Response.Status; -import jakarta.ws.rs.core.UriInfo; +import jakarta.ws.rs.core.*; import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.Unmarshaller; @@ -48,6 +43,8 @@ public class CasIdentityProvider extends AbstractIdentityProvider Date: Wed, 18 Sep 2024 20:09:23 -0400 Subject: [PATCH 2/4] Remove unneeded event parameter --- .../johnjcool/keycloak/broker/cas/CasIdentityProvider.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java b/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java index b7d12e7..f41c010 100644 --- a/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java +++ b/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java @@ -94,7 +94,7 @@ public Endpoint callback( final RealmModel realm, final org.keycloak.broker.provider.IdentityProvider.AuthenticationCallback callback, final EventBuilder event) { - return new Endpoint(callback, realm, event, this); + return new Endpoint(callback, realm, this); } public static final class Endpoint { @@ -109,7 +109,6 @@ public static final class Endpoint { Endpoint( final AuthenticationCallback callback, final RealmModel realm, - final EventBuilder event, final CasIdentityProvider provider) { this.callback = callback; this.realm = realm; From 3b86ad24d62030ce86cee4b698c5541effc0344e Mon Sep 17 00:00:00 2001 From: Kristaps Berzinch Date: Wed, 18 Sep 2024 20:15:40 -0400 Subject: [PATCH 3/4] Remove unneeded local variables --- .../keycloak/broker/cas/CasIdentityProvider.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java b/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java index f41c010..0b753dd 100644 --- a/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java +++ b/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java @@ -17,7 +17,6 @@ import jakarta.xml.bind.Unmarshaller; import java.io.IOException; import java.io.StringReader; -import java.net.URI; import org.jboss.logging.Logger; import org.keycloak.broker.provider.AbstractIdentityProvider; import org.keycloak.broker.provider.AuthenticationRequest; @@ -79,8 +78,9 @@ public Response keycloakInitiatedBrowserLogout( final UserSessionModel userSession, final UriInfo uriInfo, final RealmModel realm) { - URI logoutUrl = createLogoutUrl(getConfig(), realm, uriInfo).build(); - return Response.status(302).location(logoutUrl).build(); + return Response.status(302) + .location(createLogoutUrl(getConfig(), realm, uriInfo).build()) + .build(); } @Override @@ -123,11 +123,8 @@ public static final class Endpoint { public Response authResponse( @QueryParam(PROVIDER_PARAMETER_TICKET) final String ticket, @CookieParam(STATE_COOKIE_NAME) final Cookie stateCookie) { - BrokeredIdentityContext federatedIdentity = - getFederatedIdentity( - config, ticket, session.getContext().getUri(), stateCookie.getValue()); - - return callback.authenticated(federatedIdentity); + return callback.authenticated(getFederatedIdentity( + config, ticket, session.getContext().getUri(), stateCookie.getValue())); } @GET From e7394181876244f8190b32ddc422d0a8ceb89b2c Mon Sep 17 00:00:00 2001 From: Kristaps Berzinch Date: Wed, 18 Sep 2024 20:17:05 -0400 Subject: [PATCH 4/4] Format --- .../johnjcool/keycloak/broker/cas/CasIdentityProvider.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java b/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java index 0b753dd..3833611 100644 --- a/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java +++ b/src/main/java/io/github/johnjcool/keycloak/broker/cas/CasIdentityProvider.java @@ -123,8 +123,9 @@ public static final class Endpoint { public Response authResponse( @QueryParam(PROVIDER_PARAMETER_TICKET) final String ticket, @CookieParam(STATE_COOKIE_NAME) final Cookie stateCookie) { - return callback.authenticated(getFederatedIdentity( - config, ticket, session.getContext().getUri(), stateCookie.getValue())); + return callback.authenticated( + getFederatedIdentity( + config, ticket, session.getContext().getUri(), stateCookie.getValue())); } @GET