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 * AttributeValueRegexFunctor.cpp
24 * A match function that evaluates an attribute's value against the provided
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 #include <xercesc/util/regx/RegularExpression.hpp>
39 using namespace shibsp;
41 using xmltooling::XMLHelper;
45 static const XMLCh attributeID[] = UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
46 static const XMLCh options[] = UNICODE_LITERAL_7(o,p,t,i,o,n,s);
47 static const XMLCh regex[] = UNICODE_LITERAL_5(r,e,g,e,x);
50 * A match function that evaluates an attribute's value against the provided regular expression.
52 class SHIBSP_DLLLOCAL AttributeValueRegexFunctor : public MatchFunctor
55 RegularExpression* m_regex;
57 bool hasValue(const FilteringContext& filterContext) const;
58 bool matches(const Attribute& attribute, size_t index) const;
61 AttributeValueRegexFunctor(const DOMElement* e)
62 : m_attributeID(XMLHelper::getAttrString(e, nullptr, attributeID)), m_regex(nullptr) {
63 const XMLCh* r = e ? e->getAttributeNS(nullptr,regex) : nullptr;
65 throw ConfigurationException("AttributeValueRegex MatchFunctor requires non-empty regex attribute.");
67 m_regex = new RegularExpression(r, e->getAttributeNS(nullptr,options));
69 catch (XMLException& ex) {
70 xmltooling::auto_ptr_char temp(ex.getMessage());
71 throw ConfigurationException(temp.get());
75 bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
76 if (m_attributeID.empty())
77 throw AttributeFilteringException("No attributeID specified.");
78 return hasValue(filterContext);
81 bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
82 if (m_attributeID.empty() || m_attributeID == attribute.getId())
83 return matches(attribute, index);
84 return hasValue(filterContext);
88 MatchFunctor* SHIBSP_DLLLOCAL AttributeValueRegexFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
90 return new AttributeValueRegexFunctor(p.second);
95 bool AttributeValueRegexFunctor::hasValue(const FilteringContext& filterContext) const
98 pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
99 filterContext.getAttributes().equal_range(m_attributeID);
100 for (; attrs.first != attrs.second; ++attrs.first) {
101 count = attrs.first->second->valueCount();
102 for (size_t index = 0; index < count; ++index) {
103 if (matches(*(attrs.first->second), index))
110 bool AttributeValueRegexFunctor::matches(const Attribute& attribute, size_t index) const
112 const char* val = attribute.getString(index);
115 XMLCh* temp = xmltooling::fromUTF8(val);
116 bool ret = m_regex->matches(temp);