Remove extra header
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / ChainingAttributeResolver.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  * ChainingAttributeResolver.cpp
23  *
24  * Chains together multiple AttributeResolver plugins.
25  */
26
27 #include "internal.h"
28 #include "Application.h"
29 #include "ServiceProvider.h"
30 #include "attribute/Attribute.h"
31 #include "attribute/resolver/AttributeResolver.h"
32 #include "attribute/resolver/ResolutionContext.h"
33
34 #include <boost/ptr_container/ptr_vector.hpp>
35 #include <xercesc/util/XMLUniDefs.hpp>
36 #include <saml/Assertion.h>
37 #include <xmltooling/util/XMLHelper.h>
38
39 using namespace shibsp;
40 using namespace opensaml::saml2;
41 using namespace opensaml::saml2md;
42 using namespace xmltooling;
43 using namespace boost;
44 using namespace std;
45
46 namespace shibsp {
47
48     struct SHIBSP_DLLLOCAL ChainingContext : public ResolutionContext
49     {
50         ChainingContext(
51             const Application& application,
52             const EntityDescriptor* issuer,
53             const XMLCh* protocol,
54             const NameID* nameid,
55             const XMLCh* authncontext_class,
56             const XMLCh* authncontext_decl,
57             const vector<const opensaml::Assertion*>* tokens,
58             const vector<shibsp::Attribute*>* attributes
59             ) : m_app(application), m_issuer(issuer), m_protocol(protocol), m_nameid(nameid), m_authclass(authncontext_class), m_authdecl(authncontext_decl), m_session(nullptr) {
60             if (tokens)
61                 m_tokens.assign(tokens->begin(), tokens->end());
62             if (attributes)
63                 m_attributes.assign(attributes->begin(), attributes->end());
64         }
65
66         ChainingContext(const Application& application, const Session& session) : m_app(application), m_session(&session) {
67         }
68
69         ~ChainingContext() {
70             for_each(m_ownedAttributes.begin(), m_ownedAttributes.end(), xmltooling::cleanup<shibsp::Attribute>());
71             for_each(m_ownedAssertions.begin(), m_ownedAssertions.end(), xmltooling::cleanup<opensaml::Assertion>());
72         }
73
74         vector<shibsp::Attribute*>& getResolvedAttributes() {
75             return m_ownedAttributes;
76         }
77         vector<opensaml::Assertion*>& getResolvedAssertions() {
78             return m_ownedAssertions;
79         }
80
81         vector<shibsp::Attribute*> m_ownedAttributes;
82         vector<opensaml::Assertion*> m_ownedAssertions;
83
84         const Application& m_app;
85         const EntityDescriptor* m_issuer;
86         const XMLCh* m_protocol;
87         const NameID* m_nameid;
88         const XMLCh* m_authclass;
89         const XMLCh* m_authdecl;
90         vector<const opensaml::Assertion*> m_tokens;
91         vector<shibsp::Attribute*> m_attributes;
92
93         const Session* m_session;
94     };
95
96     class SHIBSP_DLLLOCAL ChainingAttributeResolver : public AttributeResolver
97     {
98     public:
99         ChainingAttributeResolver(const DOMElement* e);
100         virtual ~ChainingAttributeResolver() {}
101
102         Lockable* lock() {
103             return this;
104         }
105         void unlock() {
106         }
107
108         ResolutionContext* createResolutionContext(
109             const Application& application,
110             const EntityDescriptor* issuer,
111             const XMLCh* protocol,
112             const NameID* nameid=nullptr,
113             const XMLCh* authncontext_class=nullptr,
114             const XMLCh* authncontext_decl=nullptr,
115             const vector<const opensaml::Assertion*>* tokens=nullptr,
116             const vector<shibsp::Attribute*>* attributes=nullptr
117             ) const {
118             return new ChainingContext(application, issuer, protocol, nameid, authncontext_class, authncontext_decl, tokens, attributes);
119         }
120
121         ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
122             return new ChainingContext(application, session);
123         }
124
125         void resolveAttributes(ResolutionContext& ctx) const;
126
127         void getAttributeIds(vector<string>& attributes) const {
128             for (ptr_vector<AttributeResolver>::iterator i = m_resolvers.begin(); i != m_resolvers.end(); ++i) {
129                 Locker locker(&(*i));
130                 i->getAttributeIds(attributes);
131             }
132         }
133
134     private:
135         mutable ptr_vector<AttributeResolver> m_resolvers;
136     };
137
138     static const XMLCh _AttributeResolver[] =   UNICODE_LITERAL_17(A,t,t,r,i,b,u,t,e,R,e,s,o,l,v,e,r);
139     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
140
141     SHIBSP_DLLLOCAL PluginManager<AttributeResolver,string,const DOMElement*>::Factory QueryResolverFactory;
142     SHIBSP_DLLLOCAL PluginManager<AttributeResolver,string,const DOMElement*>::Factory SimpleAggregationResolverFactory;
143
144     AttributeResolver* SHIBSP_DLLLOCAL ChainingResolverFactory(const DOMElement* const & e)
145     {
146         return new ChainingAttributeResolver(e);
147     }
148 };
149
150 void SHIBSP_API shibsp::registerAttributeResolvers()
151 {
152     SPConfig::getConfig().AttributeResolverManager.registerFactory(QUERY_ATTRIBUTE_RESOLVER, QueryResolverFactory);
153     SPConfig::getConfig().AttributeResolverManager.registerFactory(SIMPLEAGGREGATION_ATTRIBUTE_RESOLVER, SimpleAggregationResolverFactory);
154     SPConfig::getConfig().AttributeResolverManager.registerFactory(CHAINING_ATTRIBUTE_RESOLVER, ChainingResolverFactory);
155 }
156
157 ResolutionContext::ResolutionContext()
158 {
159 }
160
161 ResolutionContext::~ResolutionContext()
162 {
163 }
164
165 AttributeResolver::AttributeResolver()
166 {
167 }
168
169 AttributeResolver::~AttributeResolver()
170 {
171 }
172
173 ChainingAttributeResolver::ChainingAttributeResolver(const DOMElement* e)
174 {
175     SPConfig& conf = SPConfig::getConfig();
176
177     // Load up the chain of handlers.
178     e = XMLHelper::getFirstChildElement(e, _AttributeResolver);
179     while (e) {
180         string t(XMLHelper::getAttrString(e, nullptr, _type));
181         if (!t.empty()) {
182             try {
183                 Category::getInstance(SHIBSP_LOGCAT".AttributeResolver.Chaining").info(
184                     "building AttributeResolver of type (%s)...", t.c_str()
185                     );
186                 auto_ptr<AttributeResolver> np(conf.AttributeResolverManager.newPlugin(t.c_str(), e));
187                 m_resolvers.push_back(np.get());
188                 np.release();
189             }
190             catch (exception& ex) {
191                 Category::getInstance(SHIBSP_LOGCAT".AttributeResolver.Chaining").error(
192                     "caught exception processing embedded AttributeResolver element: %s", ex.what()
193                     );
194             }
195         }
196         e = XMLHelper::getNextSiblingElement(e, _AttributeResolver);
197     }
198 }
199
200 void ChainingAttributeResolver::resolveAttributes(ResolutionContext& ctx) const
201 {
202     ChainingContext& chain = dynamic_cast<ChainingContext&>(ctx);
203     for (ptr_vector<AttributeResolver>::iterator i = m_resolvers.begin(); i != m_resolvers.end(); ++i) {
204         Locker locker(&(*i));
205         scoped_ptr<ResolutionContext> context(
206             chain.m_session ?
207                 i->createResolutionContext(chain.m_app, *chain.m_session) :
208                 i->createResolutionContext(
209                     chain.m_app, chain.m_issuer, chain.m_protocol, chain.m_nameid, chain.m_authclass, chain.m_authdecl, &chain.m_tokens, &chain.m_attributes
210                     )
211             );
212
213         i->resolveAttributes(*context);
214
215         chain.m_attributes.insert(chain.m_attributes.end(), context->getResolvedAttributes().begin(), context->getResolvedAttributes().end());
216         chain.m_ownedAttributes.insert(chain.m_ownedAttributes.end(), context->getResolvedAttributes().begin(), context->getResolvedAttributes().end());
217         context->getResolvedAttributes().clear();
218
219         chain.m_tokens.insert(chain.m_tokens.end(), context->getResolvedAssertions().begin(), context->getResolvedAssertions().end());
220         chain.m_ownedAssertions.insert(chain.m_ownedAssertions.end(), context->getResolvedAssertions().begin(), context->getResolvedAssertions().end());
221         context->getResolvedAssertions().clear();
222     }
223 }