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,
82 const XMLCh* protocol,
84 const XMLCh* authncontext_class=NULL,
85 const XMLCh* authncontext_decl=NULL,
86 const vector<const opensaml::Assertion*>* tokens=NULL,
87 const multimap<string,shibsp::Attribute*>* attributes=NULL
89 auto_ptr<ChainingContext> chain(new ChainingContext());
90 for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i)
91 chain->m_contexts.push_back(
92 (*i)->createResolutionContext(application, issuer, protocol, nameid, authncontext_class, authncontext_decl, tokens, attributes)
94 return chain.release();
97 ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
98 auto_ptr<ChainingContext> chain(new ChainingContext());
99 for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i)
100 chain->m_contexts.push_back((*i)->createResolutionContext(application, session));
101 return chain.release();
104 void resolveAttributes(ResolutionContext& ctx) const;
106 void getAttributeIds(vector<string>& attributes) const {
107 for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i)
108 (*i)->getAttributeIds(attributes);
112 vector<AttributeResolver*> m_resolvers;
115 static const XMLCh _AttributeResolver[] = UNICODE_LITERAL_17(A,t,t,r,i,b,u,t,e,R,e,s,o,l,v,e,r);
116 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
118 AttributeResolver* SHIBSP_DLLLOCAL ChainingAttributeResolverFactory(const DOMElement* & e)
120 return new ChainingAttributeResolver(e);
124 ChainingAttributeResolver::ChainingAttributeResolver(const DOMElement* e)
126 SPConfig& conf = SPConfig::getConfig();
128 // Load up the chain of handlers.
129 e = e ? XMLHelper::getFirstChildElement(e, _AttributeResolver) : NULL;
131 auto_ptr_char type(e->getAttributeNS(NULL,_type));
132 if (type.get() && *(type.get())) {
134 m_resolvers.push_back(conf.AttributeResolverManager.newPlugin(type.get(),e));
136 catch (exception& ex) {
137 Category::getInstance(SHIBSP_LOGCAT".AttributeResolver").error(
138 "caught exception processing embedded AttributeResolver element: %s", ex.what()
142 e = XMLHelper::getNextSiblingElement(e, _AttributeResolver);
146 void ChainingAttributeResolver::resolveAttributes(ResolutionContext& ctx) const
148 ChainingContext& chain = dynamic_cast<ChainingContext&>(ctx);
149 vector<ResolutionContext*>::iterator ictx = chain.m_contexts.begin();
150 for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i, ++ictx) {
151 (*i)->resolveAttributes(*(*ictx));
152 chain.getResolvedAttributes().insert((*ictx)->getResolvedAttributes().begin(), (*ictx)->getResolvedAttributes().end());
153 (*ictx)->getResolvedAttributes().clear();
154 chain.getResolvedAssertions().insert(chain.getResolvedAssertions().end(), (*ictx)->getResolvedAssertions().begin(), (*ictx)->getResolvedAssertions().end());
155 (*ictx)->getResolvedAssertions().clear();