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 <xercesc/util/XMLUniDefs.hpp>
31 #include <xmltooling/util/XMLHelper.h>
33 using namespace shibsp;
34 using namespace opensaml::saml2;
35 using namespace opensaml::saml2md;
36 using namespace xmltooling;
41 struct SHIBSP_DLLLOCAL ChainingContext : public ResolutionContext
44 const Application& application,
45 const EntityDescriptor* issuer,
46 const XMLCh* protocol,
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_authclass(authncontext_class), m_authdecl(authncontext_decl), m_session(NULL) {
54 m_tokens.assign(tokens->begin(), tokens->end());
56 m_attributes.assign(attributes->begin(), attributes->end());
59 ChainingContext(const Application& application, const Session& session) : m_app(application), m_session(&session) {
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>());
67 vector<shibsp::Attribute*>& getResolvedAttributes() {
68 return m_ownedAttributes;
70 vector<opensaml::Assertion*>& getResolvedAssertions() {
71 return m_ownedAssertions;
74 vector<shibsp::Attribute*> m_ownedAttributes;
75 vector<opensaml::Assertion*> m_ownedAssertions;
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;
86 const Session* m_session;
89 class SHIBSP_DLLLOCAL ChainingAttributeResolver : public AttributeResolver
92 ChainingAttributeResolver(const DOMElement* e);
93 virtual ~ChainingAttributeResolver() {
94 for_each(m_resolvers.begin(), m_resolvers.end(), xmltooling::cleanup<AttributeResolver>());
103 ResolutionContext* createResolutionContext(
104 const Application& application,
105 const EntityDescriptor* issuer,
106 const XMLCh* protocol,
107 const NameID* nameid,
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
113 return new ChainingContext(application, issuer, protocol, nameid, authncontext_class, authncontext_decl, tokens, attributes);
116 ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
117 return new ChainingContext(application, session);
120 void resolveAttributes(ResolutionContext& ctx) const;
122 void getAttributeIds(vector<string>& attributes) const {
123 for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i) {
125 (*i)->getAttributeIds(attributes);
130 vector<AttributeResolver*> m_resolvers;
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);
136 SHIBSP_DLLLOCAL PluginManager<AttributeResolver,string,const DOMElement*>::Factory QueryResolverFactory;
137 AttributeResolver* SHIBSP_DLLLOCAL ChainingResolverFactory(const DOMElement* const & e)
139 return new ChainingAttributeResolver(e);
143 void SHIBSP_API shibsp::registerAttributeResolvers()
145 SPConfig::getConfig().AttributeResolverManager.registerFactory(QUERY_ATTRIBUTE_RESOLVER, QueryResolverFactory);
146 SPConfig::getConfig().AttributeResolverManager.registerFactory(CHAINING_ATTRIBUTE_RESOLVER, ChainingResolverFactory);
149 ChainingAttributeResolver::ChainingAttributeResolver(const DOMElement* e)
151 SPConfig& conf = SPConfig::getConfig();
153 // Load up the chain of handlers.
154 e = e ? XMLHelper::getFirstChildElement(e, _AttributeResolver) : NULL;
156 auto_ptr_char type(e->getAttributeNS(NULL,_type));
157 if (type.get() && *(type.get())) {
159 m_resolvers.push_back(conf.AttributeResolverManager.newPlugin(type.get(),e));
161 catch (exception& ex) {
162 Category::getInstance(SHIBSP_LOGCAT".AttributeResolver").error(
163 "caught exception processing embedded AttributeResolver element: %s", ex.what()
167 e = XMLHelper::getNextSiblingElement(e, _AttributeResolver);
171 void ChainingAttributeResolver::resolveAttributes(ResolutionContext& ctx) const
173 ChainingContext& chain = dynamic_cast<ChainingContext&>(ctx);
174 for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i) {
176 auto_ptr<ResolutionContext> context(
178 (*i)->createResolutionContext(chain.m_app, *chain.m_session) :
179 (*i)->createResolutionContext(
180 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 (*i)->resolveAttributes(*context.get());
186 chain.m_attributes.insert(chain.m_attributes.end(), context->getResolvedAttributes().begin(), context->getResolvedAttributes().end());
187 chain.m_ownedAttributes.insert(chain.m_attributes.end(), context->getResolvedAttributes().begin(), context->getResolvedAttributes().end());
188 context->getResolvedAttributes().clear();
190 chain.m_tokens.insert(chain.m_tokens.end(), context->getResolvedAssertions().begin(), context->getResolvedAssertions().end());
191 chain.m_ownedAssertions.insert(chain.m_ownedAssertions.end(), context->getResolvedAssertions().begin(), context->getResolvedAssertions().end());
192 context->getResolvedAssertions().clear();