/opensaml-2.4.3/saml/saml2/metadata/impl/BlacklistMetadataFilter.cpp

# · C++ · 142 lines · 97 code · 21 blank · 24 comment · 12 complexity · af037638b88ea40779a18ce77207d9e7 MD5 · raw file

  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. * BlacklistMetadataFilter.cpp
  22. *
  23. * Removes blacklisted entities from a metadata instance
  24. */
  25. #include "internal.h"
  26. #include "saml2/metadata/Metadata.h"
  27. #include "saml2/metadata/MetadataFilter.h"
  28. #include <xmltooling/logging.h>
  29. #include <xmltooling/util/NDC.h>
  30. using namespace opensaml::saml2md;
  31. using namespace xmltooling::logging;
  32. using namespace xmltooling;
  33. using namespace std;
  34. namespace opensaml {
  35. namespace saml2md {
  36. class SAML_DLLLOCAL BlacklistMetadataFilter : public MetadataFilter
  37. {
  38. public:
  39. BlacklistMetadataFilter(const DOMElement* e);
  40. ~BlacklistMetadataFilter() {}
  41. const char* getId() const { return BLACKLIST_METADATA_FILTER; }
  42. void doFilter(XMLObject& xmlObject) const;
  43. private:
  44. void doFilter(EntitiesDescriptor& entities) const;
  45. bool found(const XMLCh* id) const {
  46. if (!id)
  47. return false;
  48. return m_set.count(id)==1;
  49. }
  50. set<xstring> m_set;
  51. };
  52. MetadataFilter* SAML_DLLLOCAL BlacklistMetadataFilterFactory(const DOMElement* const & e)
  53. {
  54. return new BlacklistMetadataFilter(e);
  55. }
  56. };
  57. };
  58. static const XMLCh Exclude[] = UNICODE_LITERAL_7(E,x,c,l,u,d,e);
  59. BlacklistMetadataFilter::BlacklistMetadataFilter(const DOMElement* e)
  60. {
  61. e = XMLHelper::getFirstChildElement(e);
  62. while (e) {
  63. if (XMLString::equals(e->getLocalName(), Exclude) && e->hasChildNodes()) {
  64. m_set.insert(e->getFirstChild()->getNodeValue());
  65. }
  66. e = XMLHelper::getNextSiblingElement(e);
  67. }
  68. }
  69. void BlacklistMetadataFilter::doFilter(XMLObject& xmlObject) const
  70. {
  71. #ifdef _DEBUG
  72. NDC ndc("doFilter");
  73. #endif
  74. try {
  75. EntitiesDescriptor& entities = dynamic_cast<EntitiesDescriptor&>(xmlObject);
  76. if (found(entities.getName()))
  77. throw MetadataFilterException("BlacklistMetadataFilter instructed to filter the root/only group in the metadata.");
  78. doFilter(entities);
  79. return;
  80. }
  81. catch (bad_cast) {
  82. }
  83. try {
  84. EntityDescriptor& entity = dynamic_cast<EntityDescriptor&>(xmlObject);
  85. if (found(entity.getEntityID()))
  86. throw MetadataFilterException("BlacklistMetadataFilter instructed to filter the root/only entity in the metadata.");
  87. return;
  88. }
  89. catch (bad_cast) {
  90. }
  91. throw MetadataFilterException("BlacklistMetadataFilter was given an improper metadata instance to filter.");
  92. }
  93. void BlacklistMetadataFilter::doFilter(EntitiesDescriptor& entities) const
  94. {
  95. Category& log=Category::getInstance(SAML_LOGCAT".MetadataFilter.Blacklist");
  96. VectorOf(EntityDescriptor) v=entities.getEntityDescriptors();
  97. for (VectorOf(EntityDescriptor)::size_type i=0; i<v.size(); ) {
  98. const XMLCh* id=v[i]->getEntityID();
  99. if (found(id)) {
  100. auto_ptr_char id2(id);
  101. log.info("filtering out blacklisted entity (%s)", id2.get());
  102. v.erase(v.begin() + i);
  103. }
  104. else {
  105. i++;
  106. }
  107. }
  108. VectorOf(EntitiesDescriptor) w=entities.getEntitiesDescriptors();
  109. for (VectorOf(EntitiesDescriptor)::size_type j=0; j<w.size(); ) {
  110. const XMLCh* name=w[j]->getName();
  111. if (found(name)) {
  112. auto_ptr_char name2(name);
  113. log.info("filtering out blacklisted group (%s)", name2.get());
  114. w.erase(w.begin() + j);
  115. }
  116. else {
  117. doFilter(*(w[j]));
  118. j++;
  119. }
  120. }
  121. }