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 * SAML1SOAPEncoder.cpp
20 * SAML 1.x SOAP binding message encoder
24 #include "exceptions.h"
25 #include "binding/HTTPResponse.h"
26 #include "binding/MessageEncoder.h"
27 #include "signature/ContentReference.h"
28 #include "saml1/core/Protocols.h"
31 #include <log4cpp/Category.hh>
32 #include <xmltooling/util/NDC.h>
33 #include <xmltooling/soap/SOAP.h>
35 using namespace opensaml::saml1p;
36 using namespace opensaml;
37 using namespace xmlsignature;
38 using namespace soap11;
39 using namespace xmltooling;
40 using namespace log4cpp;
45 class SAML_DLLLOCAL SAML1SOAPEncoder : public MessageEncoder
48 SAML1SOAPEncoder(const DOMElement* e) {}
49 virtual ~SAML1SOAPEncoder() {}
52 GenericResponse& genericResponse,
54 const char* destination,
55 const char* recipientID=NULL,
56 const char* relayState=NULL,
57 const Credential* credential=NULL,
58 const XMLCh* signatureAlg=NULL,
59 const XMLCh* digestAlg=NULL
63 MessageEncoder* SAML_DLLLOCAL SAML1SOAPEncoderFactory(const DOMElement* const & e)
65 return new SAML1SOAPEncoder(e);
70 long SAML1SOAPEncoder::encode(
71 GenericResponse& genericResponse,
73 const char* destination,
74 const char* recipientID,
75 const char* relayState,
76 const Credential* credential,
77 const XMLCh* signatureAlg,
78 const XMLCh* digestAlg
82 xmltooling::NDC ndc("encode");
84 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1SOAP");
86 log.debug("validating input");
87 if (xmlObject->getParent())
88 throw BindingException("Cannot encode XML content with parent.");
90 genericResponse.setContentType("text/xml");
91 HTTPResponse* httpResponse = dynamic_cast<HTTPResponse*>(&genericResponse);
93 httpResponse->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
94 httpResponse->setResponseHeader("Pragma", "no-cache");
97 DOMElement* rootElement = NULL;
98 Response* response = dynamic_cast<Response*>(xmlObject);
101 Envelope* env = EnvelopeBuilder::buildEnvelope();
102 Body* body = BodyBuilder::buildBody();
104 body->getUnknownXMLObjects().push_back(response);
106 if (response->getSignature()) {
107 log.debug("response already signed, skipping signature operation");
108 rootElement = env->marshall();
111 log.debug("signing and marshalling the response");
113 // Build a Signature.
114 Signature* sig = SignatureBuilder::buildSignature();
115 response->setSignature(sig);
117 sig->setSignatureAlgorithm(signatureAlg);
119 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
121 cr->setDigestAlgorithm(digestAlg);
124 // Sign response while marshalling.
125 vector<Signature*> sigs(1,sig);
126 rootElement = env->marshall((DOMDocument*)NULL,&sigs,credential);
130 log.debug("marshalling the response");
131 rootElement = env->marshall();
136 log.debug("sending serialized response");
137 long ret = genericResponse.sendResponse(s);
139 // Cleanup by destroying XML.
143 catch (XMLToolingException&) {
144 // A bit weird...we have to "revert" things so that the response is isolated
145 // so the caller can free it.
146 if (response->getParent()) {
147 response->getParent()->detach();
154 Fault* fault = dynamic_cast<Fault*>(xmlObject);
157 log.debug("building Envelope and marshalling Fault");
158 Envelope* env = EnvelopeBuilder::buildEnvelope();
159 Body* body = BodyBuilder::buildBody();
161 body->getUnknownXMLObjects().push_back(fault);
162 rootElement = env->marshall();
165 XMLHelper::serialize(rootElement, xmlbuf);
166 istringstream s(xmlbuf);
167 log.debug("sending serialized fault");
168 long ret = genericResponse.sendError(s);
170 // Cleanup by destroying XML.
174 catch (XMLToolingException&) {
175 // A bit weird...we have to "revert" things so that the fault is isolated
176 // so the caller can free it.
177 if (fault->getParent()) {
178 fault->getParent()->detach();
185 Envelope* env = dynamic_cast<Envelope*>(xmlObject);
187 log.debug("marshalling envelope");
188 rootElement = env->marshall();
192 env->getBody()->hasChildren() &&
193 dynamic_cast<Fault*>(env->getBody()->getUnknownXMLObjects().front()));
196 XMLHelper::serialize(rootElement, xmlbuf);
197 istringstream s(xmlbuf);
198 log.debug("sending serialized envelope");
199 long ret = error ? genericResponse.sendError(s) : genericResponse.sendResponse(s);
201 // Cleanup by destroying XML.
206 throw BindingException("XML content for SAML 1.x SOAP Encoder must be a SAML 1.x <Response> or SOAP Fault/Envelope.");