850e7531af057890f93fb2504891889355033f26
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / ChainingAttributeExtractor.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  * ChainingAttributeExtractor.cpp
23  *
24  * Chains together multiple AttributeExtractor plugins.
25  */
26
27 #include "internal.h"
28 #include "Application.h"
29 #include "ServiceProvider.h"
30 #include "attribute/Attribute.h"
31 #include "attribute/resolver/AttributeExtractor.h"
32
33 #include <boost/ptr_container/ptr_vector.hpp>
34 #include <xercesc/util/XMLUniDefs.hpp>
35 #include <xmltooling/util/XMLHelper.h>
36
37 using namespace shibsp;
38 using namespace opensaml::saml2md;
39 using namespace xmltooling;
40 using namespace boost;
41 using namespace std;
42
43 namespace shibsp {
44
45     class SHIBSP_DLLLOCAL ChainingAttributeExtractor : public AttributeExtractor
46     {
47     public:
48         ChainingAttributeExtractor(const DOMElement* e);
49         virtual ~ChainingAttributeExtractor() {}
50
51         Lockable* lock() {
52             return this;
53         }
54         void unlock() {
55         }
56
57         void extractAttributes(
58             const Application& application,
59             const RoleDescriptor* issuer,
60             const XMLObject& xmlObject,
61             vector<Attribute*>& attributes
62             ) const {
63             // Make sure new version gets run.
64             extractAttributes(application, nullptr, issuer, xmlObject, attributes);
65         }
66
67         void extractAttributes(
68             const Application& application,
69             const GenericRequest* request,
70             const RoleDescriptor* issuer,
71             const XMLObject& xmlObject,
72             vector<Attribute*>& attributes
73             ) const {
74             for (ptr_vector<AttributeExtractor>::iterator i = m_extractors.begin(); i != m_extractors.end(); ++i) {
75                 Locker locker(&(*i));
76                 i->extractAttributes(application, request, issuer, xmlObject, attributes);
77             }
78         }
79
80         void getAttributeIds(vector<string>& attributes) const {
81             for (ptr_vector<AttributeExtractor>::iterator i = m_extractors.begin(); i != m_extractors.end(); ++i) {
82                 Locker locker(&(*i));
83                 i->getAttributeIds(attributes);
84             }
85         }
86
87         void generateMetadata(SPSSODescriptor& role) const {
88             for (ptr_vector<AttributeExtractor>::iterator i = m_extractors.begin(); i != m_extractors.end(); ++i) {
89                 Locker locker(&(*i));
90                 i->generateMetadata(role);
91             }
92         }
93
94     private:
95         mutable ptr_vector<AttributeExtractor> m_extractors;
96     };
97
98     static const XMLCh _AttributeExtractor[] =  UNICODE_LITERAL_18(A,t,t,r,i,b,u,t,e,E,x,t,r,a,c,t,o,r);
99     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
100
101     SHIBSP_DLLLOCAL PluginManager<AttributeExtractor,string,const DOMElement*>::Factory AssertionAttributeExtractorFactory;
102     SHIBSP_DLLLOCAL PluginManager<AttributeExtractor,string,const DOMElement*>::Factory MetadataAttributeExtractorFactory;
103     SHIBSP_DLLLOCAL PluginManager<AttributeExtractor,string,const DOMElement*>::Factory DelegationAttributeExtractorFactory;
104     SHIBSP_DLLLOCAL PluginManager<AttributeExtractor,string,const DOMElement*>::Factory KeyDescriptorAttributeExtractorFactory;
105     SHIBSP_DLLLOCAL PluginManager<AttributeExtractor,string,const DOMElement*>::Factory XMLAttributeExtractorFactory;
106     AttributeExtractor* SHIBSP_DLLLOCAL ChainingExtractorFactory(const DOMElement* const & e)
107     {
108         return new ChainingAttributeExtractor(e);
109     }
110 };
111
112 void SHIBSP_API shibsp::registerAttributeExtractors()
113 {
114     SPConfig::getConfig().AttributeExtractorManager.registerFactory(ASSERTION_ATTRIBUTE_EXTRACTOR, AssertionAttributeExtractorFactory);
115     SPConfig::getConfig().AttributeExtractorManager.registerFactory(METADATA_ATTRIBUTE_EXTRACTOR, MetadataAttributeExtractorFactory);
116     SPConfig::getConfig().AttributeExtractorManager.registerFactory(DELEGATION_ATTRIBUTE_EXTRACTOR, DelegationAttributeExtractorFactory);
117     SPConfig::getConfig().AttributeExtractorManager.registerFactory(KEYDESCRIPTOR_ATTRIBUTE_EXTRACTOR, KeyDescriptorAttributeExtractorFactory);
118     SPConfig::getConfig().AttributeExtractorManager.registerFactory(XML_ATTRIBUTE_EXTRACTOR, XMLAttributeExtractorFactory);
119     SPConfig::getConfig().AttributeExtractorManager.registerFactory(CHAINING_ATTRIBUTE_EXTRACTOR, ChainingExtractorFactory);
120 }
121
122 AttributeExtractor::AttributeExtractor()
123 {
124 }
125
126 AttributeExtractor::~AttributeExtractor()
127 {
128 }
129
130 void AttributeExtractor::generateMetadata(SPSSODescriptor& role) const
131 {
132 }
133
134 void AttributeExtractor::extractAttributes(
135     const Application& application,
136     const GenericRequest* request,
137     const RoleDescriptor* issuer,
138     const XMLObject& xmlObject,
139     vector<Attribute*>& attributes
140     ) const
141 {
142     // Default call into deprecated method.
143     extractAttributes(application, issuer, xmlObject, attributes);
144 }
145
146 void AttributeExtractor::extractAttributes(
147     const Application& application,
148     const RoleDescriptor* issuer,
149     const XMLObject& xmlObject,
150     vector<Attribute*>& attributes
151     ) const
152 {
153     // Empty default for deprecated method.
154 }
155
156 ChainingAttributeExtractor::ChainingAttributeExtractor(const DOMElement* e)
157 {
158     SPConfig& conf = SPConfig::getConfig();
159
160     // Load up the chain of handlers.
161     e = XMLHelper::getFirstChildElement(e, _AttributeExtractor);
162     while (e) {
163         string t(XMLHelper::getAttrString(e, nullptr, _type));
164         if (!t.empty()) {
165             try {
166                 Category::getInstance(SHIBSP_LOGCAT".AttributeExtractor.Chaining").info(
167                     "building AttributeExtractor of type (%s)...", t.c_str()
168                     );
169                 auto_ptr<AttributeExtractor> np(conf.AttributeExtractorManager.newPlugin(t.c_str(), e));
170                 m_extractors.push_back(np.get());
171                 np.release();
172             }
173             catch (exception& ex) {
174                 Category::getInstance(SHIBSP_LOGCAT".AttributeExtractor.Chaining").error(
175                     "caught exception processing embedded AttributeExtractor element: %s", ex.what()
176                     );
177             }
178         }
179         e = XMLHelper::getNextSiblingElement(e, _AttributeExtractor);
180     }
181 }