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