ecb881cb80f6e174ab9d750f7c2c10a125d6ebe2
[shibboleth/cpp-sp.git] / shibsp / handler / impl / ChainingSessionInitiator.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * ChainingSessionInitiator.cpp
23  * 
24  * Chains together multiple SessionInitiator handlers in sequence.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "handler/AbstractHandler.h"
30 #include "handler/SessionInitiator.h"
31 #include "util/SPConstants.h"
32
33 #include <boost/bind.hpp>
34 #include <boost/ptr_container/ptr_vector.hpp>
35 #include <xercesc/util/XMLUniDefs.hpp>
36 #include <xmltooling/util/XMLHelper.h>
37
38 using namespace shibsp;
39 using namespace xmltooling;
40 using namespace boost;
41 using namespace std;
42
43 namespace shibsp {
44
45 #if defined (_MSC_VER)
46     #pragma warning( push )
47     #pragma warning( disable : 4250 )
48 #endif
49
50     class SHIBSP_DLLLOCAL ChainingSessionInitiator : public SessionInitiator, public AbstractHandler
51     {
52     public:
53         ChainingSessionInitiator(const DOMElement* e, const char* appId);
54         virtual ~ChainingSessionInitiator() {}
55         
56         pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
57
58 #ifndef SHIBSP_LITE
59         void generateMetadata(opensaml::saml2md::SPSSODescriptor& role, const char* handlerURL) const {
60             doGenerateMetadata(role, handlerURL);   // assumes all chains support the RequestInitiator protocol
61             for_each(m_handlers.begin(), m_handlers.end(), boost::bind(&SessionInitiator::generateMetadata, _1, boost::ref(role), handlerURL));
62         }
63 #endif
64
65     private:
66         ptr_vector<SessionInitiator> m_handlers;
67     };
68
69 #if defined (_MSC_VER)
70     #pragma warning( pop )
71 #endif
72
73     static const XMLCh _SessionInitiator[] =    UNICODE_LITERAL_16(S,e,s,s,i,o,n,I,n,i,t,i,a,t,o,r);
74     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
75
76     class SHIBSP_DLLLOCAL SessionInitiatorNodeFilter : public DOMNodeFilter
77     {
78     public:
79 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE
80         short
81 #else
82         FilterAction
83 #endif
84         acceptNode(const DOMNode* node) const {
85             if (XMLHelper::isNodeNamed(node,shibspconstants::SHIB2SPCONFIG_NS,_SessionInitiator))
86                 return FILTER_REJECT;
87             return FILTER_ACCEPT;
88         }
89     };
90
91     static SHIBSP_DLLLOCAL SessionInitiatorNodeFilter g_SINFilter;
92
93     SessionInitiator* SHIBSP_DLLLOCAL ChainingSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
94     {
95         return new ChainingSessionInitiator(p.first, p.second);
96     }
97 };
98
99 ChainingSessionInitiator::ChainingSessionInitiator(const DOMElement* e, const char* appId)
100     : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.Chaining"), &g_SINFilter)
101 {
102     SPConfig& conf = SPConfig::getConfig();
103
104     // Load up the chain of handlers.
105     e = e ? XMLHelper::getFirstChildElement(e, _SessionInitiator) : nullptr;
106     while (e) {
107         string t(XMLHelper::getAttrString(e, nullptr, _type));
108         if (!t.empty()) {
109             try {
110                 auto_ptr<SessionInitiator> np(conf.SessionInitiatorManager.newPlugin(t.c_str(), make_pair(e, appId)));
111                 m_handlers.push_back(np.get());
112                 np.release();
113                 m_handlers.back().setParent(this);
114             }
115             catch (std::exception& ex) {
116                 m_log.error("caught exception processing embedded SessionInitiator element: %s", ex.what());
117             }
118         }
119         e = XMLHelper::getNextSiblingElement(e, _SessionInitiator);
120     }
121
122     m_supportedOptions.insert("isPassive");
123 }
124
125 pair<bool,long> ChainingSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
126 {
127     if (!checkCompatibility(request, isHandler))
128         return make_pair(false, 0L);
129
130     pair<bool,long> ret;
131     for (ptr_vector<SessionInitiator>::const_iterator i = m_handlers.begin(); i != m_handlers.end(); ++i) {
132         ret = i->run(request, entityID, isHandler);
133         if (ret.first)
134             return ret;
135     }
136     throw ConfigurationException("None of the configured SessionInitiators handled the request.");
137 }