2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * SimpleAggregationAttributeResolver.cpp
24 * AttributeResolver based on SAML queries to third-party AA sources.
28 #include "exceptions.h"
29 #include "Application.h"
30 #include "ServiceProvider.h"
31 #include "SessionCache.h"
32 #include "attribute/NameIDAttribute.h"
33 #include "attribute/SimpleAttribute.h"
34 #include "attribute/filtering/AttributeFilter.h"
35 #include "attribute/filtering/BasicFilteringContext.h"
36 #include "attribute/resolver/AttributeExtractor.h"
37 #include "attribute/resolver/AttributeResolver.h"
38 #include "attribute/resolver/ResolutionContext.h"
39 #include "binding/SOAPClient.h"
40 #include "metadata/MetadataProviderCriteria.h"
41 #include "security/SecurityPolicy.h"
42 #include "security/SecurityPolicyProvider.h"
43 #include "util/SPConstants.h"
45 #include <boost/algorithm/string.hpp>
46 #include <boost/iterator/indirect_iterator.hpp>
47 #include <boost/ptr_container/ptr_vector.hpp>
48 #include <saml/exceptions.h>
49 #include <saml/SAMLConfig.h>
50 #include <saml/saml2/binding/SAML2SOAPClient.h>
51 #include <saml/saml2/core/Protocols.h>
52 #include <saml/saml2/metadata/Metadata.h>
53 #include <saml/saml2/metadata/MetadataCredentialCriteria.h>
54 #include <saml/saml2/metadata/MetadataProvider.h>
55 #include <xmltooling/XMLToolingConfig.h>
56 #include <xmltooling/security/TrustEngine.h>
57 #include <xmltooling/util/NDC.h>
58 #include <xmltooling/util/URLEncoder.h>
59 #include <xmltooling/util/XMLHelper.h>
60 #include <xercesc/util/XMLUniDefs.hpp>
62 using namespace shibsp;
63 using namespace opensaml::saml2;
64 using namespace opensaml::saml2p;
65 using namespace opensaml::saml2md;
66 using namespace opensaml;
67 using namespace xmltooling;
68 using namespace boost;
73 class SHIBSP_DLLLOCAL SimpleAggregationContext : public ResolutionContext
76 SimpleAggregationContext(const Application& application, const Session& session)
81 m_class(session.getAuthnContextClassRef()),
82 m_decl(session.getAuthnContextDeclRef()),
83 m_inputTokens(nullptr),
84 m_inputAttributes(nullptr) {
87 SimpleAggregationContext(
88 const Application& application,
89 const GenericRequest* request=nullptr,
90 const NameID* nameid=nullptr,
91 const XMLCh* entityID=nullptr,
92 const XMLCh* authncontext_class=nullptr,
93 const XMLCh* authncontext_decl=nullptr,
94 const vector<const opensaml::Assertion*>* tokens=nullptr,
95 const vector<shibsp::Attribute*>* attributes=nullptr
96 ) : m_app(application),
100 m_entityid(entityID),
101 m_class(authncontext_class),
102 m_decl(authncontext_decl),
103 m_inputTokens(tokens),
104 m_inputAttributes(attributes) {
107 ~SimpleAggregationContext() {
108 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<shibsp::Attribute>());
109 for_each(m_assertions.begin(), m_assertions.end(), xmltooling::cleanup<opensaml::Assertion>());
112 const Application& getApplication() const {
115 const GenericRequest* getRequest() const {
118 const char* getEntityID() const {
119 return m_session ? m_session->getEntityID() : m_entityid.get();
121 const NameID* getNameID() const {
122 return m_session ? m_session->getNameID() : m_nameid;
124 const XMLCh* getClassRef() const {
125 return m_class.get();
127 const XMLCh* getDeclRef() const {
130 const Session* getSession() const {
133 const vector<shibsp::Attribute*>* getInputAttributes() const {
134 return m_inputAttributes;
136 const vector<const opensaml::Assertion*>* getInputTokens() const {
137 return m_inputTokens;
139 vector<shibsp::Attribute*>& getResolvedAttributes() {
142 vector<opensaml::Assertion*>& getResolvedAssertions() {
147 const Application& m_app;
148 const GenericRequest* m_request;
149 const Session* m_session;
150 const NameID* m_nameid;
151 auto_ptr_char m_entityid;
152 auto_ptr_XMLCh m_class;
153 auto_ptr_XMLCh m_decl;
154 const vector<const opensaml::Assertion*>* m_inputTokens;
155 const vector<shibsp::Attribute*>* m_inputAttributes;
156 vector<shibsp::Attribute*> m_attributes;
157 vector<opensaml::Assertion*> m_assertions;
160 class SHIBSP_DLLLOCAL SimpleAggregationResolver : public AttributeResolver
163 SimpleAggregationResolver(const DOMElement* e);
164 ~SimpleAggregationResolver() {}
166 Lockable* lock() {return this;}
170 ResolutionContext* createResolutionContext(
171 const Application& application,
172 const EntityDescriptor* issuer,
173 const XMLCh* protocol,
174 const NameID* nameid=nullptr,
175 const XMLCh* authncontext_class=nullptr,
176 const XMLCh* authncontext_decl=nullptr,
177 const vector<const opensaml::Assertion*>* tokens=nullptr,
178 const vector<shibsp::Attribute*>* attributes=nullptr
180 return createResolutionContext(application, nullptr, issuer, protocol, nameid, authncontext_class, authncontext_decl, tokens, attributes);
183 ResolutionContext* createResolutionContext(
184 const Application& application,
185 const GenericRequest* request,
186 const EntityDescriptor* issuer,
187 const XMLCh* protocol,
188 const NameID* nameid=nullptr,
189 const XMLCh* authncontext_class=nullptr,
190 const XMLCh* authncontext_decl=nullptr,
191 const vector<const opensaml::Assertion*>* tokens=nullptr,
192 const vector<shibsp::Attribute*>* attributes=nullptr
194 return new SimpleAggregationContext(
195 application, request, nameid, (issuer ? issuer->getEntityID() : nullptr), authncontext_class, authncontext_decl, tokens, attributes
199 ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
200 return new SimpleAggregationContext(application,session);
203 void resolveAttributes(ResolutionContext& ctx) const;
205 void getAttributeIds(vector<string>& attributes) const {
207 m_extractor->getAttributeIds(attributes);
211 void doQuery(SimpleAggregationContext& ctx, const char* entityID, const NameID* name) const;
216 vector<string> m_attributeIds;
218 scoped_ptr<MetadataProvider> m_metadata;
219 scoped_ptr<TrustEngine> m_trust;
220 scoped_ptr<AttributeExtractor> m_extractor;
221 scoped_ptr<AttributeFilter> m_filter;
222 ptr_vector<saml2::Attribute> m_designators;
223 vector< pair<string,bool> > m_sources;
224 vector<string> m_exceptionId;
227 AttributeResolver* SHIBSP_DLLLOCAL SimpleAggregationResolverFactory(const DOMElement* const & e)
229 return new SimpleAggregationResolver(e);
232 static const XMLCh _AttributeExtractor[] = UNICODE_LITERAL_18(A,t,t,r,i,b,u,t,e,E,x,t,r,a,c,t,o,r);
233 static const XMLCh _AttributeFilter[] = UNICODE_LITERAL_15(A,t,t,r,i,b,u,t,e,F,i,l,t,e,r);
234 static const XMLCh attributeId[] = UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,d);
235 static const XMLCh Entity[] = UNICODE_LITERAL_6(E,n,t,i,t,y);
236 static const XMLCh EntityReference[] = UNICODE_LITERAL_15(E,n,t,i,t,y,R,e,f,e,r,e,n,c,e);
237 static const XMLCh exceptionId[] = UNICODE_LITERAL_11(e,x,c,e,p,t,i,o,n,I,d);
238 static const XMLCh format[] = UNICODE_LITERAL_6(f,o,r,m,a,t);
239 static const XMLCh _MetadataProvider[] = UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);
240 static const XMLCh policyId[] = UNICODE_LITERAL_8(p,o,l,i,c,y,I,d);
241 static const XMLCh subjectMatch[] = UNICODE_LITERAL_12(s,u,b,j,e,c,t,M,a,t,c,h);
242 static const XMLCh _TrustEngine[] = UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
243 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
246 SimpleAggregationResolver::SimpleAggregationResolver(const DOMElement* e)
247 : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver.SimpleAggregation")),
248 m_policyId(XMLHelper::getAttrString(e, nullptr, policyId)),
249 m_subjectMatch(XMLHelper::getAttrBool(e, false, subjectMatch))
252 xmltooling::NDC ndc("SimpleAggregationResolver");
255 const XMLCh* aid = e ? e->getAttributeNS(nullptr, attributeId) : nullptr;
257 auto_ptr_char dup(aid);
258 string sdup(dup.get());
260 split(m_attributeIds, sdup, is_space(), algorithm::token_compress_on);
262 aid = e->getAttributeNS(nullptr, format);
267 string exid(XMLHelper::getAttrString(e, nullptr, exceptionId));
269 m_exceptionId.push_back(exid);
271 DOMElement* child = XMLHelper::getFirstChildElement(e, _MetadataProvider);
273 string t(XMLHelper::getAttrString(child, nullptr, _type));
275 throw ConfigurationException("MetadataProvider element missing type attribute.");
276 m_log.info("building MetadataProvider of type %s...", t.c_str());
277 m_metadata.reset(SAMLConfig::getConfig().MetadataProviderManager.newPlugin(t.c_str(), child));
281 child = XMLHelper::getFirstChildElement(e, _TrustEngine);
283 string t(XMLHelper::getAttrString(child, nullptr, _type));
285 throw ConfigurationException("TrustEngine element missing type attribute.");
286 m_log.info("building TrustEngine of type %s...", t.c_str());
287 m_trust.reset(XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), child));
290 child = XMLHelper::getFirstChildElement(e, _AttributeExtractor);
292 string t(XMLHelper::getAttrString(child, nullptr, _type));
294 throw ConfigurationException("AttributeExtractor element missing type attribute.");
295 m_log.info("building AttributeExtractor of type %s...", t.c_str());
296 m_extractor.reset(SPConfig::getConfig().AttributeExtractorManager.newPlugin(t.c_str(), child));
299 child = XMLHelper::getFirstChildElement(e, _AttributeFilter);
301 string t(XMLHelper::getAttrString(child, nullptr, _type));
303 throw ConfigurationException("AttributeFilter element missing type attribute.");
304 m_log.info("building AttributeFilter of type %s...", t.c_str());
305 m_filter.reset(SPConfig::getConfig().AttributeFilterManager.newPlugin(t.c_str(), child));
308 child = XMLHelper::getFirstChildElement(e);
310 if (child->hasChildNodes() && XMLString::equals(child->getLocalName(), Entity)) {
311 aid = child->getFirstChild()->getNodeValue();
313 auto_ptr_char taid(aid);
314 m_sources.push_back(pair<string,bool>(taid.get(),true));
317 else if (child->hasChildNodes() && XMLString::equals(child->getLocalName(), EntityReference)) {
318 aid = child->getFirstChild()->getNodeValue();
320 auto_ptr_char taid(aid);
321 m_sources.push_back(pair<string,bool>(taid.get(),false));
324 else if (XMLHelper::isNodeNamed(child, samlconstants::SAML20_NS, saml2::Attribute::LOCAL_NAME)) {
326 auto_ptr<XMLObject> obj(saml2::AttributeBuilder::buildOneFromElement(child));
327 saml2::Attribute* down = dynamic_cast<saml2::Attribute*>(obj.get());
329 m_designators.push_back(down);
333 catch (std::exception& ex) {
334 m_log.error("exception loading attribute designator: %s", ex.what());
337 child = XMLHelper::getNextSiblingElement(child);
341 void SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const char* entityID, const NameID* name) const
344 xmltooling::NDC ndc("doQuery");
346 const Application& application = ctx.getApplication();
347 MetadataProviderCriteria mc(application, entityID, &AttributeAuthorityDescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
348 Locker mlocker(m_metadata.get());
349 const AttributeAuthorityDescriptor* AA=nullptr;
350 pair<const EntityDescriptor*,const RoleDescriptor*> mdresult =
351 (m_metadata ? m_metadata.get() : application.getMetadataProvider())->getEntityDescriptor(mc);
352 if (!mdresult.first) {
353 m_log.warn("unable to locate metadata for provider (%s)", entityID);
356 else if (!(AA=dynamic_cast<const AttributeAuthorityDescriptor*>(mdresult.second))) {
357 m_log.warn("no SAML 2 AttributeAuthority role found in metadata for (%s)", entityID);
361 const PropertySet* relyingParty = application.getRelyingParty(mdresult.first);
362 pair<bool,bool> signedAssertions = relyingParty->getBool("requireSignedAssertions");
363 pair<bool,const char*> encryption = relyingParty->getString("encryption");
365 // Locate policy key.
366 const char* policyId = m_policyId.empty() ? application.getString("policyId").second : m_policyId.c_str();
368 // Set up policy and SOAP client.
369 scoped_ptr<SecurityPolicy> policy(
370 application.getServiceProvider().getSecurityPolicyProvider()->createSecurityPolicy(application, nullptr, policyId)
373 policy->setMetadataProvider(m_metadata.get());
375 policy->setTrustEngine(m_trust.get());
376 policy->getAudiences().push_back(relyingParty->getXMLString("entityID").second);
378 MetadataCredentialCriteria mcc(*AA);
379 shibsp::SOAPClient soaper(*policy.get());
381 auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
382 auto_ptr<saml2p::StatusResponseType> srt;
383 const vector<AttributeService*>& endpoints=AA->getAttributeServices();
384 for (indirect_iterator<vector<AttributeService*>::const_iterator> ep = make_indirect_iterator(endpoints.begin());
385 !srt.get() && ep != make_indirect_iterator(endpoints.end()); ++ep) {
386 if (!XMLString::equals(ep->getBinding(), binding.get()) || !ep->getLocation())
388 auto_ptr_char loc(ep->getLocation());
390 auto_ptr<saml2::Subject> subject(saml2::SubjectBuilder::buildSubject());
392 // Encrypt the NameID?
393 if (encryption.first && (!strcmp(encryption.second, "true") || !strcmp(encryption.second, "back"))) {
394 auto_ptr<EncryptedID> encrypted(EncryptedIDBuilder::buildEncryptedID());
397 *(policy->getMetadataProvider()),
400 relyingParty->getXMLString("encryptionAlg").second
402 subject->setEncryptedID(encrypted.get());
406 subject->setNameID(name->cloneNameID());
409 saml2p::AttributeQuery* query = saml2p::AttributeQueryBuilder::buildAttributeQuery();
410 query->setSubject(subject.release());
411 Issuer* iss = IssuerBuilder::buildIssuer();
412 iss->setName(relyingParty->getXMLString("entityID").second);
413 query->setIssuer(iss);
414 for (ptr_vector<saml2::Attribute>::const_iterator ad = m_designators.begin(); ad != m_designators.end(); ++ad) {
415 auto_ptr<saml2::Attribute> adwrapper(ad->cloneAttribute());
416 query->getAttributes().push_back(adwrapper.get());
420 SAML2SOAPClient client(soaper, false);
421 client.sendSAML(query, application.getId(), mcc, loc.get());
422 srt.reset(client.receiveSAML());
424 catch (std::exception& ex) {
425 m_log.error("exception during SAML query to %s: %s", loc.get(), ex.what());
431 m_log.error("unable to obtain a SAML response from attribute authority (%s)", entityID);
432 throw BindingException("Unable to obtain a SAML response from attribute authority.");
435 saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt.get());
437 m_log.error("message was not a samlp:Response");
438 throw FatalProfileException("Attribute authority returned an unrecognized message.");
440 else if (!response->getStatus() || !response->getStatus()->getStatusCode() ||
441 !XMLString::equals(response->getStatus()->getStatusCode()->getValue(), saml2p::StatusCode::SUCCESS)) {
442 m_log.error("attribute authority (%s) returned a SAML error", entityID);
443 throw FatalProfileException("Attribute authority returned a SAML error.");
446 saml2::Assertion* newtoken = nullptr;
447 auto_ptr<saml2::Assertion> newtokenwrapper;
448 const vector<saml2::Assertion*>& assertions = const_cast<const saml2p::Response*>(response)->getAssertions();
449 if (assertions.empty()) {
450 // Check for encryption.
451 const vector<saml2::EncryptedAssertion*>& encassertions =
452 const_cast<const saml2p::Response*>(response)->getEncryptedAssertions();
453 if (encassertions.empty()) {
454 m_log.warn("response from attribute authority was empty");
457 else if (encassertions.size() > 1) {
458 m_log.warn("simple resolver only supports one assertion in the query response");
461 CredentialResolver* cr=application.getCredentialResolver();
463 m_log.warn("found encrypted assertion, but no CredentialResolver was available");
464 throw FatalProfileException("Assertion was encrypted, but no decryption credentials are available.");
467 // With this flag on, we block unauthenticated ciphertext when decrypting,
468 // unless the protocol was authenticated.
469 pair<bool,bool> authenticatedCipher = application.getBool("requireAuthenticatedEncryption");
470 if (policy->isAuthenticated())
471 authenticatedCipher.second = false;
473 // Attempt to decrypt it.
475 Locker credlocker(cr);
476 auto_ptr<XMLObject> tokenwrapper(
477 encassertions.front()->decrypt(
478 *cr, relyingParty->getXMLString("entityID").second, &mcc, authenticatedCipher.first && authenticatedCipher.second
481 newtoken = dynamic_cast<saml2::Assertion*>(tokenwrapper.get());
483 tokenwrapper.release();
484 newtokenwrapper.reset(newtoken);
485 if (m_log.isDebugEnabled())
486 m_log.debugStream() << "decrypted assertion: " << *newtoken << logging::eol;
489 catch (std::exception& ex) {
490 m_log.error("failed to decrypt assertion: %s", ex.what());
495 if (assertions.size() > 1)
496 m_log.warn("simple resolver only supports one assertion in the query response");
497 newtoken = assertions.front();
500 if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
501 m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
502 throw SecurityPolicyException("Rejected unsigned assertion based on local policy.");
506 // We're going to insist that the assertion issuer is the same as the peer.
507 // Reset the policy's message bits and extract them from the assertion.
509 policy->setMessageID(newtoken->getID());
510 policy->setIssueInstant(newtoken->getIssueInstantEpoch());
511 policy->setIssuer(newtoken->getIssuer());
512 policy->evaluate(*newtoken);
514 // Now we can check the security status of the policy.
515 if (!policy->isAuthenticated())
516 throw SecurityPolicyException("Security of SAML 2.0 query result not established.");
518 if (m_subjectMatch) {
519 // Check for subject match.
520 auto_ptr<NameID> nameIDwrapper;
521 NameID* respName = newtoken->getSubject() ? newtoken->getSubject()->getNameID() : nullptr;
523 // Check for encryption.
524 EncryptedID* encname = newtoken->getSubject() ? newtoken->getSubject()->getEncryptedID() : nullptr;
526 CredentialResolver* cr=application.getCredentialResolver();
528 m_log.warn("found EncryptedID, but no CredentialResolver was available");
530 Locker credlocker(cr);
531 auto_ptr<XMLObject> decryptedID(encname->decrypt(*cr, relyingParty->getXMLString("entityID").second, &mcc));
532 respName = dynamic_cast<NameID*>(decryptedID.get());
534 decryptedID.release();
535 nameIDwrapper.reset(respName);
536 if (m_log.isDebugEnabled())
537 m_log.debugStream() << "decrypted NameID: " << *respName << logging::eol;
543 if (!respName || !XMLString::equals(respName->getName(), name->getName()) ||
544 !XMLString::equals(respName->getFormat(), name->getFormat()) ||
545 !XMLString::equals(respName->getNameQualifier(), name->getNameQualifier()) ||
546 !XMLString::equals(respName->getSPNameQualifier(), name->getSPNameQualifier())) {
548 m_log.warnStream() << "ignoring Assertion without strongly matching NameID in Subject: " <<
549 *respName << logging::eol;
551 m_log.warn("ignoring Assertion without NameID in Subject");
556 catch (std::exception& ex) {
557 m_log.error("assertion failed policy validation: %s", ex.what());
561 // If the token's embedded, detach it.
562 if (!newtokenwrapper.get()) {
564 srt.release(); // detach blows away the Response, so avoid a double free
565 newtokenwrapper.reset(newtoken);
567 ctx.getResolvedAssertions().push_back(newtoken);
568 newtokenwrapper.release();
570 // Finally, extract and filter the result.
572 AttributeExtractor* extractor = m_extractor ? m_extractor.get() : application.getAttributeExtractor();
574 Locker extlocker(extractor);
575 extractor->extractAttributes(application, ctx.getRequest(), AA, *newtoken, ctx.getResolvedAttributes());
578 AttributeFilter* filter = m_filter ? m_filter.get() : application.getAttributeFilter();
580 BasicFilteringContext fc(application, ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
581 Locker filtlocker(filter);
582 filter->filterAttributes(fc, ctx.getResolvedAttributes());
585 catch (std::exception& ex) {
586 m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
587 for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), xmltooling::cleanup<shibsp::Attribute>());
588 ctx.getResolvedAttributes().clear();
593 void SimpleAggregationResolver::resolveAttributes(ResolutionContext& ctx) const
596 xmltooling::NDC ndc("resolveAttributes");
599 SimpleAggregationContext& qctx = dynamic_cast<SimpleAggregationContext&>(ctx);
601 // First we manufacture the appropriate NameID to use.
602 scoped_ptr<NameID> n;
603 for (vector<string>::const_iterator a = m_attributeIds.begin(); !n.get() && a != m_attributeIds.end(); ++a) {
604 const Attribute* attr=nullptr;
605 if (qctx.getSession()) {
606 // Input attributes should be available via multimap.
607 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> range =
608 qctx.getSession()->getIndexedAttributes().equal_range(*a);
609 for (; !attr && range.first != range.second; ++range.first) {
610 if (range.first->second->valueCount() > 0)
611 attr = range.first->second;
614 else if (qctx.getInputAttributes()) {
615 // Have to loop over unindexed set.
616 const vector<Attribute*>* matches = qctx.getInputAttributes();
617 for (indirect_iterator<vector<Attribute*>::const_iterator> match = make_indirect_iterator(matches->begin());
618 !attr && match != make_indirect_iterator(matches->end()); ++match) {
619 if (*a == match->getId() && match->valueCount() > 0)
625 m_log.debug("using input attribute (%s) as identifier for queries", attr->getId());
626 n.reset(NameIDBuilder::buildNameID());
627 const NameIDAttribute* down = dynamic_cast<const NameIDAttribute*>(attr);
629 // We can create a NameID directly from the source material.
630 const NameIDAttribute::Value& v = down->getValues().front();
631 auto_arrayptr<XMLCh> val(fromUTF8(v.m_Name.c_str()));
632 n->setName(val.get());
634 if (!v.m_Format.empty()) {
635 auto_arrayptr<XMLCh> format(fromUTF8(v.m_Format.c_str()));
636 n->setFormat(format.get());
638 if (!v.m_NameQualifier.empty()) {
639 auto_arrayptr<XMLCh> nq(fromUTF8(v.m_NameQualifier.c_str()));
640 n->setNameQualifier(nq.get());
642 if (!v.m_SPNameQualifier.empty()) {
643 auto_arrayptr<XMLCh> spnq(fromUTF8(v.m_SPNameQualifier.c_str()));
644 n->setSPNameQualifier(spnq.get());
646 if (!v.m_SPProvidedID.empty()) {
647 auto_arrayptr<XMLCh> sppid(fromUTF8(v.m_SPProvidedID.c_str()));
648 n->setSPProvidedID(sppid.get());
652 // We have to mock up the NameID.
653 auto_arrayptr<XMLCh> val(fromUTF8(attr->getSerializedValues().front().c_str()));
654 n->setName(val.get());
655 if (!m_format.empty())
656 n->setFormat(m_format.c_str());
662 if (qctx.getNameID() && m_attributeIds.empty()) {
663 m_log.debug("using authenticated NameID as identifier for queries");
666 m_log.warn("unable to resolve attributes, no suitable query identifier found");
673 // Put initial IdP into history to prevent extra query.
674 if (qctx.getEntityID())
675 history.insert(qctx.getEntityID());
677 // Prepare to track exceptions.
678 auto_ptr<SimpleAttribute> exceptAttr;
679 if (!m_exceptionId.empty())
680 exceptAttr.reset(new SimpleAttribute(m_exceptionId));
682 // We have a master loop over all the possible sources of material.
683 for (vector< pair<string,bool> >::const_iterator source = m_sources.begin(); source != m_sources.end(); ++source) {
684 if (source->second) {
685 // A literal entityID to query.
686 if (history.count(source->first) == 0) {
687 m_log.debug("issuing SAML query to (%s)", source->first.c_str());
689 doQuery(qctx, source->first.c_str(), n ? n.get() : qctx.getNameID());
691 catch (std::exception& ex) {
692 if (exceptAttr.get())
693 exceptAttr->getValues().push_back(XMLToolingConfig::getConfig().getURLEncoder()->encode(ex.what()));
695 history.insert(source->first);
698 m_log.debug("skipping previously queried attribute source (%s)", source->first.c_str());
702 m_log.debug("using attribute sources referenced in attribute (%s)", source->first.c_str());
703 if (qctx.getSession()) {
704 // Input attributes should be available via multimap.
705 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> range =
706 qctx.getSession()->getIndexedAttributes().equal_range(source->first);
707 for (; range.first != range.second; ++range.first) {
708 const vector<string>& links = range.first->second->getSerializedValues();
709 for (vector<string>::const_iterator link = links.begin(); link != links.end(); ++link) {
710 if (history.count(*link) == 0) {
711 m_log.debug("issuing SAML query to (%s)", link->c_str());
713 doQuery(qctx, link->c_str(), n ? n.get() : qctx.getNameID());
715 catch (std::exception& ex) {
716 if (exceptAttr.get())
717 exceptAttr->getValues().push_back(XMLToolingConfig::getConfig().getURLEncoder()->encode(ex.what()));
719 history.insert(*link);
722 m_log.debug("skipping previously queried attribute source (%s)", link->c_str());
727 else if (qctx.getInputAttributes()) {
728 // Have to loop over unindexed set.
729 const vector<Attribute*>* matches = qctx.getInputAttributes();
730 for (indirect_iterator<vector<Attribute*>::const_iterator> match = make_indirect_iterator(matches->begin());
731 match != make_indirect_iterator(matches->end()); ++match) {
732 if (source->first == match->getId()) {
733 const vector<string>& links = match->getSerializedValues();
734 for (vector<string>::const_iterator link = links.begin(); link != links.end(); ++link) {
735 if (history.count(*link) == 0) {
736 m_log.debug("issuing SAML query to (%s)", link->c_str());
738 doQuery(qctx, link->c_str(), n ? n.get() : qctx.getNameID());
740 catch (std::exception& ex) {
741 if (exceptAttr.get())
742 exceptAttr->getValues().push_back(XMLToolingConfig::getConfig().getURLEncoder()->encode(ex.what()));
744 history.insert(*link);
747 m_log.debug("skipping previously queried attribute source (%s)", link->c_str());
756 if (exceptAttr.get()) {
757 qctx.getResolvedAttributes().push_back(exceptAttr.get());
758 exceptAttr.release();