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