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 * AttributeValueStringFunctor.cpp
20 * A match function that matches the value of an attribute against the
25 #include "exceptions.h"
26 #include "attribute/Attribute.h"
27 #include "attribute/filtering/FilteringContext.h"
28 #include "attribute/filtering/FilterPolicyContext.h"
29 #include "attribute/filtering/MatchFunctor.h"
31 using namespace shibsp;
36 static const XMLCh attributeID[] = UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
37 static const XMLCh value[] = UNICODE_LITERAL_5(v,a,l,u,e);
38 static const XMLCh ignoreCase[] = UNICODE_LITERAL_10(i,g,n,o,r,e,C,a,s,e);
41 * A match function that matches the value of an attribute against the specified value.
43 class SHIBSP_DLLLOCAL AttributeValueStringFunctor : public MatchFunctor
45 xmltooling::auto_ptr_char m_attributeID;
48 bool hasValue(const FilteringContext& filterContext) const;
49 bool matches(const Attribute& attribute, size_t index) const;
52 AttributeValueStringFunctor(const DOMElement* e)
53 : m_value(e ? xmltooling::toUTF8(e->getAttributeNS(nullptr,value)) : nullptr), m_attributeID(e ? e->getAttributeNS(nullptr,attributeID) : nullptr) {
54 if (!m_value || !*m_value) {
56 throw ConfigurationException("AttributeValueString MatchFunctor requires non-empty value attribute.");
58 if (e && e->hasAttributeNS(nullptr,ignoreCase)) {
59 xmltooling::logging::Category::getInstance(SHIBSP_LOGCAT".AttributeFilter").warn(
60 "ignoreCase property ignored by AttributeValueString MatchFunctor in favor of attribute's caseSensitive property"
65 virtual ~AttributeValueStringFunctor() {
69 bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
70 if (!m_attributeID.get() || !*m_attributeID.get())
71 throw AttributeFilteringException("No attributeID specified.");
72 return hasValue(filterContext);
75 bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
76 if (!m_attributeID.get() || !*m_attributeID.get() || XMLString::equals(m_attributeID.get(), attribute.getId()))
77 return matches(attribute, index);
78 return hasValue(filterContext);
82 MatchFunctor* SHIBSP_DLLLOCAL AttributeValueStringFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
84 return new AttributeValueStringFunctor(p.second);
89 bool AttributeValueStringFunctor::hasValue(const FilteringContext& filterContext) const
92 pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
93 filterContext.getAttributes().equal_range(m_attributeID.get());
94 for (; attrs.first != attrs.second; ++attrs.first) {
95 count = attrs.first->second->valueCount();
96 for (size_t index = 0; index < count; ++index) {
97 if (matches(*(attrs.first->second), index))
104 bool AttributeValueStringFunctor::matches(const Attribute& attribute, size_t index) const
106 const char* val = attribute.getString(index);
109 if (attribute.isCaseSensitive())
110 return !strcmp(m_value, val);
112 #ifdef HAVE_STRCASECMP
113 return !strcasecmp(m_value, val);
115 return !stricmp(m_value, val);