Remove extra header
[shibboleth/cpp-sp.git] / shibsp / attribute / filtering / impl / AttributeIssuerRegexFunctor.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  * AttributeIssuerRegexFunctor.cpp
23  * 
24  * A match function that evaluates to true if the Attribute issuer matches the provided regular
25  * expression.
26  */
27
28 #include "internal.h"
29 #include "exceptions.h"
30 #include "attribute/filtering/FilteringContext.h"
31 #include "attribute/filtering/FilterPolicyContext.h"
32 #include "attribute/filtering/MatchFunctor.h"
33
34 #include <xercesc/util/regx/RegularExpression.hpp>
35
36 namespace shibsp {
37
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 to true if the Attribute issuer matches the provided regular
43      * expression.
44      */
45     class SHIBSP_DLLLOCAL AttributeIssuerRegexFunctor : public MatchFunctor
46     {
47         boost::scoped_ptr<RegularExpression> m_regex;
48     public:
49         AttributeIssuerRegexFunctor(const DOMElement* e) {
50             const XMLCh* r = e ? e->getAttributeNS(nullptr, regex) : nullptr;
51             if (!r || !*r)
52                 throw ConfigurationException("AttributeIssuerRegex MatchFunctor requires non-empty regex attribute.");
53             try {
54                 m_regex.reset(new RegularExpression(r, e->getAttributeNS(nullptr,options)));
55             }
56             catch (XMLException& ex) {
57                 xmltooling::auto_ptr_char temp(ex.getMessage());
58                 throw ConfigurationException(temp.get());
59             }
60         }
61
62         virtual ~AttributeIssuerRegexFunctor() {}
63
64         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
65             return m_regex->matches(filterContext.getAttributeIssuer());
66         }
67
68         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
69             return m_regex->matches(filterContext.getAttributeIssuer());
70         }
71     };
72
73     MatchFunctor* SHIBSP_DLLLOCAL AttributeIssuerRegexFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
74     {
75         return new AttributeIssuerRegexFunctor(p.second);
76     }
77
78 };