6982717cc7139461f26dd16283816808bad33f13
[shibboleth/cpp-sp.git] / shibsp / attribute / filtering / impl / AttributeValueRegexFunctor.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * AttributeValueRegexFunctor.cpp
23  * 
24  * A match function that evaluates an attribute's value against the provided
25  * regular expression.
26  */
27
28 #include "internal.h"
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"
34
35 #include <xmltooling/util/XMLHelper.h>
36
37 #include <xercesc/util/regx/RegularExpression.hpp>
38
39 using namespace shibsp;
40 using namespace std;
41 using xmltooling::XMLHelper;
42
43 namespace shibsp {
44
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);
48
49     /**
50      * A match function that evaluates an attribute's value against the provided regular expression.
51      */
52     class SHIBSP_DLLLOCAL AttributeValueRegexFunctor : public MatchFunctor
53     {
54         string m_attributeID;
55         RegularExpression* m_regex;
56
57         bool hasValue(const FilteringContext& filterContext) const;
58         bool matches(const Attribute& attribute, size_t index) const;
59
60     public:
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;
64             if (!r || !*r)
65                 throw ConfigurationException("AttributeValueRegex MatchFunctor requires non-empty regex attribute.");
66             try {
67                 m_regex = new RegularExpression(r, e->getAttributeNS(nullptr,options));
68             }
69             catch (XMLException& ex) {
70                 xmltooling::auto_ptr_char temp(ex.getMessage());
71                 throw ConfigurationException(temp.get());
72             }
73         }
74
75         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
76             if (m_attributeID.empty())
77                 throw AttributeFilteringException("No attributeID specified.");
78             return hasValue(filterContext);
79         }
80
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);
85         }
86     };
87
88     MatchFunctor* SHIBSP_DLLLOCAL AttributeValueRegexFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
89     {
90         return new AttributeValueRegexFunctor(p.second);
91     }
92
93 };
94
95 bool AttributeValueRegexFunctor::hasValue(const FilteringContext& filterContext) const
96 {
97     size_t count;
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))
104                 return true;
105         }
106     }
107     return false;
108 }
109
110 bool AttributeValueRegexFunctor::matches(const Attribute& attribute, size_t index) const
111 {
112     const char* val = attribute.getString(index);
113     if (!val)
114         return false;
115     XMLCh* temp = xmltooling::fromUTF8(val);
116     bool ret = m_regex->matches(temp);
117     delete[] temp;
118     return ret;
119 }