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.
20 * Handles SAML 2.0 NameID management protocol messages.
24 #include "exceptions.h"
25 #include "Application.h"
26 #include "ServiceProvider.h"
27 #include "handler/AbstractHandler.h"
28 #include "handler/RemotedHandler.h"
29 #include "util/SPConstants.h"
32 # include "SessionCache.h"
33 # include "security/SecurityPolicy.h"
34 # include "util/TemplateParameters.h"
36 # include <saml/SAMLConfig.h>
37 # include <saml/saml2/core/Protocols.h>
38 # include <saml/saml2/metadata/EndpointManager.h>
39 # include <saml/saml2/metadata/MetadataCredentialCriteria.h>
40 # include <xmltooling/util/URLEncoder.h>
41 using namespace opensaml::saml2;
42 using namespace opensaml::saml2p;
43 using namespace opensaml::saml2md;
44 using namespace opensaml;
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 SAML2NameIDMgmt : public AbstractHandler, public RemotedHandler
61 SAML2NameIDMgmt(const DOMElement* e, const char* appId);
62 virtual ~SAML2NameIDMgmt() {
64 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
66 XMLString::release(&m_outgoing);
67 for_each(m_encoders.begin(), m_encoders.end(), cleanup_pair<const XMLCh*,MessageEncoder>());
72 void receive(DDF& in, ostream& out);
73 pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
76 void generateMetadata(SPSSODescriptor& role, const char* handlerURL) const {
77 const char* loc = getString("Location").second;
78 string hurl(handlerURL);
82 auto_ptr_XMLCh widen(hurl.c_str());
83 ManageNameIDService* ep = ManageNameIDServiceBuilder::buildManageNameIDService();
84 ep->setLocation(widen.get());
85 ep->setBinding(getXMLString("Binding").second);
86 role.getManageNameIDServices().push_back(ep);
87 role.addSupport(samlconstants::SAML20P_NS);
90 const char* getType() const {
91 return "ManageNameIDService";
96 pair<bool,long> doRequest(const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse) const;
99 bool notifyBackChannel(const Application& application, const char* requestURL, const NameID& nameid, const NewID* newid) const;
101 pair<bool,long> sendResponse(
102 const XMLCh* requestID,
104 const XMLCh* subcode,
106 const char* relayState,
107 const RoleDescriptor* role,
108 const Application& application,
109 HTTPResponse& httpResponse,
114 MessageDecoder* m_decoder;
116 vector<const XMLCh*> m_bindings;
117 map<const XMLCh*,MessageEncoder*> m_encoders;
121 #if defined (_MSC_VER)
122 #pragma warning( pop )
125 Handler* SHIBSP_DLLLOCAL SAML2NameIDMgmtFactory(const pair<const DOMElement*,const char*>& p)
127 return new SAML2NameIDMgmt(p.first, p.second);
131 SAML2NameIDMgmt::SAML2NameIDMgmt(const DOMElement* e, const char* appId)
132 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".NameIDMgmt.SAML2"))
134 ,m_role(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME), m_decoder(NULL), m_outgoing(NULL)
138 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
139 SAMLConfig& conf = SAMLConfig::getConfig();
141 // Handle incoming binding.
142 m_decoder = conf.MessageDecoderManager.newPlugin(
143 getString("Binding").second, pair<const DOMElement*,const XMLCh*>(e,shibspconstants::SHIB2SPCONFIG_NS)
145 m_decoder->setArtifactResolver(SPConfig::getConfig().getArtifactResolver());
147 if (m_decoder->isUserAgentPresent()) {
148 // Handle front-channel binding setup.
149 pair<bool,const XMLCh*> outgoing = getXMLString("outgoingBindings", m_configNS.get());
150 if (outgoing.first) {
151 m_outgoing = XMLString::replicate(outgoing.second);
152 XMLString::trim(m_outgoing);
155 // No override, so we'll install a default binding precedence.
156 string prec = string(samlconstants::SAML20_BINDING_HTTP_REDIRECT) + ' ' + samlconstants::SAML20_BINDING_HTTP_POST + ' ' +
157 samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN + ' ' + samlconstants::SAML20_BINDING_HTTP_ARTIFACT;
158 m_outgoing = XMLString::transcode(prec.c_str());
162 XMLCh* start = m_outgoing;
163 while (start && *start) {
164 pos = XMLString::indexOf(start,chSpace);
166 *(start + pos)=chNull;
167 m_bindings.push_back(start);
169 auto_ptr_char b(start);
170 MessageEncoder * encoder = conf.MessageEncoderManager.newPlugin(
171 b.get(), pair<const DOMElement*,const XMLCh*>(e,shibspconstants::SHIB2SPCONFIG_NS)
173 m_encoders[start] = encoder;
174 m_log.debug("supporting outgoing front-channel binding (%s)", b.get());
176 catch (exception& ex) {
177 m_log.error("error building MessageEncoder: %s", ex.what());
180 start = start + pos + 1;
186 MessageEncoder* encoder = conf.MessageEncoderManager.newPlugin(
187 getString("Binding").second, pair<const DOMElement*,const XMLCh*>(e,shibspconstants::SHIB2SPCONFIG_NS)
189 m_encoders.insert(pair<const XMLCh*,MessageEncoder*>(NULL, encoder));
194 string address(appId);
195 address += getString("Location").second;
196 setAddress(address.c_str());
199 pair<bool,long> SAML2NameIDMgmt::run(SPRequest& request, bool isHandler) const
201 SPConfig& conf = SPConfig::getConfig();
202 if (conf.isEnabled(SPConfig::OutOfProcess)) {
203 // When out of process, we run natively and directly process the message.
204 return doRequest(request.getApplication(), request, request);
207 // When not out of process, we remote all the message processing.
208 vector<string> headers(1,"Cookie");
209 DDF out,in = wrap(request, &headers, true);
210 DDFJanitor jin(in), jout(out);
211 out=request.getServiceProvider().getListenerService()->send(in);
212 return unwrap(request, out);
216 void SAML2NameIDMgmt::receive(DDF& in, ostream& out)
219 const char* aid=in["application_id"].string();
220 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
222 // Something's horribly wrong.
223 m_log.error("couldn't find application (%s) for NameID mgmt", aid ? aid : "(missing)");
224 throw ConfigurationException("Unable to locate application for NameID mgmt, deleted?");
227 // Unpack the request.
228 auto_ptr<HTTPRequest> req(getRequest(in));
230 // Wrap a response shim.
232 DDFJanitor jout(ret);
233 auto_ptr<HTTPResponse> resp(getResponse(ret));
235 // Since we're remoted, the result should either be a throw, which we pass on,
236 // a false/0 return, which we just return as an empty structure, or a response/redirect,
237 // which we capture in the facade and send back.
238 doRequest(*app, *req.get(), *resp.get());
242 pair<bool,long> SAML2NameIDMgmt::doRequest(
243 const Application& application, const HTTPRequest& request, HTTPResponse& response
247 SessionCache* cache = application.getServiceProvider().getSessionCache();
249 // Locate policy key.
250 pair<bool,const char*> policyId = getString("policyId", m_configNS.get()); // namespace-qualified if inside handler element
252 policyId = application.getString("policyId"); // unqualified in Application(s) element
254 // Access policy properties.
255 const PropertySet* settings = application.getServiceProvider().getPolicySettings(policyId.second);
256 pair<bool,bool> validate = settings->getBool("validate");
258 // Lock metadata for use by policy.
259 Locker metadataLocker(application.getMetadataProvider());
261 // Create the policy.
262 shibsp::SecurityPolicy policy(application, &m_role, validate.first && validate.second);
264 // Decode the message.
266 auto_ptr<XMLObject> msg(m_decoder->decode(relayState, request, policy));
267 const ManageNameIDRequest* mgmtRequest = dynamic_cast<ManageNameIDRequest*>(msg.get());
269 if (!policy.isAuthenticated())
270 throw SecurityPolicyException("Security of ManageNameIDRequest not established.");
272 // Message from IdP to change or terminate a NameID.
274 // If this is front-channel, we have to have a session_id to use already.
275 string session_id = cache->active(application, request);
276 if (m_decoder->isUserAgentPresent() && session_id.empty()) {
277 m_log.error("no active session");
279 mgmtRequest->getID(),
280 StatusCode::REQUESTER, StatusCode::UNKNOWN_PRINCIPAL, "No active session found in request.",
282 policy.getIssuerMetadata(),
289 EntityDescriptor* entity = policy.getIssuerMetadata() ? dynamic_cast<EntityDescriptor*>(policy.getIssuerMetadata()->getParent()) : NULL;
291 bool ownedName = false;
292 NameID* nameid = mgmtRequest->getNameID();
294 // Check for EncryptedID.
295 EncryptedID* encname = mgmtRequest->getEncryptedID();
297 CredentialResolver* cr=application.getCredentialResolver();
299 m_log.warn("found encrypted NameID, but no decryption credential was available");
301 Locker credlocker(cr);
302 auto_ptr<MetadataCredentialCriteria> mcc(
303 policy.getIssuerMetadata() ? new MetadataCredentialCriteria(*policy.getIssuerMetadata()) : NULL
306 auto_ptr<XMLObject> decryptedID(encname->decrypt(*cr,application.getRelyingParty(entity)->getXMLString("entityID").second,mcc.get()));
307 nameid = dynamic_cast<NameID*>(decryptedID.get());
310 decryptedID.release();
313 catch (exception& ex) {
314 m_log.error(ex.what());
320 // No NameID, so must respond with an error.
321 m_log.error("NameID not found in request");
323 mgmtRequest->getID(),
324 StatusCode::REQUESTER, StatusCode::UNKNOWN_PRINCIPAL, "NameID not found in request.",
326 policy.getIssuerMetadata(),
329 m_decoder->isUserAgentPresent()
333 auto_ptr<NameID> namewrapper(ownedName ? nameid : NULL);
335 // For a front-channel request, we have to match the information in the request
336 // against the current session.
337 if (!session_id.empty()) {
338 if (!cache->matches(application, request, entity, *nameid, NULL)) {
340 mgmtRequest->getID(),
341 StatusCode::REQUESTER, StatusCode::REQUEST_DENIED, "Active session did not match NameID mgmt request.",
343 policy.getIssuerMetadata(),
352 // Determine what's happening...
353 bool ownedNewID = false;
355 if (!mgmtRequest->getTerminate()) {
356 // Better be a NewID in there.
357 newid = mgmtRequest->getNewID();
359 // Check for NewEncryptedID.
360 NewEncryptedID* encnewid = mgmtRequest->getNewEncryptedID();
362 CredentialResolver* cr=application.getCredentialResolver();
364 m_log.warn("found encrypted NewID, but no decryption credential was available");
366 Locker credlocker(cr);
367 auto_ptr<MetadataCredentialCriteria> mcc(
368 policy.getIssuerMetadata() ? new MetadataCredentialCriteria(*policy.getIssuerMetadata()) : NULL
371 auto_ptr<XMLObject> decryptedID(encnewid->decrypt(*cr,application.getRelyingParty(entity)->getXMLString("entityID").second,mcc.get()));
372 newid = dynamic_cast<NewID*>(decryptedID.get());
375 decryptedID.release();
378 catch (exception& ex) {
379 m_log.error(ex.what());
386 // No NewID, so must respond with an error.
387 m_log.error("NewID not found in request");
389 mgmtRequest->getID(),
390 StatusCode::REQUESTER, NULL, "NewID not found in request.",
392 policy.getIssuerMetadata(),
395 m_decoder->isUserAgentPresent()
400 auto_ptr<NewID> newwrapper(ownedNewID ? newid : NULL);
402 // TODO: maybe support in-place modification of sessions?
404 vector<string> sessions;
406 time_t expires = logoutRequest->getNotOnOrAfter() ? logoutRequest->getNotOnOrAfterEpoch() : 0;
407 cache->logout(entity, *nameid, &indexes, expires, application, sessions);
409 // Now we actually terminate everything except for the active session,
410 // if this is front-channel, for notification purposes.
411 for (vector<string>::const_iterator sit = sessions.begin(); sit != sessions.end(); ++sit)
412 if (session_id && strcmp(sit->c_str(), session_id))
413 cache->remove(sit->c_str(), application);
415 catch (exception& ex) {
416 m_log.error("error while logging out matching sessions: %s", ex.what());
418 logoutRequest->getID(),
419 StatusCode::RESPONDER, NULL, ex.what(),
421 policy.getIssuerMetadata(),
424 m_decoder->isUserAgentPresent()
429 // Do back-channel app notifications.
430 // Not supporting front-channel due to privacy fears.
431 bool worked = notifyBackChannel(application, request.getRequestURL(), *nameid, newid);
434 mgmtRequest->getID(),
435 worked ? StatusCode::SUCCESS : StatusCode::RESPONDER,
439 policy.getIssuerMetadata(),
442 m_decoder->isUserAgentPresent()
446 // A ManageNameIDResponse completes an SP-initiated sequence, currently not supported.
448 const ManageNameIDResponse* mgmtResponse = dynamic_cast<ManageNameIDResponse*>(msg.get());
450 if (!policy.isAuthenticated()) {
451 SecurityPolicyException ex("Security of ManageNameIDResponse not established.");
452 if (policy.getIssuerMetadata())
453 annotateException(&ex, policy.getIssuerMetadata()); // throws it
456 checkError(mgmtResponse, policy.getIssuerMetadata()); // throws if Status doesn't look good...
458 // Return template for completion.
459 return sendLogoutPage(application, response, false, "Global logout completed.");
463 FatalProfileException ex("Incoming message was not a samlp:ManageNameIDRequest.");
464 if (policy.getIssuerMetadata())
465 annotateException(&ex, policy.getIssuerMetadata()); // throws it
467 return make_pair(false,0L); // never happen, satisfies compiler
469 throw ConfigurationException("Cannot process NameID mgmt message using lite version of shibsp library.");
475 pair<bool,long> SAML2NameIDMgmt::sendResponse(
476 const XMLCh* requestID,
478 const XMLCh* subcode,
480 const char* relayState,
481 const RoleDescriptor* role,
482 const Application& application,
483 HTTPResponse& httpResponse,
487 // Get endpoint and encoder to use.
488 const EndpointType* ep = NULL;
489 const MessageEncoder* encoder = NULL;
491 const IDPSSODescriptor* idp = dynamic_cast<const IDPSSODescriptor*>(role);
492 for (vector<const XMLCh*>::const_iterator b = m_bindings.begin(); idp && b!=m_bindings.end(); ++b) {
493 if (ep=EndpointManager<ManageNameIDService>(idp->getManageNameIDServices()).getByBinding(*b)) {
494 map<const XMLCh*,MessageEncoder*>::const_iterator enc = m_encoders.find(*b);
495 if (enc!=m_encoders.end())
496 encoder = enc->second;
500 if (!ep || !encoder) {
501 auto_ptr_char id(dynamic_cast<EntityDescriptor*>(role->getParent())->getEntityID());
502 m_log.error("unable to locate compatible NIM service for provider (%s)", id.get());
503 MetadataException ex("Unable to locate endpoint at IdP ($entityID) to send ManageNameIDResponse.");
504 annotateException(&ex, role); // throws it
508 encoder = m_encoders.begin()->second;
512 auto_ptr<ManageNameIDResponse> nim(ManageNameIDResponseBuilder::buildManageNameIDResponse());
513 nim->setInResponseTo(requestID);
515 const XMLCh* loc = ep->getResponseLocation();
517 loc = ep->getLocation();
518 nim->setDestination(loc);
520 Issuer* issuer = IssuerBuilder::buildIssuer();
521 nim->setIssuer(issuer);
522 issuer->setName(application.getRelyingParty(dynamic_cast<EntityDescriptor*>(role->getParent()))->getXMLString("entityID").second);
523 fillStatus(*nim.get(), code, subcode, msg);
525 auto_ptr_char dest(nim->getDestination());
527 long ret = sendMessage(*encoder, nim.get(), relayState, dest.get(), role, application, httpResponse);
528 nim.release(); // freed by encoder
529 return make_pair(true,ret);
532 #include "util/SPConstants.h"
533 #include <xmltooling/impl/AnyElement.h>
534 #include <xmltooling/soap/SOAP.h>
535 #include <xmltooling/soap/SOAPClient.h>
536 using namespace soap11;
538 static const XMLCh NameIDNotification[] = UNICODE_LITERAL_18(N,a,m,e,I,D,N,o,t,i,f,i,c,a,t,i,o,n);
540 class SHIBSP_DLLLOCAL SOAPNotifier : public soap11::SOAPClient
544 virtual ~SOAPNotifier() {}
546 void prepareTransport(SOAPTransport& transport) {
547 transport.setVerifyHost(false);
552 bool SAML2NameIDMgmt::notifyBackChannel(
553 const Application& application, const char* requestURL, const NameID& nameid, const NewID* newid
556 unsigned int index = 0;
557 string endpoint = application.getNotificationURL(requestURL, false, index++);
558 if (endpoint.empty())
561 auto_ptr<Envelope> env(EnvelopeBuilder::buildEnvelope());
562 Body* body = BodyBuilder::buildBody();
564 ElementProxy* msg = new AnyElementImpl(shibspconstants::SHIB2SPNOTIFY_NS, NameIDNotification);
565 body->getUnknownXMLObjects().push_back(msg);
566 msg->getUnknownXMLObjects().push_back(nameid.clone());
568 msg->getUnknownXMLObjects().push_back(newid->clone());
570 msg->getUnknownXMLObjects().push_back(TerminateBuilder::buildTerminate());
574 while (!endpoint.empty()) {
576 soaper.send(*env.get(), SOAPTransport::Address(application.getId(), application.getId(), endpoint.c_str()));
577 delete soaper.receive();
579 catch (exception& ex) {
580 m_log.error("error notifying application of logout event: %s", ex.what());
584 endpoint = application.getNotificationURL(requestURL, false, index++);