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 * EntityAttributesEntityMatcher.cpp
24 * EntityMatcher that applies a set of input attributes.
28 #include "saml2/metadata/EntityMatcher.h"
29 #include "saml2/metadata/Metadata.h"
31 #include <boost/shared_ptr.hpp>
32 #include <boost/iterator/indirect_iterator.hpp>
33 #include <boost/lambda/bind.hpp>
34 #include <boost/lambda/casts.hpp>
35 #include <boost/lambda/lambda.hpp>
36 #include <xercesc/util/XMLUniDefs.hpp>
37 #include <xmltooling/util/XMLHelper.h>
39 using namespace opensaml::saml2md;
40 using namespace opensaml::saml2;
41 using namespace opensaml;
42 using namespace xmltooling;
43 using namespace boost::lambda;
44 using namespace boost;
49 class SAML_DLLLOCAL EntityAttributesEntityMatcher : public EntityMatcher
52 EntityAttributesEntityMatcher(const DOMElement* e);
53 ~EntityAttributesEntityMatcher() {}
55 bool matches(const EntityDescriptor& entity) const;
58 bool _matches(const EntityAttributes*, const Attribute*) const;
61 vector< boost::shared_ptr<Attribute> > m_tags;
64 EntityMatcher* SAML_DLLLOCAL EntityAttributesEntityMatcherFactory(const DOMElement* const & e)
66 return new EntityAttributesEntityMatcher(e);
69 static const XMLCh trimTags[] = UNICODE_LITERAL_8(t,r,i,m,T,a,g,s);
74 EntityAttributesEntityMatcher::EntityAttributesEntityMatcher(const DOMElement* e)
75 : m_trimTags(XMLHelper::getAttrBool(e, false, trimTags))
77 DOMElement* child = XMLHelper::getFirstChildElement(e, samlconstants::SAML20_NS, Attribute::LOCAL_NAME);
79 boost::shared_ptr<XMLObject> obj(AttributeBuilder::buildOneFromElement(child));
80 m_tags.push_back(boost::shared_dynamic_cast<Attribute>(obj));
81 child = XMLHelper::getNextSiblingElement(child, samlconstants::SAML20_NS, Attribute::LOCAL_NAME);
85 throw XMLToolingException("EntityAttributes EntityMatcher requires at least one saml2:Attribute to match.");
88 bool EntityAttributesEntityMatcher::matches(const EntityDescriptor& entity) const
90 // Check for a tag match in the EntityAttributes extension of the entity and its parent(s).
91 const Extensions* exts = entity.getExtensions();
93 const vector<XMLObject*>& children = exts->getUnknownXMLObjects();
94 const XMLObject* xo = find_if(children, ll_dynamic_cast<EntityAttributes*>(_1) != ((EntityAttributes*)nullptr));
96 // If we find a matching tag, we win. Each tag is treated in OR fashion.
97 if (find_if(m_tags.begin(), m_tags.end(),
98 lambda::bind(&EntityAttributesEntityMatcher::_matches, this, dynamic_cast<const EntityAttributes*>(xo),
99 lambda::bind(&boost::shared_ptr<Attribute>::get, _1))) != m_tags.end()) {
105 const EntitiesDescriptor* group = dynamic_cast<EntitiesDescriptor*>(entity.getParent());
107 exts = group->getExtensions();
109 const vector<XMLObject*>& children = exts->getUnknownXMLObjects();
110 const XMLObject* xo = find_if(children, ll_dynamic_cast<EntityAttributes*>(_1) != ((EntityAttributes*)nullptr));
112 // If we find a matching tag, we win. Each tag is treated in OR fashion.
113 if (find_if(m_tags.begin(), m_tags.end(),
114 lambda::bind(&EntityAttributesEntityMatcher::_matches, this, dynamic_cast<const EntityAttributes*>(xo),
115 lambda::bind(&boost::shared_ptr<Attribute>::get, _1))) != m_tags.end()) {
120 group = dynamic_cast<EntitiesDescriptor*>(group->getParent());
126 bool EntityAttributesEntityMatcher::_matches(const EntityAttributes* ea, const Attribute* tag) const
128 const vector<Attribute*>& attrs = ea->getAttributes();
129 const vector<XMLObject*>& tagvals = tag->getAttributeValues();
130 if (!attrs.empty() && !tagvals.empty()) {
131 // Track whether we've found every tag value.
132 vector<bool> flags(tagvals.size());
134 // Check each attribute/tag in the candidate.
135 for (indirect_iterator<vector<Attribute*>::const_iterator> a = make_indirect_iterator(attrs.begin());
136 a != make_indirect_iterator(attrs.end()); ++a) {
137 // Compare Name and NameFormat for a matching tag.
138 if (XMLString::equals(a->getName(), tag->getName()) &&
139 (!tag->getNameFormat() || XMLString::equals(tag->getNameFormat(), Attribute::UNSPECIFIED) ||
140 XMLString::equals(tag->getNameFormat(), a->getNameFormat()))) {
141 // Check each tag value's simple content for a match.
142 for (vector<XMLObject*>::size_type tagindex = 0; tagindex < tagvals.size(); ++tagindex) {
143 const XMLObject* tagval = tagvals[tagindex];
144 const XMLCh* tagvalstr = (tagval->getDOM()) ? tagval->getDOM()->getTextContent() : tagval->getTextContent();
145 const vector<XMLObject*>& cvals = const_cast<const Attribute&>(*a).getAttributeValues();
146 for (indirect_iterator<vector<XMLObject*>::const_iterator> cval = make_indirect_iterator(cvals.begin());
147 cval != make_indirect_iterator(cvals.end()); ++cval) {
148 const XMLCh* cvalstr = cval->getDOM() ? cval->getDOM()->getTextContent() : cval->getTextContent();
149 if (tagvalstr && cvalstr) {
150 if (XMLString::equals(tagvalstr, cvalstr)) {
151 flags[tagindex] = true;
154 else if (m_trimTags) {
155 XMLCh* dup = XMLString::replicate(cvalstr);
156 XMLString::trim(dup);
157 if (XMLString::equals(tagvalstr, dup)) {
158 XMLString::release(&dup);
159 flags[tagindex] = true;
162 XMLString::release(&dup);
170 if (find(flags.begin(), flags.end(), false) == flags.end())