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 * 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/LogoutHandler.h"
32 # include "binding/SOAPClient.h"
33 # include "metadata/MetadataProviderCriteria.h"
34 # include <saml/SAMLConfig.h>
35 # include <saml/saml2/core/Protocols.h>
36 # include <saml/saml2/binding/SAML2SOAPClient.h>
37 # include <saml/saml2/metadata/EndpointManager.h>
38 # include <saml/saml2/metadata/MetadataCredentialCriteria.h>
39 using namespace opensaml::saml2;
40 using namespace opensaml::saml2p;
41 using namespace opensaml::saml2md;
42 using namespace opensaml;
44 # include "lite/SAMLConstants.h"
47 using namespace shibsp;
48 using namespace xmltooling;
53 #if defined (_MSC_VER)
54 #pragma warning( push )
55 #pragma warning( disable : 4250 )
58 class SHIBSP_DLLLOCAL SAML2LogoutInitiator : public AbstractHandler, public LogoutHandler
61 SAML2LogoutInitiator(const DOMElement* e, const char* appId);
62 virtual ~SAML2LogoutInitiator() {
64 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
65 XMLString::release(&m_outgoing);
66 for_each(m_encoders.begin(), m_encoders.end(), cleanup_pair<const XMLCh*,MessageEncoder>());
71 void setParent(const PropertySet* parent);
72 void receive(DDF& in, ostream& out);
73 pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
76 const char* getType() const {
77 return "LogoutInitiator";
82 pair<bool,long> doRequest(
83 const Application& application, const HTTPRequest& request, HTTPResponse& httpResponse, Session* session
88 LogoutRequest* buildRequest(
89 const Application& application, const Session& session, const RoleDescriptor& role, const MessageEncoder* encoder=NULL
93 vector<const XMLCh*> m_bindings;
94 map<const XMLCh*,MessageEncoder*> m_encoders;
96 auto_ptr_char m_protocol;
99 #if defined (_MSC_VER)
100 #pragma warning( pop )
103 Handler* SHIBSP_DLLLOCAL SAML2LogoutInitiatorFactory(const pair<const DOMElement*,const char*>& p)
105 return new SAML2LogoutInitiator(p.first, p.second);
109 SAML2LogoutInitiator::SAML2LogoutInitiator(const DOMElement* e, const char* appId)
110 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".LogoutInitiator.SAML2")), m_appId(appId),
114 m_protocol(samlconstants::SAML20P_NS)
117 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
118 // Handle outgoing binding setup.
119 pair<bool,const XMLCh*> outgoing = getXMLString("outgoingBindings");
120 if (outgoing.first) {
121 m_outgoing = XMLString::replicate(outgoing.second);
122 XMLString::trim(m_outgoing);
125 // No override, so we'll install a default binding precedence.
126 string prec = string(samlconstants::SAML20_BINDING_HTTP_REDIRECT) + ' ' + samlconstants::SAML20_BINDING_HTTP_POST + ' ' +
127 samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN + ' ' + samlconstants::SAML20_BINDING_HTTP_ARTIFACT;
128 m_outgoing = XMLString::transcode(prec.c_str());
132 XMLCh* start = m_outgoing;
133 while (start && *start) {
134 pos = XMLString::indexOf(start,chSpace);
136 *(start + pos)=chNull;
137 m_bindings.push_back(start);
139 auto_ptr_char b(start);
140 MessageEncoder * encoder =
141 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(b.get(),pair<const DOMElement*,const XMLCh*>(e,NULL));
142 if (encoder->isUserAgentPresent()) {
143 m_encoders[start] = encoder;
144 m_log.debug("supporting outgoing binding (%s)", b.get());
148 m_log.warn("skipping outgoing binding (%s), not a front-channel mechanism", b.get());
151 catch (exception& ex) {
152 m_log.error("error building MessageEncoder: %s", ex.what());
155 start = start + pos + 1;
162 pair<bool,const char*> loc = getString("Location");
164 string address = m_appId + loc.second + "::run::SAML2LI";
165 setAddress(address.c_str());
169 void SAML2LogoutInitiator::setParent(const PropertySet* parent)
171 DOMPropertySet::setParent(parent);
172 pair<bool,const char*> loc = getString("Location");
174 string address = m_appId + loc.second + "::run::SAML2LI";
175 setAddress(address.c_str());
178 m_log.warn("no Location property in SAML2 LogoutInitiator (or parent), can't register as remoted handler");
182 pair<bool,long> SAML2LogoutInitiator::run(SPRequest& request, bool isHandler) const
184 // Defer to base class for front-channel loop first.
185 pair<bool,long> ret = LogoutHandler::run(request, isHandler);
189 // At this point we know the front-channel is handled.
190 // We need the session to do any other work.
192 Session* session = NULL;
194 session = request.getSession(false, true, false); // don't cache it and ignore all checks
196 return make_pair(false,0L);
198 // We only handle SAML 2.0 sessions.
199 if (!XMLString::equals(session->getProtocol(), m_protocol.get())) {
201 return make_pair(false,0L);
204 catch (exception& ex) {
205 m_log.error("error accessing current session: %s", ex.what());
206 return make_pair(false,0L);
209 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
210 // When out of process, we run natively.
211 return doRequest(request.getApplication(), request, request, session);
214 // When not out of process, we remote the request.
216 vector<string> headers(1,"Cookie");
217 DDF out,in = wrap(request,&headers);
218 DDFJanitor jin(in), jout(out);
219 out=request.getServiceProvider().getListenerService()->send(in);
220 return unwrap(request, out);
224 void SAML2LogoutInitiator::receive(DDF& in, ostream& out)
227 // Defer to base class for notifications
228 if (in["notify"].integer() == 1)
229 return LogoutHandler::receive(in, out);
232 const char* aid=in["application_id"].string();
233 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
235 // Something's horribly wrong.
236 m_log.error("couldn't find application (%s) for logout", aid ? aid : "(missing)");
237 throw ConfigurationException("Unable to locate application for logout, deleted?");
240 // Unpack the request.
241 auto_ptr<HTTPRequest> req(getRequest(in));
243 // Set up a response shim.
245 DDFJanitor jout(ret);
246 auto_ptr<HTTPResponse> resp(getResponse(ret));
248 Session* session = NULL;
250 session = app->getServiceProvider().getSessionCache()->find(*app, *req.get(), NULL, NULL);
252 catch (exception& ex) {
253 m_log.error("error accessing current session: %s", ex.what());
256 // With no session, we just skip the request and let it fall through to an empty struct return.
258 if (session->getNameID() && session->getEntityID()) {
259 // Since we're remoted, the result should either be a throw, which we pass on,
260 // a false/0 return, which we just return as an empty structure, or a response/redirect,
261 // which we capture in the facade and send back.
262 doRequest(*app, *req.get(), *resp.get(), session);
265 m_log.error("no NameID or issuing entityID found in session");
267 app->getServiceProvider().getSessionCache()->remove(*app, *req.get(), resp.get());
272 throw ConfigurationException("Cannot perform logout using lite version of shibsp library.");
276 pair<bool,long> SAML2LogoutInitiator::doRequest(
277 const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse, Session* session
280 // Do back channel notification.
281 vector<string> sessions(1, session->getID());
282 if (!notifyBackChannel(application, httpRequest.getRequestURL(), sessions, false)) {
284 application.getServiceProvider().getSessionCache()->remove(application, httpRequest, &httpResponse);
285 return sendLogoutPage(application, httpRequest, httpResponse, true, "Partial logout failure.");
289 pair<bool,long> ret = make_pair(false,0L);
291 // With a session in hand, we can create a LogoutRequest message, if we can find a compatible endpoint.
292 MetadataProvider* m = application.getMetadataProvider();
293 Locker metadataLocker(m);
294 MetadataProviderCriteria mc(application, session->getEntityID(), &IDPSSODescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
295 pair<const EntityDescriptor*,const RoleDescriptor*> entity = m->getEntityDescriptor(mc);
297 throw MetadataException(
298 "Unable to locate metadata for identity provider ($entityID)", namedparams(1, "entityID", session->getEntityID())
301 else if (!entity.second) {
302 throw MetadataException(
303 "Unable to locate SAML 2.0 IdP role for identity provider ($entityID).", namedparams(1, "entityID", session->getEntityID())
307 const IDPSSODescriptor* role = dynamic_cast<const IDPSSODescriptor*>(entity.second);
308 const EndpointType* ep=NULL;
309 const MessageEncoder* encoder=NULL;
310 vector<const XMLCh*>::const_iterator b;
311 for (b = m_bindings.begin(); b!=m_bindings.end(); ++b) {
312 if (ep=EndpointManager<SingleLogoutService>(role->getSingleLogoutServices()).getByBinding(*b)) {
313 map<const XMLCh*,MessageEncoder*>::const_iterator enc = m_encoders.find(*b);
314 if (enc!=m_encoders.end())
315 encoder = enc->second;
319 if (!ep || !encoder) {
320 m_log.warn("no compatible front channel SingleLogoutService, trying back channel...");
321 shibsp::SecurityPolicy policy(application);
322 shibsp::SOAPClient soaper(policy);
323 MetadataCredentialCriteria mcc(*role);
325 LogoutResponse* logoutResponse=NULL;
326 auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
327 const vector<SingleLogoutService*>& endpoints=role->getSingleLogoutServices();
328 for (vector<SingleLogoutService*>::const_iterator epit=endpoints.begin(); !logoutResponse && epit!=endpoints.end(); ++epit) {
330 if (!XMLString::equals((*epit)->getBinding(),binding.get()))
332 LogoutRequest* msg = buildRequest(application, *session, *role);
333 auto_ptr_char dest((*epit)->getLocation());
335 SAML2SOAPClient client(soaper, false);
336 client.sendSAML(msg, application.getId(), mcc, dest.get());
337 StatusResponseType* srt = client.receiveSAML();
338 if (!(logoutResponse = dynamic_cast<LogoutResponse*>(srt))) {
343 catch (exception& ex) {
344 m_log.error("error sending LogoutRequest message: %s", ex.what());
349 if (!logoutResponse) {
350 ret = sendLogoutPage(
351 application, httpRequest, httpResponse, false,
353 "Identity provider does not support SAML 2 Single Logout protocol." :
354 "Identity provider did not respond to logout request."
357 else if (!logoutResponse->getStatus() || !logoutResponse->getStatus()->getStatusCode() ||
358 !XMLString::equals(logoutResponse->getStatus()->getStatusCode()->getValue(), saml2p::StatusCode::SUCCESS)) {
359 delete logoutResponse;
360 ret = sendLogoutPage(application, httpRequest, httpResponse, false, "Identity provider returned a SAML error in response to logout request.");
363 delete logoutResponse;
364 const char* returnloc = httpRequest.getParameter("return");
366 ret.second = httpResponse.sendRedirect(returnloc);
369 ret = sendLogoutPage(application, httpRequest, httpResponse, false, "Logout completed successfully.");
375 application.getServiceProvider().getSessionCache()->remove(application, httpRequest, &httpResponse);
380 // Save off return location as RelayState.
382 const char* returnloc = httpRequest.getParameter("return");
384 relayState = returnloc;
385 preserveRelayState(application, httpResponse, relayState);
388 auto_ptr<LogoutRequest> msg(buildRequest(application, *session, *role, encoder));
390 msg->setDestination(ep->getLocation());
391 auto_ptr_char dest(ep->getLocation());
392 ret.second = sendMessage(*encoder, msg.get(), relayState.c_str(), dest.get(), role, application, httpResponse);
394 msg.release(); // freed by encoder
396 catch (exception& ex) {
397 m_log.error("error issuing SAML 2.0 logout request: %s", ex.what());
403 application.getServiceProvider().getSessionCache()->remove(application, httpRequest, &httpResponse);
409 application.getServiceProvider().getSessionCache()->remove(application, httpRequest, &httpResponse);
410 throw ConfigurationException("Cannot perform logout using lite version of shibsp library.");
416 LogoutRequest* SAML2LogoutInitiator::buildRequest(
417 const Application& application, const Session& session, const RoleDescriptor& role, const MessageEncoder* encoder
420 const PropertySet* relyingParty = application.getRelyingParty(dynamic_cast<EntityDescriptor*>(role.getParent()));
422 auto_ptr<LogoutRequest> msg(LogoutRequestBuilder::buildLogoutRequest());
423 Issuer* issuer = IssuerBuilder::buildIssuer();
424 msg->setIssuer(issuer);
425 issuer->setName(relyingParty->getXMLString("entityID").second);
426 auto_ptr_XMLCh index(session.getSessionIndex());
427 if (index.get() && *index.get()) {
428 SessionIndex* si = SessionIndexBuilder::buildSessionIndex();
429 msg->getSessionIndexs().push_back(si);
430 si->setSessionIndex(index.get());
433 const NameID* nameid = session.getNameID();
434 pair<bool,const char*> flag = relyingParty->getString("encryption");
436 (!strcmp(flag.second, "true") || (encoder && !strcmp(flag.second, "front")) || (!encoder && !strcmp(flag.second, "back")))) {
437 auto_ptr<EncryptedID> encrypted(EncryptedIDBuilder::buildEncryptedID());
438 MetadataCredentialCriteria mcc(role);
441 *(application.getMetadataProvider()),
443 encoder ? encoder->isCompact() : false,
444 relyingParty->getXMLString("encryptionAlg").second
446 msg->setEncryptedID(encrypted.release());
449 msg->setNameID(nameid->cloneNameID());
453 // No encoder being used, so sign for SOAP client manually.
454 flag = relyingParty->getString("signing");
455 if (flag.first && (!strcmp(flag.second, "true") || !strcmp(flag.second, "back"))) {
456 CredentialResolver* credResolver=application.getCredentialResolver();
458 Locker credLocker(credResolver);
459 // Fill in criteria to use.
460 MetadataCredentialCriteria mcc(role);
461 mcc.setUsage(Credential::SIGNING_CREDENTIAL);
462 pair<bool,const char*> keyName = relyingParty->getString("keyName");
464 mcc.getKeyNames().insert(keyName.second);
465 pair<bool,const XMLCh*> sigalg = relyingParty->getXMLString("signingAlg");
467 mcc.setXMLAlgorithm(sigalg.second);
468 const Credential* cred = credResolver->resolve(&mcc);
470 xmlsignature::Signature* sig = xmlsignature::SignatureBuilder::buildSignature();
471 msg->setSignature(sig);
473 sig->setSignatureAlgorithm(sigalg.second);
474 sigalg = relyingParty->getXMLString("digestAlg");
476 ContentReference* cr = dynamic_cast<ContentReference*>(sig->getContentReference());
478 cr->setDigestAlgorithm(sigalg.second);
481 // Sign response while marshalling.
482 vector<xmlsignature::Signature*> sigs(1,sig);
483 msg->marshall((DOMDocument*)NULL,&sigs,cred);
486 m_log.warn("no signing credential resolved, leaving message unsigned");
490 m_log.warn("no credential resolver installed, leaving message unsigned");
495 return msg.release();