2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * ChainingAttributeResolver.cpp
24 * Chains together multiple AttributeResolver plugins.
28 #include "Application.h"
29 #include "ServiceProvider.h"
30 #include "attribute/Attribute.h"
31 #include "attribute/resolver/AttributeResolver.h"
32 #include "attribute/resolver/ResolutionContext.h"
34 #include <saml/Assertion.h>
35 #include <xercesc/util/XMLUniDefs.hpp>
36 #include <xmltooling/util/XMLHelper.h>
38 using namespace shibsp;
39 using namespace opensaml::saml2;
40 using namespace opensaml::saml2md;
41 using namespace xmltooling;
46 struct SHIBSP_DLLLOCAL ChainingContext : public ResolutionContext
49 const Application& application,
50 const EntityDescriptor* issuer,
51 const XMLCh* protocol,
53 const XMLCh* authncontext_class,
54 const XMLCh* authncontext_decl,
55 const vector<const opensaml::Assertion*>* tokens,
56 const vector<shibsp::Attribute*>* attributes
57 ) : m_app(application), m_issuer(issuer), m_protocol(protocol), m_nameid(nameid), m_authclass(authncontext_class), m_authdecl(authncontext_decl), m_session(nullptr) {
59 m_tokens.assign(tokens->begin(), tokens->end());
61 m_attributes.assign(attributes->begin(), attributes->end());
64 ChainingContext(const Application& application, const Session& session) : m_app(application), m_session(&session) {
68 for_each(m_ownedAttributes.begin(), m_ownedAttributes.end(), xmltooling::cleanup<shibsp::Attribute>());
69 for_each(m_ownedAssertions.begin(), m_ownedAssertions.end(), xmltooling::cleanup<opensaml::Assertion>());
72 vector<shibsp::Attribute*>& getResolvedAttributes() {
73 return m_ownedAttributes;
75 vector<opensaml::Assertion*>& getResolvedAssertions() {
76 return m_ownedAssertions;
79 vector<shibsp::Attribute*> m_ownedAttributes;
80 vector<opensaml::Assertion*> m_ownedAssertions;
82 const Application& m_app;
83 const EntityDescriptor* m_issuer;
84 const XMLCh* m_protocol;
85 const NameID* m_nameid;
86 const XMLCh* m_authclass;
87 const XMLCh* m_authdecl;
88 vector<const opensaml::Assertion*> m_tokens;
89 vector<shibsp::Attribute*> m_attributes;
91 const Session* m_session;
94 class SHIBSP_DLLLOCAL ChainingAttributeResolver : public AttributeResolver
97 ChainingAttributeResolver(const DOMElement* e);
98 virtual ~ChainingAttributeResolver() {
99 for_each(m_resolvers.begin(), m_resolvers.end(), xmltooling::cleanup<AttributeResolver>());
108 ResolutionContext* createResolutionContext(
109 const Application& application,
110 const EntityDescriptor* issuer,
111 const XMLCh* protocol,
112 const NameID* nameid=nullptr,
113 const XMLCh* authncontext_class=nullptr,
114 const XMLCh* authncontext_decl=nullptr,
115 const vector<const opensaml::Assertion*>* tokens=nullptr,
116 const vector<shibsp::Attribute*>* attributes=nullptr
118 return new ChainingContext(application, issuer, protocol, nameid, authncontext_class, authncontext_decl, tokens, attributes);
121 ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
122 return new ChainingContext(application, session);
125 void resolveAttributes(ResolutionContext& ctx) const;
127 void getAttributeIds(vector<string>& attributes) const {
128 for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i) {
130 (*i)->getAttributeIds(attributes);
135 vector<AttributeResolver*> m_resolvers;
138 static const XMLCh _AttributeResolver[] = UNICODE_LITERAL_17(A,t,t,r,i,b,u,t,e,R,e,s,o,l,v,e,r);
139 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
141 SHIBSP_DLLLOCAL PluginManager<AttributeResolver,string,const DOMElement*>::Factory QueryResolverFactory;
142 SHIBSP_DLLLOCAL PluginManager<AttributeResolver,string,const DOMElement*>::Factory SimpleAggregationResolverFactory;
144 AttributeResolver* SHIBSP_DLLLOCAL ChainingResolverFactory(const DOMElement* const & e)
146 return new ChainingAttributeResolver(e);
150 void SHIBSP_API shibsp::registerAttributeResolvers()
152 SPConfig::getConfig().AttributeResolverManager.registerFactory(QUERY_ATTRIBUTE_RESOLVER, QueryResolverFactory);
153 SPConfig::getConfig().AttributeResolverManager.registerFactory(SIMPLEAGGREGATION_ATTRIBUTE_RESOLVER, SimpleAggregationResolverFactory);
154 SPConfig::getConfig().AttributeResolverManager.registerFactory(CHAINING_ATTRIBUTE_RESOLVER, ChainingResolverFactory);
157 ResolutionContext::ResolutionContext()
161 ResolutionContext::~ResolutionContext()
165 AttributeResolver::AttributeResolver()
169 AttributeResolver::~AttributeResolver()
173 ChainingAttributeResolver::ChainingAttributeResolver(const DOMElement* e)
175 SPConfig& conf = SPConfig::getConfig();
177 // Load up the chain of handlers.
178 e = XMLHelper::getFirstChildElement(e, _AttributeResolver);
180 string t(XMLHelper::getAttrString(e, nullptr, _type));
183 Category::getInstance(SHIBSP_LOGCAT".AttributeResolver.Chaining").info(
184 "building AttributeResolver of type (%s)...", t.c_str()
186 m_resolvers.push_back(conf.AttributeResolverManager.newPlugin(t.c_str(), e));
188 catch (exception& ex) {
189 Category::getInstance(SHIBSP_LOGCAT".AttributeResolver.Chaining").error(
190 "caught exception processing embedded AttributeResolver element: %s", ex.what()
194 e = XMLHelper::getNextSiblingElement(e, _AttributeResolver);
198 void ChainingAttributeResolver::resolveAttributes(ResolutionContext& ctx) const
200 ChainingContext& chain = dynamic_cast<ChainingContext&>(ctx);
201 for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i) {
203 auto_ptr<ResolutionContext> context(
205 (*i)->createResolutionContext(chain.m_app, *chain.m_session) :
206 (*i)->createResolutionContext(
207 chain.m_app, chain.m_issuer, chain.m_protocol, chain.m_nameid, chain.m_authclass, chain.m_authdecl, &chain.m_tokens, &chain.m_attributes
211 (*i)->resolveAttributes(*context.get());
213 chain.m_attributes.insert(chain.m_attributes.end(), context->getResolvedAttributes().begin(), context->getResolvedAttributes().end());
214 chain.m_ownedAttributes.insert(chain.m_ownedAttributes.end(), context->getResolvedAttributes().begin(), context->getResolvedAttributes().end());
215 context->getResolvedAttributes().clear();
217 chain.m_tokens.insert(chain.m_tokens.end(), context->getResolvedAssertions().begin(), context->getResolvedAssertions().end());
218 chain.m_ownedAssertions.insert(chain.m_ownedAssertions.end(), context->getResolvedAssertions().begin(), context->getResolvedAssertions().end());
219 context->getResolvedAssertions().clear();