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