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