2 * Copyright 2001-2006 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * WhitelistMetadataFilter.cpp
20 * Removes non-whitelisted entities from a metadata instance
24 #include "saml2/metadata/MetadataFilter.h"
26 #include <log4cpp/Category.hh>
27 #include <xmltooling/util/NDC.h>
29 using namespace opensaml::saml2md;
30 using namespace xmltooling;
31 using namespace log4cpp;
37 class SAML_DLLLOCAL WhitelistMetadataFilter : public MetadataFilter
40 WhitelistMetadataFilter(const DOMElement* e);
41 ~WhitelistMetadataFilter() {}
43 const char* getId() const { return WHITELIST_METADATA_FILTER; }
44 void doFilter(XMLObject& xmlObject) const;
47 void doFilter(EntitiesDescriptor& entities) const;
49 bool found(const XMLCh* id) const {
53 return m_set.count(id)==1;
55 auto_ptr_char id2(id);
56 return m_set.count(id2.get())==1;
67 MetadataFilter* SAML_DLLLOCAL WhitelistMetadataFilterFactory(const DOMElement* const & e)
69 return new WhitelistMetadataFilter(e);
75 static const XMLCh Include[] = UNICODE_LITERAL_7(I,n,c,l,u,d,e);
77 WhitelistMetadataFilter::WhitelistMetadataFilter(const DOMElement* e)
79 e = XMLHelper::getFirstChildElement(e);
81 if (XMLString::equals(e->getLocalName(), Include) && e->hasChildNodes()) {
83 m_set.insert(e->getFirstChild()->getNodeValue());
85 auto_ptr_char id(e->getFirstChild()->getNodeValue());
86 m_set.insert(id.get());
89 e = XMLHelper::getNextSiblingElement(e);
93 void WhitelistMetadataFilter::doFilter(XMLObject& xmlObject) const
100 doFilter(dynamic_cast<EntitiesDescriptor&>(xmlObject));
107 EntityDescriptor& entity = dynamic_cast<EntityDescriptor&>(xmlObject);
108 if (!found(entity.getEntityID()))
109 throw MetadataFilterException("WhitelistMetadataFilter instructed to filter the root/only entity in the metadata.");
115 throw MetadataFilterException("WhitelistMetadataFilter was given an improper metadata instance to filter.");
118 void WhitelistMetadataFilter::doFilter(EntitiesDescriptor& entities) const
120 Category& log=Category::getInstance(SAML_LOGCAT".Metadata");
122 VectorOf(EntityDescriptor) v=entities.getEntityDescriptors();
123 for (VectorOf(EntityDescriptor)::size_type i=0; i<v.size(); ) {
124 const XMLCh* id=v[i]->getEntityID();
126 auto_ptr_char id2(id);
127 log.info("filtering out non-whitelisted entity (%s)", id2.get());
128 v.erase(v.begin() + i);
135 const vector<EntitiesDescriptor*>& groups=const_cast<const EntitiesDescriptor&>(entities).getEntitiesDescriptors();
136 for (vector<EntitiesDescriptor*>::const_iterator j=groups.begin(); j!=groups.end(); j++)