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/MessageEncoder.h"
26 #include "signature/ContentReference.h"
27 #include "saml1/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::saml1p;
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 SAML1SOAPEncoder : public MessageEncoder
49 SAML1SOAPEncoder(const DOMElement* e) {}
50 virtual ~SAML1SOAPEncoder() {}
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 SAML1SOAPEncoderFactory(const DOMElement* const & e)
67 return new SAML1SOAPEncoder(e);
72 long SAML1SOAPEncoder::encode(
73 GenericResponse& genericResponse,
75 const char* destination,
76 const EntityDescriptor* recipient,
77 const char* relayState,
78 const ArtifactGenerator* artifactGenerator,
79 const Credential* credential,
80 const XMLCh* signatureAlg,
81 const XMLCh* digestAlg
85 xmltooling::NDC ndc("encode");
87 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1SOAP");
89 log.debug("validating input");
90 if (xmlObject->getParent())
91 throw BindingException("Cannot encode XML content with parent.");
93 genericResponse.setContentType("text/xml");
94 HTTPResponse* httpResponse = dynamic_cast<HTTPResponse*>(&genericResponse);
96 httpResponse->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
97 httpResponse->setResponseHeader("Pragma", "no-cache");
100 DOMElement* rootElement = NULL;
101 Response* response = dynamic_cast<Response*>(xmlObject);
104 Envelope* env = EnvelopeBuilder::buildEnvelope();
105 Body* body = BodyBuilder::buildBody();
107 body->getUnknownXMLObjects().push_back(response);
109 if (response->getSignature()) {
110 log.debug("response already signed, skipping signature operation");
111 rootElement = env->marshall();
114 log.debug("signing and marshalling the response");
116 // Build a Signature.
117 Signature* sig = SignatureBuilder::buildSignature();
118 response->setSignature(sig);
120 sig->setSignatureAlgorithm(signatureAlg);
122 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
124 cr->setDigestAlgorithm(digestAlg);
127 // Sign response while marshalling.
128 vector<Signature*> sigs(1,sig);
129 rootElement = env->marshall((DOMDocument*)NULL,&sigs,credential);
133 log.debug("marshalling the response");
134 rootElement = env->marshall();
139 log.debug("sending serialized response");
140 long ret = genericResponse.sendResponse(s);
142 // Cleanup by destroying XML.
146 catch (XMLToolingException&) {
147 // A bit weird...we have to "revert" things so that the response is isolated
148 // so the caller can free it.
149 if (response->getParent()) {
150 response->getParent()->detach();
157 Fault* fault = dynamic_cast<Fault*>(xmlObject);
160 log.debug("building Envelope and marshalling Fault");
161 Envelope* env = EnvelopeBuilder::buildEnvelope();
162 Body* body = BodyBuilder::buildBody();
164 body->getUnknownXMLObjects().push_back(fault);
165 rootElement = env->marshall();
168 XMLHelper::serialize(rootElement, xmlbuf);
169 istringstream s(xmlbuf);
170 log.debug("sending serialized fault");
171 long ret = genericResponse.sendError(s);
173 // Cleanup by destroying XML.
177 catch (XMLToolingException&) {
178 // A bit weird...we have to "revert" things so that the fault is isolated
179 // so the caller can free it.
180 if (fault->getParent()) {
181 fault->getParent()->detach();
188 Envelope* env = dynamic_cast<Envelope*>(xmlObject);
190 log.debug("marshalling envelope");
191 rootElement = env->marshall();
195 env->getBody()->hasChildren() &&
196 dynamic_cast<Fault*>(env->getBody()->getUnknownXMLObjects().front()));
199 XMLHelper::serialize(rootElement, xmlbuf);
200 istringstream s(xmlbuf);
201 log.debug("sending serialized envelope");
202 long ret = error ? genericResponse.sendError(s) : genericResponse.sendResponse(s);
204 // Cleanup by destroying XML.
209 throw BindingException("XML content for SAML 1.x SOAP Encoder must be a SAML 1.x <Response> or SOAP Fault/Envelope.");