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