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 #include <xmltooling/util/XMLHelper.h>
33 using namespace shibsp;
35 using xmltooling::XMLHelper;
39 static const XMLCh attributeID[] = UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
40 static const XMLCh value[] = UNICODE_LITERAL_5(v,a,l,u,e);
41 static const XMLCh ignoreCase[] = UNICODE_LITERAL_10(i,g,n,o,r,e,C,a,s,e);
44 * A match function that matches the value of an attribute against the specified value.
46 class SHIBSP_DLLLOCAL AttributeValueStringFunctor : public MatchFunctor
51 bool hasValue(const FilteringContext& filterContext) const;
52 bool matches(const Attribute& attribute, size_t index) const;
55 AttributeValueStringFunctor(const DOMElement* e)
56 : m_attributeID(XMLHelper::getAttrString(e, nullptr, attributeID)),
57 m_value(e ? xmltooling::toUTF8(e->getAttributeNS(nullptr,value)) : nullptr) {
58 if (!m_value || !*m_value) {
60 throw ConfigurationException("AttributeValueString MatchFunctor requires non-empty value attribute.");
62 if (e && e->hasAttributeNS(nullptr,ignoreCase)) {
63 xmltooling::logging::Category::getInstance(SHIBSP_LOGCAT".AttributeFilter").warn(
64 "ignoreCase property ignored by AttributeValueString MatchFunctor in favor of attribute's caseSensitive property"
69 virtual ~AttributeValueStringFunctor() {
73 bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
74 if (m_attributeID.empty())
75 throw AttributeFilteringException("No attributeID specified.");
76 return hasValue(filterContext);
79 bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
80 if (m_attributeID.empty() || m_attributeID == attribute.getId())
81 return matches(attribute, index);
82 return hasValue(filterContext);
86 MatchFunctor* SHIBSP_DLLLOCAL AttributeValueStringFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
88 return new AttributeValueStringFunctor(p.second);
93 bool AttributeValueStringFunctor::hasValue(const FilteringContext& filterContext) const
96 pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
97 filterContext.getAttributes().equal_range(m_attributeID);
98 for (; attrs.first != attrs.second; ++attrs.first) {
99 count = attrs.first->second->valueCount();
100 for (size_t index = 0; index < count; ++index) {
101 if (matches(*(attrs.first->second), index))
108 bool AttributeValueStringFunctor::matches(const Attribute& attribute, size_t index) const
110 const char* val = attribute.getString(index);
113 if (attribute.isCaseSensitive())
114 return !strcmp(m_value, val);
116 #ifdef HAVE_STRCASECMP
117 return !strcasecmp(m_value, val);
119 return !stricmp(m_value, val);