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 * Shib1SessionInitiator.cpp
20 * Shibboleth 1.x AuthnRequest support.
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "ServiceProvider.h"
27 #include "SPRequest.h"
28 #include "handler/AbstractHandler.h"
29 #include "handler/RemotedHandler.h"
30 #include "handler/SessionInitiator.h"
31 #include "util/SPConstants.h"
33 #include <saml/saml2/metadata/Metadata.h>
34 #include <saml/saml2/metadata/EndpointManager.h>
35 #include <xmltooling/XMLToolingConfig.h>
36 #include <xmltooling/util/URLEncoder.h>
38 using namespace shibsp;
39 using namespace opensaml::saml2md;
40 using namespace opensaml;
41 using namespace xmltooling;
42 using namespace log4cpp;
47 #if defined (_MSC_VER)
48 #pragma warning( push )
49 #pragma warning( disable : 4250 )
52 class SHIBSP_DLLLOCAL Shib1SessionInitiator : public SessionInitiator, public AbstractHandler, public RemotedHandler
55 Shib1SessionInitiator(const DOMElement* e, const char* appId)
56 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator")), m_appId(appId) {
57 // If Location isn't set, defer address registration until the setParent call.
58 pair<bool,const char*> loc = getString("Location");
60 string address = m_appId + loc.second + "::run::Shib1SI";
61 setAddress(address.c_str());
64 virtual ~Shib1SessionInitiator() {}
66 void setParent(const PropertySet* parent);
67 void receive(DDF& in, ostream& out);
68 pair<bool,long> run(SPRequest& request, const char* entityID=NULL, bool isHandler=true) const;
71 pair<bool,long> doRequest(
72 const Application& application,
73 HTTPResponse& httpResponse,
75 const char* acsLocation,
82 #if defined (_MSC_VER)
83 #pragma warning( pop )
86 SessionInitiator* SHIBSP_DLLLOCAL Shib1SessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
88 return new Shib1SessionInitiator(p.first, p.second);
93 void Shib1SessionInitiator::setParent(const PropertySet* parent)
95 DOMPropertySet::setParent(parent);
96 pair<bool,const char*> loc = getString("Location");
98 string address = m_appId + loc.second + "::run::Shib1SI";
99 setAddress(address.c_str());
102 m_log.warn("no Location property in Shib1 SessionInitiator (or parent), can't register as remoted handler");
106 pair<bool,long> Shib1SessionInitiator::run(SPRequest& request, const char* entityID, bool isHandler) const
108 // We have to know the IdP to function.
109 if (!entityID || !*entityID)
110 return make_pair(false,0);
113 const Handler* ACS=NULL;
115 const Application& app=request.getApplication();
118 option=request.getParameter("acsIndex");
120 ACS = app.getAssertionConsumerServiceByIndex(atoi(option));
122 option = request.getParameter("target");
126 // Since we're passing the ACS by value, we need to compute the return URL,
127 // so we'll need the target resource for real.
128 recoverRelayState(request.getApplication(), request, target, false);
131 // We're running as a "virtual handler" from within the filter.
132 // The target resource is the current one and everything else is defaulted.
133 target=request.getRequestURL();
136 // Since we're not passing by index, we need to fully compute the return URL and binding.
138 ACS = app.getDefaultAssertionConsumerService();
140 // Compute the ACS URL. We add the ACS location to the base handlerURL.
141 string ACSloc=request.getHandlerURL(target.c_str());
142 pair<bool,const char*> loc=ACS ? ACS->getString("Location") : pair<bool,const char*>(false,NULL);
143 if (loc.first) ACSloc+=loc.second;
146 // We may already have RelayState set if we looped back here,
147 // but just in case target is a resource, we reset it back.
149 option = request.getParameter("target");
154 m_log.debug("attempting to initiate session using Shibboleth with provider (%s)", entityID);
156 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess))
157 return doRequest(app, request, entityID, ACSloc.c_str(), target);
160 DDF out,in = DDF(m_address.c_str()).structure();
161 DDFJanitor jin(in), jout(out);
162 in.addmember("application_id").string(app.getId());
163 in.addmember("entity_id").string(entityID);
164 in.addmember("acsLocation").string(ACSloc.c_str());
166 in.addmember("RelayState").string(target.c_str());
168 // Remote the processing.
169 out = request.getServiceProvider().getListenerService()->send(in);
170 return unwrap(request, out);
173 void Shib1SessionInitiator::receive(DDF& in, ostream& out)
176 const char* aid=in["application_id"].string();
177 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
179 // Something's horribly wrong.
180 m_log.error("couldn't find application (%s) to generate AuthnRequest", aid ? aid : "(missing)");
181 throw ConfigurationException("Unable to locate application for new session, deleted?");
184 const char* entityID = in["entity_id"].string();
185 const char* acsLocation = in["acsLocation"].string();
186 if (!entityID || !acsLocation)
187 throw ConfigurationException("No entityID or acsLocation parameter supplied to remoted SessionInitiator.");
190 DDFJanitor jout(ret);
192 // Wrap the outgoing object with a Response facade.
193 auto_ptr<HTTPResponse> http(getResponse(ret));
195 string relayState(in["RelayState"].string() ? in["RelayState"].string() : "");
197 // Since we're remoted, the result should either be a throw, which we pass on,
198 // a false/0 return, which we just return as an empty structure, or a response/redirect,
199 // which we capture in the facade and send back.
200 doRequest(*app, *http.get(), entityID, acsLocation, relayState);
204 pair<bool,long> Shib1SessionInitiator::doRequest(
205 const Application& app,
206 HTTPResponse& httpResponse,
207 const char* entityID,
208 const char* acsLocation,
212 // Use metadata to invoke the SSO service directly.
213 MetadataProvider* m=app.getMetadataProvider();
215 const EntityDescriptor* entity=m->getEntityDescriptor(entityID);
217 m_log.error("unable to locate metadata for provider (%s)", entityID);
218 return make_pair(false,0);
220 const IDPSSODescriptor* role=entity->getIDPSSODescriptor(shibspconstants::SHIB1_PROTOCOL_ENUM);
222 m_log.error("unable to locate Shibboleth-aware identity provider role for provider (%s)", entityID);
223 return make_pair(false,0);
225 const EndpointType* ep=EndpointManager<SingleSignOnService>(role->getSingleSignOnServices()).getByBinding(
226 shibspconstants::SHIB1_AUTHNREQUEST_PROFILE_URI
229 m_log.error("unable to locate compatible SSO service for provider (%s)", entityID);
230 return make_pair(false,0);
233 preserveRelayState(app, httpResponse, relayState);
235 // Shib 1.x requires a target value.
236 if (relayState.empty())
237 relayState = "default";
240 sprintf(timebuf,"%u",time(NULL));
241 const URLEncoder* urlenc = XMLToolingConfig::getConfig().getURLEncoder();
242 auto_ptr_char dest(ep->getLocation());
243 string req=string(dest.get()) + (strchr(dest.get(),'?') ? '&' : '?') + "shire=" + urlenc->encode(acsLocation) +
244 "&time=" + timebuf + "&target=" + urlenc->encode(relayState.c_str()) +
245 "&providerId=" + urlenc->encode(app.getString("entityID").second);
247 return make_pair(true, httpResponse.sendRedirect(req.c_str()));