spSingleLogoutPOST.jsp revision e8721886dbfd32e88cc7077cbee4b6bb1b44b443
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%--
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Copyright (c) 2008 Sun Microsystems Inc. All Rights Reserved
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync The contents of this file are subject to the terms
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync of the Common Development and Distribution License
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (the License). You may not use this file except in
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync compliance with the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync You can obtain a copy of the License at
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync https://opensso.dev.java.net/public/CDDLv1.0.html or
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync opensso/legal/CDDLv1.0.txt
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync See the License for the specific language governing
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync permission and limitations under the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync When distributing Covered Code, include this CDDL
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Header Notice in each file and include the License file
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync at opensso/legal/CDDLv1.0.txt.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync If applicable, add the following below the CDDL Header,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync with the fields enclosed by brackets [] replaced by
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync your own identifying information:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "Portions Copyrighted [year] [name of copyright owner]"
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync $Id: spSingleLogoutPOST.jsp,v 1.8 2009/06/24 23:05:31 mrudulahg Exp $
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync--%>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%@ page import="com.sun.identity.shared.debug.Debug" %>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%@ page import="com.sun.identity.sae.api.SecureAttrs" %>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%@ page import="com.sun.identity.saml.common.SAMLUtils" %>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%@ page import="com.sun.identity.saml2.common.SAML2Utils" %>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%@ page import="com.sun.identity.saml2.common.SAML2Constants" %>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%@ page import="com.sun.identity.saml2.common.SAML2Exception" %>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%@ page import="com.sun.identity.saml2.jaxb.entityconfig.SPSSOConfigElement" %>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%@ page import="com.sun.identity.saml2.meta.SAML2MetaUtils" %>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%@ page import="com.sun.identity.saml2.meta.SAML2MetaManager" %>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%@ page import="com.sun.identity.saml2.profile.CacheObject" %>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%@ page import="com.sun.identity.saml2.profile.SPCache" %>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync<%@ page import="com.sun.identity.saml2.profile.SPSingleLogout" %>
<%@ page import="com.sun.identity.saml2.profile.IDPCache" %>
<%@ page import="com.sun.identity.saml2.profile.IDPSingleLogout" %>
<%@ page import="com.sun.identity.saml2.protocol.LogoutRequest" %>
<%@ page import="com.sun.identity.saml2.assertion.Issuer" %>
<%@ page import="com.sun.identity.saml2.profile.IDPProxyUtil" %>
<%@ page import="com.sun.identity.saml2.protocol.ProtocolFactory" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Properties" %>
<%--
spSingleLogoutPOST.jsp
- receives the LogoutRequest and sends the LogoutResponse to
Identity Provider from the Service Provider.
OR
- receives the LogoutResponse from the Identity Provider.
Required parameters to this jsp are :
- RelayState - the target URL on successful Single Logout
- SAMLRequest - the LogoutRequest
OR
- SAMLResponse - the LogoutResponse
Check the SAML2 Documentation for supported parameters.
--%>
<%
// Retrieves the LogoutRequest or LogoutResponse
//Retrieves :
//- RelayState - the target URL on successful Single Logout
//- SAMLRequest - the LogoutRequest
//OR
//- SAMLResponse - the LogoutResponse
String relayState = request.getParameter(SAML2Constants.RELAY_STATE);
if (relayState != null) {
CacheObject tmpRs=
(CacheObject) SPCache.relayStateHash.remove(relayState);
if ((tmpRs != null)) {
relayState = (String) tmpRs.getObject();
}
}
String samlResponse = request.getParameter(SAML2Constants.SAML_RESPONSE);
if (samlResponse != null) {
try {
/**
* Gets and processes the Single <code>LogoutResponse</code> from IDP,
* destroys the local session, checks response's issuer
* and inResponseTo.
*
* @param request the HttpServletRequest.
* @param response the HttpServletResponse.
* @param samlResponse <code>LogoutResponse</code> in the
* XML string format.
* @param relayState the target URL on successful
* <code>LogoutResponse</code>.
* @throws SAML2Exception if error processing
* <code>LogoutResponse</code>.
*/
Map infoMap =
SPSingleLogout.processLogoutResponse(request,response,
samlResponse, relayState);
String inRes = (String) infoMap.get("inResponseTo");
LogoutRequest origLogoutRequest = (LogoutRequest)
IDPCache.proxySPLogoutReqCache.get(inRes);
if (origLogoutRequest != null && !origLogoutRequest.equals("")) {
IDPCache.proxySPLogoutReqCache.remove(inRes);
IDPProxyUtil.sendProxyLogoutResponse(response,
origLogoutRequest.getID(), infoMap,
origLogoutRequest.getIssuer().getValue(),
SAML2Constants.HTTP_POST);
return;
}
} catch (SAML2Exception sse) {
SAML2Utils.debug.error("Error processing LogoutResponse :", sse);
SAMLUtils.sendError(request, response, response.SC_BAD_REQUEST,
"LogoutResponseProcessingError",
SAML2Utils.bundle.getString("LogoutResponseProcessingError") +
" " + sse.getMessage());
return;
} catch (Exception e) {
SAML2Utils.debug.error("Error processing LogoutResponse ",e);
SAMLUtils.sendError(request, response, response.SC_BAD_REQUEST,
"LogoutResponseProcessingError",
SAML2Utils.bundle.getString("LogoutResponseProcessingError") +
" " + e.getMessage());
return;
}
if (relayState != null && relayState.length() != 0) {
try {
response.sendRedirect(relayState);
} catch (java.io.IOException ioe) {
if (SAML2Utils.debug.messageEnabled()) {
SAML2Utils.debug.message(
"Exception when redirecting to " +
relayState, ioe);
}
}
} else {
%>
<jsp:forward page="/saml2/jsp/default.jsp?message=spSloSuccess" />
<%
}
} else {
String samlRequest = request.getParameter(SAML2Constants.SAML_REQUEST);
if (samlRequest != null) {
// Logout SP app via SAE first. App is obligated to redirect back
// to complete this SLO request.
if (!SPCache.isFedlet) {
if (processSAELogout(request, response)) {
return;
}
}
try {
/**
* Gets and processes the Single <code>LogoutRequest</code> from
* IDP.
*
* @param request the HttpServletRequest.
* @param response the HttpServletResponse.
* @param samlRequest <code>LogoutRequest</code> in the
* XML string format.
* @param relayState the target URL on successful
* <code>LogoutRequest</code>.
* @throws SAML2Exception if error processing
* <code>LogoutRequest</code>.
*/
SPSingleLogout.processLogoutRequest(request,response,
samlRequest,relayState);
} catch (SAML2Exception sse) {
SAML2Utils.debug.error("Error processing LogoutRequest :", sse);
SAMLUtils.sendError(request, response, response.SC_BAD_REQUEST,
"LogoutRequestProcessingError",
SAML2Utils.bundle.getString("LogoutRequestProcessingError")
+ " " + sse.getMessage());
return;
} catch (Exception e) {
SAML2Utils.debug.error("Error processing LogoutRequest ",e);
SAMLUtils.sendError(request, response, response.SC_BAD_REQUEST,
"LogoutRequestProcessingError",
SAML2Utils.bundle.getString("LogoutRequestProcessingError")
+ " " + e.getMessage());
return;
}
}
}
%>
<%!
boolean processSAELogout(
HttpServletRequest request, HttpServletResponse response)
{
String saeData = request.getParameter(SecureAttrs.SAE_PARAM_APPRETURN);
if (saeData != null) { // App returned back.
return false;
}
try {
String metaAlias =
SAML2MetaUtils.getMetaAliasByUri(request.getRequestURI()) ;
String realm = SAML2MetaUtils.getRealmByMetaAlias(metaAlias);
SAML2MetaManager mm = SAML2Utils.getSAML2MetaManager();
String entityId = mm.getEntityByMetaAlias(metaAlias);
SPSSOConfigElement spConfig = mm.getSPSSOConfig(realm, entityId);
String appSLOUrlStr = null;
if (spConfig != null) {
appSLOUrlStr = SAML2Utils.getAttributeValueFromSPSSOConfig(
spConfig, SAML2Constants.SAE_SP_LOGOUT_URL);
}
if (appSLOUrlStr == null) {
SAML2Utils.debug.message(
"spSLOPOST:SAE:appSLOUrl not configured.");
return false;
}
if (SAML2Utils.debug.messageEnabled()) {
SAML2Utils.debug.message(
"spSLOPOST:SAE:processing App SLO"+ appSLOUrlStr);
}
StringBuffer appSLOUrl = new StringBuffer(appSLOUrlStr);
Map hp = SAML2Utils.getSAEAttrs(
realm, entityId, SAML2Constants.SP_ROLE, appSLOUrlStr);
if (hp == null) {
SAML2Utils.debug.error(
"spSLOPOST:SAE:processing App SLO: getSAEAttrs returned null");
return false;
}
String cryptoType = (String) hp.get(SecureAttrs.SAE_CRYPTO_TYPE);
String secret = null;
String encSecret = null;
String encAlg = (String)hp.get(
SecureAttrs.SAE_CONFIG_DATA_ENCRYPTION_ALG);
String encStrength = (String)hp.get(
SecureAttrs.SAE_CONFIG_ENCRYPTION_KEY_STRENGTH);
if (SecureAttrs.SAE_CRYPTO_TYPE_SYM.equals(cryptoType)) {
// Shared secret between FM-IDP and IDPApp
secret = (String) hp.get(SecureAttrs.SAE_CONFIG_SHARED_SECRET );
encSecret = secret;
} else {
// IDPApp's public key
secret = (String) hp.get(SecureAttrs.SAE_CONFIG_PRIVATE_KEY_ALIAS);
encSecret =
(String) hp.get(SecureAttrs.SAE_CONFIG_PUBLIC_KEY_ALIAS);
}
if (secret == null || secret.length() == 0) {
SAML2Utils.debug.error(
"spSLOPOST:SAE:processing App SLO:getSAEAttrs no secret/key");
return false;
}
if (encAlg == null) {
encSecret = null;
}
String returnURL = request.getRequestURL()+
"?"+request.getQueryString()+"&"+
SecureAttrs.SAE_PARAM_APPRETURN+"=true";
HashMap map = new HashMap();
map.put(SecureAttrs.SAE_PARAM_CMD, SecureAttrs.SAE_CMD_LOGOUT);
map.put(SecureAttrs.SAE_PARAM_APPSLORETURNURL, returnURL);
String saInstanceName = cryptoType + "_" + encAlg + "_" + encStrength;
SecureAttrs sa = SecureAttrs.getInstance(saInstanceName);
if (sa == null) {
Properties prop = new Properties();
prop.setProperty(SecureAttrs.SAE_CONFIG_CERT_CLASS,
"com.sun.identity.sae.api.FMCerts");
if (encAlg != null) {
prop.setProperty(
SecureAttrs.SAE_CONFIG_DATA_ENCRYPTION_ALG, encAlg);
}
if (encStrength != null) {
prop.setProperty(
SecureAttrs.SAE_CONFIG_ENCRYPTION_KEY_STRENGTH,encStrength); }
SecureAttrs.init(saInstanceName, cryptoType, prop);
sa = SecureAttrs.getInstance(saInstanceName);
}
if (sa == null) {
SAML2Utils.debug.error(
"spSLOPOST:SAE:processing App SLO:null SecureAttrs instance");
return false;
}
String encodedString = sa.getEncodedString(map, secret, encSecret);
if (encodedString != null) {
if (appSLOUrl.indexOf("?") > 0) {
appSLOUrl.append("&").append(SecureAttrs.SAE_PARAM_DATA)
.append("=").append(encodedString);
} else {
appSLOUrl.append("?").append(SecureAttrs.SAE_PARAM_DATA)
.append("=").append(encodedString);
}
if (SAML2Utils.debug.messageEnabled()) {
SAML2Utils.debug.message("spSLOPOST:SAE:about to redirect"+
appSLOUrl);
}
response.sendRedirect(appSLOUrl.toString());
return true;
} else {
SAML2Utils.debug.error(
"spSLOPOST:SAE:SecureAttrs.getEncodedStr failed");
}
} catch (Exception ex) {
SAML2Utils.debug.error("spSLOPOST:SAE:SecureAttrs.Fatal:",ex);
}
return false;
}
%>