SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-sp.git] / shibsp / handler / impl / SessionInitiator.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  * SessionInitiator.cpp
23  * 
24  * Pluggable runtime functionality that handles initiating sessions.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "SPRequest.h"
30 #include "handler/SessionInitiator.h"
31
32 using namespace shibsp;
33 using namespace xmltooling;
34 using namespace std;
35
36 #ifndef SHIBSP_LITE
37 # include <saml/saml2/metadata/Metadata.h>
38 using namespace opensaml::saml2md;
39 #endif
40
41 namespace shibsp {
42     SHIBSP_DLLLOCAL PluginManager< SessionInitiator,string,pair<const DOMElement*,const char*> >::Factory ChainingSessionInitiatorFactory;
43     SHIBSP_DLLLOCAL PluginManager< SessionInitiator,string,pair<const DOMElement*,const char*> >::Factory Shib1SessionInitiatorFactory;
44     SHIBSP_DLLLOCAL PluginManager< SessionInitiator,string,pair<const DOMElement*,const char*> >::Factory SAML2SessionInitiatorFactory;
45     SHIBSP_DLLLOCAL PluginManager< SessionInitiator,string,pair<const DOMElement*,const char*> >::Factory WAYFSessionInitiatorFactory;
46     SHIBSP_DLLLOCAL PluginManager< SessionInitiator,string,pair<const DOMElement*,const char*> >::Factory SAMLDSSessionInitiatorFactory;
47     SHIBSP_DLLLOCAL PluginManager< SessionInitiator,string,pair<const DOMElement*,const char*> >::Factory TransformSessionInitiatorFactory;
48     SHIBSP_DLLLOCAL PluginManager< SessionInitiator,string,pair<const DOMElement*,const char*> >::Factory FormSessionInitiatorFactory;
49     SHIBSP_DLLLOCAL PluginManager< SessionInitiator,string,pair<const DOMElement*,const char*> >::Factory CookieSessionInitiatorFactory;
50 };
51
52 map<string,string> SessionInitiator::m_remapper;
53
54 void SHIBSP_API shibsp::registerSessionInitiators()
55 {
56     SPConfig& conf=SPConfig::getConfig();
57     conf.SessionInitiatorManager.registerFactory(CHAINING_SESSION_INITIATOR, ChainingSessionInitiatorFactory);
58     conf.SessionInitiatorManager.registerFactory(SHIB1_SESSION_INITIATOR, Shib1SessionInitiatorFactory);
59     conf.SessionInitiatorManager.registerFactory(SAML2_SESSION_INITIATOR, SAML2SessionInitiatorFactory);
60     conf.SessionInitiatorManager.registerFactory(WAYF_SESSION_INITIATOR, WAYFSessionInitiatorFactory);
61     conf.SessionInitiatorManager.registerFactory(SAMLDS_SESSION_INITIATOR, SAMLDSSessionInitiatorFactory);
62     conf.SessionInitiatorManager.registerFactory(TRANSFORM_SESSION_INITIATOR, TransformSessionInitiatorFactory);
63     conf.SessionInitiatorManager.registerFactory(FORM_SESSION_INITIATOR, FormSessionInitiatorFactory);
64     conf.SessionInitiatorManager.registerFactory(COOKIE_SESSION_INITIATOR, CookieSessionInitiatorFactory);
65
66     SessionInitiator::m_remapper["defaultACSIndex"] = "acsIndex";
67 }
68
69 SessionInitiator::SessionInitiator()
70 {
71 }
72
73 SessionInitiator::~SessionInitiator()
74 {
75 }
76
77 #ifndef SHIBSP_LITE
78 const char* SessionInitiator::getType() const
79 {
80     return "SessionInitiator";
81 }
82
83 void SessionInitiator::generateMetadata(SPSSODescriptor& role, const char* handlerURL) const
84 {
85     // In case any plugins were directly calling this before, we stub it out.
86 }
87
88 void SessionInitiator::doGenerateMetadata(SPSSODescriptor& role, const char* handlerURL) const
89 {
90     if (getParent())
91         return;
92     const char* loc = getString("Location").second;
93     string hurl(handlerURL);
94     if (*loc != '/')
95         hurl += '/';
96     hurl += loc;
97     auto_ptr_XMLCh widen(hurl.c_str());
98
99     RequestInitiator* ep = RequestInitiatorBuilder::buildRequestInitiator();
100     ep->setLocation(widen.get());
101     ep->setBinding(samlconstants::SP_REQUEST_INIT_NS);
102     Extensions* ext = role.getExtensions();
103     if (!ext) {
104         ext = ExtensionsBuilder::buildExtensions();
105         role.setExtensions(ext);
106     }
107     ext->getUnknownXMLObjects().push_back(ep);
108 }
109 #endif
110
111 const set<string>& SessionInitiator::getSupportedOptions() const
112 {
113     return m_supportedOptions;
114 }
115
116 bool SessionInitiator::checkCompatibility(SPRequest& request, bool isHandler) const
117 {
118     bool isPassive = false;
119     if (isHandler) {
120         const char* flag = request.getParameter("isPassive");
121         if (flag) {
122             isPassive = (*flag=='1' || *flag=='t');
123         }
124         else {
125             pair<bool,bool> flagprop = getBool("isPassive");
126             isPassive = (flagprop.first && flagprop.second);
127         }
128     }
129     else {
130         // It doesn't really make sense to use isPassive with automated sessions, but...
131         pair<bool,bool> flagprop = request.getRequestSettings().first->getBool("isPassive");
132         if (!flagprop.first)
133             flagprop = getBool("isPassive");
134         isPassive = (flagprop.first && flagprop.second);
135     }
136
137     // Check for support of isPassive if it's used.
138     if (isPassive && getSupportedOptions().count("isPassive") == 0) {
139         if (getParent()) {
140             log(SPRequest::SPInfo, "handler does not support isPassive option");
141             return false;
142         }
143         throw ConfigurationException("Unsupported option (isPassive) supplied to SessionInitiator.");
144     }
145
146     return true;
147 }
148
149 pair<bool,long> SessionInitiator::run(SPRequest& request, bool isHandler) const
150 {
151     cleanRelayState(request.getApplication(), request, request);
152
153     const char* entityID = nullptr;
154     pair<bool,const char*> param = getString("entityIDParam");
155     if (isHandler) {
156         entityID = request.getParameter(param.first ? param.second : "entityID");
157         if (!param.first && (!entityID || !*entityID))
158             entityID=request.getParameter("providerId");
159     }
160     if (!entityID || !*entityID) {
161         param = request.getRequestSettings().first->getString("entityID");
162         if (param.first)
163             entityID = param.second;
164     }
165     if (!entityID || !*entityID)
166         entityID = getString("entityID").second;
167
168     string copy(entityID ? entityID : "");
169
170     try {
171         return run(request, copy, isHandler);
172     }
173     catch (exception& ex) {
174         // If it's a handler operation, and isPassive is used or returnOnError is set, we trap the error.
175         if (isHandler) {
176             bool returnOnError = false;
177             const char* flag = request.getParameter("isPassive");
178             if (flag && (*flag == 't' || *flag == '1')) {
179                 returnOnError = true;
180             }
181             else {
182                 pair<bool,bool> flagprop = getBool("isPassive");
183                 if (flagprop.first && flagprop.second) {
184                     returnOnError = true;
185                 }
186                 else {
187                     flag = request.getParameter("returnOnError");
188                     if (flag) {
189                         returnOnError = (*flag=='1' || *flag=='t');
190                     }
191                     else {
192                         flagprop = getBool("returnOnError");
193                         returnOnError = (flagprop.first && flagprop.second);
194                     }
195                 }
196             }
197
198             if (returnOnError) {
199                 // Log it and attempt to recover relay state so we can get back.
200                 log(SPRequest::SPError, ex.what());
201                 log(SPRequest::SPInfo, "trapping SessionInitiator error condition and returning to target location");
202                 flag = request.getParameter("target");
203                 string target(flag ? flag : "");
204                 recoverRelayState(request.getApplication(), request, request, target, false);
205                 return make_pair(true, request.sendRedirect(target.c_str()));
206             }
207         }
208         throw;
209     }
210 }
211
212 #ifndef SHIBSP_LITE
213
214 AuthnRequestEvent* SessionInitiator::newAuthnRequestEvent(const Application& application, const xmltooling::HTTPRequest* request) const
215 {
216     if (!SPConfig::getConfig().isEnabled(SPConfig::Logging))
217         return nullptr;
218     try {
219         auto_ptr<TransactionLog::Event> event(SPConfig::getConfig().EventManager.newPlugin(AUTHNREQUEST_EVENT, nullptr));
220         AuthnRequestEvent* ar_event = dynamic_cast<AuthnRequestEvent*>(event.get());
221         if (ar_event) {
222             ar_event->m_request = request;
223             ar_event->m_app = &application;
224             event.release();
225             return ar_event;
226         }
227         else {
228             Category::getInstance(SHIBSP_LOGCAT ".SessionInitiator").warn("unable to audit event, log event object was of an incorrect type");
229         }
230     }
231     catch (exception& ex) {
232         Category::getInstance(SHIBSP_LOGCAT ".SessionInitiator").warn("exception auditing event: %s", ex.what());
233     }
234     return nullptr;
235 }
236
237 #endif