Imported Upstream version 2.3.1+dfsg
[shibboleth/sp.git] / shibsp / handler / impl / SAMLDSSessionInitiator.cpp
index 90d189c..85b65bb 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.
  */
 
@@ -37,6 +37,8 @@ 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
 
@@ -65,24 +67,53 @@ namespace shibsp {
                 m_returnParam = url.second;
         }
         virtual ~SAMLDSSessionInitiator() {}
-        
+
         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);
+
+            // Initial guess at index to use.
+            pair<bool,unsigned int> ix = getUnsignedInt("index");
+            if (!ix.first)
+                ix.second = 1;
+
+            // Find maximum index in use and go one higher.
+            if (role.getExtensions()) {
+                const vector<XMLObject*>& exts = const_cast<const Extensions*>(role.getExtensions())->getUnknownXMLObjects();
+                for (vector<XMLObject*>::const_reverse_iterator i = exts.rbegin(); i != exts.rend(); ++i) {
+                    if (XMLString::equals((*i)->getElementQName().getLocalPart(), LOCAL_NAME) &&
+                        XMLString::equals((*i)->getElementQName().getNamespaceURI(), m_discoNS.get())) {
+                        const AttributeExtensibleXMLObject* sub = dynamic_cast<const AttributeExtensibleXMLObject*>(*i);
+                        if (sub) {
+                            const XMLCh* val = sub->getAttribute(xmltooling::QName(NULL,IndexedEndpointType::INDEX_ATTRIB_NAME));
+                            if (val) {
+                                int maxindex = XMLString::parseInt(val);
+                                if (ix.second <= maxindex)
+                                    ix.second = maxindex + 1;
+                                break;
+                            }
+                        }
+                    }
+                }
+            }
+
             const char* loc = getString("Location").second;
             string hurl(handlerURL);
             if (*loc != '/')
                 hurl += '/';
             hurl += loc;
             auto_ptr_XMLCh widen(hurl.c_str());
+
+            ostringstream os;
+            os << ix.second;
+            auto_ptr_XMLCh widen2(os.str().c_str());
+
             ElementProxy* ep = new AnyElementImpl(m_discoNS.get(), LOCAL_NAME);
-            ep->setAttribute(QName(NULL,EndpointType::LOCATION_ATTRIB_NAME), widen.get());
-            ep->setAttribute(QName(NULL,EndpointType::BINDING_ATTRIB_NAME), getXMLString("Binding").second);
-            pair<bool,const XMLCh*> ix = getXMLString("index");
-            ep->setAttribute(QName(NULL,IndexedEndpointType::INDEX_ATTRIB_NAME), ix.first ? ix.second : xmlconstants::XML_ONE);
-            
+            ep->setAttribute(xmltooling::QName(NULL,EndpointType::LOCATION_ATTRIB_NAME), widen.get());
+            ep->setAttribute(xmltooling::QName(NULL,EndpointType::BINDING_ATTRIB_NAME), m_discoNS.get());
+            ep->setAttribute(xmltooling::QName(NULL,IndexedEndpointType::INDEX_ATTRIB_NAME), widen2.get());
             Extensions* ext = role.getExtensions();
             if (!ext) {
                 ext = ExtensionsBuilder::buildExtensions();
@@ -122,6 +153,7 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, string& entityID
     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");
@@ -131,7 +163,7 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, string& entityID
             ex.addProperty("statusCode2", "urn:oasis:names:tc:SAML:2.0:status:NoAvailableIDP");
             ex.raise();
         }
-        
+
         option = request.getParameter("target");
         if (option)
             target = option;
@@ -140,6 +172,10 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, string& entityID
         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.
@@ -148,9 +184,12 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, string& entityID
         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());
@@ -166,6 +205,8 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, string& entityID
             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) {
@@ -210,7 +251,7 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, string& entityID
         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;