2 * Copyright 2001-2011 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 * SAML2LogoutInitiator.cpp
20 * Triggers SP-initiated logout for SAML 2.0 sessions.
24 #include "exceptions.h"
25 #include "Application.h"
26 #include "ServiceProvider.h"
27 #include "SessionCache.h"
28 #include "handler/AbstractHandler.h"
29 #include "handler/LogoutInitiator.h"
32 # include "binding/SOAPClient.h"
33 # include "metadata/MetadataProviderCriteria.h"
34 # include "security/SecurityPolicy.h"
35 # include <saml/exceptions.h>
36 # include <saml/SAMLConfig.h>
37 # include <saml/saml2/core/Protocols.h>
38 # include <saml/saml2/binding/SAML2SOAPClient.h>
39 # include <saml/saml2/metadata/EndpointManager.h>
40 # include <saml/saml2/metadata/Metadata.h>
41 # include <saml/saml2/metadata/MetadataCredentialCriteria.h>
42 using namespace opensaml::saml2;
43 using namespace opensaml::saml2p;
44 using namespace opensaml::saml2md;
45 using namespace opensaml;
47 # include "lite/SAMLConstants.h"
50 using namespace shibsp;
51 using namespace xmltooling;
56 #if defined (_MSC_VER)
57 #pragma warning( push )
58 #pragma warning( disable : 4250 )
61 class SHIBSP_DLLLOCAL SAML2LogoutInitiator : public AbstractHandler, public LogoutInitiator
64 SAML2LogoutInitiator(const DOMElement* e, const char* appId);
65 virtual ~SAML2LogoutInitiator() {
67 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
68 XMLString::release(&m_outgoing);
69 for_each(m_encoders.begin(), m_encoders.end(), cleanup_pair<const XMLCh*,MessageEncoder>());
74 void init(const char* location); // encapsulates actions that need to run either in the c'tor or setParent
76 void setParent(const PropertySet* parent);
77 void receive(DDF& in, ostream& out);
78 pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
80 const XMLCh* getProtocolFamily() const {
81 return samlconstants::SAML20P_NS;
85 pair<bool,long> doRequest(
86 const Application& application, const HTTPRequest& request, HTTPResponse& httpResponse, Session* session
91 LogoutRequest* buildRequest(
92 const Application& application, const Session& session, const RoleDescriptor& role, const MessageEncoder* encoder=nullptr
96 vector<const XMLCh*> m_bindings;
97 map<const XMLCh*,MessageEncoder*> m_encoders;
99 auto_ptr_char m_protocol;
102 #if defined (_MSC_VER)
103 #pragma warning( pop )
106 Handler* SHIBSP_DLLLOCAL SAML2LogoutInitiatorFactory(const pair<const DOMElement*,const char*>& p)
108 return new SAML2LogoutInitiator(p.first, p.second);
112 SAML2LogoutInitiator::SAML2LogoutInitiator(const DOMElement* e, const char* appId)
113 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".LogoutInitiator.SAML2")), m_appId(appId),
117 m_protocol(samlconstants::SAML20P_NS)
119 // If Location isn't set, defer initialization until the setParent call.
120 pair<bool,const char*> loc = getString("Location");
126 void SAML2LogoutInitiator::setParent(const PropertySet* parent)
128 DOMPropertySet::setParent(parent);
129 pair<bool,const char*> loc = getString("Location");
133 void SAML2LogoutInitiator::init(const char* location)
136 string address = m_appId + location + "::run::SAML2LI";
137 setAddress(address.c_str());
140 m_log.warn("no Location property in SAML2 LogoutInitiator (or parent), can't register as remoted handler");
144 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
145 // Handle outgoing binding setup.
146 pair<bool,const XMLCh*> outgoing = getXMLString("outgoingBindings");
147 if (outgoing.first) {
148 m_outgoing = XMLString::replicate(outgoing.second);
149 XMLString::trim(m_outgoing);
152 // No override, so we'll install a default binding precedence.
153 string prec = string(samlconstants::SAML20_BINDING_HTTP_REDIRECT) + ' ' + samlconstants::SAML20_BINDING_HTTP_POST + ' ' +
154 samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN + ' ' + samlconstants::SAML20_BINDING_HTTP_ARTIFACT;
155 m_outgoing = XMLString::transcode(prec.c_str());
159 XMLCh* start = m_outgoing;
160 while (start && *start) {
161 pos = XMLString::indexOf(start,chSpace);
163 *(start + pos)=chNull;
164 m_bindings.push_back(start);
166 auto_ptr_char b(start);
167 MessageEncoder * encoder =
168 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(b.get(), pair<const DOMElement*,const XMLCh*>(getElement(), nullptr));
169 if (encoder->isUserAgentPresent() && XMLString::equals(getProtocolFamily(), encoder->getProtocolFamily())) {
170 m_encoders[start] = encoder;
171 m_log.debug("supporting outgoing binding (%s)", b.get());
175 m_log.warn("skipping outgoing binding (%s), not a SAML 2.0 front-channel mechanism", b.get());
178 catch (exception& ex) {
179 m_log.error("error building MessageEncoder: %s", ex.what());
182 start = start + pos + 1;
191 pair<bool,long> SAML2LogoutInitiator::run(SPRequest& request, bool isHandler) const
193 // Defer to base class for front-channel loop first.
194 pair<bool,long> ret = LogoutHandler::run(request, isHandler);
198 // At this point we know the front-channel is handled.
199 // We need the session to do any other work.
201 Session* session = nullptr;
203 session = request.getSession(false, true, false); // don't cache it and ignore all checks
205 return make_pair(false,0L);
207 // We only handle SAML 2.0 sessions.
208 if (!XMLString::equals(session->getProtocol(), m_protocol.get())) {
210 return make_pair(false,0L);
213 catch (exception& ex) {
214 m_log.error("error accessing current session: %s", ex.what());
215 return make_pair(false,0L);
218 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
219 // When out of process, we run natively.
220 return doRequest(request.getApplication(), request, request, session);
223 // When not out of process, we remote the request.
225 vector<string> headers(1,"Cookie");
226 DDF out,in = wrap(request,&headers);
227 DDFJanitor jin(in), jout(out);
228 out=request.getServiceProvider().getListenerService()->send(in);
229 return unwrap(request, out);
233 void SAML2LogoutInitiator::receive(DDF& in, ostream& out)
236 // Defer to base class for notifications
237 if (in["notify"].integer() == 1)
238 return LogoutHandler::receive(in, out);
241 const char* aid=in["application_id"].string();
242 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : nullptr;
244 // Something's horribly wrong.
245 m_log.error("couldn't find application (%s) for logout", aid ? aid : "(missing)");
246 throw ConfigurationException("Unable to locate application for logout, deleted?");
249 // Unpack the request.
250 auto_ptr<HTTPRequest> req(getRequest(in));
252 // Set up a response shim.
254 DDFJanitor jout(ret);
255 auto_ptr<HTTPResponse> resp(getResponse(ret));
257 Session* session = nullptr;
259 session = app->getServiceProvider().getSessionCache()->find(*app, *req.get(), nullptr, nullptr);
261 catch (exception& ex) {
262 m_log.error("error accessing current session: %s", ex.what());
265 // With no session, we just skip the request and let it fall through to an empty struct return.
267 if (session->getNameID() && session->getEntityID()) {
268 // Since we're remoted, the result should either be a throw, which we pass on,
269 // a false/0 return, which we just return as an empty structure, or a response/redirect,
270 // which we capture in the facade and send back.
271 doRequest(*app, *req.get(), *resp.get(), session);
274 m_log.log(getParent() ? Priority::WARN : Priority::ERROR, "bypassing SAML 2.0 logout, no NameID or issuing entityID found in session");
276 app->getServiceProvider().getSessionCache()->remove(*app, *req.get(), resp.get());
281 throw ConfigurationException("Cannot perform logout using lite version of shibsp library.");
285 pair<bool,long> SAML2LogoutInitiator::doRequest(
286 const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse, Session* session
289 // Do back channel notification.
290 vector<string> sessions(1, session->getID());
291 if (!notifyBackChannel(application, httpRequest.getRequestURL(), sessions, false)) {
293 application.getServiceProvider().getSessionCache()->remove(application, httpRequest, &httpResponse);
294 return sendLogoutPage(application, httpRequest, httpResponse, "partial");
298 pair<bool,long> ret = make_pair(false,0L);
300 // With a session in hand, we can create a LogoutRequest message, if we can find a compatible endpoint.
301 MetadataProvider* m = application.getMetadataProvider();
302 Locker metadataLocker(m);
303 MetadataProviderCriteria mc(application, session->getEntityID(), &IDPSSODescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
304 pair<const EntityDescriptor*,const RoleDescriptor*> entity = m->getEntityDescriptor(mc);
306 throw MetadataException(
307 "Unable to locate metadata for identity provider ($entityID)", namedparams(1, "entityID", session->getEntityID())
310 else if (!entity.second) {
311 throw MetadataException(
312 "Unable to locate SAML 2.0 IdP role for identity provider ($entityID).", namedparams(1, "entityID", session->getEntityID())
316 const IDPSSODescriptor* role = dynamic_cast<const IDPSSODescriptor*>(entity.second);
317 if (role->getSingleLogoutServices().empty()) {
318 throw MetadataException(
319 "No SingleLogoutService endpoints in metadata for identity provider ($entityID).", namedparams(1, "entityID", session->getEntityID())
323 const EndpointType* ep=nullptr;
324 const MessageEncoder* encoder=nullptr;
325 vector<const XMLCh*>::const_iterator b;
326 for (b = m_bindings.begin(); b!=m_bindings.end(); ++b) {
327 if (ep=EndpointManager<SingleLogoutService>(role->getSingleLogoutServices()).getByBinding(*b)) {
328 map<const XMLCh*,MessageEncoder*>::const_iterator enc = m_encoders.find(*b);
329 if (enc!=m_encoders.end())
330 encoder = enc->second;
334 if (!ep || !encoder) {
335 m_log.debug("no compatible front channel SingleLogoutService, trying back channel...");
336 shibsp::SecurityPolicy policy(application);
337 shibsp::SOAPClient soaper(policy);
338 MetadataCredentialCriteria mcc(*role);
340 LogoutResponse* logoutResponse=nullptr;
341 auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
342 const vector<SingleLogoutService*>& endpoints=role->getSingleLogoutServices();
343 for (vector<SingleLogoutService*>::const_iterator epit=endpoints.begin(); !logoutResponse && epit!=endpoints.end(); ++epit) {
345 if (!XMLString::equals((*epit)->getBinding(),binding.get()))
347 LogoutRequest* msg = buildRequest(application, *session, *role);
348 auto_ptr_char dest((*epit)->getLocation());
350 SAML2SOAPClient client(soaper, false);
351 client.sendSAML(msg, application.getId(), mcc, dest.get());
352 StatusResponseType* srt = client.receiveSAML();
353 if (!(logoutResponse = dynamic_cast<LogoutResponse*>(srt))) {
358 catch (exception& ex) {
359 m_log.error("error sending LogoutRequest message: %s", ex.what());
365 if (!logoutResponse) {
366 if (endpoints.empty())
367 m_log.info("IdP doesn't support single logout protocol over a compatible binding");
369 m_log.warn("IdP didn't respond to logout request");
370 ret = sendLogoutPage(application, httpRequest, httpResponse, "partial");
373 // Check the status, looking for non-success or a partial logout code.
374 const StatusCode* sc = logoutResponse->getStatus() ? logoutResponse->getStatus()->getStatusCode() : nullptr;
375 bool partial = (!sc || !XMLString::equals(sc->getValue(), StatusCode::SUCCESS));
376 if (!partial && sc->getStatusCode()) {
377 // Success, but still need to check for partial.
378 partial = XMLString::equals(sc->getStatusCode()->getValue(), StatusCode::PARTIAL_LOGOUT);
380 delete logoutResponse;
382 ret = sendLogoutPage(application, httpRequest, httpResponse, "partial");
384 const char* returnloc = httpRequest.getParameter("return");
386 limitRelayState(m_log, application, httpRequest, returnloc);
387 ret.second = httpResponse.sendRedirect(returnloc);
390 ret = sendLogoutPage(application, httpRequest, httpResponse, "global");
397 application.getServiceProvider().getSessionCache()->remove(application, httpRequest, &httpResponse);
403 // Save off return location as RelayState.
405 const char* returnloc = httpRequest.getParameter("return");
407 limitRelayState(m_log, application, httpRequest, returnloc);
408 relayState = returnloc;
409 preserveRelayState(application, httpResponse, relayState);
412 auto_ptr<LogoutRequest> msg(buildRequest(application, *session, *role, encoder));
414 msg->setDestination(ep->getLocation());
415 auto_ptr_char dest(ep->getLocation());
416 ret.second = sendMessage(*encoder, msg.get(), relayState.c_str(), dest.get(), role, application, httpResponse);
418 msg.release(); // freed by encoder
420 catch (exception& ex) {
421 m_log.error("error issuing SAML 2.0 logout request: %s", ex.what());
427 application.getServiceProvider().getSessionCache()->remove(application, httpRequest, &httpResponse);
433 application.getServiceProvider().getSessionCache()->remove(application, httpRequest, &httpResponse);
434 throw ConfigurationException("Cannot perform logout using lite version of shibsp library.");
440 LogoutRequest* SAML2LogoutInitiator::buildRequest(
441 const Application& application, const Session& session, const RoleDescriptor& role, const MessageEncoder* encoder
444 const PropertySet* relyingParty = application.getRelyingParty(dynamic_cast<EntityDescriptor*>(role.getParent()));
446 auto_ptr<LogoutRequest> msg(LogoutRequestBuilder::buildLogoutRequest());
447 Issuer* issuer = IssuerBuilder::buildIssuer();
448 msg->setIssuer(issuer);
449 issuer->setName(relyingParty->getXMLString("entityID").second);
450 auto_ptr_XMLCh index(session.getSessionIndex());
451 if (index.get() && *index.get()) {
452 SessionIndex* si = SessionIndexBuilder::buildSessionIndex();
453 msg->getSessionIndexs().push_back(si);
454 si->setSessionIndex(index.get());
457 const NameID* nameid = session.getNameID();
458 pair<bool,const char*> flag = relyingParty->getString("encryption");
460 (!strcmp(flag.second, "true") || (encoder && !strcmp(flag.second, "front")) || (!encoder && !strcmp(flag.second, "back")))) {
461 auto_ptr<EncryptedID> encrypted(EncryptedIDBuilder::buildEncryptedID());
462 MetadataCredentialCriteria mcc(role);
465 *(application.getMetadataProvider()),
467 encoder ? encoder->isCompact() : false,
468 relyingParty->getXMLString("encryptionAlg").second
470 msg->setEncryptedID(encrypted.release());
473 msg->setNameID(nameid->cloneNameID());
476 return msg.release();