d49ff7ef4259d6a0900aaa71b6257061fefb5719
[shibboleth/cpp-sp.git] / shibsp / attribute / filtering / impl / AttributeValueRegexFunctor.cpp
1 /*
2  *  Copyright 2001-2010 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * AttributeValueRegexFunctor.cpp
19  * 
20  * A match function that evaluates an attribute's value against the provided
21  * regular expression.
22  */
23
24 #include "internal.h"
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"
30
31 #include <xercesc/util/regx/RegularExpression.hpp>
32
33 using namespace shibsp;
34 using namespace std;
35
36 namespace shibsp {
37
38     static const XMLCh attributeID[] =  UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
39     static const XMLCh options[] =  UNICODE_LITERAL_7(o,p,t,i,o,n,s);
40     static const XMLCh regex[] =    UNICODE_LITERAL_5(r,e,g,e,x);
41
42     /**
43      * A match function that evaluates an attribute's value against the provided regular expression.
44      */
45     class SHIBSP_DLLLOCAL AttributeValueRegexFunctor : public MatchFunctor
46     {
47         xmltooling::auto_ptr_char m_attributeID;
48         RegularExpression* m_regex;
49
50         bool hasValue(const FilteringContext& filterContext) const;
51         bool matches(const Attribute& attribute, size_t index) const;
52
53     public:
54         AttributeValueRegexFunctor(const DOMElement* e)
55                 : m_attributeID(e ? e->getAttributeNS(nullptr,attributeID) : nullptr) {
56             const XMLCh* r = e ? e->getAttributeNS(nullptr,regex) : nullptr;
57             if (!r || !*r)
58                 throw ConfigurationException("AttributeValueRegex MatchFunctor requires non-empty regex attribute.");
59             try {
60                 m_regex = new RegularExpression(r, e->getAttributeNS(nullptr,options));
61             }
62             catch (XMLException& ex) {
63                 xmltooling::auto_ptr_char temp(ex.getMessage());
64                 throw ConfigurationException(temp.get());
65             }
66         }
67
68         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
69             if (!m_attributeID.get() || !*m_attributeID.get())
70                 throw AttributeFilteringException("No attributeID specified.");
71             return hasValue(filterContext);
72         }
73
74         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
75             if (!m_attributeID.get() || !*m_attributeID.get() || XMLString::equals(m_attributeID.get(), attribute.getId()))
76                 return matches(attribute, index);
77             return hasValue(filterContext);
78         }
79     };
80
81     MatchFunctor* SHIBSP_DLLLOCAL AttributeValueRegexFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
82     {
83         return new AttributeValueRegexFunctor(p.second);
84     }
85
86 };
87
88 bool AttributeValueRegexFunctor::hasValue(const FilteringContext& filterContext) const
89 {
90     size_t count;
91     pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
92         filterContext.getAttributes().equal_range(m_attributeID.get());
93     for (; attrs.first != attrs.second; ++attrs.first) {
94         count = attrs.first->second->valueCount();
95         for (size_t index = 0; index < count; ++index) {
96             if (matches(*(attrs.first->second), index))
97                 return true;
98         }
99     }
100     return false;
101 }
102
103 bool AttributeValueRegexFunctor::matches(const Attribute& attribute, size_t index) const
104 {
105     const char* val = attribute.getString(index);
106     if (!val)
107         return false;
108     XMLCh* temp = xmltooling::fromUTF8(val);
109     bool ret = m_regex->matches(temp);
110     delete[] temp;
111     return ret;
112 }