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/MessageEncoder.h"
26 #include "signature/ContentReference.h"
27 #include "saml2/core/Protocols.h"
30 #include <log4cpp/Category.hh>
31 #include <xmltooling/io/HTTPResponse.h>
32 #include <xmltooling/util/NDC.h>
33 #include <xmltooling/soap/SOAP.h>
35 using namespace opensaml::saml2p;
36 using namespace opensaml::saml2md;
37 using namespace opensaml;
38 using namespace xmlsignature;
39 using namespace soap11;
40 using namespace xmltooling;
41 using namespace log4cpp;
46 class SAML_DLLLOCAL SAML2SOAPEncoder : public MessageEncoder
49 SAML2SOAPEncoder(const DOMElement* e);
50 virtual ~SAML2SOAPEncoder() {}
53 GenericResponse& genericResponse,
55 const char* destination,
56 const EntityDescriptor* recipient=NULL,
57 const char* relayState=NULL,
58 const ArtifactGenerator* artifactGenerator=NULL,
59 const Credential* credential=NULL,
60 const XMLCh* signatureAlg=NULL,
61 const XMLCh* digestAlg=NULL
65 MessageEncoder* SAML_DLLLOCAL SAML2SOAPEncoderFactory(const DOMElement* const & e)
67 return new SAML2SOAPEncoder(e);
72 SAML2SOAPEncoder::SAML2SOAPEncoder(const DOMElement* e) {}
74 long SAML2SOAPEncoder::encode(
75 GenericResponse& genericResponse,
77 const char* destination,
78 const EntityDescriptor* recipient,
79 const char* relayState,
80 const ArtifactGenerator* artifactGenerator,
81 const Credential* credential,
82 const XMLCh* signatureAlg,
83 const XMLCh* digestAlg
87 xmltooling::NDC ndc("encode");
89 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2SOAP");
91 log.debug("validating input");
92 if (xmlObject->getParent())
93 throw BindingException("Cannot encode XML content with parent.");
95 genericResponse.setContentType("text/xml");
96 HTTPResponse* httpResponse = dynamic_cast<HTTPResponse*>(&genericResponse);
98 httpResponse->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
99 httpResponse->setResponseHeader("Pragma", "no-cache");
102 DOMElement* rootElement = NULL;
103 StatusResponseType* response = dynamic_cast<StatusResponseType*>(xmlObject);
106 Envelope* env = EnvelopeBuilder::buildEnvelope();
107 Body* body = BodyBuilder::buildBody();
109 body->getUnknownXMLObjects().push_back(response);
111 if (response->getSignature()) {
112 log.debug("response already signed, skipping signature operation");
113 rootElement = env->marshall();
116 log.debug("signing and marshalling the response");
118 // Build a Signature.
119 Signature* sig = SignatureBuilder::buildSignature();
120 response->setSignature(sig);
122 sig->setSignatureAlgorithm(signatureAlg);
124 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
126 cr->setDigestAlgorithm(digestAlg);
129 // Sign response while marshalling.
130 vector<Signature*> sigs(1,sig);
131 rootElement = env->marshall((DOMDocument*)NULL,&sigs,credential);
135 log.debug("marshalling the response");
136 rootElement = env->marshall();
141 log.debug("sending serialized response");
142 long ret = genericResponse.sendResponse(s);
144 // Cleanup by destroying XML.
148 catch (XMLToolingException&) {
149 // A bit weird...we have to "revert" things so that the response is isolated
150 // so the caller can free it.
151 if (response->getParent()) {
152 response->getParent()->detach();
159 Fault* fault = dynamic_cast<Fault*>(xmlObject);
162 log.debug("building Envelope and marshalling Fault");
163 Envelope* env = EnvelopeBuilder::buildEnvelope();
164 Body* body = BodyBuilder::buildBody();
166 body->getUnknownXMLObjects().push_back(fault);
167 rootElement = env->marshall();
170 XMLHelper::serialize(rootElement, xmlbuf);
171 istringstream s(xmlbuf);
172 log.debug("sending serialized fault");
173 long ret = genericResponse.sendError(s);
175 // Cleanup by destroying XML.
179 catch (XMLToolingException&) {
180 // A bit weird...we have to "revert" things so that the fault is isolated
181 // so the caller can free it.
182 if (fault->getParent()) {
183 fault->getParent()->detach();
190 Envelope* env = dynamic_cast<Envelope*>(xmlObject);
192 log.debug("marshalling envelope");
193 rootElement = env->marshall();
197 env->getBody()->hasChildren() &&
198 dynamic_cast<Fault*>(env->getBody()->getUnknownXMLObjects().front()));
201 XMLHelper::serialize(rootElement, xmlbuf);
202 istringstream s(xmlbuf);
203 log.debug("sending serialized envelope");
204 long ret = error ? genericResponse.sendError(s) : genericResponse.sendResponse(s);
206 // Cleanup by destroying XML.
211 throw BindingException("XML content for SAML 2.0 SOAP Encoder must be a SAML 2.0 response or SOAP Fault/Envelope.");