2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * ChainingAttributeResolver.cpp
20 * Chains together multiple AttributeResolver plugins.
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"
30 #include <log4cpp/Category.hh>
31 #include <xercesc/util/XMLUniDefs.hpp>
32 #include <xmltooling/util/XMLHelper.h>
34 using namespace shibsp;
35 using namespace opensaml::saml2;
36 using namespace opensaml::saml2md;
37 using namespace xmltooling;
38 using namespace log4cpp;
43 struct SHIBSP_DLLLOCAL ChainingContext : public ResolutionContext
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>());
51 multimap<string,shibsp::Attribute*>& getResolvedAttributes() {
54 vector<opensaml::Assertion*>& getResolvedAssertions() {
58 vector<ResolutionContext*> m_contexts;
59 multimap<string,shibsp::Attribute*> m_attributes;
60 vector<opensaml::Assertion*> m_assertions;
63 class SHIBSP_DLLLOCAL ChainingAttributeResolver : public AttributeResolver
66 ChainingAttributeResolver(const DOMElement* e);
67 virtual ~ChainingAttributeResolver() {
68 for_each(m_resolvers.begin(), m_resolvers.end(), xmltooling::cleanup<AttributeResolver>());
72 for_each(m_resolvers.begin(), m_resolvers.end(), mem_fun(&AttributeResolver::lock));
76 for_each(m_resolvers.begin(), m_resolvers.end(), mem_fun(&AttributeResolver::unlock));
79 ResolutionContext* createResolutionContext(
80 const Application& application,
81 const EntityDescriptor* issuer,
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
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)
93 return chain.release();
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();
103 void resolveAttributes(ResolutionContext& ctx) const;
105 void getAttributeIds(vector<string>& attributes) const {
106 for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i)
107 (*i)->getAttributeIds(attributes);
111 vector<AttributeResolver*> m_resolvers;
114 static const XMLCh _AttributeResolver[] = UNICODE_LITERAL_17(A,t,t,r,i,b,u,t,e,R,e,s,o,l,v,e,r);
115 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
117 AttributeResolver* SHIBSP_DLLLOCAL ChainingAttributeResolverFactory(const DOMElement* & e)
119 return new ChainingAttributeResolver(e);
123 ChainingAttributeResolver::ChainingAttributeResolver(const DOMElement* e)
125 SPConfig& conf = SPConfig::getConfig();
127 // Load up the chain of handlers.
128 e = e ? XMLHelper::getFirstChildElement(e, _AttributeResolver) : NULL;
130 auto_ptr_char type(e->getAttributeNS(NULL,_type));
131 if (type.get() && *(type.get())) {
133 m_resolvers.push_back(conf.AttributeResolverManager.newPlugin(type.get(),e));
135 catch (exception& ex) {
136 Category::getInstance(SHIBSP_LOGCAT".AttributeResolver").error(
137 "caught exception processing embedded AttributeResolver element: %s", ex.what()
141 e = XMLHelper::getNextSiblingElement(e, _AttributeResolver);
145 void ChainingAttributeResolver::resolveAttributes(ResolutionContext& ctx) const
147 ChainingContext& chain = dynamic_cast<ChainingContext&>(ctx);
148 vector<ResolutionContext*>::iterator ictx = chain.m_contexts.begin();
149 for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i, ++ictx) {
150 (*i)->resolveAttributes(*(*ictx));
151 chain.getResolvedAttributes().insert((*ictx)->getResolvedAttributes().begin(), (*ictx)->getResolvedAttributes().end());
152 (*ictx)->getResolvedAttributes().clear();
153 chain.getResolvedAssertions().insert(chain.getResolvedAssertions().end(), (*ictx)->getResolvedAssertions().begin(), (*ictx)->getResolvedAssertions().end());
154 (*ictx)->getResolvedAssertions().clear();