86f9f4540e743653f0aa26ab7dc4ea36951f4624
[shibboleth/sp.git] / shibsp / handler / impl / WAYFSessionInitiator.cpp
1 /*
2  *  Copyright 2001-2010 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 #ifndef SHIBSP_LITE
31 # include <saml/util/SAMLConstants.h>
32 #else
33 # include "lite/SAMLConstants.h"
34 #endif
35
36 #include <ctime>
37 #include <xmltooling/XMLToolingConfig.h>
38 #include <xmltooling/util/URLEncoder.h>
39
40 using namespace shibsp;
41 using namespace opensaml;
42 using namespace xmltooling;
43 using namespace std;
44
45 namespace shibsp {
46
47 #if defined (_MSC_VER)
48     #pragma warning( push )
49     #pragma warning( disable : 4250 )
50 #endif
51
52     class SHIBSP_DLLLOCAL WAYFSessionInitiator : public SessionInitiator, public AbstractHandler
53     {
54     public:
55         WAYFSessionInitiator(const DOMElement* e, const char* appId)
56                 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.WAYF"), NULL, &m_remapper), m_url(NULL) {
57             pair<bool,const char*> url = getString("URL");
58             if (!url.first)
59                 throw ConfigurationException("WAYF SessionInitiator requires a URL property.");
60             m_url = url.second;
61         }
62         virtual ~WAYFSessionInitiator() {}
63         
64         pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
65
66     private:
67         const char* m_url;
68     };
69
70 #if defined (_MSC_VER)
71     #pragma warning( pop )
72 #endif
73
74     SessionInitiator* SHIBSP_DLLLOCAL WAYFSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
75     {
76         return new WAYFSessionInitiator(p.first, p.second);
77     }
78
79 };
80
81 pair<bool,long> WAYFSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
82 {
83     // The IdP CANNOT be specified for us to run. Otherwise, we'd be redirecting to a WAYF
84     // anytime the IdP's metadata was wrong.
85     if (!entityID.empty() || !checkCompatibility(request, isHandler))
86         return make_pair(false,0L);
87
88     string target;
89     pair<bool,const char*> prop;
90     const Handler* ACS=NULL;
91     const Application& app=request.getApplication();
92     pair<bool,const char*> discoveryURL = pair<bool,const char*>(true, m_url);
93
94     if (isHandler) {
95         prop.second = request.getParameter("acsIndex");
96         if (prop.second && *prop.second) {
97             ACS = app.getAssertionConsumerServiceByIndex(atoi(prop.second));
98             if (!ACS)
99                 request.log(SPRequest::SPWarn, "invalid acsIndex specified in request, using acsIndex property");
100         }
101
102         prop = getString("target", request);
103         if (prop.first)
104             target = prop.second;
105
106         // Since we're passing the ACS by value, we need to compute the return URL,
107         // so we'll need the target resource for real.
108         recoverRelayState(request.getApplication(), request, request, target, false);
109
110         prop.second = request.getParameter("discoveryURL");
111         if (prop.second && *prop.second)
112             discoveryURL.second = prop.second;
113     }
114     else {
115         // Check for a hardwired target value in the map or handler.
116         prop = getString("target", request, HANDLER_PROPERTY_MAP|HANDLER_PROPERTY_FIXED);
117         if (prop.first)
118             target = prop.second;
119         else
120             target = request.getRequestURL();
121
122         discoveryURL = request.getRequestSettings().first->getString("discoveryURL");
123     }
124     
125     // Since we're not passing by index, we need to fully compute the return URL.
126     if (!ACS) {
127         pair<bool,unsigned int> index = getUnsignedInt("acsIndex", request, HANDLER_PROPERTY_MAP|HANDLER_PROPERTY_FIXED);
128         if (index.first) {
129             ACS = app.getAssertionConsumerServiceByIndex(index.second);
130             if (!ACS)
131                 request.log(SPRequest::SPWarn, "invalid acsIndex property, using default ACS location");
132         }
133         if (!ACS)
134             ACS = app.getDefaultAssertionConsumerService();
135     }
136
137     // Validate the ACS for use with this protocol.
138     pair<bool,const char*> ACSbinding = ACS ? ACS->getString("Binding") : pair<bool,const char*>(false,NULL);
139     if (ACSbinding.first) {
140         pair<bool,const char*> compatibleBindings = getString("compatibleBindings");
141         if (compatibleBindings.first && strstr(compatibleBindings.second, ACSbinding.second) == NULL) {
142             m_log.error("configured or requested ACS has non-SAML 1.x binding");
143             throw ConfigurationException("Configured or requested ACS has non-SAML 1.x binding ($1).", params(1, ACSbinding.second));
144         }
145         else if (strcmp(ACSbinding.second, samlconstants::SAML1_PROFILE_BROWSER_POST) &&
146                  strcmp(ACSbinding.second, samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT)) {
147             m_log.error("configured or requested ACS has non-SAML 1.x binding");
148             throw ConfigurationException("Configured or requested ACS has non-SAML 1.x binding ($1).", params(1, ACSbinding.second));
149         }
150     }
151
152     if (!discoveryURL.first)
153         discoveryURL.second = m_url;
154     m_log.debug("sending request to WAYF (%s)", discoveryURL.second);
155
156     // Compute the ACS URL. We add the ACS location to the base handlerURL.
157     string ACSloc = request.getHandlerURL(target.c_str());
158     prop = ACS ? ACS->getString("Location") : pair<bool,const char*>(false,NULL);
159     if (prop.first)
160         ACSloc += prop.second;
161
162     if (isHandler) {
163         // We may already have RelayState set if we looped back here,
164         // but we've turned it back into a resource by this point, so if there's
165         // a target on the URL, reset to that value.
166         prop.second = request.getParameter("target");
167         if (prop.second && *prop.second)
168             target = prop.second;
169     }
170
171     preserveRelayState(app, request, target);
172     if (!isHandler)
173         preservePostData(app, request, request, target.c_str());
174
175     // WAYF requires a target value.
176     if (target.empty())
177         target = "default";
178
179     char timebuf[16];
180     sprintf(timebuf,"%lu",time(NULL));
181     const URLEncoder* urlenc = XMLToolingConfig::getConfig().getURLEncoder();
182     string req=string(discoveryURL.second) + (strchr(discoveryURL.second,'?') ? '&' : '?') + "shire=" + urlenc->encode(ACSloc.c_str()) +
183         "&time=" + timebuf + "&target=" + urlenc->encode(target.c_str()) +
184         "&providerId=" + urlenc->encode(app.getString("entityID").second);
185
186     return make_pair(true, request.sendRedirect(req.c_str()));
187 }