2 * Copyright 2001-2006 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * SAML2SOAPEncoder.cpp
20 * SAML 2.0 SOAP binding message encoder
24 #include "exceptions.h"
25 #include "binding/HTTPResponse.h"
26 #include "saml2/binding/SAML2SOAPEncoder.h"
27 #include "saml2/core/Protocols.h"
30 #include <log4cpp/Category.hh>
31 #include <xmltooling/util/NDC.h>
32 #include <xmltooling/soap/SOAP.h>
34 using namespace opensaml::saml2p;
35 using namespace opensaml;
36 using namespace xmlsignature;
37 using namespace soap11;
38 using namespace xmltooling;
39 using namespace log4cpp;
44 MessageEncoder* SAML_DLLLOCAL SAML2SOAPEncoderFactory(const DOMElement* const & e)
46 return new SAML2SOAPEncoder(e);
51 SAML2SOAPEncoder::SAML2SOAPEncoder(const DOMElement* e) {}
53 long SAML2SOAPEncoder::encode(
54 GenericResponse& genericResponse,
56 const char* destination,
57 const char* recipientID,
58 const char* relayState,
59 const CredentialResolver* credResolver,
60 const XMLCh* sigAlgorithm
64 xmltooling::NDC ndc("encode");
66 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2SOAP");
68 log.debug("validating input");
69 if (xmlObject->getParent())
70 throw BindingException("Cannot encode XML content with parent.");
72 genericResponse.setContentType("text/xml");
73 HTTPResponse* httpResponse = dynamic_cast<HTTPResponse*>(&genericResponse);
75 httpResponse->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
76 httpResponse->setResponseHeader("Pragma", "no-cache");
79 DOMElement* rootElement = NULL;
80 StatusResponseType* response = dynamic_cast<StatusResponseType*>(xmlObject);
83 Envelope* env = EnvelopeBuilder::buildEnvelope();
84 Body* body = BodyBuilder::buildBody();
86 body->getUnknownXMLObjects().push_back(response);
88 if (response->getSignature()) {
89 log.debug("response already signed, skipping signature operation");
90 rootElement = env->marshall();
93 log.debug("signing and marshalling the response");
96 Signature* sig = buildSignature(credResolver, sigAlgorithm);
97 response->setSignature(sig);
99 // Sign response while marshalling.
100 vector<Signature*> sigs(1,sig);
101 rootElement = env->marshall((DOMDocument*)NULL,&sigs);
105 log.debug("marshalling the response");
106 rootElement = env->marshall();
111 log.debug("sending serialized response");
112 long ret = genericResponse.sendResponse(s);
114 // Cleanup by destroying XML.
118 catch (XMLToolingException&) {
119 // A bit weird...we have to "revert" things so that the response is isolated
120 // so the caller can free it.
121 if (response->getParent()) {
122 response->getParent()->detach();
129 Fault* fault = dynamic_cast<Fault*>(xmlObject);
132 log.debug("building Envelope and marshalling Fault");
133 Envelope* env = EnvelopeBuilder::buildEnvelope();
134 Body* body = BodyBuilder::buildBody();
136 body->getUnknownXMLObjects().push_back(fault);
137 rootElement = env->marshall();
140 XMLHelper::serialize(rootElement, xmlbuf);
141 istringstream s(xmlbuf);
142 log.debug("sending serialized fault");
143 long ret = genericResponse.sendError(s);
145 // Cleanup by destroying XML.
149 catch (XMLToolingException&) {
150 // A bit weird...we have to "revert" things so that the fault is isolated
151 // so the caller can free it.
152 if (fault->getParent()) {
153 fault->getParent()->detach();
160 Envelope* env = dynamic_cast<Envelope*>(xmlObject);
162 log.debug("marshalling envelope");
163 rootElement = env->marshall();
167 env->getBody()->hasChildren() &&
168 dynamic_cast<Fault*>(env->getBody()->getUnknownXMLObjects().front()));
171 XMLHelper::serialize(rootElement, xmlbuf);
172 istringstream s(xmlbuf);
173 log.debug("sending serialized envelope");
174 long ret = error ? genericResponse.sendError(s) : genericResponse.sendResponse(s);
176 // Cleanup by destroying XML.
181 throw BindingException("XML content for SAML 2.0 SOAP Encoder must be a SAML 2.0 response or SOAP Fault/Envelope.");