2 * Copyright 2001-2007 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 "binding/MessageEncoder.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 class SAML_DLLLOCAL SAML2SOAPEncoder : public MessageEncoder
47 SAML2SOAPEncoder(const DOMElement* e);
48 virtual ~SAML2SOAPEncoder() {}
51 GenericResponse& genericResponse,
52 xmltooling::XMLObject* xmlObject,
53 const char* destination,
54 const char* recipientID=NULL,
55 const char* relayState=NULL,
56 const xmltooling::CredentialResolver* credResolver=NULL,
57 const XMLCh* sigAlgorithm=NULL
61 MessageEncoder* SAML_DLLLOCAL SAML2SOAPEncoderFactory(const DOMElement* const & e)
63 return new SAML2SOAPEncoder(e);
68 SAML2SOAPEncoder::SAML2SOAPEncoder(const DOMElement* e) {}
70 long SAML2SOAPEncoder::encode(
71 GenericResponse& genericResponse,
73 const char* destination,
74 const char* recipientID,
75 const char* relayState,
76 const CredentialResolver* credResolver,
77 const XMLCh* sigAlgorithm
81 xmltooling::NDC ndc("encode");
83 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2SOAP");
85 log.debug("validating input");
86 if (xmlObject->getParent())
87 throw BindingException("Cannot encode XML content with parent.");
89 genericResponse.setContentType("text/xml");
90 HTTPResponse* httpResponse = dynamic_cast<HTTPResponse*>(&genericResponse);
92 httpResponse->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
93 httpResponse->setResponseHeader("Pragma", "no-cache");
96 DOMElement* rootElement = NULL;
97 StatusResponseType* response = dynamic_cast<StatusResponseType*>(xmlObject);
100 Envelope* env = EnvelopeBuilder::buildEnvelope();
101 Body* body = BodyBuilder::buildBody();
103 body->getUnknownXMLObjects().push_back(response);
105 if (response->getSignature()) {
106 log.debug("response already signed, skipping signature operation");
107 rootElement = env->marshall();
110 log.debug("signing and marshalling the response");
112 // Build a Signature.
113 Signature* sig = buildSignature(credResolver, sigAlgorithm);
114 response->setSignature(sig);
116 // Sign response while marshalling.
117 vector<Signature*> sigs(1,sig);
118 rootElement = env->marshall((DOMDocument*)NULL,&sigs);
122 log.debug("marshalling the response");
123 rootElement = env->marshall();
128 log.debug("sending serialized response");
129 long ret = genericResponse.sendResponse(s);
131 // Cleanup by destroying XML.
135 catch (XMLToolingException&) {
136 // A bit weird...we have to "revert" things so that the response is isolated
137 // so the caller can free it.
138 if (response->getParent()) {
139 response->getParent()->detach();
146 Fault* fault = dynamic_cast<Fault*>(xmlObject);
149 log.debug("building Envelope and marshalling Fault");
150 Envelope* env = EnvelopeBuilder::buildEnvelope();
151 Body* body = BodyBuilder::buildBody();
153 body->getUnknownXMLObjects().push_back(fault);
154 rootElement = env->marshall();
157 XMLHelper::serialize(rootElement, xmlbuf);
158 istringstream s(xmlbuf);
159 log.debug("sending serialized fault");
160 long ret = genericResponse.sendError(s);
162 // Cleanup by destroying XML.
166 catch (XMLToolingException&) {
167 // A bit weird...we have to "revert" things so that the fault is isolated
168 // so the caller can free it.
169 if (fault->getParent()) {
170 fault->getParent()->detach();
177 Envelope* env = dynamic_cast<Envelope*>(xmlObject);
179 log.debug("marshalling envelope");
180 rootElement = env->marshall();
184 env->getBody()->hasChildren() &&
185 dynamic_cast<Fault*>(env->getBody()->getUnknownXMLObjects().front()));
188 XMLHelper::serialize(rootElement, xmlbuf);
189 istringstream s(xmlbuf);
190 log.debug("sending serialized envelope");
191 long ret = error ? genericResponse.sendError(s) : genericResponse.sendResponse(s);
193 // Cleanup by destroying XML.
198 throw BindingException("XML content for SAML 2.0 SOAP Encoder must be a SAML 2.0 response or SOAP Fault/Envelope.");