2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * SAML2LogoutInitiator.cpp
24 * Triggers SP-initiated logout for SAML 2.0 sessions.
28 #include "exceptions.h"
29 #include "Application.h"
30 #include "ServiceProvider.h"
31 #include "SessionCache.h"
32 #include "handler/AbstractHandler.h"
33 #include "handler/LogoutInitiator.h"
36 # include "binding/SOAPClient.h"
37 # include "metadata/MetadataProviderCriteria.h"
38 # include "security/SecurityPolicy.h"
39 # include <boost/algorithm/string.hpp>
40 # include <boost/iterator/indirect_iterator.hpp>
41 # include <saml/exceptions.h>
42 # include <saml/SAMLConfig.h>
43 # include <saml/saml2/core/Protocols.h>
44 # include <saml/saml2/binding/SAML2SOAPClient.h>
45 # include <saml/saml2/metadata/EndpointManager.h>
46 # include <saml/saml2/metadata/Metadata.h>
47 # include <saml/saml2/metadata/MetadataCredentialCriteria.h>
48 using namespace opensaml::saml2;
49 using namespace opensaml::saml2p;
50 using namespace opensaml::saml2md;
51 using namespace opensaml;
53 # include "lite/SAMLConstants.h"
56 #include <boost/scoped_ptr.hpp>
58 using namespace shibsp;
59 using namespace xmltooling;
60 using namespace boost;
65 #if defined (_MSC_VER)
66 #pragma warning( push )
67 #pragma warning( disable : 4250 )
70 class SHIBSP_DLLLOCAL SAML2LogoutInitiator : public AbstractHandler, public LogoutInitiator
73 SAML2LogoutInitiator(const DOMElement* e, const char* appId);
74 virtual ~SAML2LogoutInitiator() {}
76 void init(const char* location); // encapsulates actions that need to run either in the c'tor or setParent
78 void setParent(const PropertySet* parent);
79 void receive(DDF& in, ostream& out);
80 pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
82 const XMLCh* getProtocolFamily() const {
83 return samlconstants::SAML20P_NS;
87 pair<bool,long> doRequest(
88 const Application& application, const HTTPRequest& request, HTTPResponse& httpResponse, Session* session
93 auto_ptr<LogoutRequest> buildRequest(
94 const Application& application, const Session& session, const RoleDescriptor& role, const MessageEncoder* encoder=nullptr
97 LogoutEvent* newLogoutEvent(
98 const Application& application, const HTTPRequest* request=nullptr, const Session* session=nullptr
100 LogoutEvent* e = LogoutHandler::newLogoutEvent(application, request, session);
102 e->m_protocol = m_protocol.get();
106 vector<string> m_bindings;
107 map< string,boost::shared_ptr<MessageEncoder> > m_encoders;
109 auto_ptr_char m_protocol;
112 #if defined (_MSC_VER)
113 #pragma warning( pop )
116 Handler* SHIBSP_DLLLOCAL SAML2LogoutInitiatorFactory(const pair<const DOMElement*,const char*>& p)
118 return new SAML2LogoutInitiator(p.first, p.second);
122 SAML2LogoutInitiator::SAML2LogoutInitiator(const DOMElement* e, const char* appId)
123 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".LogoutInitiator.SAML2")), m_appId(appId), m_protocol(samlconstants::SAML20P_NS)
125 // If Location isn't set, defer initialization until the setParent call.
126 pair<bool,const char*> loc = getString("Location");
132 void SAML2LogoutInitiator::setParent(const PropertySet* parent)
134 DOMPropertySet::setParent(parent);
135 pair<bool,const char*> loc = getString("Location");
139 void SAML2LogoutInitiator::init(const char* location)
142 string address = m_appId + location + "::run::SAML2LI";
143 setAddress(address.c_str());
146 m_log.warn("no Location property in SAML2 LogoutInitiator (or parent), can't register as remoted handler");
150 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
152 pair<bool,const char*> outgoing = getString("outgoingBindings");
153 if (outgoing.first) {
154 dupBindings = outgoing.second;
157 // No override, so we'll install a default binding precedence.
158 dupBindings = string(samlconstants::SAML20_BINDING_HTTP_REDIRECT) + ' ' + samlconstants::SAML20_BINDING_HTTP_POST + ' ' +
159 samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN + ' ' + samlconstants::SAML20_BINDING_HTTP_ARTIFACT;
161 split(m_bindings, dupBindings, is_space(), algorithm::token_compress_on);
162 for (vector<string>::const_iterator b = m_bindings.begin(); b != m_bindings.end(); ++b) {
164 boost::shared_ptr<MessageEncoder> encoder(
165 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(*b, pair<const DOMElement*,const XMLCh*>(getElement(),nullptr))
167 if (encoder->isUserAgentPresent() && XMLString::equals(getProtocolFamily(), encoder->getProtocolFamily())) {
168 m_encoders[*b] = encoder;
169 m_log.debug("supporting outgoing binding (%s)", b->c_str());
172 m_log.warn("skipping outgoing binding (%s), not a SAML 2.0 front-channel mechanism", b->c_str());
175 catch (std::exception& ex) {
176 m_log.error("error building MessageEncoder: %s", ex.what());
184 pair<bool,long> SAML2LogoutInitiator::run(SPRequest& request, bool isHandler) const
186 // Defer to base class for front-channel loop first.
187 pair<bool,long> ret = LogoutHandler::run(request, isHandler);
191 // At this point we know the front-channel is handled.
192 // We need the session to do any other work.
194 Session* session = nullptr;
196 session = request.getSession(false, true, false); // don't cache it and ignore all checks
198 return make_pair(false, 0L);
200 // We only handle SAML 2.0 sessions.
201 if (!XMLString::equals(session->getProtocol(), m_protocol.get())) {
203 return make_pair(false, 0L);
206 catch (std::exception& ex) {
207 m_log.error("error accessing current session: %s", ex.what());
208 return make_pair(false, 0L);
211 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
212 // When out of process, we run natively.
213 return doRequest(request.getApplication(), request, request, session);
216 // When not out of process, we remote the request.
218 vector<string> headers(1,"Cookie");
219 DDF out,in = wrap(request,&headers);
220 DDFJanitor jin(in), jout(out);
221 out=request.getServiceProvider().getListenerService()->send(in);
222 return unwrap(request, out);
226 void SAML2LogoutInitiator::receive(DDF& in, ostream& out)
229 // Defer to base class for notifications
230 if (in["notify"].integer() == 1)
231 return LogoutHandler::receive(in, out);
234 const char* aid=in["application_id"].string();
235 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : nullptr;
237 // Something's horribly wrong.
238 m_log.error("couldn't find application (%s) for logout", aid ? aid : "(missing)");
239 throw ConfigurationException("Unable to locate application for logout, deleted?");
242 // Unpack the request.
243 scoped_ptr<HTTPRequest> req(getRequest(in));
245 // Set up a response shim.
247 DDFJanitor jout(ret);
248 scoped_ptr<HTTPResponse> resp(getResponse(ret));
250 Session* session = nullptr;
252 session = app->getServiceProvider().getSessionCache()->find(*app, *req, nullptr, nullptr);
254 catch (std::exception& ex) {
255 m_log.error("error accessing current session: %s", ex.what());
258 // With no session, we just skip the request and let it fall through to an empty struct return.
260 if (session->getNameID() && session->getEntityID()) {
261 // Since we're remoted, the result should either be a throw, which we pass on,
262 // a false/0 return, which we just return as an empty structure, or a response/redirect,
263 // which we capture in the facade and send back.
264 doRequest(*app, *req, *resp, session);
268 m_log.log(getParent() ? Priority::WARN : Priority::ERROR, "bypassing SAML 2.0 logout, no NameID or issuing entityID found in session");
269 app->getServiceProvider().getSessionCache()->remove(*app, *req, resp.get());
274 throw ConfigurationException("Cannot perform logout using lite version of shibsp library.");
278 pair<bool,long> SAML2LogoutInitiator::doRequest(
279 const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse, Session* session
282 Locker sessionLocker(session, false);
284 scoped_ptr<LogoutEvent> logout_event(newLogoutEvent(application, &httpRequest, session));
287 // Do back channel notification.
288 vector<string> sessions(1, session->getID());
289 if (!notifyBackChannel(application, httpRequest.getRequestURL(), sessions, false)) {
292 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_PARTIAL;
293 application.getServiceProvider().getTransactionLog()->write(*logout_event);
296 sessionLocker.assign();
298 application.getServiceProvider().getSessionCache()->remove(application, httpRequest, &httpResponse);
299 return sendLogoutPage(application, httpRequest, httpResponse, "partial");
303 pair<bool,long> ret = make_pair(false, 0L);
305 // With a session in hand, we can create a LogoutRequest message, if we can find a compatible endpoint.
306 MetadataProvider* m = application.getMetadataProvider();
307 Locker metadataLocker(m);
308 MetadataProviderCriteria mc(application, session->getEntityID(), &IDPSSODescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
309 pair<const EntityDescriptor*,const RoleDescriptor*> entity = m->getEntityDescriptor(mc);
311 throw MetadataException(
312 "Unable to locate metadata for identity provider ($entityID)", namedparams(1, "entityID", session->getEntityID())
315 else if (!entity.second) {
316 throw MetadataException(
317 "Unable to locate SAML 2.0 IdP role for identity provider ($entityID).", namedparams(1, "entityID", session->getEntityID())
321 const IDPSSODescriptor* role = dynamic_cast<const IDPSSODescriptor*>(entity.second);
322 if (role->getSingleLogoutServices().empty()) {
323 throw MetadataException(
324 "No SingleLogoutService endpoints in metadata for identity provider ($entityID).", namedparams(1, "entityID", session->getEntityID())
328 const EndpointType* ep = nullptr;
329 const MessageEncoder* encoder = nullptr;
330 for (vector<string>::const_iterator b = m_bindings.begin(); b != m_bindings.end(); ++b) {
331 auto_ptr_XMLCh wideb(b->c_str());
332 if (ep = EndpointManager<SingleLogoutService>(role->getSingleLogoutServices()).getByBinding(wideb.get())) {
333 map< string,boost::shared_ptr<MessageEncoder> >::const_iterator enc = m_encoders.find(*b);
334 if (enc != m_encoders.end())
335 encoder = enc->second.get();
339 if (!ep || !encoder) {
340 m_log.debug("no compatible front channel SingleLogoutService, trying back channel...");
341 shibsp::SecurityPolicy policy(application);
342 shibsp::SOAPClient soaper(policy);
343 MetadataCredentialCriteria mcc(*role);
345 LogoutResponse* logoutResponse = nullptr;
346 scoped_ptr<StatusResponseType> srt;
347 auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
348 const vector<SingleLogoutService*>& endpoints = role->getSingleLogoutServices();
349 for (indirect_iterator<vector<SingleLogoutService*>::const_iterator> epit = make_indirect_iterator(endpoints.begin());
350 !logoutResponse && epit != make_indirect_iterator(endpoints.end()); ++epit) {
352 if (!XMLString::equals(epit->getBinding(), binding.get()))
354 auto_ptr<LogoutRequest> msg(buildRequest(application, *session, *role));
358 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_UNKNOWN;
359 logout_event->m_saml2Request = msg.get();
360 application.getServiceProvider().getTransactionLog()->write(*logout_event);
363 auto_ptr_char dest(epit->getLocation());
364 SAML2SOAPClient client(soaper, false);
365 client.sendSAML(msg.release(), application.getId(), mcc, dest.get());
366 srt.reset(client.receiveSAML());
367 if (!(logoutResponse = dynamic_cast<LogoutResponse*>(srt.get()))) {
371 catch (std::exception& ex) {
372 m_log.error("error sending LogoutRequest message: %s", ex.what());
378 if (!logoutResponse) {
379 if (endpoints.empty())
380 m_log.info("IdP doesn't support single logout protocol over a compatible binding");
382 m_log.warn("IdP didn't respond to logout request");
384 // Log the end result.
386 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_PARTIAL;
387 application.getServiceProvider().getTransactionLog()->write(*logout_event);
390 ret = sendLogoutPage(application, httpRequest, httpResponse, "partial");
393 // Check the status, looking for non-success or a partial logout code.
394 const StatusCode* sc = logoutResponse->getStatus() ? logoutResponse->getStatus()->getStatusCode() : nullptr;
395 bool partial = (!sc || !XMLString::equals(sc->getValue(), StatusCode::SUCCESS));
396 if (!partial && sc->getStatusCode()) {
397 // Success, but still need to check for partial.
398 partial = XMLString::equals(sc->getStatusCode()->getValue(), StatusCode::PARTIAL_LOGOUT);
401 // Log the end result.
403 logout_event->m_logoutType = partial ? LogoutEvent::LOGOUT_EVENT_PARTIAL : LogoutEvent::LOGOUT_EVENT_GLOBAL;
404 logout_event->m_saml2Response = logoutResponse;
405 application.getServiceProvider().getTransactionLog()->write(*logout_event);
409 ret = sendLogoutPage(application, httpRequest, httpResponse, "partial");
411 const char* returnloc = httpRequest.getParameter("return");
413 limitRelayState(m_log, application, httpRequest, returnloc);
414 ret.second = httpResponse.sendRedirect(returnloc);
417 ret = sendLogoutPage(application, httpRequest, httpResponse, "global");
422 sessionLocker.assign();
424 application.getServiceProvider().getSessionCache()->remove(application, httpRequest, &httpResponse);
430 // Save off return location as RelayState.
432 const char* returnloc = httpRequest.getParameter("return");
434 limitRelayState(m_log, application, httpRequest, returnloc);
435 relayState = returnloc;
436 preserveRelayState(application, httpResponse, relayState);
439 auto_ptr<LogoutRequest> msg(buildRequest(application, *session, *role, encoder));
440 msg->setDestination(ep->getLocation());
444 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_UNKNOWN;
445 logout_event->m_saml2Request = msg.get();
446 application.getServiceProvider().getTransactionLog()->write(*logout_event);
449 auto_ptr_char dest(ep->getLocation());
450 ret.second = sendMessage(*encoder, msg.get(), relayState.c_str(), dest.get(), role, application, httpResponse, true);
452 msg.release(); // freed by encoder
455 sessionLocker.assign();
457 application.getServiceProvider().getSessionCache()->remove(application, httpRequest, &httpResponse);
460 catch (MetadataException& mex) {
461 // Less noise for IdPs that don't support logout (i.e. most)
462 m_log.info("unable to issue SAML 2.0 logout request: %s", mex.what());
464 catch (std::exception& ex) {
465 m_log.error("error issuing SAML 2.0 logout request: %s", ex.what());
470 throw ConfigurationException("Cannot perform logout using lite version of shibsp library.");
476 auto_ptr<LogoutRequest> SAML2LogoutInitiator::buildRequest(
477 const Application& application, const Session& session, const RoleDescriptor& role, const MessageEncoder* encoder
480 const PropertySet* relyingParty = application.getRelyingParty(dynamic_cast<EntityDescriptor*>(role.getParent()));
482 auto_ptr<LogoutRequest> msg(LogoutRequestBuilder::buildLogoutRequest());
483 Issuer* issuer = IssuerBuilder::buildIssuer();
484 msg->setIssuer(issuer);
485 issuer->setName(relyingParty->getXMLString("entityID").second);
486 auto_ptr_XMLCh index(session.getSessionIndex());
487 if (index.get() && *index.get()) {
488 SessionIndex* si = SessionIndexBuilder::buildSessionIndex();
489 msg->getSessionIndexs().push_back(si);
490 si->setSessionIndex(index.get());
493 const NameID* nameid = session.getNameID();
494 pair<bool,const char*> flag = relyingParty->getString("encryption");
496 (!strcmp(flag.second, "true") || (encoder && !strcmp(flag.second, "front")) || (!encoder && !strcmp(flag.second, "back")))) {
497 auto_ptr<EncryptedID> encrypted(EncryptedIDBuilder::buildEncryptedID());
498 MetadataCredentialCriteria mcc(role);
501 *(application.getMetadataProvider()),
503 encoder ? encoder->isCompact() : false,
504 relyingParty->getXMLString("encryptionAlg").second
506 msg->setEncryptedID(encrypted.get());
510 msg->setNameID(nameid->cloneNameID());
513 msg->setID(SAMLConfig::getConfig().generateIdentifier());
514 msg->setIssueInstant(time(nullptr));