2 * Copyright 2001-2010 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 * SAML2SessionInitiator.cpp
20 * SAML 2.0 AuthnRequest support.
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "ServiceProvider.h"
27 #include "handler/AbstractHandler.h"
28 #include "handler/RemotedHandler.h"
29 #include "handler/SessionInitiator.h"
30 #include "util/SPConstants.h"
33 # include "metadata/MetadataProviderCriteria.h"
34 # include <saml/SAMLConfig.h>
35 # include <saml/saml2/core/Protocols.h>
36 # include <saml/saml2/metadata/EndpointManager.h>
37 # include <saml/saml2/metadata/Metadata.h>
38 # include <saml/saml2/metadata/MetadataCredentialCriteria.h>
39 # include <saml/util/SAMLConstants.h>
40 using namespace opensaml::saml2;
41 using namespace opensaml::saml2p;
42 using namespace opensaml::saml2md;
44 # include "lite/SAMLConstants.h"
45 # include <xercesc/util/XMLUniDefs.hpp>
48 using namespace shibsp;
49 using namespace opensaml;
50 using namespace xmltooling;
55 #if defined (_MSC_VER)
56 #pragma warning( push )
57 #pragma warning( disable : 4250 )
60 class SHIBSP_DLLLOCAL SAML2SessionInitiator : public SessionInitiator, public AbstractHandler, public RemotedHandler
63 SAML2SessionInitiator(const DOMElement* e, const char* appId);
64 virtual ~SAML2SessionInitiator() {
66 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
67 XMLString::release(&m_outgoing);
68 for_each(m_encoders.begin(), m_encoders.end(), cleanup_pair<const XMLCh*,MessageEncoder>());
69 delete m_requestTemplate;
75 void setParent(const PropertySet* parent);
76 void receive(DDF& in, ostream& out);
77 pair<bool,long> unwrap(SPRequest& request, DDF& out) const;
78 pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
80 const XMLCh* getProtocolFamily() const {
81 return samlconstants::SAML20P_NS;
85 pair<bool,long> doRequest(
86 const Application& application,
87 const HTTPRequest* httpRequest,
88 HTTPResponse& httpResponse,
90 const XMLCh* acsIndex,
92 const char* acsLocation,
93 const XMLCh* acsBinding,
96 const char* authnContextClassRef,
97 const char* authnContextComparison,
98 const char* NameIDFormat,
99 const char* SPNameQualifier,
104 auto_ptr_char m_paosNS,m_ecpNS;
105 auto_ptr_XMLCh m_paosBinding;
108 vector<const XMLCh*> m_bindings;
109 map<const XMLCh*,MessageEncoder*> m_encoders;
110 MessageEncoder* m_ecp;
111 AuthnRequest* m_requestTemplate;
117 #if defined (_MSC_VER)
118 #pragma warning( pop )
121 SessionInitiator* SHIBSP_DLLLOCAL SAML2SessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
123 return new SAML2SessionInitiator(p.first, p.second);
128 SAML2SessionInitiator::SAML2SessionInitiator(const DOMElement* e, const char* appId)
129 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.SAML2"), nullptr, &m_remapper), m_appId(appId),
130 m_paosNS(samlconstants::PAOS_NS), m_ecpNS(samlconstants::SAML20ECP_NS), m_paosBinding(samlconstants::SAML20_BINDING_PAOS)
132 static const XMLCh ECP[] = UNICODE_LITERAL_3(E,C,P);
133 const XMLCh* flag = e ? e->getAttributeNS(nullptr,ECP) : nullptr;
135 m_ecp = (flag && (*flag == chLatin_t || *flag == chDigit_1));
139 m_requestTemplate=nullptr;
141 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
142 // Check for a template AuthnRequest to build from.
143 DOMElement* child = XMLHelper::getFirstChildElement(e, samlconstants::SAML20P_NS, AuthnRequest::LOCAL_NAME);
145 m_requestTemplate = dynamic_cast<AuthnRequest*>(AuthnRequestBuilder::buildOneFromElement(child));
147 // If directed, build an ECP encoder.
148 if (flag && (*flag == chLatin_t || *flag == chDigit_1)) {
150 m_ecp = SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
151 samlconstants::SAML20_BINDING_PAOS, pair<const DOMElement*,const XMLCh*>(e,nullptr)
154 catch (exception& ex) {
155 m_log.error("error building PAOS/ECP MessageEncoder: %s", ex.what());
159 // Handle outgoing binding setup.
160 pair<bool,const XMLCh*> outgoing = getXMLString("outgoingBindings");
161 if (outgoing.first) {
162 m_outgoing = XMLString::replicate(outgoing.second);
163 XMLString::trim(m_outgoing);
166 // No override, so we'll install a default binding precedence.
167 string prec = string(samlconstants::SAML20_BINDING_HTTP_REDIRECT) + ' ' + samlconstants::SAML20_BINDING_HTTP_POST + ' ' +
168 samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN + ' ' + samlconstants::SAML20_BINDING_HTTP_ARTIFACT;
169 m_outgoing = XMLString::transcode(prec.c_str());
173 XMLCh* start = m_outgoing;
174 while (start && *start) {
175 pos = XMLString::indexOf(start,chSpace);
177 *(start + pos)=chNull;
178 m_bindings.push_back(start);
180 auto_ptr_char b(start);
181 MessageEncoder * encoder = SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
182 b.get(),pair<const DOMElement*,const XMLCh*>(e,nullptr)
184 if (encoder->isUserAgentPresent() && XMLString::equals(getProtocolFamily(), encoder->getProtocolFamily())) {
185 m_encoders[start] = encoder;
186 m_log.debug("supporting outgoing binding (%s)", b.get());
190 m_log.warn("skipping outgoing binding (%s), not a SAML 2.0 front-channel mechanism", b.get());
193 catch (exception& ex) {
194 m_log.error("error building MessageEncoder: %s", ex.what());
197 start = start + pos + 1;
204 // If Location isn't set, defer address registration until the setParent call.
205 pair<bool,const char*> loc = getString("Location");
207 string address = m_appId + loc.second + "::run::SAML2SI";
208 setAddress(address.c_str());
211 m_supportedOptions.insert("isPassive");
214 void SAML2SessionInitiator::setParent(const PropertySet* parent)
216 DOMPropertySet::setParent(parent);
217 pair<bool,const char*> loc = getString("Location");
219 string address = m_appId + loc.second + "::run::SAML2SI";
220 setAddress(address.c_str());
223 m_log.warn("no Location property in SAML2 SessionInitiator (or parent), can't register as remoted handler");
227 pair<bool,long> SAML2SessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
229 // First check for ECP support, since that doesn't require an IdP to be known.
231 if (m_ecp && request.getHeader("Accept").find("application/vnd.paos+xml") != string::npos) {
232 string PAOS = request.getHeader("PAOS");
233 if (PAOS.find(m_paosNS.get()) != string::npos && PAOS.find(m_ecpNS.get()) != string::npos)
237 // We have to know the IdP to function unless this is ECP.
238 if ((!ECP && entityID.empty()) || !checkCompatibility(request, isHandler))
239 return make_pair(false,0L);
242 pair<bool,const char*> prop;
243 const Handler* ACS=nullptr;
244 pair<bool,const char*> acClass, acComp, nidFormat, spQual;
245 bool isPassive=false,forceAuthn=false;
246 const Application& app=request.getApplication();
248 // ECP means the ACS will be by value no matter what.
249 pair<bool,bool> acsByIndex = ECP ? make_pair(true,false) : getBool("acsByIndex");
252 prop.second = request.getParameter("acsIndex");
253 if (prop.second && *prop.second) {
254 ACS = app.getAssertionConsumerServiceByIndex(atoi(prop.second));
256 request.log(SPRequest::SPWarn, "invalid acsIndex specified in request, using acsIndex property");
257 else if (ECP && !XMLString::equals(ACS->getString("Binding").second, samlconstants::SAML20_BINDING_PAOS)) {
258 request.log(SPRequest::SPWarn, "acsIndex in request referenced a non-PAOS ACS, using default ACS location");
263 prop = getString("target", request);
265 target = prop.second;
267 // Always need to recover target URL to compute handler below.
268 recoverRelayState(app, request, request, target, false);
270 pair<bool,bool> flag = getBool("isPassive", request);
271 isPassive = (flag.first && flag.second);
274 flag = getBool("forceAuthn", request);
275 forceAuthn = (flag.first && flag.second);
278 // Populate via parameter, map, or property.
279 acClass = getString("authnContextClassRef", request);
280 acComp = getString("authnContextComparison", request);
281 nidFormat = getString("NameIDFormat", request);
282 spQual = getString("SPNameQualifier", request);
285 // Check for a hardwired target value in the map or handler.
286 prop = getString("target", request, HANDLER_PROPERTY_MAP|HANDLER_PROPERTY_FIXED);
288 target = prop.second;
290 target = request.getRequestURL();
292 pair<bool,bool> flag = getBool("isPassive", request, HANDLER_PROPERTY_MAP|HANDLER_PROPERTY_FIXED);
293 isPassive = flag.first && flag.second;
295 flag = getBool("forceAuthn", request, HANDLER_PROPERTY_MAP|HANDLER_PROPERTY_FIXED);
296 forceAuthn = flag.first && flag.second;
299 // Populate via map or property.
300 acClass = getString("authnContextClassRef", request, HANDLER_PROPERTY_MAP|HANDLER_PROPERTY_FIXED);
301 acComp = getString("authnContextComparison", request, HANDLER_PROPERTY_MAP|HANDLER_PROPERTY_FIXED);
302 nidFormat = getString("NameIDFormat", request, HANDLER_PROPERTY_MAP|HANDLER_PROPERTY_FIXED);
303 spQual = getString("SPNameQualifier", request, HANDLER_PROPERTY_MAP|HANDLER_PROPERTY_FIXED);
307 m_log.debug("attempting to initiate session using SAML 2.0 Enhanced Client Profile");
309 m_log.debug("attempting to initiate session using SAML 2.0 with provider (%s)", entityID.c_str());
313 ACS = app.getAssertionConsumerServiceByBinding(samlconstants::SAML20_BINDING_PAOS);
315 throw ConfigurationException("Unable to locate PAOS response endpoint.");
318 // Try fixed index property, or incoming binding set, or default, in order.
319 pair<bool,unsigned int> index = getUnsignedInt("acsIndex", request, HANDLER_PROPERTY_MAP|HANDLER_PROPERTY_FIXED);
321 ACS = app.getAssertionConsumerServiceByIndex(index.second);
323 request.log(SPRequest::SPWarn, "invalid acsIndex property, using default ACS location");
326 for (vector<string>::const_iterator b = m_incomingBindings.begin(); !ACS && b != m_incomingBindings.end(); ++b) {
327 ACS = app.getAssertionConsumerServiceByBinding(b->c_str());
328 if (ACS && !XMLString::equals(getProtocolFamily(), ACS->getProtocolFamily()))
333 ACS = app.getDefaultAssertionConsumerService();
337 // Validate the ACS for use with this protocol.
338 if (!ECP && ACS && !XMLString::equals(getProtocolFamily(), ACS->getProtocolFamily())) {
339 m_log.error("configured or requested ACS has non-SAML 2.0 binding");
340 throw ConfigurationException("Configured or requested ACS has non-SAML 2.0 binding ($1).", params(1, ACS->getString("Binding").second));
343 // To invoke the request builder, the key requirement is to figure out how
344 // to express the ACS, by index or value, and if by value, where.
345 // We have to compute the handlerURL no matter what, because we may need to
346 // flip the index to an SSL-version.
347 string ACSloc = request.getHandlerURL(target.c_str());
349 SPConfig& conf = SPConfig::getConfig();
350 if (conf.isEnabled(SPConfig::OutOfProcess)) {
351 if (acsByIndex.first && acsByIndex.second) {
354 // We may already have RelayState set if we looped back here,
355 // but we've turned it back into a resource by this point, so if there's
356 // a target on the URL, reset to that value.
357 prop.second = request.getParameter("target");
358 if (prop.second && *prop.second)
359 target = prop.second;
362 // Determine index to use.
363 pair<bool,const XMLCh*> ix = pair<bool,const XMLCh*>(false,nullptr);
365 if (!strncmp(ACSloc.c_str(), "https", 5)) {
366 ix = ACS->getXMLString("sslIndex", shibspconstants::ASCII_SHIB2SPCONFIG_NS);
368 ix = ACS->getXMLString("index");
371 ix = ACS->getXMLString("index");
376 app, &request, request, entityID.c_str(),
378 ACS ? XMLString::equals(ACS->getString("Binding").second, samlconstants::SAML20_BINDING_HTTP_ARTIFACT) : false,
380 isPassive, forceAuthn,
381 acClass.first ? acClass.second : nullptr,
382 acComp.first ? acComp.second : nullptr,
383 nidFormat.first ? nidFormat.second : nullptr,
384 spQual.first ? spQual.second : nullptr,
389 // Since we're not passing by index, we need to fully compute the return URL and binding.
390 // Compute the ACS URL. We add the ACS location to the base handlerURL.
391 prop = ACS ? ACS->getString("Location") : pair<bool,const char*>(false,nullptr);
393 ACSloc += prop.second;
396 // We may already have RelayState set if we looped back here,
397 // but we've turned it back into a resource by this point, so if there's
398 // a target on the URL, reset to that value.
399 prop.second = request.getParameter("target");
400 if (prop.second && *prop.second)
401 target = prop.second;
405 app, &request, request, entityID.c_str(),
407 ACS ? XMLString::equals(ACS->getString("Binding").second, samlconstants::SAML20_BINDING_HTTP_ARTIFACT) : false,
408 ACSloc.c_str(), ACS ? ACS->getXMLString("Binding").second : nullptr,
409 isPassive, forceAuthn,
410 acClass.first ? acClass.second : nullptr,
411 acComp.first ? acComp.second : nullptr,
412 nidFormat.first ? nidFormat.second : nullptr,
413 spQual.first ? spQual.second : nullptr,
419 DDF out,in = DDF(m_address.c_str()).structure();
420 DDFJanitor jin(in), jout(out);
421 in.addmember("application_id").string(app.getId());
422 if (!entityID.empty())
423 in.addmember("entity_id").string(entityID.c_str());
425 in.addmember("isPassive").integer(1);
427 in.addmember("forceAuthn").integer(1);
429 in.addmember("authnContextClassRef").string(acClass.second);
431 in.addmember("authnContextComparison").string(acComp.second);
433 in.addmember("NameIDFormat").string(nidFormat.second);
435 in.addmember("SPNameQualifier").string(spQual.second);
436 if (acsByIndex.first && acsByIndex.second) {
438 // Determine index to use.
439 pair<bool,const char*> ix = pair<bool,const char*>(false,nullptr);
440 if (!strncmp(ACSloc.c_str(), "https", 5)) {
441 ix = ACS->getString("sslIndex", shibspconstants::ASCII_SHIB2SPCONFIG_NS);
443 ix = ACS->getString("index");
446 ix = ACS->getString("index");
448 in.addmember("acsIndex").string(ix.second);
449 if (XMLString::equals(ACS->getString("Binding").second, samlconstants::SAML20_BINDING_HTTP_ARTIFACT))
450 in.addmember("artifact").integer(1);
454 // Since we're not passing by index, we need to fully compute the return URL and binding.
455 // Compute the ACS URL. We add the ACS location to the base handlerURL.
456 prop = ACS ? ACS->getString("Location") : pair<bool,const char*>(false,nullptr);
458 ACSloc += prop.second;
459 in.addmember("acsLocation").string(ACSloc.c_str());
461 prop = ACS->getString("Binding");
462 in.addmember("acsBinding").string(prop.second);
463 if (XMLString::equals(prop.second, samlconstants::SAML20_BINDING_HTTP_ARTIFACT))
464 in.addmember("artifact").integer(1);
469 // We may already have RelayState set if we looped back here,
470 // but we've turned it back into a resource by this point, so if there's
471 // a target on the URL, reset to that value.
472 prop.second = request.getParameter("target");
473 if (prop.second && *prop.second)
474 target = prop.second;
477 in.addmember("RelayState").unsafe_string(target.c_str());
479 // Remote the processing.
480 out = request.getServiceProvider().getListenerService()->send(in);
481 return unwrap(request, out);
484 pair<bool,long> SAML2SessionInitiator::unwrap(SPRequest& request, DDF& out) const
486 // See if there's any response to send back.
487 if (!out["redirect"].isnull() || !out["response"].isnull()) {
488 // If so, we're responsible for handling the POST data, probably by dropping a cookie.
489 preservePostData(request.getApplication(), request, request, out["RelayState"].string());
491 return RemotedHandler::unwrap(request, out);
494 void SAML2SessionInitiator::receive(DDF& in, ostream& out)
497 const char* aid=in["application_id"].string();
498 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : nullptr;
500 // Something's horribly wrong.
501 m_log.error("couldn't find application (%s) to generate AuthnRequest", aid ? aid : "(missing)");
502 throw ConfigurationException("Unable to locate application for new session, deleted?");
506 DDFJanitor jout(ret);
508 // Wrap the outgoing object with a Response facade.
509 auto_ptr<HTTPResponse> http(getResponse(ret));
511 auto_ptr_XMLCh index(in["acsIndex"].string());
512 auto_ptr_XMLCh bind(in["acsBinding"].string());
514 string relayState(in["RelayState"].string() ? in["RelayState"].string() : "");
515 string postData(in["PostData"].string() ? in["PostData"].string() : "");
517 // Since we're remoted, the result should either be a throw, which we pass on,
518 // a false/0 return, which we just return as an empty structure, or a response/redirect,
519 // which we capture in the facade and send back.
521 *app, nullptr, *http.get(), in["entity_id"].string(),
523 (in["artifact"].integer() != 0),
524 in["acsLocation"].string(), bind.get(),
525 in["isPassive"].integer()==1, in["forceAuthn"].integer()==1,
526 in["authnContextClassRef"].string(), in["authnContextComparison"].string(),
527 in["NameIDFormat"].string(), in["SPNameQualifier"].string(),
532 ret.addmember("RelayState").unsafe_string(relayState.c_str());
538 class _sameIdP : public binary_function<const IDPEntry*, const XMLCh*, bool>
541 bool operator()(const IDPEntry* entry, const XMLCh* entityID) const {
542 return entry ? XMLString::equals(entry->getProviderID(), entityID) : false;
548 pair<bool,long> SAML2SessionInitiator::doRequest(
549 const Application& app,
550 const HTTPRequest* httpRequest,
551 HTTPResponse& httpResponse,
552 const char* entityID,
553 const XMLCh* acsIndex,
554 bool artifactInbound,
555 const char* acsLocation,
556 const XMLCh* acsBinding,
559 const char* authnContextClassRef,
560 const char* authnContextComparison,
561 const char* NameIDFormat,
562 const char* SPNameQualifier,
567 bool ECP = XMLString::equals(acsBinding, m_paosBinding.get());
569 pair<const EntityDescriptor*,const RoleDescriptor*> entity = pair<const EntityDescriptor*,const RoleDescriptor*>(nullptr,nullptr);
570 const IDPSSODescriptor* role = nullptr;
571 const EndpointType* ep = nullptr;
572 const MessageEncoder* encoder = nullptr;
574 // We won't need this for ECP, but safety dictates we get the lock here.
575 MetadataProvider* m=app.getMetadataProvider();
581 m_log.error("MessageEncoder for PAOS binding not available");
582 return make_pair(false,0L);
586 // Use metadata to locate the IdP's SSO service.
587 MetadataProviderCriteria mc(app, entityID, &IDPSSODescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
588 entity=m->getEntityDescriptor(mc);
590 m_log.warn("unable to locate metadata for provider (%s)", entityID);
591 throw MetadataException("Unable to locate metadata for identity provider ($entityID)", namedparams(1, "entityID", entityID));
593 else if (!entity.second) {
594 m_log.log(getParent() ? Priority::INFO : Priority::WARN, "unable to locate SAML 2.0 identity provider role for provider (%s)", entityID);
596 return make_pair(false,0L);
597 throw MetadataException("Unable to locate SAML 2.0 identity provider role for provider ($entityID)", namedparams(1, "entityID", entityID));
599 else if (artifactInbound && !SPConfig::getConfig().getArtifactResolver()->isSupported(dynamic_cast<const SSODescriptorType&>(*entity.second))) {
600 m_log.warn("artifact binding selected for response, but identity provider lacks support");
602 return make_pair(false,0L);
603 throw MetadataException("Identity provider ($entityID) lacks SAML 2.0 artifact support.", namedparams(1, "entityID", entityID));
606 // Loop over the supportable outgoing bindings.
607 role = dynamic_cast<const IDPSSODescriptor*>(entity.second);
608 vector<const XMLCh*>::const_iterator b;
609 for (b = m_bindings.begin(); b!=m_bindings.end(); ++b) {
610 if (ep=EndpointManager<SingleSignOnService>(role->getSingleSignOnServices()).getByBinding(*b)) {
611 map<const XMLCh*,MessageEncoder*>::const_iterator enc = m_encoders.find(*b);
612 if (enc!=m_encoders.end())
613 encoder = enc->second;
617 if (!ep || !encoder) {
618 m_log.warn("unable to locate compatible SSO service for provider (%s)", entityID);
620 return make_pair(false,0L);
621 throw MetadataException("Unable to locate compatible SSO service for provider ($entityID)", namedparams(1, "entityID", entityID));
625 preserveRelayState(app, httpResponse, relayState);
627 auto_ptr<AuthnRequest> req(m_requestTemplate ? m_requestTemplate->cloneAuthnRequest() : AuthnRequestBuilder::buildAuthnRequest());
628 if (m_requestTemplate) {
629 // Freshen TS and ID.
631 req->setIssueInstant(time(nullptr));
635 req->setDestination(ep->getLocation());
636 if (acsIndex && *acsIndex)
637 req->setAssertionConsumerServiceIndex(acsIndex);
639 auto_ptr_XMLCh wideloc(acsLocation);
640 req->setAssertionConsumerServiceURL(wideloc.get());
642 if (acsBinding && *acsBinding)
643 req->setProtocolBinding(acsBinding);
645 req->IsPassive(isPassive);
647 req->ForceAuthn(forceAuthn);
648 if (!req->getIssuer()) {
649 Issuer* issuer = IssuerBuilder::buildIssuer();
650 req->setIssuer(issuer);
651 issuer->setName(app.getRelyingParty(entity.first)->getXMLString("entityID").second);
653 if (!req->getNameIDPolicy()) {
654 NameIDPolicy* namepol = NameIDPolicyBuilder::buildNameIDPolicy();
655 req->setNameIDPolicy(namepol);
656 namepol->AllowCreate(true);
658 if (NameIDFormat && *NameIDFormat) {
659 auto_ptr_XMLCh wideform(NameIDFormat);
660 req->getNameIDPolicy()->setFormat(wideform.get());
662 if (SPNameQualifier && *SPNameQualifier) {
663 auto_ptr_XMLCh widequal(SPNameQualifier);
664 req->getNameIDPolicy()->setSPNameQualifier(widequal.get());
666 if (authnContextClassRef || authnContextComparison) {
667 RequestedAuthnContext* reqContext = req->getRequestedAuthnContext();
669 reqContext = RequestedAuthnContextBuilder::buildRequestedAuthnContext();
670 req->setRequestedAuthnContext(reqContext);
672 if (authnContextClassRef) {
673 reqContext->getAuthnContextDeclRefs().clear();
674 auto_ptr_XMLCh wideclass(authnContextClassRef);
675 AuthnContextClassRef* cref = AuthnContextClassRefBuilder::buildAuthnContextClassRef();
676 cref->setReference(wideclass.get());
677 reqContext->getAuthnContextClassRefs().push_back(cref);
680 if (reqContext->getAuthnContextClassRefs().empty() && reqContext->getAuthnContextDeclRefs().empty()) {
681 req->setRequestedAuthnContext(nullptr);
683 else if (authnContextComparison) {
684 auto_ptr_XMLCh widecomp(authnContextComparison);
685 reqContext->setComparison(widecomp.get());
689 pair<bool,bool> requestDelegation = getBool("requestDelegation");
690 if (requestDelegation.first && requestDelegation.second && entity.first) {
691 // Request delegation by including the IdP as an Audience.
692 // Also specify the expected session lifetime as the bound on the assertion lifetime.
693 const PropertySet* sessionProps = app.getPropertySet("Sessions");
694 pair<bool,unsigned int> lifetime = sessionProps ? sessionProps->getUnsignedInt("lifetime") : pair<bool,unsigned int>(true,28800);
695 if (!lifetime.first || lifetime.second == 0)
696 lifetime.second = 28800;
697 if (!req->getConditions())
698 req->setConditions(ConditionsBuilder::buildConditions());
699 req->getConditions()->setNotOnOrAfter(time(nullptr) + lifetime.second + 300);
700 AudienceRestriction* audrest = AudienceRestrictionBuilder::buildAudienceRestriction();
701 req->getConditions()->getConditions().push_back(audrest);
702 Audience* aud = AudienceBuilder::buildAudience();
703 audrest->getAudiences().push_back(aud);
704 aud->setAudienceURI(entity.first->getEntityID());
707 if (ECP && entityID) {
708 auto_ptr_XMLCh wideid(entityID);
709 Scoping* scoping = req->getScoping();
711 scoping = ScopingBuilder::buildScoping();
712 req->setScoping(scoping);
714 IDPList* idplist = scoping->getIDPList();
716 idplist = IDPListBuilder::buildIDPList();
717 scoping->setIDPList(idplist);
719 VectorOf(IDPEntry) entries = idplist->getIDPEntrys();
720 if (find_if(entries, bind2nd(_sameIdP(), wideid.get())) == nullptr) {
721 IDPEntry* entry = IDPEntryBuilder::buildIDPEntry();
722 entry->setProviderID(wideid.get());
723 entries.push_back(entry);
727 auto_ptr_char dest(ep ? ep->getLocation() : nullptr);
730 // If the request object is available, we're responsible for the POST data.
731 preservePostData(app, *httpRequest, httpResponse, relayState.c_str());
734 long ret = sendMessage(
735 *encoder, req.get(), relayState.c_str(), dest.get(), role, app, httpResponse, role ? role->WantAuthnRequestsSigned() : false
737 req.release(); // freed by encoder
738 return make_pair(true,ret);
740 return make_pair(false,0L);