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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * WhitelistMetadataFilter.cpp
24 * Removes non-whitelisted entities from a metadata instance
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataFilter.h"
31 #include <xmltooling/logging.h>
32 #include <xmltooling/util/NDC.h>
34 using namespace opensaml::saml2md;
35 using namespace xmltooling::logging;
36 using namespace xmltooling;
42 class SAML_DLLLOCAL WhitelistMetadataFilter : public MetadataFilter
45 WhitelistMetadataFilter(const DOMElement* e);
46 ~WhitelistMetadataFilter() {}
48 const char* getId() const { return WHITELIST_METADATA_FILTER; }
49 void doFilter(XMLObject& xmlObject) const;
52 void doFilter(EntitiesDescriptor& entities) const;
54 bool found(const XMLCh* id) const {
57 return m_set.count(id)==1;
63 MetadataFilter* SAML_DLLLOCAL WhitelistMetadataFilterFactory(const DOMElement* const & e)
65 return new WhitelistMetadataFilter(e);
71 static const XMLCh Include[] = UNICODE_LITERAL_7(I,n,c,l,u,d,e);
73 WhitelistMetadataFilter::WhitelistMetadataFilter(const DOMElement* e)
75 e = XMLHelper::getFirstChildElement(e);
77 if (XMLString::equals(e->getLocalName(), Include) && e->hasChildNodes()) {
78 m_set.insert(e->getFirstChild()->getNodeValue());
80 e = XMLHelper::getNextSiblingElement(e);
84 void WhitelistMetadataFilter::doFilter(XMLObject& xmlObject) const
91 doFilter(dynamic_cast<EntitiesDescriptor&>(xmlObject));
98 EntityDescriptor& entity = dynamic_cast<EntityDescriptor&>(xmlObject);
99 if (!found(entity.getEntityID()))
100 throw MetadataFilterException("WhitelistMetadataFilter instructed to filter the root/only entity in the metadata.");
106 throw MetadataFilterException("WhitelistMetadataFilter was given an improper metadata instance to filter.");
109 void WhitelistMetadataFilter::doFilter(EntitiesDescriptor& entities) const
111 Category& log=Category::getInstance(SAML_LOGCAT".MetadataFilter.Whitelist");
113 VectorOf(EntityDescriptor) v=entities.getEntityDescriptors();
114 for (VectorOf(EntityDescriptor)::size_type i=0; i<v.size(); ) {
115 const XMLCh* id=v[i]->getEntityID();
117 auto_ptr_char id2(id);
118 log.info("filtering out non-whitelisted entity (%s)", id2.get());
119 v.erase(v.begin() + i);
126 const vector<EntitiesDescriptor*>& groups=const_cast<const EntitiesDescriptor&>(entities).getEntitiesDescriptors();
127 for (vector<EntitiesDescriptor*>::const_iterator j=groups.begin(); j!=groups.end(); j++)