Reducing header overuse, non-inlining selected methods (CPPOST-35).
[shibboleth/cpp-sp.git] / shibsp / handler / impl / SAMLDSSessionInitiator.cpp
index 20b1ff4..1c0a7e3 100644 (file)
@@ -1,6 +1,6 @@
 /*
- *  Copyright 2001-2007 Internet2
- * 
+ *  Copyright 2001-2009 Internet2
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -16,7 +16,7 @@
 
 /**
  * SAMLDSSessionInitiator.cpp
- * 
+ *
  * SAML Discovery Service support.
  */
 
@@ -28,6 +28,7 @@
 #include "handler/SessionInitiator.h"
 
 #include <xmltooling/XMLToolingConfig.h>
+#include <xmltooling/impl/AnyElement.h>
 #include <xmltooling/util/URLEncoder.h>
 
 using namespace shibsp;
@@ -35,6 +36,12 @@ using namespace opensaml;
 using namespace xmltooling;
 using namespace std;
 
+#ifndef SHIBSP_LITE
+# include <saml/saml2/metadata/Metadata.h>
+# include <saml/saml2/metadata/MetadataProvider.h>
+using namespace opensaml::saml2md;
+#endif
+
 namespace shibsp {
 
 #if defined (_MSC_VER)
@@ -46,7 +53,11 @@ namespace shibsp {
     {
     public:
         SAMLDSSessionInitiator(const DOMElement* e, const char* appId)
-                : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.SAMLDS")), m_url(NULL), m_returnParam(NULL) {
+                : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.SAMLDS")), m_url(NULL), m_returnParam(NULL)
+#ifndef SHIBSP_LITE
+                    ,m_discoNS("urn:oasis:names:tc:SAML:profiles:SSO:idp-discovery-protocol")
+#endif
+        {
             pair<bool,const char*> url = getString("URL");
             if (!url.first)
                 throw ConfigurationException("SAMLDS SessionInitiator requires a URL property.");
@@ -56,12 +67,39 @@ namespace shibsp {
                 m_returnParam = url.second;
         }
         virtual ~SAMLDSSessionInitiator() {}
-        
-        pair<bool,long> run(SPRequest& request, const char* entityID=NULL, bool isHandler=true) const;
+
+        pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
+
+#ifndef SHIBSP_LITE
+        void generateMetadata(SPSSODescriptor& role, const char* handlerURL) const {
+            static const XMLCh LOCAL_NAME[] = UNICODE_LITERAL_17(D,i,s,c,o,v,e,r,y,R,e,s,p,o,n,s,e);
+            const char* loc = getString("Location").second;
+            string hurl(handlerURL);
+            if (*loc != '/')
+                hurl += '/';
+            hurl += loc;
+            auto_ptr_XMLCh widen(hurl.c_str());
+            ElementProxy* ep = new AnyElementImpl(m_discoNS.get(), LOCAL_NAME);
+            ep->setAttribute(xmltooling::QName(NULL,EndpointType::LOCATION_ATTRIB_NAME), widen.get());
+            ep->setAttribute(xmltooling::QName(NULL,EndpointType::BINDING_ATTRIB_NAME), m_discoNS.get());
+            pair<bool,const XMLCh*> ix = getXMLString("index");
+            ep->setAttribute(xmltooling::QName(NULL,IndexedEndpointType::INDEX_ATTRIB_NAME), ix.first ? ix.second : xmlconstants::XML_ONE);
+
+            Extensions* ext = role.getExtensions();
+            if (!ext) {
+                ext = ExtensionsBuilder::buildExtensions();
+                role.setExtensions(ext);
+            }
+            ext->getUnknownXMLObjects().push_back(ep);
+        }
+#endif
 
     private:
         const char* m_url;
         const char* m_returnParam;
+#ifndef SHIBSP_LITE
+        auto_ptr_XMLCh m_discoNS;
+#endif
     };
 
 #if defined (_MSC_VER)
@@ -75,17 +113,18 @@ namespace shibsp {
 
 };
 
-pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, const char* entityID, bool isHandler) const
+pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
 {
     // The IdP CANNOT be specified for us to run. Otherwise, we'd be redirecting to a DS
     // anytime the IdP's metadata was wrong.
-    if (entityID && *entityID)
+    if (!entityID.empty())
         return make_pair(false,0L);
 
     string target;
     const char* option;
     bool isPassive=false;
     const Application& app=request.getApplication();
+    pair<bool,const char*> discoveryURL = pair<bool,const char*>(true, m_url);
 
     if (isHandler) {
         option = request.getParameter("SAMLDS");
@@ -95,7 +134,7 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, const char* enti
             ex.addProperty("statusCode2", "urn:oasis:names:tc:SAML:2.0:status:NoAvailableIDP");
             ex.raise();
         }
-        
+
         option = request.getParameter("target");
         if (option)
             target = option;
@@ -104,6 +143,10 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, const char* enti
         option = request.getParameter("isPassive");
         if (option)
             isPassive = !strcmp(option,"true");
+
+        option = request.getParameter("discoveryURL");
+        if (option)
+            discoveryURL.second = option;
     }
     else {
         // We're running as a "virtual handler" from within the filter.
@@ -112,9 +155,12 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, const char* enti
         target=request.getRequestURL();
         pair<bool,bool> passopt = getBool("isPassive");
         isPassive = passopt.first && passopt.second;
+        discoveryURL = request.getRequestSettings().first->getString("discoveryURL");
     }
 
-    m_log.debug("sending request to SAMLDS (%s)", m_url);
+    if (!discoveryURL.first)
+        discoveryURL.second = m_url;
+    m_log.debug("sending request to SAMLDS (%s)", discoveryURL.second);
 
     // Compute the return URL. We start with a self-referential link.
     string returnURL=request.getHandlerURL(target.c_str());
@@ -130,6 +176,8 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, const char* enti
             target = option;
     }
     preserveRelayState(request.getApplication(), request, target);
+    if (!isHandler)
+        preservePostData(request.getApplication(), request, request, target.c_str());
 
     const URLEncoder* urlenc = XMLToolingConfig::getConfig().getURLEncoder();
     if (isHandler) {
@@ -174,7 +222,7 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, const char* enti
         returnURL = returnURL + "&target=" + urlenc->encode(target.c_str());;
     }
 
-    string req=string(m_url) + (strchr(m_url,'?') ? '&' : '?') + "entityID=" + urlenc->encode(app.getString("entityID").second) +
+    string req=string(discoveryURL.second) + (strchr(discoveryURL.second,'?') ? '&' : '?') + "entityID=" + urlenc->encode(app.getString("entityID").second) +
         "&return=" + urlenc->encode(returnURL.c_str());
     if (m_returnParam)
         req = req + "&returnIDParam=" + m_returnParam;