From ceed8fb052685c0105d225a61d8574d36e171166 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Fri, 24 Nov 2023 13:37:32 +0000
Subject: [PATCH] Get rid of @Context variables in Endpoint classes

---
 src/main/java/org/keycloak/protocol/cas/CASLoginProtocolService.java            |   14 ++-----
 src/main/java/org/keycloak/protocol/cas/endpoints/SamlValidateEndpoint.java     |    5 +-
 src/main/java/org/keycloak/protocol/cas/endpoints/ServiceValidateEndpoint.java  |    8 +--
 src/main/java/org/keycloak/protocol/cas/endpoints/ValidateEndpoint.java         |    5 +-
 src/main/java/org/keycloak/protocol/cas/endpoints/LogoutEndpoint.java           |   19 +--------
 src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java |   12 +----
 6 files changed, 19 insertions(+), 44 deletions(-)

diff --git a/src/main/java/org/keycloak/protocol/cas/CASLoginProtocolService.java b/src/main/java/org/keycloak/protocol/cas/CASLoginProtocolService.java
index e8cbb39..97e4491 100644
--- a/src/main/java/org/keycloak/protocol/cas/CASLoginProtocolService.java
+++ b/src/main/java/org/keycloak/protocol/cas/CASLoginProtocolService.java
@@ -18,12 +18,6 @@
     private RealmModel realm;
     private EventBuilder event;
 
