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/scoped_ptr.hpp>
32 #include <boost/shared_ptr.hpp>
33 #include <boost/iterator/indirect_iterator.hpp>
34 #include <boost/lambda/bind.hpp>
35 #include <boost/lambda/casts.hpp>
36 #include <boost/lambda/lambda.hpp>
37 #include <xercesc/util/XMLUniDefs.hpp>
38 #include <xercesc/util/regx/RegularExpression.hpp>
39 #include <xmltooling/logging.h>
40 #include <xmltooling/util/XMLHelper.h>
42 using namespace opensaml::saml2md;
43 using namespace opensaml::saml2;
44 using namespace opensaml;
45 using namespace xmltooling::logging;
46 using namespace xmltooling;
47 using namespace boost::lambda;
48 using namespace boost;
53 class SAML_DLLLOCAL EntityAttributesEntityMatcher : public EntityMatcher
56 EntityAttributesEntityMatcher(const DOMElement* e);
57 ~EntityAttributesEntityMatcher() {}
59 bool matches(const EntityDescriptor& entity) const;
62 bool _matches(const EntityAttributes*, const Attribute*) const;
65 vector< boost::shared_ptr<Attribute> > m_tags;
69 EntityMatcher* SAML_DLLLOCAL EntityAttributesEntityMatcherFactory(const DOMElement* const & e)
71 return new EntityAttributesEntityMatcher(e);
74 static const XMLCh attributeName[] = UNICODE_LITERAL_13(a,t,t,r,i,b,u,t,e,N,a,m,e);
75 static const XMLCh attributeNameFormat[] = UNICODE_LITERAL_19(a,t,t,r,i,b,u,t,e,N,a,m,e,F,o,r,m,a,t);
76 static const XMLCh attributeValue[] = UNICODE_LITERAL_14(a,t,t,r,i,b,u,t,e,V,a,l,u,e);
77 static const XMLCh attributeValueRegex[] = UNICODE_LITERAL_19(a,t,t,r,i,b,u,t,e,V,a,l,u,e,R,e,g,e,x);
78 static const XMLCh regex[] = UNICODE_LITERAL_5(r,e,g,e,x);
79 static const XMLCh trimTags[] = UNICODE_LITERAL_8(t,r,i,m,T,a,g,s);
84 EntityAttributesEntityMatcher::EntityAttributesEntityMatcher(const DOMElement* e)
85 : m_trimTags(XMLHelper::getAttrBool(e, false, trimTags)),
86 m_log(Category::getInstance(SAML_LOGCAT".EntityMatcher.EntityAttributes"))
88 // Check for shorthand syntax.
89 if (e && e->hasAttributeNS(nullptr, attributeName) && (e->hasAttributeNS(nullptr, attributeValue) || e->hasAttributeNS(nullptr, attributeValueRegex))) {
90 boost::shared_ptr<Attribute> np(AttributeBuilder::buildAttribute());
91 np->setName(e->getAttributeNS(nullptr, attributeName));
92 np->setNameFormat(e->getAttributeNS(nullptr, attributeNameFormat));
93 auto_ptr<AttributeValue> nval(AttributeValueBuilder::buildAttributeValue());
94 if (e->hasAttributeNS(nullptr, attributeValue)) {
95 nval->setTextContent(e->getAttributeNS(nullptr, attributeValue));
98 nval->setTextContent(e->getAttributeNS(nullptr, attributeValueRegex));
99 // Use as a signal later that the value is a regex.
100 nval->setAttribute(xmltooling::QName(nullptr, regex), xmlconstants::XML_ONE);
102 np->getAttributeValues().push_back(nval.get());
104 m_tags.push_back(np);
107 DOMElement* child = XMLHelper::getFirstChildElement(e, samlconstants::SAML20_NS, Attribute::LOCAL_NAME);
109 boost::shared_ptr<XMLObject> obj(AttributeBuilder::buildOneFromElement(child));
110 m_tags.push_back(boost::shared_dynamic_cast<Attribute>(obj));
111 child = XMLHelper::getNextSiblingElement(child, samlconstants::SAML20_NS, Attribute::LOCAL_NAME);
115 throw XMLToolingException("EntityAttributes EntityMatcher requires at least one saml2:Attribute to match.");
118 bool EntityAttributesEntityMatcher::matches(const EntityDescriptor& entity) const
120 bool extFound = false;
122 // Check for a tag match in the EntityAttributes extension of the entity and its parent(s).
123 const Extensions* exts = entity.getExtensions();
125 const vector<XMLObject*>& children = exts->getUnknownXMLObjects();
126 const XMLObject* xo = find_if(children, ll_dynamic_cast<EntityAttributes*>(_1) != ((EntityAttributes*)nullptr));
129 // If we find a matching tag, we win. Each tag is treated in OR fashion.
130 if (find_if(m_tags.begin(), m_tags.end(),
131 lambda::bind(&EntityAttributesEntityMatcher::_matches, this, dynamic_cast<const EntityAttributes*>(xo),
132 lambda::bind(&boost::shared_ptr<Attribute>::get, _1))) != m_tags.end()) {
138 const EntitiesDescriptor* group = dynamic_cast<EntitiesDescriptor*>(entity.getParent());
140 exts = group->getExtensions();
142 const vector<XMLObject*>& children = exts->getUnknownXMLObjects();
143 const XMLObject* xo = find_if(children, ll_dynamic_cast<EntityAttributes*>(_1) != ((EntityAttributes*)nullptr));
146 // If we find a matching tag, we win. Each tag is treated in OR fashion.
147 if (find_if(m_tags.begin(), m_tags.end(),
148 lambda::bind(&EntityAttributesEntityMatcher::_matches, this, dynamic_cast<const EntityAttributes*>(xo),
149 lambda::bind(&boost::shared_ptr<Attribute>::get, _1))) != m_tags.end()) {
154 group = dynamic_cast<EntitiesDescriptor*>(group->getParent());
157 if (!extFound && m_log.isDebugEnabled()) {
158 auto_ptr_char id (entity.getEntityID());
159 m_log.debug("no EntityAttributes extension found for (%s)", id.get());
165 bool EntityAttributesEntityMatcher::_matches(const EntityAttributes* ea, const Attribute* tag) const
167 const vector<Attribute*>& attrs = ea->getAttributes();
168 const vector<XMLObject*>& tagvals = tag->getAttributeValues();
169 if (!attrs.empty() && !tagvals.empty()) {
170 // Track whether we've found every tag value.
171 vector<bool> flags(tagvals.size());
173 // Holds the active regex, if any.
174 scoped_ptr<RegularExpression> re;
175 xmltooling::QName regexQName(nullptr, regex);
177 // Check each attribute/tag in the candidate.
178 for (indirect_iterator<vector<Attribute*>::const_iterator> a = make_indirect_iterator(attrs.begin());
179 a != make_indirect_iterator(attrs.end()); ++a) {
180 // Compare Name and NameFormat for a matching tag.
181 if (XMLString::equals(a->getName(), tag->getName()) &&
182 (!tag->getNameFormat() || XMLString::equals(tag->getNameFormat(), Attribute::UNSPECIFIED) ||
183 XMLString::equals(tag->getNameFormat(), a->getNameFormat()))) {
185 // Check each tag value's simple content for a match.
186 for (vector<XMLObject*>::size_type tagindex = 0; tagindex < tagvals.size(); ++tagindex) {
187 const XMLObject* tagval = tagvals[tagindex];
188 const XMLCh* tagvalstr = (tagval->getDOM()) ? tagval->getDOM()->getTextContent() : tagval->getTextContent();
191 // Check for a regex flag.
192 if (dynamic_cast<const AttributeExtensibleXMLObject*>(tagval)) {
193 const XMLCh* reflag = dynamic_cast<const AttributeExtensibleXMLObject*>(tagval)->getAttribute(regexQName);
194 if (reflag && (*reflag == chDigit_1 || *reflag == chLatin_t)) {
196 re.reset(new RegularExpression(tagvalstr));
198 catch (XMLException& ex) {
199 auto_ptr_char msg(ex.getMessage());
200 m_log.error(msg.get());
205 const vector<XMLObject*>& cvals = const_cast<const Attribute&>(*a).getAttributeValues();
206 for (indirect_iterator<vector<XMLObject*>::const_iterator> cval = make_indirect_iterator(cvals.begin());
207 cval != make_indirect_iterator(cvals.end()); ++cval) {
208 const XMLCh* cvalstr = cval->getDOM() ? cval->getDOM()->getTextContent() : cval->getTextContent();
209 if (tagvalstr && cvalstr) {
212 if (re->matches(cvalstr)) {
213 flags[tagindex] = true;
217 catch (XMLException& ex) {
218 auto_ptr_char msg(ex.getMessage());
219 m_log.error(msg.get());
222 else if (XMLString::equals(tagvalstr, cvalstr)) {
223 flags[tagindex] = true;
226 else if (m_trimTags) {
227 XMLCh* dup = XMLString::replicate(cvalstr);
228 XMLString::trim(dup);
229 if (XMLString::equals(tagvalstr, dup)) {
230 XMLString::release(&dup);
231 flags[tagindex] = true;
234 XMLString::release(&dup);
242 if (find(flags.begin(), flags.end(), false) == flags.end())