X-Git-Url: http://www.project-moonshot.org/gitweb/?p=shibboleth%2Fcpp-opensaml.git;a=blobdiff_plain;f=saml%2Fsaml1%2Fbinding%2Fimpl%2FSAML1SOAPEncoder.cpp;h=a036dd3e8244c66018319e17b59e30a8184291f1;hp=1bb42b37b1f9688c613826d74fe06601d1b98e56;hb=07dbbcf3a8fc0aba6fefe741f1760442a0a6ea65;hpb=11cd3b15c71ee22f2818d810a17c213123e8c248 diff --git a/saml/saml1/binding/impl/SAML1SOAPEncoder.cpp b/saml/saml1/binding/impl/SAML1SOAPEncoder.cpp index 1bb42b3..a036dd3 100644 --- a/saml/saml1/binding/impl/SAML1SOAPEncoder.cpp +++ b/saml/saml1/binding/impl/SAML1SOAPEncoder.cpp @@ -27,7 +27,7 @@ #include "saml1/core/Protocols.h" #include -#include +#include #include #include #include @@ -37,8 +37,8 @@ using namespace opensaml::saml2md; using namespace opensaml; using namespace xmlsignature; using namespace soap11; +using namespace xmltooling::logging; using namespace xmltooling; -using namespace log4cpp; using namespace std; namespace opensaml { @@ -97,25 +97,42 @@ long SAML1SOAPEncoder::encode( genericResponse.setContentType("text/xml"); HTTPResponse* httpResponse = dynamic_cast(&genericResponse); if (httpResponse) { + httpResponse->setResponseHeader("Expires", "01-Jan-1997 12:00:00 GMT"); httpResponse->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private"); httpResponse->setResponseHeader("Pragma", "no-cache"); } + bool detachOnFailure = false; DOMElement* rootElement = NULL; + + // Check for a naked Response. Response* response = dynamic_cast(xmlObject); if (response) { + // Wrap it in a SOAP envelope and point xmlObject at that. + detachOnFailure = true; + Envelope* env = EnvelopeBuilder::buildEnvelope(); + Body* body = BodyBuilder::buildBody(); + env->setBody(body); + body->getUnknownXMLObjects().push_back(response); + xmlObject = env; + } + + // Now check for a full Envelope (which might have just been created). + Envelope* env = dynamic_cast(xmlObject); + if (env) { + if (!response) { + response = (env->getBody() && env->getBody()->hasChildren()) ? + dynamic_cast(env->getBody()->getUnknownXMLObjects().front()) : NULL; + } try { - Envelope* env = EnvelopeBuilder::buildEnvelope(); - Body* body = BodyBuilder::buildBody(); - env->setBody(body); - body->getUnknownXMLObjects().push_back(response); - if (credential) { + // Now check for signing requirements. + if (response && credential) { if (response->getSignature()) { log.debug("response already signed, skipping signature operation"); rootElement = env->marshall(); } else { - log.debug("signing and marshalling the response"); + log.debug("signing the response and marshalling the envelope"); // Build a Signature. Signature* sig = SignatureBuilder::buildSignature(); @@ -128,31 +145,39 @@ long SAML1SOAPEncoder::encode( cr->setDigestAlgorithm(digestAlg); } - // Sign response while marshalling. + // Sign message while marshalling. vector sigs(1,sig); rootElement = env->marshall((DOMDocument*)NULL,&sigs,credential); } } else { - log.debug("marshalling the response"); + log.debug("marshalling the envelope"); rootElement = env->marshall(); } - + stringstream s; s << *rootElement; - log.debug("sending serialized response"); - long ret = genericResponse.sendResponse(s); + + if (log.isDebugEnabled()) + log.debug("marshalled envelope:\n%s", s.str().c_str()); + + log.debug("sending serialized envelope"); + bool error = (!response && env->getBody() && env->getBody()->hasChildren() && + dynamic_cast(env->getBody()->getUnknownXMLObjects().front())); + long ret = error ? genericResponse.sendError(s) : genericResponse.sendResponse(s); // Cleanup by destroying XML. delete env; return ret; } catch (XMLToolingException&) { - // A bit weird...we have to "revert" things so that the response is isolated - // so the caller can free it. - if (response->getParent()) { - response->getParent()->detach(); - response->detach(); + if (response && detachOnFailure) { + // A bit weird...we have to "revert" things so that the response is isolated + // so the caller can free it. + if (response->getParent()) { + response->getParent()->detach(); + response->detach(); + } } throw; } @@ -161,17 +186,20 @@ long SAML1SOAPEncoder::encode( Fault* fault = dynamic_cast(xmlObject); if (fault) { try { - log.debug("building Envelope and marshalling Fault"); + log.debug("building envelope and marshalling fault"); Envelope* env = EnvelopeBuilder::buildEnvelope(); Body* body = BodyBuilder::buildBody(); 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:\n%s", s.str().c_str()); + + log.debug("sending serialized envelope"); long ret = genericResponse.sendError(s); // Cleanup by destroying XML. @@ -188,27 +216,6 @@ long SAML1SOAPEncoder::encode( throw; } } - - Envelope* env = dynamic_cast(xmlObject); - if (env) { - log.debug("marshalling envelope"); - rootElement = env->marshall(); - - bool error = - (env->getBody() && - env->getBody()->hasChildren() && - dynamic_cast(env->getBody()->getUnknownXMLObjects().front())); - - string xmlbuf; - XMLHelper::serialize(rootElement, xmlbuf); - istringstream s(xmlbuf); - log.debug("sending serialized envelope"); - long ret = error ? genericResponse.sendError(s) : genericResponse.sendResponse(s); - // Cleanup by destroying XML. - delete env; - return ret; - } - throw BindingException("XML content for SAML 1.x SOAP Encoder must be a SAML 1.x or SOAP Fault/Envelope."); }