saml adapter endpoint changes
This commit is contained in:
1 parent
93f85d34a7
commit
a068d83dd8
77 files changed
+1920
-299
No files matched your search
@@ -38,12 +38,16 @@ public abstract class SamlAuthenticator {
|
||||
|
||||
protected abstract void completeAuthentication(SamlSession samlSession);
|
||||
|
||||
private SamlAuthenticationHandler createAuthenticationHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
protected SamlAuthenticationHandler createAuthenticationHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
if (EcpAuthenticationHandler.canHandle(facade)) {
|
||||
return EcpAuthenticationHandler.create(facade, deployment, sessionStore);
|
||||
}
|
||||
|
||||
// defaults to the web browser sso profile
|
||||
return createBrowserHandler(facade, deployment, sessionStore);
|
||||
}
|
||||
|
||||
protected SamlAuthenticationHandler createBrowserHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
return WebBrowserSsoAuthenticationHandler.create(facade, deployment, sessionStore);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package org.keycloak.adapters.saml;
|
||||
|
||||
import org.keycloak.adapters.spi.HttpFacade;
|
||||
import org.keycloak.saml.BaseSAML2BindingBuilder;
|
||||
import org.keycloak.saml.common.constants.GeneralConstants;
|
||||
import org.keycloak.saml.common.exceptions.ConfigurationException;
|
||||
import org.keycloak.saml.common.exceptions.ProcessingException;
|
||||
import org.w3c.dom.Document;
|
||||
@@ -32,4 +33,41 @@ public class SamlUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a url to redirect to if there is an IDP initiated login. Looks for a redirectTo query param first, then looks
|
||||
* in RelayState, if not in either defaults to context path.
|
||||
*
|
||||
* @param facade
|
||||
* @param contextPath
|
||||
* @param baseUri
|
||||
* @return
|
||||
*/
|
||||
public static String getRedirectTo(HttpFacade facade, String contextPath, String baseUri) {
|
||||
String redirectTo = facade.getRequest().getQueryParamValue("redirectTo");
|
||||
if (redirectTo != null && !redirectTo.isEmpty()) {
|
||||
return buildRedirectTo(baseUri, redirectTo);
|
||||
} else {
|
||||
redirectTo = facade.getRequest().getFirstParam(GeneralConstants.RELAY_STATE);
|
||||
if (redirectTo != null) {
|
||||
int index = redirectTo.indexOf("redirectTo=");
|
||||
if (index >= 0) {
|
||||
String to = redirectTo.substring(index + "redirectTo=".length());
|
||||
index = to.indexOf(';');
|
||||
if (index >=0) {
|
||||
to = to.substring(0, index);
|
||||
}
|
||||
return buildRedirectTo(baseUri, to);
|
||||
}
|
||||
}
|
||||
if (contextPath.isEmpty()) baseUri += "/";
|
||||
return baseUri;
|
||||
}
|
||||
}
|
||||
|
||||
private static String buildRedirectTo(String baseUri, String redirectTo) {
|
||||
if (redirectTo.startsWith("/")) redirectTo = redirectTo.substring(1);
|
||||
if (baseUri.endsWith("/")) baseUri = baseUri.substring(0, baseUri.length() - 1);
|
||||
redirectTo = baseUri + "/" + redirectTo;
|
||||
return redirectTo;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package org.keycloak.adapters.saml.profile.webbrowsersso;
|
||||
|
||||
import org.keycloak.adapters.saml.OnSessionCreated;
|
||||
import org.keycloak.adapters.saml.SamlDeployment;
|
||||
import org.keycloak.adapters.saml.SamlSessionStore;
|
||||
import org.keycloak.adapters.saml.profile.SamlInvocationContext;
|
||||
import org.keycloak.adapters.spi.AuthOutcome;
|
||||
import org.keycloak.adapters.spi.HttpFacade;
|
||||
import org.keycloak.saml.common.constants.GeneralConstants;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||
* @version $Revision: 1 $
|
||||
*/
|
||||
public class BrowserHandler extends WebBrowserSsoAuthenticationHandler {
|
||||
public BrowserHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
super(facade, deployment, sessionStore);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthOutcome handle(OnSessionCreated onCreateSession) {
|
||||
return doHandle(new SamlInvocationContext(null, null, null), onCreateSession);
|
||||
}
|
||||
}
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
package org.keycloak.adapters.saml.profile.webbrowsersso;
|
||||
|
||||
import org.keycloak.adapters.saml.OnSessionCreated;
|
||||
import org.keycloak.adapters.saml.SamlDeployment;
|
||||
import org.keycloak.adapters.saml.SamlSessionStore;
|
||||
import org.keycloak.adapters.saml.profile.SamlInvocationContext;
|
||||
import org.keycloak.adapters.spi.AuthOutcome;
|
||||
import org.keycloak.adapters.spi.HttpFacade;
|
||||
import org.keycloak.saml.common.constants.GeneralConstants;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||
* @version $Revision: 1 $
|
||||
*/
|
||||
public class SamlEndpoint extends WebBrowserSsoAuthenticationHandler {
|
||||
public SamlEndpoint(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
super(facade, deployment, sessionStore);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthOutcome handle(OnSessionCreated onCreateSession) {
|
||||
String samlRequest = facade.getRequest().getFirstParam(GeneralConstants.SAML_REQUEST_KEY);
|
||||
String samlResponse = facade.getRequest().getFirstParam(GeneralConstants.SAML_RESPONSE_KEY);
|
||||
String relayState = facade.getRequest().getFirstParam(GeneralConstants.RELAY_STATE);
|
||||
if (samlRequest != null) {
|
||||
return handleSamlRequest(samlRequest, relayState);
|
||||
} else if (samlResponse != null) {
|
||||
return handleSamlResponse(samlResponse, relayState, onCreateSession);
|
||||
}
|
||||
return AuthOutcome.NOT_ATTEMPTED;
|
||||
|
||||
}
|
||||
}
|
||||
Regular → Executable
+1
-1
@@ -25,7 +25,7 @@ public class WebBrowserSsoAuthenticationHandler extends AbstractSamlAuthenticati
|
||||
return new WebBrowserSsoAuthenticationHandler(facade, deployment, sessionStore);
|
||||
}
|
||||
|
||||
private WebBrowserSsoAuthenticationHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
WebBrowserSsoAuthenticationHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
super(facade, deployment, sessionStore);
|
||||
}
|
||||
|
||||
|
||||
+32
-6
@@ -13,6 +13,10 @@ import org.eclipse.jetty.server.UserIdentity;
|
||||
import org.eclipse.jetty.server.handler.ContextHandler;
|
||||
import org.eclipse.jetty.util.URIUtil;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.keycloak.adapters.saml.SamlSessionStore;
|
||||
import org.keycloak.adapters.saml.profile.SamlAuthenticationHandler;
|
||||
import org.keycloak.adapters.saml.profile.webbrowsersso.BrowserHandler;
|
||||
import org.keycloak.adapters.saml.profile.webbrowsersso.SamlEndpoint;
|
||||
import org.keycloak.adapters.spi.AdapterSessionStore;
|
||||
import org.keycloak.adapters.spi.AuthChallenge;
|
||||
import org.keycloak.adapters.spi.AuthOutcome;
|
||||
@@ -234,16 +238,38 @@ public abstract class AbstractSamlAuthenticator extends LoginAuthenticator {
|
||||
log.debug("*** deployment isn't configured return false");
|
||||
return Authentication.UNAUTHENTICATED;
|
||||
}
|
||||
if (!mandatory)
|
||||
boolean isEndpoint = request.getRequestURI().substring(request.getContextPath().length()).endsWith("/saml");
|
||||
if (!mandatory && !isEndpoint)
|
||||
return new DeferredAuthentication(this);
|
||||
JettySamlSessionStore tokenStore = getTokenStore(request, facade, deployment);
|
||||
|
||||
SamlAuthenticator authenticator = new SamlAuthenticator(facade, deployment, tokenStore ) {
|
||||
@Override
|
||||
protected void completeAuthentication(SamlSession account) {
|
||||
SamlAuthenticator authenticator = null;
|
||||
if (isEndpoint) {
|
||||
authenticator = new SamlAuthenticator(facade, deployment, tokenStore) {
|
||||
@Override
|
||||
protected void completeAuthentication(SamlSession account) {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SamlAuthenticationHandler createBrowserHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
return new SamlEndpoint(facade, deployment, sessionStore);
|
||||
}
|
||||
};
|
||||
|
||||
} else {
|
||||
authenticator = new SamlAuthenticator(facade, deployment, tokenStore) {
|
||||
@Override
|
||||
protected void completeAuthentication(SamlSession account) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SamlAuthenticationHandler createBrowserHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
return new BrowserHandler(facade, deployment, sessionStore);
|
||||
}
|
||||
};
|
||||
}
|
||||
AuthOutcome outcome = authenticator.authenticate();
|
||||
if (outcome == AuthOutcome.AUTHENTICATED) {
|
||||
if (facade.isEnded()) {
|
||||
|
||||
+9
-1
@@ -3,12 +3,14 @@ package org.keycloak.adapters.saml.jetty;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.keycloak.adapters.saml.SamlDeployment;
|
||||
import org.keycloak.adapters.saml.SamlUtil;
|
||||
import org.keycloak.adapters.spi.AdapterSessionStore;
|
||||
import org.keycloak.adapters.spi.HttpFacade;
|
||||
import org.keycloak.adapters.spi.SessionIdMapper;
|
||||
import org.keycloak.adapters.jetty.spi.JettyUserSessionManagement;
|
||||
import org.keycloak.adapters.saml.SamlSession;
|
||||
import org.keycloak.adapters.saml.SamlSessionStore;
|
||||
import org.keycloak.common.util.KeycloakUriBuilder;
|
||||
import org.keycloak.dom.saml.v2.protocol.StatusType;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
@@ -151,7 +153,13 @@ public class JettySamlSessionStore implements SamlSessionStore {
|
||||
|
||||
@Override
|
||||
public String getRedirectUri() {
|
||||
return (String)request.getSession(true).getAttribute(SAML_REDIRECT_URI);
|
||||
String redirect = (String)request.getSession(true).getAttribute(SAML_REDIRECT_URI);
|
||||
if (redirect == null) {
|
||||
String contextPath = request.getContextPath();
|
||||
String baseUri = KeycloakUriBuilder.fromUri(request.getRequestURL().toString()).replacePath(contextPath).build().toString();
|
||||
return SamlUtil.getRedirectTo(facade, contextPath, baseUri);
|
||||
}
|
||||
return redirect;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+9
-1
@@ -1,12 +1,14 @@
|
||||
package org.keycloak.adapters.saml.servlet;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
import org.keycloak.adapters.saml.SamlUtil;
|
||||
import org.keycloak.adapters.spi.HttpFacade;
|
||||
import org.keycloak.adapters.spi.KeycloakAccount;
|
||||
import org.keycloak.adapters.spi.SessionIdMapper;
|
||||
import org.keycloak.adapters.saml.SamlSession;
|
||||
import org.keycloak.adapters.saml.SamlSessionStore;
|
||||
import org.keycloak.adapters.servlet.FilterSessionStore;
|
||||
import org.keycloak.common.util.KeycloakUriBuilder;
|
||||
import org.keycloak.dom.saml.v2.protocol.StatusType;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -145,7 +147,13 @@ public class FilterSamlSessionStore extends FilterSessionStore implements SamlSe
|
||||
public String getRedirectUri() {
|
||||
HttpSession session = request.getSession(false);
|
||||
if (session == null) return null;
|
||||
return (String)session.getAttribute(REDIRECT_URI);
|
||||
String redirect = (String)session.getAttribute(REDIRECT_URI);
|
||||
if (redirect == null) {
|
||||
String contextPath = request.getContextPath();
|
||||
String baseUri = KeycloakUriBuilder.fromUri(request.getRequestURL().toString()).replacePath(contextPath).build().toString();
|
||||
return SamlUtil.getRedirectTo(facade, contextPath, baseUri);
|
||||
}
|
||||
return redirect;
|
||||
}
|
||||
|
||||
}
|
||||
+39
-6
@@ -23,11 +23,16 @@ import org.keycloak.adapters.saml.SamlAuthenticator;
|
||||
import org.keycloak.adapters.saml.SamlDeployment;
|
||||
import org.keycloak.adapters.saml.SamlDeploymentContext;
|
||||
import org.keycloak.adapters.saml.SamlSession;
|
||||
import org.keycloak.adapters.saml.SamlSessionStore;
|
||||
import org.keycloak.adapters.saml.config.parsers.DeploymentBuilder;
|
||||
import org.keycloak.adapters.saml.config.parsers.ResourceLoader;
|
||||
import org.keycloak.adapters.saml.profile.SamlAuthenticationHandler;
|
||||
import org.keycloak.adapters.saml.profile.webbrowsersso.BrowserHandler;
|
||||
import org.keycloak.adapters.saml.profile.webbrowsersso.SamlEndpoint;
|
||||
import org.keycloak.adapters.servlet.ServletHttpFacade;
|
||||
import org.keycloak.adapters.spi.AuthChallenge;
|
||||
import org.keycloak.adapters.spi.AuthOutcome;
|
||||
import org.keycloak.adapters.spi.HttpFacade;
|
||||
import org.keycloak.adapters.spi.InMemorySessionIdMapper;
|
||||
import org.keycloak.adapters.spi.SessionIdMapper;
|
||||
import org.keycloak.saml.common.exceptions.ParsingException;
|
||||
@@ -38,11 +43,16 @@ import org.keycloak.saml.common.exceptions.ParsingException;
|
||||
*/
|
||||
public class SamlFilter implements Filter {
|
||||
protected SamlDeploymentContext deploymentContext;
|
||||
protected SessionIdMapper idMapper = new InMemorySessionIdMapper();
|
||||
protected SessionIdMapper idMapper;
|
||||
private final static Logger log = Logger.getLogger("" + SamlFilter.class);
|
||||
|
||||
@Override
|
||||
public void init(final FilterConfig filterConfig) throws ServletException {
|
||||
deploymentContext = (SamlDeploymentContext)filterConfig.getServletContext().getAttribute(SamlDeploymentContext.class.getName());
|
||||
if (deploymentContext != null) {
|
||||
idMapper = (SessionIdMapper)filterConfig.getServletContext().getAttribute(SessionIdMapper.class.getName());
|
||||
return;
|
||||
}
|
||||
String configResolverClass = filterConfig.getInitParameter("keycloak.config.resolver");
|
||||
if (configResolverClass != null) {
|
||||
try {
|
||||
@@ -92,7 +102,9 @@ public class SamlFilter implements Filter {
|
||||
deploymentContext = new SamlDeploymentContext(deployment);
|
||||
log.fine("Keycloak is using a per-deployment configuration.");
|
||||
}
|
||||
idMapper = new InMemorySessionIdMapper();
|
||||
filterConfig.getServletContext().setAttribute(SamlDeploymentContext.class.getName(), deploymentContext);
|
||||
filterConfig.getServletContext().setAttribute(SessionIdMapper.class.getName(), idMapper);
|
||||
|
||||
}
|
||||
|
||||
@@ -108,13 +120,34 @@ public class SamlFilter implements Filter {
|
||||
return;
|
||||
}
|
||||
FilterSamlSessionStore tokenStore = new FilterSamlSessionStore(request, facade, 100000, idMapper);
|
||||
boolean isEndpoint = request.getRequestURI().substring(request.getContextPath().length()).endsWith("/saml");
|
||||
SamlAuthenticator authenticator = null;
|
||||
if (isEndpoint) {
|
||||
authenticator = new SamlAuthenticator(facade, deployment, tokenStore) {
|
||||
@Override
|
||||
protected void completeAuthentication(SamlSession account) {
|
||||
|
||||
SamlAuthenticator authenticator = new SamlAuthenticator(facade, deployment, tokenStore) {
|
||||
@Override
|
||||
protected void completeAuthentication(SamlSession account) {
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
@Override
|
||||
protected SamlAuthenticationHandler createBrowserHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
return new SamlEndpoint(facade, deployment, sessionStore);
|
||||
}
|
||||
};
|
||||
|
||||
} else {
|
||||
authenticator = new SamlAuthenticator(facade, deployment, tokenStore) {
|
||||
@Override
|
||||
protected void completeAuthentication(SamlSession account) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SamlAuthenticationHandler createBrowserHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
return new BrowserHandler(facade, deployment, sessionStore);
|
||||
}
|
||||
};
|
||||
}
|
||||
AuthOutcome outcome = authenticator.authenticate();
|
||||
if (outcome == AuthOutcome.AUTHENTICATED) {
|
||||
log.fine("AUTHENTICATED");
|
||||
|
||||
+18
-4
@@ -150,10 +150,23 @@ public abstract class AbstractSamlAuthenticatorValve extends FormAuthenticator i
|
||||
@Override
|
||||
public void invoke(Request request, Response response) throws IOException, ServletException {
|
||||
log.fine("*********************** SAML ************");
|
||||
if (request.getRequestURI().substring(request.getContextPath().length()).endsWith("/saml")) {
|
||||
CatalinaHttpFacade facade = new CatalinaHttpFacade(response, request);
|
||||
SamlDeployment deployment = deploymentContext.resolveDeployment(facade);
|
||||
if (deployment != null && deployment.isConfigured()) {
|
||||
SamlSessionStore tokenStore = getSessionStore(request, facade, deployment);
|
||||
SamlAuthenticator authenticator = new CatalinaSamlEndpoint(facade, deployment, tokenStore);
|
||||
executeAuthenticator(request, response, facade, deployment, authenticator);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
try {
|
||||
super.invoke(request, response);
|
||||
} finally {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected abstract GenericPrincipalFactory createPrincipalFactory();
|
||||
@@ -187,7 +200,11 @@ public abstract class AbstractSamlAuthenticatorValve extends FormAuthenticator i
|
||||
SamlSessionStore tokenStore = getSessionStore(request, facade, deployment);
|
||||
|
||||
|
||||
CatalinaSamlAuthenticator authenticator = new CatalinaSamlAuthenticator(facade, deployment, tokenStore);
|
||||
SamlAuthenticator authenticator = new CatalinaSamlAuthenticator(facade, deployment, tokenStore);
|
||||
return executeAuthenticator(request, response, facade, deployment, authenticator);
|
||||
}
|
||||
|
||||
protected boolean executeAuthenticator(Request request, HttpServletResponse response, CatalinaHttpFacade facade, SamlDeployment deployment, SamlAuthenticator authenticator) {
|
||||
AuthOutcome outcome = authenticator.authenticate();
|
||||
if (outcome == AuthOutcome.AUTHENTICATED) {
|
||||
log.fine("AUTHENTICATED");
|
||||
@@ -209,9 +226,6 @@ public abstract class AbstractSamlAuthenticatorValve extends FormAuthenticator i
|
||||
AuthChallenge challenge = authenticator.getChallenge();
|
||||
if (challenge != null) {
|
||||
log.fine("challenge");
|
||||
if (loginConfig == null) {
|
||||
loginConfig = request.getContext().getLoginConfig();
|
||||
}
|
||||
challenge.challenge(facade);
|
||||
}
|
||||
return false;
|
||||
|
||||
+8
@@ -1,5 +1,7 @@
|
||||
package org.keycloak.adapters.saml;
|
||||
|
||||
import org.keycloak.adapters.saml.profile.SamlAuthenticationHandler;
|
||||
import org.keycloak.adapters.saml.profile.webbrowsersso.BrowserHandler;
|
||||
import org.keycloak.adapters.spi.HttpFacade;
|
||||
|
||||
/**
|
||||
@@ -15,4 +17,10 @@ public class CatalinaSamlAuthenticator extends SamlAuthenticator {
|
||||
protected void completeAuthentication(SamlSession account) {
|
||||
// complete
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SamlAuthenticationHandler createBrowserHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
return new BrowserHandler(facade, deployment, sessionStore);
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
package org.keycloak.adapters.saml;
|
||||
|
||||
import org.keycloak.adapters.saml.profile.SamlAuthenticationHandler;
|
||||
import org.keycloak.adapters.saml.profile.webbrowsersso.BrowserHandler;
|
||||
import org.keycloak.adapters.saml.profile.webbrowsersso.SamlEndpoint;
|
||||
import org.keycloak.adapters.spi.HttpFacade;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||
* @version $Revision: 1 $
|
||||
*/
|
||||
public class CatalinaSamlEndpoint extends SamlAuthenticator {
|
||||
public CatalinaSamlEndpoint(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
super(facade, deployment, sessionStore);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void completeAuthentication(SamlSession account) {
|
||||
// complete
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SamlAuthenticationHandler createBrowserHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
return new SamlEndpoint(facade, deployment, sessionStore);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+9
-1
@@ -9,9 +9,11 @@ import org.keycloak.adapters.spi.HttpFacade;
|
||||
import org.keycloak.adapters.spi.SessionIdMapper;
|
||||
import org.keycloak.adapters.tomcat.CatalinaUserSessionManagement;
|
||||
import org.keycloak.adapters.tomcat.GenericPrincipalFactory;
|
||||
import org.keycloak.common.util.KeycloakUriBuilder;
|
||||
import org.keycloak.dom.saml.v2.protocol.StatusResponseType;
|
||||
import org.keycloak.dom.saml.v2.protocol.StatusType;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
@@ -193,7 +195,13 @@ public class CatalinaSamlSessionStore implements SamlSessionStore {
|
||||
|
||||
@Override
|
||||
public String getRedirectUri() {
|
||||
return (String)getSession(true).getAttribute(SAML_REDIRECT_URI);
|
||||
String redirect = (String)getSession(true).getAttribute(SAML_REDIRECT_URI);
|
||||
if (redirect == null) {
|
||||
String contextPath = request.getContextPath();
|
||||
String baseUri = KeycloakUriBuilder.fromUri(request.getRequestURL().toString()).replacePath(contextPath).build().toString();
|
||||
return SamlUtil.getRedirectTo(facade, contextPath, baseUri);
|
||||
}
|
||||
return redirect;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+9
-1
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
package org.keycloak.adapters.saml.undertow;
|
||||
|
||||
import org.keycloak.adapters.saml.SamlAuthenticator;
|
||||
import org.keycloak.adapters.saml.SamlDeployment;
|
||||
import org.keycloak.adapters.saml.SamlDeploymentContext;
|
||||
import org.keycloak.adapters.saml.SamlSessionStore;
|
||||
@@ -104,7 +105,14 @@ public abstract class AbstractSamlAuthMech implements AuthenticationMechanism {
|
||||
return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
|
||||
}
|
||||
SamlSessionStore sessionStore = getTokenStore(exchange, facade, deployment, securityContext);
|
||||
UndertowSamlAuthenticator authenticator = new UndertowSamlAuthenticator(securityContext, facade, deploymentContext.resolveDeployment(facade), sessionStore);
|
||||
SamlAuthenticator authenticator = null;
|
||||
if (exchange.getRequestPath().endsWith("/saml")) {
|
||||
authenticator = new UndertowSamlEndpoint(facade, deploymentContext.resolveDeployment(facade), sessionStore);
|
||||
} else {
|
||||
authenticator = new UndertowSamlAuthenticator(securityContext, facade, deploymentContext.resolveDeployment(facade), sessionStore);
|
||||
|
||||
}
|
||||
|
||||
AuthOutcome outcome = authenticator.authenticate();
|
||||
if (outcome == AuthOutcome.AUTHENTICATED) {
|
||||
registerNotifications(securityContext);
|
||||
|
||||
+16
@@ -26,7 +26,9 @@ import io.undertow.servlet.ServletExtension;
|
||||
import io.undertow.servlet.api.AuthMethodConfig;
|
||||
import io.undertow.servlet.api.DeploymentInfo;
|
||||
import io.undertow.servlet.api.LoginConfig;
|
||||
import io.undertow.servlet.api.SecurityConstraint;
|
||||
import io.undertow.servlet.api.ServletSessionConfig;
|
||||
import io.undertow.servlet.api.WebResourceCollection;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.keycloak.adapters.saml.AdapterConstants;
|
||||
import org.keycloak.adapters.saml.DefaultSamlDeployment;
|
||||
@@ -184,10 +186,24 @@ public class SamlServletExtension implements ServletExtension {
|
||||
ServletSessionConfig cookieConfig = new ServletSessionConfig();
|
||||
cookieConfig.setPath(deploymentInfo.getContextPath());
|
||||
deploymentInfo.setServletSessionConfig(cookieConfig);
|
||||
addEndpointConstraint(deploymentInfo);
|
||||
|
||||
ChangeSessionId.turnOffChangeSessionIdOnLogin(deploymentInfo);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* add security constraint to /saml so that the endpoint can be called and auth mechanism pinged.
|
||||
* @param deploymentInfo
|
||||
*/
|
||||
protected void addEndpointConstraint(DeploymentInfo deploymentInfo) {
|
||||
SecurityConstraint constraint = new SecurityConstraint();
|
||||
WebResourceCollection collection = new WebResourceCollection();
|
||||
collection.addUrlPattern("/saml");
|
||||
constraint.addWebResourceCollection(collection);
|
||||
deploymentInfo.addSecurityConstraint(constraint);
|
||||
}
|
||||
|
||||
protected ServletSamlAuthMech createAuthMech(DeploymentInfo deploymentInfo, SamlDeploymentContext deploymentContext, UndertowUserSessionManagement userSessionManagement) {
|
||||
return new ServletSamlAuthMech(deploymentContext, userSessionManagement, getErrorPage(deploymentInfo));
|
||||
}
|
||||
|
||||
+11
-3
@@ -8,19 +8,19 @@ import io.undertow.servlet.handlers.ServletRequestContext;
|
||||
import io.undertow.servlet.spec.HttpSessionImpl;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.keycloak.adapters.saml.SamlDeployment;
|
||||
import org.keycloak.adapters.saml.SamlUtil;
|
||||
import org.keycloak.adapters.spi.SessionIdMapper;
|
||||
import org.keycloak.adapters.saml.SamlSession;
|
||||
import org.keycloak.adapters.saml.SamlSessionStore;
|
||||
import org.keycloak.adapters.undertow.ChangeSessionId;
|
||||
import org.keycloak.adapters.undertow.SavedRequest;
|
||||
import org.keycloak.adapters.undertow.ServletHttpFacade;
|
||||
import org.keycloak.adapters.undertow.UndertowUserSessionManagement;
|
||||
import org.keycloak.common.util.KeycloakUriBuilder;
|
||||
import org.keycloak.dom.saml.v2.protocol.StatusType;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.security.Principal;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@@ -179,7 +179,15 @@ public class ServletSamlSessionStore implements SamlSessionStore {
|
||||
public String getRedirectUri() {
|
||||
final ServletRequestContext sc = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
|
||||
HttpSessionImpl session = sc.getCurrentServletContext().getSession(exchange, true);
|
||||
return (String)session.getAttribute(SAML_REDIRECT_URI);
|
||||
String redirect = (String)session.getAttribute(SAML_REDIRECT_URI);
|
||||
if (redirect == null) {
|
||||
ServletHttpFacade facade = new ServletHttpFacade(exchange);
|
||||
HttpServletRequest req = (HttpServletRequest)sc.getServletRequest();
|
||||
String contextPath = req.getContextPath();
|
||||
String baseUri = KeycloakUriBuilder.fromUri(req.getRequestURL().toString()).replacePath(contextPath).build().toString();
|
||||
return SamlUtil.getRedirectTo(facade, contextPath, baseUri);
|
||||
}
|
||||
return redirect;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+9
@@ -2,6 +2,9 @@ package org.keycloak.adapters.saml.undertow;
|
||||
|
||||
import io.undertow.security.api.SecurityContext;
|
||||
import io.undertow.security.idm.Account;
|
||||
import org.keycloak.adapters.saml.profile.SamlAuthenticationHandler;
|
||||
import org.keycloak.adapters.saml.profile.webbrowsersso.BrowserHandler;
|
||||
import org.keycloak.adapters.saml.profile.webbrowsersso.SamlEndpoint;
|
||||
import org.keycloak.adapters.spi.HttpFacade;
|
||||
import org.keycloak.adapters.saml.SamlAuthenticator;
|
||||
import org.keycloak.adapters.saml.SamlDeployment;
|
||||
@@ -39,4 +42,10 @@ public class UndertowSamlAuthenticator extends SamlAuthenticator {
|
||||
securityContext.authenticationComplete(undertowAccount, "KEYCLOAK-SAML", false);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SamlAuthenticationHandler createBrowserHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
return new BrowserHandler(facade, deployment, sessionStore);
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
package org.keycloak.adapters.saml.undertow;
|
||||
|
||||
import io.undertow.server.HttpHandler;
|
||||
import org.keycloak.adapters.saml.SamlAuthenticator;
|
||||
import org.keycloak.adapters.saml.SamlDeployment;
|
||||
import org.keycloak.adapters.saml.SamlSession;
|
||||
import org.keycloak.adapters.saml.SamlSessionStore;
|
||||
import org.keycloak.adapters.saml.profile.SamlAuthenticationHandler;
|
||||
import org.keycloak.adapters.saml.profile.webbrowsersso.SamlEndpoint;
|
||||
import org.keycloak.adapters.spi.HttpFacade;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||
* @version $Revision: 1 $
|
||||
*/
|
||||
public class UndertowSamlEndpoint extends SamlAuthenticator {
|
||||
public UndertowSamlEndpoint(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
super(facade, deployment, sessionStore);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void completeAuthentication(SamlSession samlSession) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SamlAuthenticationHandler createBrowserHandler(HttpFacade facade, SamlDeployment deployment, SamlSessionStore sessionStore) {
|
||||
return new SamlEndpoint(facade, deployment, sessionStore);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user