Skip to content

Commit

Permalink
Get rid of @context variables in Endpoint classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekkow committed Nov 24, 2023
1 parent 81877a6 commit e1b962f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ public class CASLoginProtocolService {
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();
Expand All @@ -43,28 +37,28 @@ public Object login() {

@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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -59,7 +46,7 @@ public Response logout(@QueryParam(CASLoginProtocol.SERVICE_PARAM) String servic
}

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,8 +17,8 @@ public class ValidateEndpoint extends AbstractValidateEndpoint {
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
Expand Down

0 comments on commit e1b962f

Please sign in to comment.