SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-sp.git] / shibsp / handler / impl / SAMLDSSessionInitiator.cpp
index 9398d8a..1d77698 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2001-2010 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- * 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
+ * UCAID licenses this file to you 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
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
 #include "handler/AbstractHandler.h"
 #include "handler/SessionInitiator.h"
 
+#include <boost/algorithm/string.hpp>
 #include <xmltooling/XMLToolingConfig.h>
-#include <xmltooling/impl/AnyElement.h>
 #include <xmltooling/util/URLEncoder.h>
 
 using namespace shibsp;
 using namespace opensaml;
 using namespace xmltooling;
+using namespace boost;
 using namespace std;
 
 #ifndef SHIBSP_LITE
@@ -58,8 +63,6 @@ namespace shibsp {
 
 #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)
@@ -69,17 +72,13 @@ namespace shibsp {
             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(nullptr,IndexedEndpointType::INDEX_ATTRIB_NAME));
-                            if (val) {
-                                int maxindex = XMLString::parseInt(val);
-                                if (ix.second <= maxindex)
-                                    ix.second = maxindex + 1;
-                                break;
-                            }
+                    const DiscoveryResponse* sub = dynamic_cast<DiscoveryResponse*>(*i);
+                    if (sub) {
+                        pair<bool,int> val = sub->getIndex();
+                        if (val.first) {
+                            if (ix.second <= val.second)
+                                ix.second = val.second + 1;
+                            break;
                         }
                     }
                 }
@@ -92,14 +91,10 @@ namespace shibsp {
             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(xmltooling::QName(nullptr,EndpointType::LOCATION_ATTRIB_NAME), widen.get());
-            ep->setAttribute(xmltooling::QName(nullptr,EndpointType::BINDING_ATTRIB_NAME), m_discoNS.get());
-            ep->setAttribute(xmltooling::QName(nullptr,IndexedEndpointType::INDEX_ATTRIB_NAME), widen2.get());
+            DiscoveryResponse* ep = DiscoveryResponseBuilder::buildDiscoveryResponse();
+            ep->setLocation(widen.get());
+            ep->setBinding(samlconstants::IDP_DISCOVERY_PROTOCOL_NS);
+            ep->setIndex(ix.second);
             Extensions* ext = role.getExtensions();
             if (!ext) {
                 ext = ExtensionsBuilder::buildExtensions();
@@ -113,9 +108,6 @@ namespace shibsp {
         const char* m_url;
         const char* m_returnParam;
         vector<string> m_preservedOptions;
-#ifndef SHIBSP_LITE
-        auto_ptr_XMLCh m_discoNS;
-#endif
     };
 
 #if defined (_MSC_VER)
@@ -130,10 +122,7 @@ namespace shibsp {
 };
 
 SAMLDSSessionInitiator::SAMLDSSessionInitiator(const DOMElement* e, const char* appId)
-        : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.SAMLDS")), m_url(nullptr), m_returnParam(nullptr)
-#ifndef SHIBSP_LITE
-            ,m_discoNS("urn:oasis:names:tc:SAML:profiles:SSO:idp-discovery-protocol")
-#endif
+        : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT ".SessionInitiator.SAMLDS")), m_url(nullptr), m_returnParam(nullptr)
 {
     pair<bool,const char*> url = getString("URL");
     if (!url.first)
@@ -145,15 +134,9 @@ SAMLDSSessionInitiator::SAMLDSSessionInitiator(const DOMElement* e, const char*
 
     pair<bool,const char*> options = getString("preservedOptions");
     if (options.first) {
-        int j = 0;
         string opt = options.second;
-        for (unsigned int i = 0;  i < opt.length();  i++) {
-            if (opt.at(i) == ' ') {
-                m_preservedOptions.push_back(opt.substr(j, i-j));
-                j = i+1;
-            }
-        }
-        m_preservedOptions.push_back(opt.substr(j, opt.length()-j));
+        trim(opt);
+        split(m_preservedOptions, opt, is_space(), algorithm::token_compress_on);
     }
     else {
         m_preservedOptions.push_back("isPassive");
@@ -177,8 +160,8 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, string& entityID
 
     string target;
     pair<bool,const char*> prop;
-    bool isPassive=false;
-    const Application& app=request.getApplication();
+    bool isPassive = false;
+    const Application& app = request.getApplication();
     pair<bool,const char*> discoveryURL = pair<bool,const char*>(true, m_url);
 
     if (isHandler) {
@@ -295,6 +278,9 @@ pair<bool,long> SAMLDSSessionInitiator::run(SPRequest& request, string& entityID
         req = req + "&returnIDParam=" + m_returnParam;
     if (isPassive)
         req += "&isPassive=true";
+    prop = getString("discoveryPolicy");
+    if (prop.first)
+        req += "&policy=" + urlenc->encode(prop.second);
 
     return make_pair(true, request.sendRedirect(req.c_str()));
 }