Change license header.
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / AuthenticationMethodRegexFunctor.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  * AuthenticationMethodRegexFunctor.cpp
23  * 
24  * A match function that evaluates to true if the user's authentication
25  * method matches the provided regular 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 user's authentication method matches the provided regular
43      * expression.
44      */
45     class SHIBSP_DLLLOCAL AuthenticationMethodRegexFunctor : public MatchFunctor
46     {
47         RegularExpression* m_regex;
48     public:
49         AuthenticationMethodRegexFunctor(const DOMElement* e) : m_regex(nullptr) {
50             const XMLCh* r = e ? e->getAttributeNS(nullptr,regex) : nullptr;
51             if (!r || !*r)
52                 throw ConfigurationException("AuthenticationMethodRegex MatchFunctor requires non-empty regex attribute.");
53             try {
54                 m_regex = 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 ~AuthenticationMethodRegexFunctor() {
63             delete m_regex;
64         }
65
66         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
67             return (m_regex->matches(filterContext.getAuthnContextClassRef()) || m_regex->matches(filterContext.getAuthnContextDeclRef()));
68         }
69
70         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
71             return (m_regex->matches(filterContext.getAuthnContextClassRef()) || m_regex->matches(filterContext.getAuthnContextDeclRef()));
72         }
73     };
74
75     MatchFunctor* SHIBSP_DLLLOCAL AuthenticationMethodRegexFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
76     {
77         return new AuthenticationMethodRegexFunctor(p.second);
78     }
79
80 };