Enhance relay state handling.
[shibboleth/sp.git] / shibsp / handler / impl / WAYFSessionInitiator.cpp
index dcb8654..e53c3ee 100644 (file)
@@ -80,18 +80,19 @@ pair<bool,long> WAYFSessionInitiator::run(SPRequest& request, const char* entity
         return make_pair(false,0);
 
     string target;
+    const char* option;
     const Handler* ACS=NULL;
     const Application& app=request.getApplication();
 
     if (isHandler) {
-        const char* option=request.getParameter("acsIndex");
+        option=request.getParameter("acsIndex");
         if (option)
             ACS=app.getAssertionConsumerServiceByIndex(atoi(option));
 
         option = request.getParameter("target");
         if (option)
             target = option;
-        recoverRelayState(request, target);
+        recoverRelayState(request, target, false);
     }
     else {
         // We're running as a "virtual handler" from within the filter.
@@ -109,13 +110,24 @@ pair<bool,long> WAYFSessionInitiator::run(SPRequest& request, const char* entity
     pair<bool,const char*> loc=ACS ? ACS->getString("Location") : pair<bool,const char*>(false,NULL);
     if (loc.first) ACSloc+=loc.second;
 
+    if (isHandler) {
+        // We may already have RelayState set if we looped back here,
+        // but just in case target is a resource, we reset it back.
+        option = request.getParameter("target");
+        if (option)
+            target = option;
+    }
     preserveRelayState(request, target);
 
+    // WAYF requires a target value.
+    if (target.empty())
+        target = "default";
+
     char timebuf[16];
     sprintf(timebuf,"%u",time(NULL));
     const URLEncoder* urlenc = XMLToolingConfig::getConfig().getURLEncoder();
     string req=string(m_url) + (strchr(m_url,'?') ? '&' : '?') + "shire=" + urlenc->encode(ACSloc.c_str()) +
-        "&time=" + timebuf + "&target=" + target +
+        "&time=" + timebuf + "&target=" + urlenc->encode(target.c_str()) +
         "&providerId=" + urlenc->encode(app.getString("entityID").second);
 
     return make_pair(true, request.sendRedirect(req.c_str()));