From 43a515d64c2780e71b3328205ccef1ce6e276cb4 Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Tue, 20 Nov 2007 22:37:51 +0000 Subject: [PATCH] Log outbound XML in msg encoders. --- saml/saml1/binding/impl/SAML1ArtifactEncoder.cpp | 4 ++++ saml/saml1/binding/impl/SAML1POSTEncoder.cpp | 26 ++++++++++++++---------- saml/saml1/binding/impl/SAML1SOAPEncoder.cpp | 22 ++++++++++++-------- saml/saml2/binding/impl/SAML2ArtifactEncoder.cpp | 3 +++ saml/saml2/binding/impl/SAML2ECPEncoder.cpp | 9 +++++--- saml/saml2/binding/impl/SAML2POSTEncoder.cpp | 20 +++++++++--------- saml/saml2/binding/impl/SAML2RedirectEncoder.cpp | 1 + saml/saml2/binding/impl/SAML2SOAPEncoder.cpp | 20 +++++++++++------- 8 files changed, 66 insertions(+), 39 deletions(-) diff --git a/saml/saml1/binding/impl/SAML1ArtifactEncoder.cpp b/saml/saml1/binding/impl/SAML1ArtifactEncoder.cpp index b592b7b..672aeeb 100644 --- a/saml/saml1/binding/impl/SAML1ArtifactEncoder.cpp +++ b/saml/saml1/binding/impl/SAML1ArtifactEncoder.cpp @@ -110,6 +110,10 @@ long SAML1ArtifactEncoder::encode( // Obtain a fresh artifact. if (!artifactGenerator) throw BindingException("SAML 1.x Artifact Encoder requires an ArtifactGenerator instance."); + + if (log.isDebugEnabled()) + log.debugStream() << "marshalled assertion: " << *xmlObject << logging::eol; + auto_ptr_char recipientID(recipient ? recipient->getEntityID() : NULL); log.debug("obtaining new artifact for relying party (%s)", recipientID.get() ? recipientID.get() : "unknown"); auto_ptr artifact(artifactGenerator->generateSAML1Artifact(recipient)); diff --git a/saml/saml1/binding/impl/SAML1POSTEncoder.cpp b/saml/saml1/binding/impl/SAML1POSTEncoder.cpp index 3a6feae..df0ee43 100644 --- a/saml/saml1/binding/impl/SAML1POSTEncoder.cpp +++ b/saml/saml1/binding/impl/SAML1POSTEncoder.cpp @@ -103,6 +103,10 @@ long SAML1POSTEncoder::encode( #endif Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1POST"); + TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine(); + if (!engine) + throw BindingException("Encoding response using POST requires a TemplateEngine instance."); + log.debug("validating input"); if (xmlObject->getParent()) throw BindingException("Cannot encode XML content with parent."); @@ -141,9 +145,14 @@ long SAML1POSTEncoder::encode( log.debug("marshalling the response"); rootElement = response->marshall(); } - - string xmlbuf; + + // Push message into template. + TemplateEngine::TemplateParameters pmap; + string& xmlbuf = pmap.m_map["SAMLResponse"]; XMLHelper::serialize(rootElement, xmlbuf); + log.debug("marshalled response: %s", xmlbuf.c_str()); + + // Replace with base-64 encoded version. unsigned int len=0; XMLByte* out=Base64::encode(reinterpret_cast(xmlbuf.data()),xmlbuf.size(),&len); if (out) { @@ -155,20 +164,15 @@ long SAML1POSTEncoder::encode( throw BindingException("Base64 encoding of XML failed."); } - // Push message into template and send result to client. + // Fill in the rest of the data and send to the client. log.debug("message encoded, sending HTML form template to client"); - TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine(); - if (!engine) - throw BindingException("Encoding response using POST requires a TemplateEngine instance."); ifstream infile(m_template.c_str()); if (!infile) throw BindingException("Failed to open HTML template for POST response ($1).", params(1,m_template.c_str())); - TemplateEngine::TemplateParameters params; - params.m_map["action"] = destination; - params.m_map["SAMLResponse"] = xmlbuf; - params.m_map["TARGET"] = relayState; + pmap.m_map["action"] = destination; + pmap.m_map["TARGET"] = relayState; stringstream s; - engine->run(infile, s, params); + engine->run(infile, s, pmap); genericResponse.setContentType("text/html"); long ret = genericResponse.sendResponse(s); diff --git a/saml/saml1/binding/impl/SAML1SOAPEncoder.cpp b/saml/saml1/binding/impl/SAML1SOAPEncoder.cpp index 025a88c..bb9f984 100644 --- a/saml/saml1/binding/impl/SAML1SOAPEncoder.cpp +++ b/saml/saml1/binding/impl/SAML1SOAPEncoder.cpp @@ -154,9 +154,12 @@ long SAML1SOAPEncoder::encode( rootElement = env->marshall(); } - string xmlbuf; - XMLHelper::serialize(rootElement, xmlbuf); - istringstream s(xmlbuf); + stringstream s; + s << *rootElement; + + if (log.isDebugEnabled()) + log.debug("marshalled envelope: %s", s.str().c_str()); + log.debug("sending serialized envelope"); bool error = (!response && env->getBody() && env->getBody()->hasChildren() && dynamic_cast(env->getBody()->getUnknownXMLObjects().front())); @@ -188,11 +191,14 @@ long SAML1SOAPEncoder::encode( env->setBody(body); body->getUnknownXMLObjects().push_back(fault); rootElement = env->marshall(); - - string xmlbuf; - XMLHelper::serialize(rootElement, xmlbuf); - istringstream s(xmlbuf); - log.debug("sending serialized fault"); + + stringstream s; + s << *rootElement; + + if (log.isDebugEnabled()) + log.debug("marshalled envelope: %s", s.str().c_str()); + + log.debug("sending serialized envelope"); long ret = genericResponse.sendError(s); // Cleanup by destroying XML. diff --git a/saml/saml2/binding/impl/SAML2ArtifactEncoder.cpp b/saml/saml2/binding/impl/SAML2ArtifactEncoder.cpp index 75401c9..e4a9cd3 100644 --- a/saml/saml2/binding/impl/SAML2ArtifactEncoder.cpp +++ b/saml/saml2/binding/impl/SAML2ArtifactEncoder.cpp @@ -162,6 +162,9 @@ long SAML2ArtifactEncoder::encode( xmlObject->marshall((DOMDocument*)NULL,&sigs,credential); } } + + if (log.isDebugEnabled()) + log.debugStream() << "marshalled message: " << *xmlObject << logging::eol; // Store the message. Last step in storage will be to delete the XML. log.debug("storing artifact and content in map"); diff --git a/saml/saml2/binding/impl/SAML2ECPEncoder.cpp b/saml/saml2/binding/impl/SAML2ECPEncoder.cpp index 99c4ddd..7a1f1ad 100644 --- a/saml/saml2/binding/impl/SAML2ECPEncoder.cpp +++ b/saml/saml2/binding/impl/SAML2ECPEncoder.cpp @@ -221,9 +221,12 @@ long SAML2ECPEncoder::encode( rootElement = env->marshall(); } - string xmlbuf; - XMLHelper::serialize(rootElement, xmlbuf); - istringstream s(xmlbuf); + stringstream s; + s << *rootElement; + + if (log.isDebugEnabled()) + log.debug("marshalled envelope: %s", s.str().c_str()); + log.debug("sending serialized envelope"); long ret = genericResponse.sendResponse(s); diff --git a/saml/saml2/binding/impl/SAML2POSTEncoder.cpp b/saml/saml2/binding/impl/SAML2POSTEncoder.cpp index 5820150..bc651c2 100644 --- a/saml/saml2/binding/impl/SAML2POSTEncoder.cpp +++ b/saml/saml2/binding/impl/SAML2POSTEncoder.cpp @@ -108,6 +108,10 @@ long SAML2POSTEncoder::encode( #endif Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2POST"); + TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine(); + if (!engine) + throw BindingException("Encoding message using POST requires a TemplateEngine instance."); + log.debug("validating input"); if (xmlObject->getParent()) throw BindingException("Cannot encode XML content with parent."); @@ -150,15 +154,12 @@ long SAML2POSTEncoder::encode( rootElement = xmlObject->marshall((DOMDocument*)NULL); } - // Start tracking data. - TemplateEngine::TemplateParameters pmap; - if (relayState && *relayState) - pmap.m_map["RelayState"] = relayState; - // Serialize the message. + TemplateEngine::TemplateParameters pmap; string& msg = pmap.m_map[(request ? "SAMLRequest" : "SAMLResponse")]; XMLHelper::serialize(rootElement, msg); - + log.debug("marshalled message: %s", msg.c_str()); + // SimpleSign. if (credential && m_simple) { log.debug("applying simple signature to message data"); @@ -199,15 +200,14 @@ long SAML2POSTEncoder::encode( msg.append(reinterpret_cast(out),len); XMLString::release(&out); - // Push message into template and send result to client. + // Push the rest of it into template and send result to client. log.debug("message encoded, sending HTML form template to client"); - TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine(); - if (!engine) - throw BindingException("Encoding message using POST requires a TemplateEngine instance."); 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.m_map["action"] = destination; + if (relayState && *relayState) + pmap.m_map["RelayState"] = relayState; stringstream s; engine->run(infile, s, pmap); genericResponse.setContentType("text/html"); diff --git a/saml/saml2/binding/impl/SAML2RedirectEncoder.cpp b/saml/saml2/binding/impl/SAML2RedirectEncoder.cpp index d0ed6d0..ac711f1 100644 --- a/saml/saml2/binding/impl/SAML2RedirectEncoder.cpp +++ b/saml/saml2/binding/impl/SAML2RedirectEncoder.cpp @@ -116,6 +116,7 @@ long SAML2RedirectEncoder::encode( DOMElement* rootElement = xmlObject->marshall(); string xmlbuf; XMLHelper::serialize(rootElement, xmlbuf); + log.debug("marshalled message: %s", xmlbuf.c_str()); unsigned int len; char* deflated = deflate(const_cast(xmlbuf.c_str()), xmlbuf.length(), &len); diff --git a/saml/saml2/binding/impl/SAML2SOAPEncoder.cpp b/saml/saml2/binding/impl/SAML2SOAPEncoder.cpp index 64acb64..98f5e9b 100644 --- a/saml/saml2/binding/impl/SAML2SOAPEncoder.cpp +++ b/saml/saml2/binding/impl/SAML2SOAPEncoder.cpp @@ -152,9 +152,12 @@ long SAML2SOAPEncoder::encode( rootElement = env->marshall(); } - string xmlbuf; - XMLHelper::serialize(rootElement, xmlbuf); - istringstream s(xmlbuf); + stringstream s; + s << *rootElement; + + if (log.isDebugEnabled()) + log.debug("marshalled envelope: %s", s.str().c_str()); + log.debug("sending serialized envelope"); bool error = (!msg && env->getBody() && env->getBody()->hasChildren() && dynamic_cast(env->getBody()->getUnknownXMLObjects().front())); @@ -187,10 +190,13 @@ long SAML2SOAPEncoder::encode( body->getUnknownXMLObjects().push_back(fault); rootElement = env->marshall(); - string xmlbuf; - XMLHelper::serialize(rootElement, xmlbuf); - istringstream s(xmlbuf); - log.debug("sending serialized fault"); + stringstream s; + s << *rootElement; + + if (log.isDebugEnabled()) + log.debug("marshalled envelope: %s", s.str().c_str()); + + log.debug("sending serialized envelope"); long ret = genericResponse.sendError(s); // Cleanup by destroying XML. -- 2.1.4