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 * AttributeValueStringFunctor.cpp
24 * A match function that matches the value of an attribute against the
29 #include "exceptions.h"
30 #include "attribute/Attribute.h"
31 #include "attribute/filtering/FilteringContext.h"
32 #include "attribute/filtering/FilterPolicyContext.h"
33 #include "attribute/filtering/MatchFunctor.h"
35 #include <xmltooling/util/XMLHelper.h>
37 using namespace shibsp;
38 using namespace xmltooling;
43 static const XMLCh attributeID[] = UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
44 static const XMLCh value[] = UNICODE_LITERAL_5(v,a,l,u,e);
45 static const XMLCh ignoreCase[] = UNICODE_LITERAL_10(i,g,n,o,r,e,C,a,s,e);
48 * A match function that matches the value of an attribute against the specified value.
50 class SHIBSP_DLLLOCAL AttributeValueStringFunctor : public MatchFunctor
53 auto_arrayptr<char> m_value;
55 bool hasValue(const FilteringContext& filterContext) const;
56 bool matches(const Attribute& attribute, size_t index) const;
59 AttributeValueStringFunctor(const DOMElement* e)
60 : m_attributeID(XMLHelper::getAttrString(e, nullptr, attributeID)),
61 m_value(e ? toUTF8(e->getAttributeNS(nullptr, value)) : nullptr) {
62 if (!m_value.get() || !*m_value.get()) {
63 throw ConfigurationException("AttributeValueString MatchFunctor requires non-empty value attribute.");
65 if (e && e->hasAttributeNS(nullptr, ignoreCase)) {
66 Category::getInstance(SHIBSP_LOGCAT".AttributeFilter").warn(
67 "ignoreCase property ignored by AttributeValueString MatchFunctor in favor of attribute's caseSensitive property"
72 virtual ~AttributeValueStringFunctor() {}
74 bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
75 if (m_attributeID.empty())
76 throw AttributeFilteringException("No attributeID specified.");
77 return hasValue(filterContext);
80 bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
81 if (m_attributeID.empty() || m_attributeID == attribute.getId())
82 return matches(attribute, index);
83 return hasValue(filterContext);
87 MatchFunctor* SHIBSP_DLLLOCAL AttributeValueStringFactory(const pair<const FilterPolicyContext*,const DOMElement*>& p)
89 return new AttributeValueStringFunctor(p.second);
94 bool AttributeValueStringFunctor::hasValue(const FilteringContext& filterContext) const
97 pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
98 filterContext.getAttributes().equal_range(m_attributeID);
99 for (; attrs.first != attrs.second; ++attrs.first) {
100 count = attrs.first->second->valueCount();
101 for (size_t index = 0; index < count; ++index) {
102 if (matches(*(attrs.first->second), index))
109 bool AttributeValueStringFunctor::matches(const Attribute& attribute, size_t index) const
111 const char* val = attribute.getString(index);
114 if (attribute.isCaseSensitive())
115 return !strcmp(m_value.get(), val);
117 #ifdef HAVE_STRCASECMP
118 return !strcasecmp(m_value.get(), val);
120 return !stricmp(m_value.get(), val);