CPPOST-96 - EntityMatcher implementation based on RPI registrationAuthority extension
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / NameEntityMatcher.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  * NameEntityMatcher.cpp
23  *
24  * EntityMatcher that matches based on name.
25  */
26
27 #include "internal.h"
28 #include "saml2/metadata/EntityMatcher.h"
29 #include "saml2/metadata/Metadata.h"
30
31 using namespace opensaml::saml2md;
32 using namespace opensaml;
33 using namespace xmltooling;
34 using namespace std;
35
36 namespace opensaml {
37     namespace saml2md {
38         class SAML_DLLLOCAL NameEntityMatcher : public EntityMatcher
39         {
40         public:
41             NameEntityMatcher(const DOMElement* e)
42                     : m_name(e ? e->getAttributeNS(nullptr, EntitiesDescriptor::NAME_ATTRIB_NAME) : nullptr) {
43                 if (!m_name || !*m_name)
44                     throw XMLToolingException("Name EntityMatcher missing required Name attribute.");
45             }
46             ~NameEntityMatcher() {}
47
48             bool matches(const EntityDescriptor& entity) const;
49
50         private:
51             const XMLCh* m_name;
52         };
53
54         EntityMatcher* SAML_DLLLOCAL NameEntityMatcherFactory(const DOMElement* const & e)
55         {
56             return new NameEntityMatcher(e);
57         }
58
59         SAML_DLLLOCAL PluginManager<EntityMatcher,string,const DOMElement*>::Factory EntityAttributesEntityMatcherFactory;
60         SAML_DLLLOCAL PluginManager<EntityMatcher,string,const DOMElement*>::Factory RegistrationAuthorityEntityMatcherFactory;
61     };
62 };
63
64 void SAML_API opensaml::saml2md::registerEntityMatchers()
65 {
66     SAMLConfig::getConfig().EntityMatcherManager.registerFactory(NAME_ENTITY_MATCHER, NameEntityMatcherFactory);
67     SAMLConfig::getConfig().EntityMatcherManager.registerFactory(ENTITYATTR_ENTITY_MATCHER, EntityAttributesEntityMatcherFactory);
68     SAMLConfig::getConfig().EntityMatcherManager.registerFactory(REGAUTH_ENTITY_MATCHER, RegistrationAuthorityEntityMatcherFactory);
69 }
70
71 EntityMatcher::EntityMatcher()
72 {
73 }
74
75 EntityMatcher::~EntityMatcher()
76 {
77 }
78
79 bool NameEntityMatcher::matches(const EntityDescriptor& entity) const
80 {
81     if (XMLString::equals(m_name, entity.getEntityID()))
82         return true;
83     const EntitiesDescriptor* group = dynamic_cast<EntitiesDescriptor*>(entity.getParent());
84     while (group) {
85         if (XMLString::equals(m_name, group->getName()))
86             return true;
87         group = dynamic_cast<EntitiesDescriptor*>(group->getParent());
88     }
89     return false;
90 }