From: Scott Cantor Date: Tue, 10 Apr 2012 15:22:52 +0000 (+0000) Subject: Handle exceptions during chain resolution X-Git-Tag: 2.5.0~141 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=shibboleth%2Fcpp-sp.git;a=commitdiff_plain;h=9c2e3847128bc6f5a949e02fc699977390892c7c Handle exceptions during chain resolution --- diff --git a/plugins/TemplateAttributeResolver.cpp b/plugins/TemplateAttributeResolver.cpp new file mode 100644 index 0000000..f809cd9 --- /dev/null +++ b/plugins/TemplateAttributeResolver.cpp @@ -0,0 +1,225 @@ +/** + * Licensed to the University Corporation for Advanced Internet + * Development, Inc. (UCAID) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + * + * UCAID licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the License. + */ + +/** + * TemplateAttributeResolver.cpp + * + * AttributeResolver plugin for composing input values. + */ + +#include "internal.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace shibsp; +using namespace xmltooling; +using namespace xercesc; +using namespace boost; +using namespace std; + +namespace shibsp { + + class SHIBSP_DLLLOCAL TemplateContext : public ResolutionContext + { + public: + TemplateContext(const vector* attributes) : m_inputAttributes(attributes) { + } + + ~TemplateContext() { + for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup()); + } + + const vector* getInputAttributes() const { + return m_inputAttributes; + } + vector& getResolvedAttributes() { + return m_attributes; + } + vector& getResolvedAssertions() { + return m_assertions; + } + + private: + const vector* m_inputAttributes; + vector m_attributes; + static vector m_assertions; // empty dummy + }; + + + class SHIBSP_DLLLOCAL TemplateAttributeResolver : public AttributeResolver + { + public: + TemplateAttributeResolver(const DOMElement* e); + virtual ~TemplateAttributeResolver() {} + + Lockable* lock() { + return this; + } + void unlock() { + } + + ResolutionContext* createResolutionContext( + const Application& application, + const opensaml::saml2md::EntityDescriptor* issuer, + const XMLCh* protocol, + const opensaml::saml2::NameID* nameid=nullptr, + const XMLCh* authncontext_class=nullptr, + const XMLCh* authncontext_decl=nullptr, + const vector* tokens=nullptr, + const vector* attributes=nullptr + ) const { + // Make sure new method gets run. + return createResolutionContext(application, nullptr, issuer, protocol, nameid, authncontext_class, authncontext_decl, tokens, attributes); + } + + ResolutionContext* createResolutionContext( + const Application& application, + const GenericRequest* request, + const opensaml::saml2md::EntityDescriptor* issuer, + const XMLCh* protocol, + const opensaml::saml2::NameID* nameid=nullptr, + const XMLCh* authncontext_class=nullptr, + const XMLCh* authncontext_decl=nullptr, + const vector* tokens=nullptr, + const vector* attributes=nullptr + ) const { + return new TemplateContext(attributes); + } + + ResolutionContext* createResolutionContext(const Application& application, const Session& session) const { + return new TemplateContext(&session.getAttributes()); + } + + void resolveAttributes(ResolutionContext& ctx) const; + + void getAttributeIds(vector& attributes) const { + attributes.push_back(m_dest.front()); + } + + private: + Category& m_log; + string m_template; + vector m_sources,m_dest; + }; + + static const XMLCh dest[] = UNICODE_LITERAL_4(d,e,s,t); + static const XMLCh _sources[] = UNICODE_LITERAL_7(s,o,u,r,c,e,s); + static const XMLCh Template[] = UNICODE_LITERAL_8(T,e,m,p,l,a,t,e); + + AttributeResolver* SHIBSP_DLLLOCAL TemplateAttributeResolverFactory(const DOMElement* const & e) + { + return new TemplateAttributeResolver(e); + } + +}; + +vector TemplateContext::m_assertions; + +TemplateAttributeResolver::TemplateAttributeResolver(const DOMElement* e) + : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver.Template")), + m_dest(1, XMLHelper::getAttrString(e, nullptr, dest)) +{ + if (m_dest.front().empty()) + throw ConfigurationException("Template AttributeResolver requires dest attribute."); + + string s(XMLHelper::getAttrString(e, nullptr, _sources)); + split(m_sources, s, is_space(), algorithm::token_compress_on); + if (m_sources.empty()) + throw ConfigurationException("Template AttributeResolver requires sources attribute."); + + e = e ? XMLHelper::getFirstChildElement(e, Template) : nullptr; + auto_ptr_char t(e ? e->getTextContent() : nullptr); + if (t.get()) { + m_template = t.get(); + trim(m_template); + } + if (m_template.empty()) + throw ConfigurationException("Template AttributeResolver requires