First three string functors, added authn context to resolver/filter contexts.
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / ChainingAttributeResolver.cpp
1 /*
2  *  Copyright 2001-2007 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 <log4cpp/Category.hh>
31 #include <xercesc/util/XMLUniDefs.hpp>
32 #include <xmltooling/util/XMLHelper.h>
33
34 using namespace shibsp;
35 using namespace opensaml::saml2;
36 using namespace opensaml::saml2md;
37 using namespace xmltooling;
38 using namespace log4cpp;
39 using namespace std;
40
41 namespace shibsp {
42
43     struct SHIBSP_DLLLOCAL ChainingContext : public ResolutionContext
44     {
45         ~ChainingContext() {
46             for_each(m_contexts.begin(), m_contexts.end(), xmltooling::cleanup<ResolutionContext>());
47             for_each(m_attributes.begin(), m_attributes.end(), cleanup_pair<string,shibsp::Attribute>());
48             for_each(m_assertions.begin(), m_assertions.end(), xmltooling::cleanup<opensaml::Assertion>());
49         }
50
51         multimap<string,shibsp::Attribute*>& getResolvedAttributes() {
52             return m_attributes;
53         }
54         vector<opensaml::Assertion*>& getResolvedAssertions() {
55             return m_assertions;
56         }
57
58         vector<ResolutionContext*> m_contexts;
59         multimap<string,shibsp::Attribute*> m_attributes;
60         vector<opensaml::Assertion*> m_assertions;
61     };
62
63     class SHIBSP_DLLLOCAL ChainingAttributeResolver : public AttributeResolver
64     {
65     public:
66         ChainingAttributeResolver(const DOMElement* e);
67         virtual ~ChainingAttributeResolver() {
68             for_each(m_resolvers.begin(), m_resolvers.end(), xmltooling::cleanup<AttributeResolver>());
69         }
70         
71         Lockable* lock() {
72             for_each(m_resolvers.begin(), m_resolvers.end(), mem_fun(&AttributeResolver::lock));
73             return this;
74         }
75         void unlock() {
76             for_each(m_resolvers.begin(), m_resolvers.end(), mem_fun(&AttributeResolver::unlock));
77         }
78         
79         ResolutionContext* createResolutionContext(
80             const Application& application,
81             const EntityDescriptor* issuer,
82             const NameID* nameid,
83             const char* authncontext_class=NULL,
84             const char* authncontext_decl=NULL,
85             const vector<const opensaml::Assertion*>* tokens=NULL,
86             const multimap<string,shibsp::Attribute*>* attributes=NULL
87             ) const {
88             auto_ptr<ChainingContext> chain(new ChainingContext());
89             for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i)
90                 chain->m_contexts.push_back(
91                     (*i)->createResolutionContext(application, issuer, nameid, authncontext_class, authncontext_decl, tokens, attributes)
92                     );
93             return chain.release();
94         }
95
96         ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
97             auto_ptr<ChainingContext> chain(new ChainingContext());
98             for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i)
99                 chain->m_contexts.push_back((*i)->createResolutionContext(application, session));
100             return chain.release();
101         }
102
103         void resolveAttributes(ResolutionContext& ctx) const;
104
105     private:
106         vector<AttributeResolver*> m_resolvers;
107     };
108
109     static const XMLCh _AttributeResolver[] =   UNICODE_LITERAL_17(A,t,t,r,i,b,u,t,e,R,e,s,o,l,v,e,r);
110     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
111
112     AttributeResolver* SHIBSP_DLLLOCAL ChainingAttributeResolverFactory(const DOMElement* & e)
113     {
114         return new ChainingAttributeResolver(e);
115     }
116 };
117
118 ChainingAttributeResolver::ChainingAttributeResolver(const DOMElement* e)
119 {
120     SPConfig& conf = SPConfig::getConfig();
121
122     // Load up the chain of handlers.
123     e = e ? XMLHelper::getFirstChildElement(e, _AttributeResolver) : NULL;
124     while (e) {
125         auto_ptr_char type(e->getAttributeNS(NULL,_type));
126         if (type.get() && *(type.get())) {
127             try {
128                 m_resolvers.push_back(conf.AttributeResolverManager.newPlugin(type.get(),e));
129             }
130             catch (exception& ex) {
131                 Category::getInstance(SHIBSP_LOGCAT".AttributeResolver").error(
132                     "caught exception processing embedded AttributeResolver element: %s", ex.what()
133                     );
134             }
135         }
136         e = XMLHelper::getNextSiblingElement(e, _AttributeResolver);
137     }
138 }
139
140 void ChainingAttributeResolver::resolveAttributes(ResolutionContext& ctx) const
141 {
142     ChainingContext& chain = dynamic_cast<ChainingContext&>(ctx);
143     vector<ResolutionContext*>::iterator ictx = chain.m_contexts.begin();
144     for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i, ++ictx) {
145         (*i)->resolveAttributes(*(*ictx));
146         chain.getResolvedAttributes().insert((*ictx)->getResolvedAttributes().begin(), (*ictx)->getResolvedAttributes().end());
147         (*ictx)->getResolvedAttributes().clear();
148         chain.getResolvedAssertions().insert(chain.getResolvedAssertions().end(), (*ictx)->getResolvedAssertions().begin(), (*ictx)->getResolvedAssertions().end());
149         (*ictx)->getResolvedAssertions().clear();
150     }
151 }