2 * Copyright 2001-2009 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 * WAYFSessionInitiator.cpp
20 * Shibboleth WAYF support.
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "SPRequest.h"
27 #include "handler/AbstractHandler.h"
28 #include "handler/SessionInitiator.h"
31 # include <saml/util/SAMLConstants.h>
33 # include "lite/SAMLConstants.h"
37 #include <xmltooling/XMLToolingConfig.h>
38 #include <xmltooling/util/URLEncoder.h>
40 using namespace shibsp;
41 using namespace opensaml;
42 using namespace xmltooling;
47 #if defined (_MSC_VER)
48 #pragma warning( push )
49 #pragma warning( disable : 4250 )
52 class SHIBSP_DLLLOCAL WAYFSessionInitiator : public SessionInitiator, public AbstractHandler
55 WAYFSessionInitiator(const DOMElement* e, const char* appId)
56 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.WAYF"), NULL, &m_remapper), m_url(NULL) {
57 pair<bool,const char*> url = getString("URL");
59 throw ConfigurationException("WAYF SessionInitiator requires a URL property.");
62 virtual ~WAYFSessionInitiator() {}
64 pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
70 #if defined (_MSC_VER)
71 #pragma warning( pop )
74 SessionInitiator* SHIBSP_DLLLOCAL WAYFSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
76 return new WAYFSessionInitiator(p.first, p.second);
81 pair<bool,long> WAYFSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
83 // The IdP CANNOT be specified for us to run. Otherwise, we'd be redirecting to a WAYF
84 // anytime the IdP's metadata was wrong.
85 if (!entityID.empty())
86 return make_pair(false,0L);
91 const Handler* ACS=NULL;
92 const Application& app=request.getApplication();
93 pair<bool,const char*> discoveryURL = pair<bool,const char*>(true, m_url);
96 option=request.getParameter("acsIndex");
98 ACS=app.getAssertionConsumerServiceByIndex(atoi(option));
100 request.log(SPRequest::SPWarn, "invalid acsIndex specified in request, using acsIndex property");
103 option = request.getParameter("target");
106 recoverRelayState(request.getApplication(), request, request, target, false);
108 option = request.getParameter("discoveryURL");
110 discoveryURL.second = option;
113 // We're running as a "virtual handler" from within the filter.
114 // The target resource is the current one and everything else is defaulted.
115 target=request.getRequestURL();
116 discoveryURL = request.getRequestSettings().first->getString("discoveryURL");
119 // Since we're not passing by index, we need to fully compute the return URL.
121 pair<bool,unsigned int> index = getUnsignedInt("acsIndex");
123 ACS = app.getAssertionConsumerServiceByIndex(index.second);
125 request.log(SPRequest::SPWarn, "invalid acsIndex property, using default ACS location");
128 ACS = app.getDefaultAssertionConsumerService();
131 // Validate the ACS for use with this protocol.
132 pair<bool,const char*> ACSbinding = ACS ? ACS->getString("Binding") : pair<bool,const char*>(false,NULL);
133 if (ACSbinding.first) {
134 pair<bool,const char*> compatibleBindings = getString("compatibleBindings");
135 if (compatibleBindings.first && strstr(compatibleBindings.second, ACSbinding.second) == NULL) {
136 m_log.info("configured or requested ACS has non-SAML 1.x binding");
137 return make_pair(false,0L);
139 else if (strcmp(ACSbinding.second, samlconstants::SAML1_PROFILE_BROWSER_POST) &&
140 strcmp(ACSbinding.second, samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT)) {
141 m_log.info("configured or requested ACS has non-SAML 1.x binding");
142 return make_pair(false,0L);
146 if (!discoveryURL.first)
147 discoveryURL.second = m_url;
148 m_log.debug("sending request to WAYF (%s)", discoveryURL.second);
150 // Compute the ACS URL. We add the ACS location to the base handlerURL.
151 string ACSloc=request.getHandlerURL(target.c_str());
152 pair<bool,const char*> loc=ACS ? ACS->getString("Location") : pair<bool,const char*>(false,NULL);
153 if (loc.first) ACSloc+=loc.second;
156 // We may already have RelayState set if we looped back here,
157 // but just in case target is a resource, we reset it back.
158 option = request.getParameter("target");
162 preserveRelayState(request.getApplication(), request, target);
164 preservePostData(request.getApplication(), request, request, target.c_str());
166 // WAYF requires a target value.
171 sprintf(timebuf,"%lu",time(NULL));
172 const URLEncoder* urlenc = XMLToolingConfig::getConfig().getURLEncoder();
173 string req=string(discoveryURL.second) + (strchr(discoveryURL.second,'?') ? '&' : '?') + "shire=" + urlenc->encode(ACSloc.c_str()) +
174 "&time=" + timebuf + "&target=" + urlenc->encode(target.c_str()) +
175 "&providerId=" + urlenc->encode(app.getString("entityID").second);
177 return make_pair(true, request.sendRedirect(req.c_str()));