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