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.
20 * Handles SAML 2.0 single logout protocol messages.
24 #include "exceptions.h"
25 #include "Application.h"
26 #include "ServiceProvider.h"
27 #include "SPRequest.h"
28 #include "handler/AbstractHandler.h"
29 #include "handler/LogoutHandler.h"
30 #include "util/SPConstants.h"
33 # include "SessionCacheEx.h"
34 # include "security/SecurityPolicy.h"
35 # include "metadata/MetadataProviderCriteria.h"
36 # include "util/TemplateParameters.h"
38 # include <saml/exceptions.h>
39 # include <saml/SAMLConfig.h>
40 # include <saml/saml2/core/Protocols.h>
41 # include <saml/saml2/metadata/EndpointManager.h>
42 # include <saml/saml2/metadata/Metadata.h>
43 # include <saml/saml2/metadata/MetadataCredentialCriteria.h>
44 # include <xmltooling/util/URLEncoder.h>
45 using namespace opensaml::saml2;
46 using namespace opensaml::saml2p;
47 using namespace opensaml::saml2md;
48 using namespace opensaml;
51 using namespace shibsp;
52 using namespace xmltooling;
57 #if defined (_MSC_VER)
58 #pragma warning( push )
59 #pragma warning( disable : 4250 )
62 class SHIBSP_DLLLOCAL SAML2Logout : public AbstractHandler, public LogoutHandler
65 SAML2Logout(const DOMElement* e, const char* appId);
66 virtual ~SAML2Logout() {
68 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
70 XMLString::release(&m_outgoing);
71 for_each(m_encoders.begin(), m_encoders.end(), cleanup_pair<const XMLCh*,MessageEncoder>());
76 void receive(DDF& in, ostream& out);
77 pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
80 void generateMetadata(SPSSODescriptor& role, const char* handlerURL) const {
81 const char* loc = getString("Location").second;
82 string hurl(handlerURL);
86 auto_ptr_XMLCh widen(hurl.c_str());
87 SingleLogoutService* ep = SingleLogoutServiceBuilder::buildSingleLogoutService();
88 ep->setLocation(widen.get());
89 ep->setBinding(getXMLString("Binding").second);
90 role.getSingleLogoutServices().push_back(ep);
91 role.addSupport(samlconstants::SAML20P_NS);
94 const char* getType() const {
95 return "SingleLogoutService";
100 pair<bool,long> doRequest(const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse) const;
103 pair<bool,long> sendResponse(
104 const XMLCh* requestID,
106 const XMLCh* subcode,
108 const char* relayState,
109 const RoleDescriptor* role,
110 const Application& application,
111 HTTPResponse& httpResponse,
115 xmltooling::QName m_role;
116 MessageDecoder* m_decoder;
118 vector<const XMLCh*> m_bindings;
119 map<const XMLCh*,MessageEncoder*> m_encoders;
123 #if defined (_MSC_VER)
124 #pragma warning( pop )
127 Handler* SHIBSP_DLLLOCAL SAML2LogoutFactory(const pair<const DOMElement*,const char*>& p)
129 return new SAML2Logout(p.first, p.second);
133 SAML2Logout::SAML2Logout(const DOMElement* e, const char* appId)
134 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".Logout.SAML2"))
136 ,m_role(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME), m_decoder(NULL), m_outgoing(NULL)
141 m_preserve.push_back("ID");
142 m_preserve.push_back("entityID");
143 m_preserve.push_back("RelayState");
145 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
146 SAMLConfig& conf = SAMLConfig::getConfig();
148 // Handle incoming binding.
149 m_decoder = conf.MessageDecoderManager.newPlugin(
150 getString("Binding").second, pair<const DOMElement*,const XMLCh*>(e,shibspconstants::SHIB2SPCONFIG_NS)
152 m_decoder->setArtifactResolver(SPConfig::getConfig().getArtifactResolver());
154 if (m_decoder->isUserAgentPresent()) {
155 // Handle front-channel binding setup.
156 pair<bool,const XMLCh*> outgoing = getXMLString("outgoingBindings", m_configNS.get());
157 if (outgoing.first) {
158 m_outgoing = XMLString::replicate(outgoing.second);
159 XMLString::trim(m_outgoing);
162 // No override, so we'll install a default binding precedence.
163 string prec = string(samlconstants::SAML20_BINDING_HTTP_REDIRECT) + ' ' + samlconstants::SAML20_BINDING_HTTP_POST + ' ' +
164 samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN + ' ' + samlconstants::SAML20_BINDING_HTTP_ARTIFACT;
165 m_outgoing = XMLString::transcode(prec.c_str());
169 XMLCh* start = m_outgoing;
170 while (start && *start) {
171 pos = XMLString::indexOf(start,chSpace);
173 *(start + pos)=chNull;
174 m_bindings.push_back(start);
176 auto_ptr_char b(start);
177 MessageEncoder * encoder = conf.MessageEncoderManager.newPlugin(
178 b.get(), pair<const DOMElement*,const XMLCh*>(e,shibspconstants::SHIB2SPCONFIG_NS)
180 if (encoder->isUserAgentPresent()) {
181 m_encoders[start] = encoder;
182 m_log.debug("supporting outgoing binding (%s)", b.get());
186 m_log.warn("skipping outgoing binding (%s), not a front-channel mechanism", b.get());
189 catch (exception& ex) {
190 m_log.error("error building MessageEncoder: %s", ex.what());
193 start = start + pos + 1;
199 MessageEncoder* encoder = conf.MessageEncoderManager.newPlugin(
200 getString("Binding").second, pair<const DOMElement*,const XMLCh*>(e,shibspconstants::SHIB2SPCONFIG_NS)
202 m_encoders.insert(pair<const XMLCh*,MessageEncoder*>(NULL, encoder));
207 string address(appId);
208 address += getString("Location").second;
209 setAddress(address.c_str());
212 pair<bool,long> SAML2Logout::run(SPRequest& request, bool isHandler) const
214 // Defer to base class for front-channel loop first.
215 // This won't initiate the loop, only continue/end it.
216 pair<bool,long> ret = LogoutHandler::run(request, isHandler);
220 SPConfig& conf = SPConfig::getConfig();
221 if (conf.isEnabled(SPConfig::OutOfProcess)) {
222 // When out of process, we run natively and directly process the message.
223 return doRequest(request.getApplication(), request, request);
226 // When not out of process, we remote all the message processing.
227 vector<string> headers(1,"Cookie");
228 DDF out,in = wrap(request, &headers, true);
229 DDFJanitor jin(in), jout(out);
230 out=request.getServiceProvider().getListenerService()->send(in);
231 return unwrap(request, out);
235 void SAML2Logout::receive(DDF& in, ostream& out)
238 const char* aid=in["application_id"].string();
239 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
241 // Something's horribly wrong.
242 m_log.error("couldn't find application (%s) for logout", aid ? aid : "(missing)");
243 throw ConfigurationException("Unable to locate application for logout, deleted?");
246 // Unpack the request.
247 auto_ptr<HTTPRequest> req(getRequest(in));
249 // Wrap a response shim.
251 DDFJanitor jout(ret);
252 auto_ptr<HTTPResponse> resp(getResponse(ret));
254 // Since we're remoted, the result should either be a throw, which we pass on,
255 // a false/0 return, which we just return as an empty structure, or a response/redirect,
256 // which we capture in the facade and send back.
257 doRequest(*app, *req.get(), *resp.get());
261 pair<bool,long> SAML2Logout::doRequest(const Application& application, const HTTPRequest& request, HTTPResponse& response) const
264 // First capture the active session ID.
265 SessionCache* cache = application.getServiceProvider().getSessionCache();
266 SessionCacheEx* cacheex = dynamic_cast<SessionCacheEx*>(cache);
267 string session_id = cache->active(application, request);
269 if (!strcmp(request.getMethod(),"GET") && request.getParameter("notifying")) {
270 // This is returning from a front-channel notification, so we have to do the back-channel and then
271 // respond. To do that, we need state from the original request.
272 if (!request.getParameter("entityID")) {
273 cache->remove(application, request, &response);
274 throw FatalProfileException("Application notification loop did not return entityID for LogoutResponse.");
277 // Best effort on back channel and to remove the user agent's session.
278 bool worked1 = false,worked2 = false;
279 if (!session_id.empty()) {
280 vector<string> sessions(1,session_id);
281 worked1 = notifyBackChannel(application, request.getRequestURL(), sessions, false);
283 cache->remove(application, request, &response);
286 catch (exception& ex) {
287 m_log.error("error removing session (%s): %s", session_id, ex.what());
291 worked1 = worked2 = true;
294 // We need metadata to issue a response.
295 MetadataProvider* m = application.getMetadataProvider();
296 Locker metadataLocker(m);
297 MetadataProviderCriteria mc(application, request.getParameter("entityID"), &IDPSSODescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
298 pair<const EntityDescriptor*,const RoleDescriptor*> entity = m->getEntityDescriptor(mc);
300 throw MetadataException(
301 "Unable to locate metadata for identity provider ($entityID)", namedparams(1, "entityID", request.getParameter("entityID"))
304 else if (!entity.second) {
305 throw MetadataException(
306 "Unable to locate SAML 2.0 IdP role for identity provider ($entityID).",
307 namedparams(1, "entityID", request.getParameter("entityID"))
311 auto_ptr_XMLCh reqid(request.getParameter("ID"));
312 if (worked1 && worked2) {
313 // Successful LogoutResponse. Has to be front-channel or we couldn't be here.
315 reqid.get(), StatusCode::SUCCESS, NULL, NULL, request.getParameter("RelayState"), entity.second, application, response, true
321 StatusCode::RESPONDER, NULL, "Unable to fully destroy principal's session.",
322 request.getParameter("RelayState"),
330 // If we get here, it's an external protocol message to decode.
332 // Locate policy key.
333 pair<bool,const char*> policyId = getString("policyId", m_configNS.get()); // namespace-qualified if inside handler element
335 policyId = application.getString("policyId"); // unqualified in Application(s) element
337 // Access policy properties.
338 const PropertySet* settings = application.getServiceProvider().getPolicySettings(policyId.second);
339 pair<bool,bool> validate = settings->getBool("validate");
341 // Lock metadata for use by policy.
342 Locker metadataLocker(application.getMetadataProvider());
344 // Create the policy.
345 shibsp::SecurityPolicy policy(application, &m_role, validate.first && validate.second);
347 // Decode the message.
349 auto_ptr<XMLObject> msg(m_decoder->decode(relayState, request, policy));
350 const LogoutRequest* logoutRequest = dynamic_cast<LogoutRequest*>(msg.get());
352 if (!policy.isAuthenticated())
353 throw SecurityPolicyException("Security of LogoutRequest not established.");
355 // Message from IdP to logout one or more sessions.
357 // If this is front-channel, we have to have a session_id to use already.
358 if (m_decoder->isUserAgentPresent() && session_id.empty()) {
359 m_log.error("no active session");
361 logoutRequest->getID(),
362 StatusCode::REQUESTER, StatusCode::UNKNOWN_PRINCIPAL, "No active session found in request.",
364 policy.getIssuerMetadata(),
371 bool ownedName = false;
372 NameID* nameid = logoutRequest->getNameID();
374 // Check for EncryptedID.
375 EncryptedID* encname = logoutRequest->getEncryptedID();
377 CredentialResolver* cr=application.getCredentialResolver();
379 m_log.warn("found encrypted NameID, but no decryption credential was available");
381 Locker credlocker(cr);
382 auto_ptr<MetadataCredentialCriteria> mcc(
383 policy.getIssuerMetadata() ? new MetadataCredentialCriteria(*policy.getIssuerMetadata()) : NULL
386 auto_ptr<XMLObject> decryptedID(
389 application.getRelyingParty(policy.getIssuerMetadata() ? dynamic_cast<EntityDescriptor*>(policy.getIssuerMetadata()->getParent()) : NULL)->getXMLString("entityID").second,
393 nameid = dynamic_cast<NameID*>(decryptedID.get());
396 decryptedID.release();
399 catch (exception& ex) {
400 m_log.error(ex.what());
406 // No NameID, so must respond with an error.
407 m_log.error("NameID not found in request");
409 logoutRequest->getID(),
410 StatusCode::REQUESTER, StatusCode::UNKNOWN_PRINCIPAL, "NameID not found in request.",
412 policy.getIssuerMetadata(),
415 m_decoder->isUserAgentPresent()
419 auto_ptr<NameID> namewrapper(ownedName ? nameid : NULL);
421 // Suck indexes out of the request for next steps.
423 EntityDescriptor* entity = policy.getIssuerMetadata() ? dynamic_cast<EntityDescriptor*>(policy.getIssuerMetadata()->getParent()) : NULL;
424 const vector<SessionIndex*> sindexes = logoutRequest->getSessionIndexs();
425 for (vector<SessionIndex*>::const_iterator i = sindexes.begin(); i != sindexes.end(); ++i) {
426 auto_ptr_char sindex((*i)->getSessionIndex());
427 indexes.insert(sindex.get());
430 // For a front-channel LogoutRequest, we have to match the information in the request
431 // against the current session.
432 if (!session_id.empty()) {
433 if (!cache->matches(application, request, entity, *nameid, &indexes)) {
435 logoutRequest->getID(),
436 StatusCode::REQUESTER, StatusCode::REQUEST_DENIED, "Active session did not match logout request.",
438 policy.getIssuerMetadata(),
447 // Now we perform "logout" by finding the matching sessions.
448 vector<string> sessions;
451 time_t expires = logoutRequest->getNotOnOrAfter() ? logoutRequest->getNotOnOrAfterEpoch() : 0;
452 cacheex->logout(application, entity, *nameid, &indexes, expires, sessions);
453 m_log.debug("session cache returned %d sessions bound to NameID in logout request", sessions.size());
455 // Now we actually terminate everything except for the active session,
456 // if this is front-channel, for notification purposes.
457 for (vector<string>::const_iterator sit = sessions.begin(); sit != sessions.end(); ++sit)
458 if (*sit != session_id)
459 cacheex->remove(application, sit->c_str()); // using the ID-based removal operation
462 m_log.warn("session cache does not support extended API, can't implement indirect logout of sessions");
463 if (!session_id.empty())
464 sessions.push_back(session_id);
467 catch (exception& ex) {
468 m_log.error("error while logging out matching sessions: %s", ex.what());
470 logoutRequest->getID(),
471 StatusCode::RESPONDER, NULL, ex.what(),
473 policy.getIssuerMetadata(),
476 m_decoder->isUserAgentPresent()
480 if (m_decoder->isUserAgentPresent()) {
481 // Pass control to the first front channel notification point, if any.
482 map<string,string> parammap;
483 if (!relayState.empty())
484 parammap["RelayState"] = relayState;
485 auto_ptr_char entityID(entity ? entity->getEntityID() : NULL);
487 parammap["entityID"] = entityID.get();
488 auto_ptr_char reqID(logoutRequest->getID());
490 parammap["ID"] = reqID.get();
491 pair<bool,long> result = notifyFrontChannel(application, request, response, ¶mmap);
496 // For back-channel requests, or if no front-channel notification is needed...
497 bool worked1 = notifyBackChannel(application, request.getRequestURL(), sessions, false);
499 if (!session_id.empty()) {
500 // One last session to yoink...
502 cache->remove(application, request, &response);
504 catch (exception& ex) {
506 m_log.error("error removing active session (%s): %s", session_id.c_str(), ex.what());
511 logoutRequest->getID(),
512 (worked1 && worked2) ? StatusCode::SUCCESS : StatusCode::RESPONDER,
513 (worked1 && worked2) ? NULL : StatusCode::PARTIAL_LOGOUT,
516 policy.getIssuerMetadata(),
519 m_decoder->isUserAgentPresent()
523 // A LogoutResponse completes an SP-initiated logout sequence.
524 const LogoutResponse* logoutResponse = dynamic_cast<LogoutResponse*>(msg.get());
525 if (logoutResponse) {
526 if (!policy.isAuthenticated()) {
527 SecurityPolicyException ex("Security of LogoutResponse not established.");
528 if (policy.getIssuerMetadata())
529 annotateException(&ex, policy.getIssuerMetadata()); // throws it
532 checkError(logoutResponse, policy.getIssuerMetadata()); // throws if Status doesn't look good...
534 // If relay state is set, recover the original return URL.
535 if (!relayState.empty())
536 recoverRelayState(application, request, response, relayState);
537 if (!relayState.empty())
538 return make_pair(true, response.sendRedirect(relayState.c_str()));
540 // Return template for completion of global logout, or redirect to homeURL.
541 return sendLogoutPage(application, request, response, false, "Global logout completed.");
544 FatalProfileException ex("Incoming message was not a samlp:LogoutRequest or samlp:LogoutResponse.");
545 if (policy.getIssuerMetadata())
546 annotateException(&ex, policy.getIssuerMetadata()); // throws it
548 return make_pair(false,0L); // never happen, satisfies compiler
550 throw ConfigurationException("Cannot process logout message using lite version of shibsp library.");
556 pair<bool,long> SAML2Logout::sendResponse(
557 const XMLCh* requestID,
559 const XMLCh* subcode,
561 const char* relayState,
562 const RoleDescriptor* role,
563 const Application& application,
564 HTTPResponse& httpResponse,
568 // Get endpoint and encoder to use.
569 const EndpointType* ep = NULL;
570 const MessageEncoder* encoder = NULL;
572 const IDPSSODescriptor* idp = dynamic_cast<const IDPSSODescriptor*>(role);
573 for (vector<const XMLCh*>::const_iterator b = m_bindings.begin(); idp && b!=m_bindings.end(); ++b) {
574 if (ep=EndpointManager<SingleLogoutService>(idp->getSingleLogoutServices()).getByBinding(*b)) {
575 map<const XMLCh*,MessageEncoder*>::const_iterator enc = m_encoders.find(*b);
576 if (enc!=m_encoders.end())
577 encoder = enc->second;
581 if (!ep || !encoder) {
582 auto_ptr_char id(dynamic_cast<EntityDescriptor*>(role->getParent())->getEntityID());
583 m_log.error("unable to locate compatible SLO service for provider (%s)", id.get());
584 MetadataException ex("Unable to locate endpoint at IdP ($entityID) to send LogoutResponse.");
585 annotateException(&ex, role); // throws it
589 encoder = m_encoders.begin()->second;
593 auto_ptr<LogoutResponse> logout(LogoutResponseBuilder::buildLogoutResponse());
594 logout->setInResponseTo(requestID);
596 const XMLCh* loc = ep->getResponseLocation();
598 loc = ep->getLocation();
599 logout->setDestination(loc);
601 Issuer* issuer = IssuerBuilder::buildIssuer();
602 logout->setIssuer(issuer);
603 issuer->setName(application.getRelyingParty(dynamic_cast<EntityDescriptor*>(role->getParent()))->getXMLString("entityID").second);
604 fillStatus(*logout.get(), code, subcode, msg);
606 auto_ptr_char dest(logout->getDestination());
608 long ret = sendMessage(*encoder, logout.get(), relayState, dest.get(), role, application, httpResponse);
609 logout.release(); // freed by encoder
610 return make_pair(true,ret);