2 * Copyright 2001-2010 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 <xmltooling/logging.h>
31 #include <xmltooling/io/HTTPResponse.h>
32 #include <xmltooling/util/NDC.h>
33 #include <xmltooling/signature/Signature.h>
34 #include <xmltooling/soap/SOAP.h>
36 using namespace opensaml::saml2p;
37 using namespace opensaml::saml2md;
38 using namespace opensaml;
39 using namespace xmlsignature;
40 using namespace soap11;
41 using namespace xmltooling::logging;
42 using namespace xmltooling;
47 class SAML_DLLLOCAL SAML2SOAPEncoder : public MessageEncoder
51 virtual ~SAML2SOAPEncoder() {}
53 bool isUserAgentPresent() const {
58 GenericResponse& genericResponse,
60 const char* destination,
61 const EntityDescriptor* recipient=nullptr,
62 const char* relayState=nullptr,
63 const ArtifactGenerator* artifactGenerator=nullptr,
64 const Credential* credential=nullptr,
65 const XMLCh* signatureAlg=nullptr,
66 const XMLCh* digestAlg=nullptr
70 MessageEncoder* SAML_DLLLOCAL SAML2SOAPEncoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
72 return new SAML2SOAPEncoder();
77 long SAML2SOAPEncoder::encode(
78 GenericResponse& genericResponse,
80 const char* destination,
81 const EntityDescriptor* recipient,
82 const char* relayState,
83 const ArtifactGenerator* artifactGenerator,
84 const Credential* credential,
85 const XMLCh* signatureAlg,
86 const XMLCh* digestAlg
90 xmltooling::NDC ndc("encode");
92 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2SOAP");
94 log.debug("validating input");
95 if (xmlObject->getParent())
96 throw BindingException("Cannot encode XML content with parent.");
98 genericResponse.setContentType("text/xml");
99 HTTPResponse* httpResponse = dynamic_cast<HTTPResponse*>(&genericResponse);
101 httpResponse->setResponseHeader("Expires", "01-Jan-1997 12:00:00 GMT");
102 httpResponse->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
103 httpResponse->setResponseHeader("Pragma", "no-cache");
106 bool detachOnFailure = false;
107 DOMElement* rootElement = nullptr;
109 // Check for a naked message.
110 SignableObject* msg = dynamic_cast<SignableObject*>(xmlObject);
112 // Wrap it in a SOAP envelope and point xmlObject at that.
113 detachOnFailure = true;
114 Envelope* env = EnvelopeBuilder::buildEnvelope();
115 Body* body = BodyBuilder::buildBody();
117 body->getUnknownXMLObjects().push_back(msg);
121 Envelope* env = dynamic_cast<Envelope*>(xmlObject);
124 msg = (env->getBody() && env->getBody()->hasChildren()) ?
125 dynamic_cast<SignableObject*>(env->getBody()->getUnknownXMLObjects().front()) : nullptr;
128 if (msg && credential) {
129 if (msg->getSignature()) {
130 log.debug("message already signed, skipping signature operation");
131 rootElement = env->marshall();
134 log.debug("signing the message and marshalling the envelope");
136 // Build a Signature.
137 Signature* sig = SignatureBuilder::buildSignature();
138 msg->setSignature(sig);
140 sig->setSignatureAlgorithm(signatureAlg);
142 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
144 cr->setDigestAlgorithm(digestAlg);
147 // Sign message while marshalling.
148 vector<Signature*> sigs(1,sig);
149 rootElement = env->marshall((DOMDocument*)nullptr,&sigs,credential);
153 log.debug("marshalling the envelope");
154 rootElement = env->marshall();
160 if (log.isDebugEnabled())
161 log.debug("marshalled envelope:\n%s", s.str().c_str());
163 log.debug("sending serialized envelope");
164 bool error = (!msg && env->getBody() && env->getBody()->hasChildren() &&
165 dynamic_cast<Fault*>(env->getBody()->getUnknownXMLObjects().front()));
166 long ret = error ? genericResponse.sendError(s) : genericResponse.sendResponse(s);
168 // Cleanup by destroying XML.
172 catch (XMLToolingException&) {
173 if (msg && detachOnFailure) {
174 // A bit weird...we have to "revert" things so that the message is isolated
175 // so the caller can free it.
176 if (msg->getParent()) {
177 msg->getParent()->detach();
185 Fault* fault = dynamic_cast<Fault*>(xmlObject);
188 log.debug("building envelope and marshalling fault");
189 Envelope* env = EnvelopeBuilder::buildEnvelope();
190 Body* body = BodyBuilder::buildBody();
192 body->getUnknownXMLObjects().push_back(fault);
193 rootElement = env->marshall();
198 if (log.isDebugEnabled())
199 log.debug("marshalled envelope:\n%s", s.str().c_str());
201 log.debug("sending serialized envelope");
202 long ret = genericResponse.sendError(s);
204 // Cleanup by destroying XML.
208 catch (XMLToolingException&) {
209 // A bit weird...we have to "revert" things so that the fault is isolated
210 // so the caller can free it.
211 if (fault->getParent()) {
212 fault->getParent()->detach();
219 throw BindingException("XML content for SAML 2.0 SOAP Encoder must be a SAML 2.0 message or SOAP Fault/Envelope.");