-    @Context
-    private HttpHeaders headers;
-
-    @Context
-    private HttpRequest request;
-
     public CASLoginProtocolService(KeycloakSession session, EventBuilder event) {
         this.session = session;
         this.realm = session.getContext().getRealm();
@@ -43,28 +37,28 @@
 
     @Path("logout")
     public Object logout() {
-        LogoutEndpoint endpoint = new LogoutEndpoint(realm);
+        LogoutEndpoint endpoint = new LogoutEndpoint(session, realm);
         ResteasyProviderFactory.getInstance().injectProperties(endpoint);
         return endpoint;
     }
 
     @Path("validate")
     public Object validate() {
-        ValidateEndpoint endpoint = new ValidateEndpoint(realm, event);
+        ValidateEndpoint endpoint = new ValidateEndpoint(session, realm, event);
         ResteasyProviderFactory.getInstance().injectProperties(endpoint);
         return endpoint;
     }
 
     @Path("samlValidate")
     public Object validateSaml11() {
-        SamlValidateEndpoint endpoint = new SamlValidateEndpoint(realm, event);
+        SamlValidateEndpoint endpoint = new SamlValidateEndpoint(session, realm, event);
         ResteasyProviderFactory.getInstance().injectProperties(endpoint);
         return endpoint;
     }
 
     @Path("serviceValidate")
     public Object serviceValidate() {
-        ServiceValidateEndpoint endpoint = new ServiceValidateEndpoint(realm, event);
+        ServiceValidateEndpoint endpoint = new ServiceValidateEndpoint(session, realm, event);
         ResteasyProviderFactory.getInstance().injectProperties(endpoint);
         return endpoint;
     }
diff --git a/src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java b/src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java
index 0b965d9..2620eb3 100644
--- a/src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java
+++ b/src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java
@@ -27,26 +27,20 @@
 
 public abstract class AbstractValidateEndpoint {
     protected final Logger logger = Logger.getLogger(getClass());
-    @Context
     protected KeycloakSession session;
-    @Context
-    protected ClientConnection clientConnection;
-    @Context
-    protected HttpRequest request;
-    @Context
-    protected HttpHeaders headers;
     protected RealmModel realm;
     protected EventBuilder event;
     protected ClientModel client;
     protected AuthenticatedClientSessionModel clientSession;
 
-    public AbstractValidateEndpoint(RealmModel realm, EventBuilder event) {
+    public AbstractValidateEndpoint(KeycloakSession session, RealmModel realm, EventBuilder event) {
+        this.session = session;
         this.realm = realm;
         this.event = event;
     }
 
     protected void checkSsl() {
-        if (!session.getContext().getUri().getBaseUri().getScheme().equals("https") && realm.getSslRequired().isRequired(clientConnection)) {
+        if (!session.getContext().getUri().getBaseUri().getScheme().equals("https") && realm.getSslRequired().isRequired(session.getContext().getConnection())) {
             throw new CASValidationException(CASErrorCode.INVALID_REQUEST, "HTTPS required", Response.Status.FORBIDDEN);
         }
     }
diff --git a/src/main/java/org/keycloak/protocol/cas/endpoints/LogoutEndpoint.java b/src/main/java/org/keycloak/protocol/cas/endpoints/LogoutEndpoint.java
index bd3a48a..6216ec3 100644
--- a/src/main/java/org/keycloak/protocol/cas/endpoints/LogoutEndpoint.java
+++ b/src/main/java/org/keycloak/protocol/cas/endpoints/LogoutEndpoint.java
@@ -2,13 +2,9 @@
 
 import jakarta.ws.rs.GET;
 import jakarta.ws.rs.QueryParam;
-import jakarta.ws.rs.core.Context;
-import jakarta.ws.rs.core.HttpHeaders;
 import jakarta.ws.rs.core.Response;
 import org.jboss.logging.Logger;
 import org.jboss.resteasy.annotations.cache.NoCache;
-import org.jboss.resteasy.spi.HttpRequest;
-import org.keycloak.common.ClientConnection;
 import org.keycloak.models.ClientModel;
 import org.keycloak.models.KeycloakSession;
 import org.keycloak.models.RealmModel;
@@ -24,23 +20,14 @@
 public class LogoutEndpoint {
     private static final Logger logger = Logger.getLogger(LogoutEndpoint.class);
 
-    @Context
     private KeycloakSession session;
-
-    @Context
-    private ClientConnection clientConnection;
-
-    @Context
-    private HttpRequest request;
-
-    @Context
-    private HttpHeaders headers;
 
     private RealmModel realm;
     private ClientModel client;
     private String redirectUri;
 
-    public LogoutEndpoint(RealmModel realm) {
+    public LogoutEndpoint(KeycloakSession session, RealmModel realm) {
+        this.session = session;
         this.realm = realm;
     }
 
@@ -59,7 +46,7 @@
             }
 
             logger.debug("Initiating CAS browser logout");
-            Response response =  AuthenticationManager.browserLogout(session, realm, authResult.getSession(), session.getContext().getUri(), clientConnection, headers);
+            Response response = AuthenticationManager.browserLogout(session, realm, authResult.getSession(), session.getContext().getUri(), session.getContext().getConnection(), session.getContext().getRequestHeaders());
             logger.debug("finishing CAS browser logout");
             return response;
         }
diff --git a/src/main/java/org/keycloak/protocol/cas/endpoints/SamlValidateEndpoint.java b/src/main/java/org/keycloak/protocol/cas/endpoints/SamlValidateEndpoint.java
index 752153a..5442d70 100644
--- a/src/main/java/org/keycloak/protocol/cas/endpoints/SamlValidateEndpoint.java
+++ b/src/main/java/org/keycloak/protocol/cas/endpoints/SamlValidateEndpoint.java
@@ -8,6 +8,7 @@
 import org.keycloak.dom.saml.v1.protocol.SAML11ResponseType;
 import org.keycloak.events.EventBuilder;
 import org.keycloak.events.EventType;
+import org.keycloak.models.KeycloakSession;
 import org.keycloak.models.RealmModel;
 import org.keycloak.models.UserModel;
 import org.keycloak.protocol.cas.CASLoginProtocol;
@@ -31,8 +32,8 @@
 import static org.keycloak.protocol.cas.CASLoginProtocol.TARGET_PARAM;
 
 public class SamlValidateEndpoint extends AbstractValidateEndpoint {
-    public SamlValidateEndpoint(RealmModel realm, EventBuilder event) {
-        super(realm, event.event(EventType.CODE_TO_TOKEN));
+    public SamlValidateEndpoint(KeycloakSession session, RealmModel realm, EventBuilder event) {
+        super(session, realm, event.event(EventType.CODE_TO_TOKEN));
     }
 
     @POST
diff --git a/src/main/java/org/keycloak/protocol/cas/endpoints/ServiceValidateEndpoint.java b/src/main/java/org/keycloak/protocol/cas/endpoints/ServiceValidateEndpoint.java
index d6b459d..2284e25 100644
--- a/src/main/java/org/keycloak/protocol/cas/endpoints/ServiceValidateEndpoint.java
+++ b/src/main/java/org/keycloak/protocol/cas/endpoints/ServiceValidateEndpoint.java
@@ -5,6 +5,7 @@
 import jakarta.ws.rs.core.Request;
 import jakarta.ws.rs.core.Response;
 import org.keycloak.events.EventBuilder;
+import org.keycloak.models.KeycloakSession;
 import org.keycloak.models.RealmModel;
 import org.keycloak.models.UserSessionModel;
 import org.keycloak.protocol.cas.representations.CASServiceResponse;
@@ -15,11 +16,8 @@
 import java.util.Map;
 
 public class ServiceValidateEndpoint extends ValidateEndpoint {
-    @Context
-    private Request restRequest;
-
-    public ServiceValidateEndpoint(RealmModel realm, EventBuilder event) {
-        super(realm, event);
+    public ServiceValidateEndpoint(KeycloakSession session, RealmModel realm, EventBuilder event) {
+        super(session, realm, event);
     }
 
     @Override
diff --git a/src/main/java/org/keycloak/protocol/cas/endpoints/ValidateEndpoint.java b/src/main/java/org/keycloak/protocol/cas/endpoints/ValidateEndpoint.java
index 810dcab..cfd0858 100644
--- a/src/main/java/org/keycloak/protocol/cas/endpoints/ValidateEndpoint.java
+++ b/src/main/java/org/keycloak/protocol/cas/endpoints/ValidateEndpoint.java
@@ -7,6 +7,7 @@
 import org.jboss.resteasy.annotations.cache.NoCache;
 import org.keycloak.events.EventBuilder;
 import org.keycloak.events.EventType;
+import org.keycloak.models.KeycloakSession;
 import org.keycloak.models.RealmModel;
 import org.keycloak.protocol.cas.CASLoginProtocol;
 import org.keycloak.protocol.cas.utils.CASValidationException;
@@ -16,8 +17,8 @@
     private static final String RESPONSE_OK = "yes\n";
     private static final String RESPONSE_FAILED = "no\n";
 
-    public ValidateEndpoint(RealmModel realm, EventBuilder event) {
-        super(realm, event);
+    public ValidateEndpoint(KeycloakSession session, RealmModel realm, EventBuilder event) {
+        super(session, realm, event);
     }
 
     @GET

--
Gitblit v1.9.1