Update ctors to use new attribute shortcuts.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / EntityRoleMetadataFilter.cpp
1 /*
2  *  Copyright 2009-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  * EntityRoleMetadataFilter.cpp
19  *
20  * Removes non-whitelisted roles from a metadata instance.
21  */
22
23 #include "internal.h"
24 #include "saml2/metadata/Metadata.h"
25 #include "saml2/metadata/MetadataFilter.h"
26
27 #include <xmltooling/logging.h>
28 #include <xmltooling/util/NDC.h>
29
30 using namespace opensaml::saml2md;
31 using namespace xmltooling::logging;
32 using namespace xmltooling;
33 using namespace std;
34
35 namespace opensaml {
36     namespace saml2md {
37
38         class SAML_DLLLOCAL EntityRoleMetadataFilter : public MetadataFilter
39         {
40         public:
41             EntityRoleMetadataFilter(const DOMElement* e);
42             ~EntityRoleMetadataFilter() {}
43
44             const char* getId() const { return ENTITYROLE_METADATA_FILTER; }
45             void doFilter(XMLObject& xmlObject) const;
46
47         private:
48             void doFilter(EntityDescriptor& entity) const;
49             void doFilter(EntitiesDescriptor& entities) const;
50
51             bool m_removeRolelessEntityDescriptors, m_removeEmptyEntitiesDescriptors;
52             set<xmltooling::QName> m_roles;
53             bool m_idp, m_sp, m_authn, m_attr, m_pdp, m_authnq, m_attrq, m_authzq;
54         };
55
56         MetadataFilter* SAML_DLLLOCAL EntityRoleMetadataFilterFactory(const DOMElement* const & e)
57         {
58             return new EntityRoleMetadataFilter(e);
59         }
60
61     };
62 };
63
64 static const XMLCh RetainedRole[] =                     UNICODE_LITERAL_12(R,e,t,a,i,n,e,d,R,o,l,e);
65 static const XMLCh removeRolelessEntityDescriptors[] =  UNICODE_LITERAL_31(r,e,m,o,v,e,R,o,l,e,l,e,s,s,E,n,t,i,t,y,D,e,s,c,r,i,p,t,o,r,s);
66 static const XMLCh removeEmptyEntitiesDescriptors[] =   UNICODE_LITERAL_30(r,e,m,o,v,e,E,m,p,t,y,E,n,t,i,t,i,e,s,D,e,s,c,r,i,p,t,o,r,s);
67
68 EntityRoleMetadataFilter::EntityRoleMetadataFilter(const DOMElement* e)
69     : m_removeRolelessEntityDescriptors(XMLHelper::getAttrBool(e, true, removeRolelessEntityDescriptors)),
70         m_removeEmptyEntitiesDescriptors(XMLHelper::getAttrBool(e, true, removeEmptyEntitiesDescriptors)),
71         m_idp(false), m_sp(false), m_authn(false), m_attr(false), m_pdp(false), m_authnq(false), m_attrq(false), m_authzq(false)
72 {
73     e = XMLHelper::getFirstChildElement(e, RetainedRole);
74     while (e) {
75         auto_ptr<xmltooling::QName> q(XMLHelper::getNodeValueAsQName(e));
76         if (q.get()) {
77             if (*q == IDPSSODescriptor::ELEMENT_QNAME)
78                 m_idp = true;
79             else if (*q == SPSSODescriptor::ELEMENT_QNAME)
80                 m_sp = true;
81             else if (*q == AuthnAuthorityDescriptor::ELEMENT_QNAME)
82                 m_authn = true;
83             else if (*q == AttributeAuthorityDescriptor::ELEMENT_QNAME)
84                 m_attr = true;
85             else if (*q == PDPDescriptor::ELEMENT_QNAME)
86                 m_pdp = true;
87             else if (*q == AuthnQueryDescriptorType::TYPE_QNAME)
88                 m_authnq = true;
89             else if (*q == AttributeQueryDescriptorType::TYPE_QNAME)
90                 m_attrq = true;
91             else if (*q == AuthzDecisionQueryDescriptorType::TYPE_QNAME)
92                 m_authzq = true;
93             else
94                 m_roles.insert(*q.get());
95         }
96         e = XMLHelper::getNextSiblingElement(e, RetainedRole);
97     }
98 }
99
100 void EntityRoleMetadataFilter::doFilter(XMLObject& xmlObject) const
101 {
102 #ifdef _DEBUG
103     NDC ndc("doFilter");
104 #endif
105
106     try {
107         doFilter(dynamic_cast<EntitiesDescriptor&>(xmlObject));
108         return;
109     }
110     catch (bad_cast) {
111     }
112
113     try {
114         doFilter(dynamic_cast<EntityDescriptor&>(xmlObject));
115         return;
116     }
117     catch (bad_cast) {
118     }
119
120     throw MetadataFilterException("EntityRoleWhiteList MetadataFilter was given an improper metadata instance to filter.");
121 }
122
123 void EntityRoleMetadataFilter::doFilter(EntitiesDescriptor& entities) const
124 {
125     Category& log=Category::getInstance(SAML_LOGCAT".MetadataFilter.EntityRoleWhiteList");
126
127     VectorOf(EntityDescriptor) v=entities.getEntityDescriptors();
128     for (VectorOf(EntityDescriptor)::size_type i=0; i<v.size(); ) {
129         doFilter(*v[i]);
130         if (m_removeRolelessEntityDescriptors) {
131             const EntityDescriptor& e = const_cast<const EntityDescriptor&>(*v[i]);
132             if (e.getIDPSSODescriptors().empty() &&
133                     e.getSPSSODescriptors().empty() &&
134                     e.getAuthnAuthorityDescriptors().empty() &&
135                     e.getAttributeAuthorityDescriptors().empty() &&
136                     e.getPDPDescriptors().empty() &&
137                     e.getAuthnQueryDescriptorTypes().empty() &&
138                     e.getAttributeQueryDescriptorTypes().empty() &&
139                     e.getAuthzDecisionQueryDescriptorTypes().empty() &&
140                     e.getRoleDescriptors().empty()) {
141                 auto_ptr_char temp(e.getEntityID());
142                 log.debug("filtering out role-less entity (%s)", temp.get());
143                 v.erase(v.begin() + i);
144                 continue;
145             }
146         }
147         i++;
148     }
149
150     VectorOf(EntitiesDescriptor) groups=entities.getEntitiesDescriptors();
151     for (VectorOf(EntitiesDescriptor)::size_type j=0; j<groups.size(); ) {
152         EntitiesDescriptor* group = groups[j];
153         doFilter(*group);
154         if (m_removeEmptyEntitiesDescriptors && group->getEntitiesDescriptors().empty() && group->getEntityDescriptors().empty()) {
155             auto_ptr_char temp(entities.getName());
156             auto_ptr_char temp2(group->getName());
157             log.debug(
158                 "filtering out empty EntitiesDescriptor (%s) from EntitiesDescriptor (%s)",
159                 temp2.get() ? temp2.get() : "unnamed",
160                 temp.get() ? temp.get() : "unnamed"
161                 );
162             groups.erase(groups.begin() + j);
163         }
164         else {
165             j++;
166         }
167     }
168 }
169
170 void EntityRoleMetadataFilter::doFilter(EntityDescriptor& entity) const
171 {
172     if (!m_idp)
173         entity.getIDPSSODescriptors().clear();
174     if (!m_sp)
175         entity.getSPSSODescriptors().clear();
176     if (!m_authn)
177         entity.getAuthnAuthorityDescriptors().clear();
178     if (!m_attr)
179         entity.getAttributeAuthorityDescriptors().clear();
180     if (!m_pdp)
181         entity.getPDPDescriptors().clear();
182     if (!m_authnq)
183         entity.getAuthnQueryDescriptorTypes().clear();
184     if (!m_attrq)
185         entity.getAttributeQueryDescriptorTypes().clear();
186     if (!m_authzq)
187         entity.getAuthzDecisionQueryDescriptorTypes().clear();
188
189     VectorOf(RoleDescriptor) v = entity.getRoleDescriptors();
190     for (VectorOf(RoleDescriptor)::size_type i=0; i<v.size(); ) {
191         const xmltooling::QName* type = v[i]->getSchemaType();
192         if (!type || m_roles.find(*type) != m_roles.end())
193             v.erase(v.begin() + i);
194         else
195             i++;
196     }
197 }