Update copyright.
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2POSTEncoder.cpp
index 3a5fe25..32c8e9c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2006 Internet2
+ *  Copyright 2001-2007 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
 
 #include "internal.h"
 #include "exceptions.h"
-#include "binding/HTTPResponse.h"
 #include "saml2/binding/SAML2POSTEncoder.h"
 #include "saml2/core/Protocols.h"
 
@@ -67,8 +66,6 @@ SAML2POSTEncoder::SAML2POSTEncoder(const DOMElement* e, bool simple) : m_simple(
         throw XMLToolingException("SAML2POSTEncoder requires template attribute.");
 }
 
-SAML2POSTEncoder::~SAML2POSTEncoder() {}
-
 long SAML2POSTEncoder::encode(
     GenericResponse& genericResponse,
     XMLObject* xmlObject,
@@ -85,9 +82,6 @@ long SAML2POSTEncoder::encode(
     Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2POST");
 
     log.debug("validating input");
-    HTTPResponse* httpResponse=dynamic_cast<HTTPResponse*>(&genericResponse);
-    if (!httpResponse)
-        throw BindingException("Unable to cast response interface to HTTPResponse type.");
     if (xmlObject->getParent())
         throw BindingException("Cannot encode XML content with parent.");
     
@@ -126,21 +120,15 @@ long SAML2POSTEncoder::encode(
     rootElement = xmlObject->marshall((DOMDocument*)NULL,&sigs);
     
     // Start tracking data.
-    map<string,string> pmap;
+    TemplateEngine::TemplateParameters pmap;
     if (relayState)
-        pmap["RelayState"] = relayState;
+        pmap.m_map["RelayState"] = relayState;
 
-    // Base64 the message.
-    string& msg = pmap[(request ? "SAMLRequest" : "SAMLResponse")];
+    // Serialize the message.
+    string& msg = pmap.m_map[(request ? "SAMLRequest" : "SAMLResponse")];
     XMLHelper::serialize(rootElement, msg);
-    unsigned int len=0;
-    XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(msg.data()),msg.size(),&len);
-    if (!out)
-        throw BindingException("Base64 encoding of XML failed.");
-    msg.erase();
-    msg.append(reinterpret_cast<char*>(out),len);
-    XMLString::release(&out);
-    
+
+    // SimpleSign.
     if (credResolver && m_simple) {
         log.debug("applying simple signature to message data");
         string input = (request ? "SAMLRequest=" : "SAMLResponse=") + msg;
@@ -149,16 +137,25 @@ long SAML2POSTEncoder::encode(
         if (!sigAlgorithm)
             sigAlgorithm = DSIGConstants::s_unicodeStrURIRSA_SHA1;
         auto_ptr_char alg(sigAlgorithm);
-        pmap["SigAlg"] = alg.get();
+        pmap.m_map["SigAlg"] = alg.get();
         input = input + "&SigAlg=" + alg.get();
 
         char sigbuf[1024];
         memset(sigbuf,0,sizeof(sigbuf));
         auto_ptr<XSECCryptoKey> key(credResolver->getKey());
         Signature::createRawSignature(key.get(), sigAlgorithm, input.c_str(), input.length(), sigbuf, sizeof(sigbuf)-1);
-        pmap["Signature"] = sigbuf;
+        pmap.m_map["Signature"] = sigbuf;
     }
     
+    // Base64 the message.
+    unsigned int len=0;
+    XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(msg.data()),msg.size(),&len);
+    if (!out)
+        throw BindingException("Base64 encoding of XML failed.");
+    msg.erase();
+    msg.append(reinterpret_cast<char*>(out),len);
+    XMLString::release(&out);
+    
     // Push message into template and send result to client.
     log.debug("message encoded, sending HTML form template to client");
     TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine();
@@ -167,11 +164,11 @@ long SAML2POSTEncoder::encode(
     ifstream infile(m_template.c_str());
     if (!infile)
         throw BindingException("Failed to open HTML template for POST message ($1).", params(1,m_template.c_str()));
-    pmap["action"] = destination;
+    pmap.m_map["action"] = destination;
     stringstream s;
     engine->run(infile, s, pmap);
-    httpResponse->setContentType("text/html");
-    long ret = httpResponse->sendResponse(s, HTTPResponse::SAML_HTTP_STATUS_OK);
+    genericResponse.setContentType("text/html");
+    long ret = genericResponse.sendResponse(s);
 
     // Cleanup by destroying XML.
     delete xmlObject;