VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / AttributeScopeMatchesShibMDScopeFunctor.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  * AttributeScopeMatchesShibMDScopeFunctor.cpp
19  * 
20  * A match function that ensures that an attributes value's scope matches
21  * a scope given in metadata for the entity or role.
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 #include "metadata/MetadataExt.h"
31
32 #include <saml/saml2/metadata/Metadata.h>
33 #include <xercesc/util/regx/RegularExpression.hpp>
34
35 using namespace opensaml::saml2md;
36 using namespace xmltooling;
37 using namespace std;
38
39 namespace shibsp {
40
41     static const XMLCh groupID[] = UNICODE_LITERAL_7(g,r,o,u,p,I,D);
42
43     /**
44      * A match function that ensures that an attributes value's scope matches a scope given in metadata for the entity or role.
45      */
46     class SHIBSP_DLLLOCAL AttributeScopeMatchesShibMDScopeFunctor : public MatchFunctor
47     {
48     public:
49         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
50             throw AttributeFilteringException("Metadata scope matching not usable as a PolicyRequirement.");
51         }
52
53         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
54             const RoleDescriptor* issuer = filterContext.getAttributeIssuerMetadata();
55             if (!issuer)
56                 return false;
57
58             const char* scope = attribute.getScope(index);
59             if (!scope || !*scope)
60                 return false;
61
62             const Scope* rule;
63             const XMLCh* widescope=nullptr;
64             const Extensions* ext = issuer->getExtensions();
65             if (ext) {
66                 const vector<XMLObject*>& exts = ext->getUnknownXMLObjects();
67                 for (vector<XMLObject*>::const_iterator e = exts.begin(); e!=exts.end(); ++e) {
68                     rule = dynamic_cast<const Scope*>(*e);
69                     if (rule) {
70                         if (!widescope)
71                             widescope = fromUTF8(scope);
72                         if (matches(*rule, widescope)) {
73                             delete[] widescope;
74                             return true;
75                         }
76                     }
77                 }
78             }
79
80             ext = dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getExtensions();
81             if (ext) {
82                 const vector<XMLObject*>& exts = ext->getUnknownXMLObjects();
83                 for (vector<XMLObject*>::const_iterator e = exts.begin(); e!=exts.end(); ++e) {
84                     rule = dynamic_cast<const Scope*>(*e);
85                     if (rule) {
86                         if (!widescope)
87                             widescope = fromUTF8(scope);
88                         if (matches(*rule, widescope)) {
89                             delete[] widescope;
90                             return true;
91                         }
92                     }
93                 }
94             }
95
96             delete[] widescope;
97             return false;
98         }
99
100     private:
101         bool matches(const Scope& rule, const XMLCh* scope) const {
102             const XMLCh* val = rule.getValue();
103             if (val && *val) {
104                 if (rule.Regexp()) {
105                     RegularExpression re(val);
106                     return re.matches(scope);
107                 }
108                 else {
109                     return XMLString::equals(val, scope);
110                 }
111             }
112             return false;
113         }
114     };
115
116     MatchFunctor* SHIBSP_DLLLOCAL AttributeScopeMatchesShibMDScopeFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
117     {
118         return new AttributeScopeMatchesShibMDScopeFunctor();
119     }
120
121 };