daaa6926986f4167bfb6036e758528a490a9444a
[shibboleth/cpp-sp.git] / shibsp / attribute / filtering / impl / NameIDQualifierStringFunctor.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  * NameIDQualifierStringFunctor.cpp
23  * 
24  * A match function that ensures that a NameID-valued attribute's qualifier(s)
25  * match particular values.
26  */
27
28 #include "internal.h"
29 #include "exceptions.h"
30 #include "attribute/NameIDAttribute.h"
31 #include "attribute/filtering/FilteringContext.h"
32 #include "attribute/filtering/FilterPolicyContext.h"
33 #include "attribute/filtering/MatchFunctor.h"
34
35 #include <saml/saml2/core/Assertions.h>
36 #include <xmltooling/util/XMLHelper.h>
37
38 using namespace shibsp;
39 using namespace xmltooling::logging;
40 using namespace xmltooling;
41 using namespace std;
42 using opensaml::saml2::NameID;
43
44 namespace shibsp {
45
46     static const XMLCh attributeID[] =  UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
47
48     /**
49      * A match function that ensures that a NameID-valued attribute's qualifier(s)
50      * match particular values.
51      */
52     class SHIBSP_DLLLOCAL NameIDQualifierStringFunctor : public MatchFunctor
53     {
54         string m_attributeID,m_matchNameQualifier,m_matchSPNameQualifier;
55
56         bool hasValue(const FilteringContext& filterContext) const;
57         bool matches(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const;
58
59     public:
60         NameIDQualifierStringFunctor(const DOMElement* e)
61             : m_attributeID(XMLHelper::getAttrString(e, nullptr, attributeID)),
62                 m_matchNameQualifier(XMLHelper::getAttrString(e, nullptr, NameID::NAMEQUALIFIER_ATTRIB_NAME)),
63                 m_matchSPNameQualifier(XMLHelper::getAttrString(e, nullptr, NameID::SPNAMEQUALIFIER_ATTRIB_NAME)) {
64         }
65
66         virtual ~NameIDQualifierStringFunctor() {
67         }
68
69         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
70             if (m_attributeID.empty())
71                 throw AttributeFilteringException("No attributeID specified.");
72             return hasValue(filterContext);
73         }
74
75         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
76             if (m_attributeID.empty() || m_attributeID == attribute.getId())
77                 return matches(filterContext, attribute, index);
78             return hasValue(filterContext);
79         }
80     };
81
82     MatchFunctor* SHIBSP_DLLLOCAL NameIDQualifierStringFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
83     {
84         return new NameIDQualifierStringFunctor(p.second);
85     }
86
87 };
88
89 bool NameIDQualifierStringFunctor::hasValue(const FilteringContext& filterContext) const
90 {
91     size_t count;
92     pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
93         filterContext.getAttributes().equal_range(m_attributeID);
94     for (; attrs.first != attrs.second; ++attrs.first) {
95         count = attrs.first->second->valueCount();
96         for (size_t index = 0; index < count; ++index) {
97             if (matches(filterContext, *(attrs.first->second), index))
98                 return true;
99         }
100     }
101     return false;
102 }
103
104 bool NameIDQualifierStringFunctor::matches(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const
105 {
106     const NameIDAttribute* nameattr = dynamic_cast<const NameIDAttribute*>(&attribute);
107     if (!nameattr) {
108         Category::getInstance(SHIBSP_LOGCAT".AttributeFilter").warn(
109             "NameIDQualifierString MatchFunctor applied to non-NameID-valued attribute (%s)", attribute.getId()
110             );
111         return false;
112     }
113
114     const NameIDAttribute::Value& val = nameattr->getValues()[index];
115     if (!val.m_NameQualifier.empty()) {
116         if (m_matchNameQualifier.empty()) {
117             auto_ptr_char issuer(filterContext.getAttributeIssuer());
118             if (issuer.get() && *issuer.get()) {
119                 if (val.m_NameQualifier != issuer.get()) {
120                     Category::getInstance(SHIBSP_LOGCAT".AttributeFilter").warn(
121                         "NameIDQualifierString MatchFunctor rejecting NameQualifier (%s), should be (%s)",
122                         val.m_NameQualifier.c_str(), issuer.get()
123                         );
124                     return false;
125                 }
126             }
127             else {
128                 Category::getInstance(SHIBSP_LOGCAT".AttributeFilter").warn(
129                     "NameIDQualifierString MatchFunctor rejecting NameQualifier (%s), attribute issuer unknown",
130                     val.m_NameQualifier.c_str()
131                     );
132                 return false;
133             }
134         }
135         else if (m_matchNameQualifier != val.m_NameQualifier) {
136             Category::getInstance(SHIBSP_LOGCAT".AttributeFilter").warn(
137                 "NameIDQualifierString MatchFunctor rejecting NameQualifier (%s), should be (%s)",
138                 val.m_NameQualifier.c_str(), m_matchNameQualifier.c_str()
139                 );
140             return false;
141         }
142     }
143     if (!val.m_SPNameQualifier.empty()) {
144         if (m_matchSPNameQualifier.empty()) {
145             auto_ptr_char req(filterContext.getAttributeRequester());
146             if (req.get() && *req.get()) {
147                 if (val.m_SPNameQualifier != req.get()) {
148                     Category::getInstance(SHIBSP_LOGCAT".AttributeFilter").warn(
149                         "NameIDQualifierString MatchFunctor rejecting SPNameQualifier (%s), should be (%s)",
150                         val.m_SPNameQualifier.c_str(), req.get()
151                         );
152                     return false;
153                 }
154             }
155             else {
156                 Category::getInstance(SHIBSP_LOGCAT".AttributeFilter").warn(
157                     "NameIDQualifierString MatchFunctor rejecting SPNameQualifier (%s), attribute requester unknown",
158                     val.m_SPNameQualifier.c_str()
159                     );
160                 return false;
161             }
162         }
163         else if (m_matchSPNameQualifier != val.m_SPNameQualifier) {
164             Category::getInstance(SHIBSP_LOGCAT".AttributeFilter").warn(
165                 "NameIDQualifierString MatchFunctor rejecting SPNameQualifier (%s), should be (%s)",
166                 val.m_SPNameQualifier.c_str(), m_matchSPNameQualifier.c_str()
167                 );
168             return false;
169         }
170     }
171
172     return true;
173 }