Drafts of remoted Shib and SAML2 SessionInitiators.
[shibboleth/cpp-sp.git] / shibsp / handler / impl / WAYFSessionInitiator.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * WAYFSessionInitiator.cpp
19  * 
20  * Shibboleth WAYF support.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "SPRequest.h"
27 #include "handler/AbstractHandler.h"
28 #include "handler/SessionInitiator.h"
29
30 #include <xmltooling/XMLToolingConfig.h>
31 #include <xmltooling/util/URLEncoder.h>
32
33 using namespace shibsp;
34 using namespace opensaml;
35 using namespace xmltooling;
36 using namespace log4cpp;
37 using namespace std;
38
39 namespace shibsp {
40
41 #if defined (_MSC_VER)
42     #pragma warning( push )
43     #pragma warning( disable : 4250 )
44 #endif
45
46     class SHIBSP_DLLLOCAL WAYFSessionInitiator : public SessionInitiator, public AbstractHandler
47     {
48     public:
49         WAYFSessionInitiator(const DOMElement* e, const char* appId)
50                 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator")), m_url(NULL) {
51             pair<bool,const char*> url = getString("URL");
52             if (!url.first)
53                 throw ConfigurationException("WAYF SessionInitiator requires a URL property.");
54             m_url = url.second;
55         }
56         virtual ~WAYFSessionInitiator() {}
57         
58         pair<bool,long> run(SPRequest& request, const char* entityID=NULL, bool isHandler=true) const;
59
60     private:
61         const char* m_url;
62     };
63
64 #if defined (_MSC_VER)
65     #pragma warning( pop )
66 #endif
67
68     SessionInitiator* SHIBSP_DLLLOCAL WAYFSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
69     {
70         return new WAYFSessionInitiator(p.first, p.second);
71     }
72
73 };
74
75 pair<bool,long> WAYFSessionInitiator::run(SPRequest& request, const char* entityID, bool isHandler) const
76 {
77     // The IdP CANNOT be specified for us to run. Otherwise, we'd be redirecting to a WAYF
78     // anytime the IdP's metadata was wrong.
79     if (entityID && *entityID)
80         return make_pair(false,0);
81
82     string target;
83     const char* option;
84     const Handler* ACS=NULL;
85     const Application& app=request.getApplication();
86
87     if (isHandler) {
88         option=request.getParameter("acsIndex");
89         if (option)
90             ACS=app.getAssertionConsumerServiceByIndex(atoi(option));
91
92         option = request.getParameter("target");
93         if (option)
94             target = option;
95         recoverRelayState(request.getApplication(), request, target, false);
96     }
97     else {
98         // We're running as a "virtual handler" from within the filter.
99         // The target resource is the current one and everything else is defaulted.
100         target=request.getRequestURL();
101     }
102     
103     if (!ACS)
104         ACS = app.getDefaultAssertionConsumerService();
105
106     m_log.debug("sending request to WAYF (%s)", m_url);
107
108     // Compute the ACS URL. We add the ACS location to the base handlerURL.
109     string ACSloc=request.getHandlerURL(target.c_str());
110     pair<bool,const char*> loc=ACS ? ACS->getString("Location") : pair<bool,const char*>(false,NULL);
111     if (loc.first) ACSloc+=loc.second;
112
113     if (isHandler) {
114         // We may already have RelayState set if we looped back here,
115         // but just in case target is a resource, we reset it back.
116         option = request.getParameter("target");
117         if (option)
118             target = option;
119     }
120     preserveRelayState(request.getApplication(), request, target);
121
122     // WAYF requires a target value.
123     if (target.empty())
124         target = "default";
125
126     char timebuf[16];
127     sprintf(timebuf,"%u",time(NULL));
128     const URLEncoder* urlenc = XMLToolingConfig::getConfig().getURLEncoder();
129     string req=string(m_url) + (strchr(m_url,'?') ? '&' : '?') + "shire=" + urlenc->encode(ACSloc.c_str()) +
130         "&time=" + timebuf + "&target=" + urlenc->encode(target.c_str()) +
131         "&providerId=" + urlenc->encode(app.getString("entityID").second);
132
133     return make_pair(true, request.sendRedirect(req.c_str()));
134 }