2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * SAMLDSSessionInitiator.cpp
20 * SAML Discovery Service support.
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "SPRequest.h"
27 #include "handler/AbstractHandler.h"
28 #include "handler/SessionInitiator.h"
30 #include <xmltooling/XMLToolingConfig.h>
31 #include <xmltooling/util/URLEncoder.h>
33 using namespace shibsp;
34 using namespace opensaml;
35 using namespace xmltooling;
36 using namespace log4cpp;
41 #if defined (_MSC_VER)
42 #pragma warning( push )
43 #pragma warning( disable : 4250 )
46 class SHIBSP_DLLLOCAL SAMLDSSessionInitiator : public SessionInitiator, public AbstractHandler
49 SAMLDSSessionInitiator(const DOMElement* e, const char* appId)
50 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator")), m_url(NULL), m_returnParam(NULL) {
51 pair<bool,const char*> url = getString("URL");
53 throw ConfigurationException("SAMLDS SessionInitiator requires a URL property.");
55 url = getString("entityIDParam");
57 m_returnParam = url.second;
59 virtual ~SAMLDSSessionInitiator() {}
61 pair<bool,long> run(SPRequest& request, const char* entityID=NULL, bool isHandler=true) const;
65 const char* m_returnParam;
68 #if defined (_MSC_VER)
69 #pragma warning( pop )
72 SessionInitiator* SHIBSP_DLLLOCAL SAMLDSSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
74 return new SAMLDSSessionInitiator(p.first, p.second);
79 pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, const char* entityID, bool isHandler) const
81 // The IdP CANNOT be specified for us to run. Otherwise, we'd be redirecting to a DS
82 // anytime the IdP's metadata was wrong.
83 if (entityID && *entityID)
84 return make_pair(false,0);
89 const Application& app=request.getApplication();
92 option = request.getParameter("target");
95 recoverRelayState(request.getApplication(), request, target, false);
97 option = request.getParameter("isPassive");
99 isPassive = !strcmp(option,"true");
102 // We're running as a "virtual handler" from within the filter.
103 // The target resource is the current one and everything else is
104 // defaulted or set by content policy.
105 target=request.getRequestURL();
106 pair<bool,bool> passopt = getBool("isPassive");
107 isPassive = passopt.first && passopt.second;
110 m_log.debug("sending request to SAMLDS (%s)", m_url);
112 // Compute the return URL. We start with a self-referential link.
113 string returnURL=request.getHandlerURL(target.c_str());
114 pair<bool,const char*> thisloc = getString("Location");
115 if (thisloc.first) returnURL += thisloc.second;
118 // We may already have RelayState set if we looped back here,
119 // but just in case target is a resource, we reset it back.
120 option = request.getParameter("target");
124 preserveRelayState(request.getApplication(), request, target);
126 const URLEncoder* urlenc = XMLToolingConfig::getConfig().getURLEncoder();
128 // Now the hard part. The base assumption is to append the entire query string, if any,
129 // to the self-link. But we want to replace target with the RelayState-preserved value
130 // to hide it from the DS.
131 const char* query = request.getQueryString();
133 // See if it starts with target.
134 if (!strncmp(query, "target=", 7)) {
135 // We skip this altogether and advance the query past it to the first separator.
136 query = strchr(query, '&');
137 // If we still have more, just append it.
138 if (query && *(++query))
139 returnURL = returnURL + '?' + query;
142 // There's something in the query before target appears, so we have to find it.
143 thisloc.second = strstr(query,"&target=");
144 if (thisloc.second) {
145 // We found it, so first append everything up to it.
147 returnURL.append(query, thisloc.second - query);
148 query = thisloc.second + 8; // move up just past the equals sign.
149 thisloc.second = strchr(query, '&');
151 returnURL += thisloc.second;
154 // No target in the existing query, so just append it as is.
155 returnURL = returnURL + '?' + query;
160 // Now append the sanitized target as needed.
162 returnURL = returnURL + (returnURL.rfind('?')==string::npos ? '?' : '&') + "target=" + urlenc->encode(target.c_str());
164 else if (!target.empty()) {
165 // For a virtual handler, we just append target to the return link.
166 returnURL = returnURL + "?target=" + urlenc->encode(target.c_str());;
169 string req=string(m_url) + (strchr(m_url,'?') ? '&' : '?') + "entityID=" + urlenc->encode(app.getString("entityID").second) +
170 "&return=" + urlenc->encode(returnURL.c_str());
172 req = req + "&returnIDParam=" + m_returnParam;
174 req += "&isPassive=true";
176 return make_pair(true, request.sendRedirect(req.c_str()));