2.0 SOAP Encoder
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2SOAPEncoder.cpp
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * SAML2SOAPEncoder.cpp
19  * 
20  * SAML 2.0 SOAP binding message encoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/HTTPResponse.h"
26 #include "saml2/binding/SAML2SOAPEncoder.h"
27 #include "saml2/core/Protocols.h"
28
29 #include <sstream>
30 #include <log4cpp/Category.hh>
31 #include <xmltooling/util/NDC.h>
32 #include <xmltooling/soap/SOAP.h>
33
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;
40 using namespace std;
41
42 namespace opensaml {
43     namespace saml2p {              
44         MessageEncoder* SAML_DLLLOCAL SAML2SOAPEncoderFactory(const DOMElement* const & e)
45         {
46             return new SAML2SOAPEncoder(e);
47         }
48     };
49 };
50
51 SAML2SOAPEncoder::SAML2SOAPEncoder(const DOMElement* e) {}
52
53 long SAML2SOAPEncoder::encode(
54     GenericResponse& genericResponse,
55     XMLObject* xmlObject,
56     const char* destination,
57     const char* recipientID,
58     const char* relayState,
59     const CredentialResolver* credResolver,
60     const XMLCh* sigAlgorithm
61     ) const
62 {
63 #ifdef _DEBUG
64     xmltooling::NDC ndc("encode");
65 #endif
66     Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1SOAP");
67
68     log.debug("validating input");
69     if (xmlObject->getParent())
70         throw BindingException("Cannot encode XML content with parent.");
71
72     genericResponse.setContentType("text/xml");
73     HTTPResponse* httpResponse = dynamic_cast<HTTPResponse*>(&genericResponse);
74     if (httpResponse) {
75         httpResponse->setHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
76         httpResponse->setHeader("Pragma", "no-cache");
77     }
78
79     DOMElement* rootElement = NULL;
80     StatusResponseType* response = dynamic_cast<StatusResponseType*>(xmlObject);
81     if (response) {
82         try {
83             Envelope* env = EnvelopeBuilder::buildEnvelope();
84             Body* body = BodyBuilder::buildBody();
85             env->setBody(body);
86             body->getXMLObjects().push_back(response);
87             if (credResolver ) {
88                 if (response->getSignature()) {
89                     log.debug("response already signed, skipping signature operation");
90                     rootElement = env->marshall();
91                 }
92                 else {
93                     log.debug("signing and marshalling the response");
94         
95                     // Build a Signature.
96                     Signature* sig = buildSignature(credResolver, sigAlgorithm);
97                     response->setSignature(sig);
98             
99                     // Sign response while marshalling.
100                     vector<Signature*> sigs(1,sig);
101                     rootElement = env->marshall((DOMDocument*)NULL,&sigs);
102                 }
103             }
104             else {
105                 log.debug("marshalling the response");
106                 rootElement = env->marshall();
107             }
108             
109             string xmlbuf;
110             XMLHelper::serialize(rootElement, xmlbuf);
111             istringstream s(xmlbuf);
112             log.debug("sending serialized response");
113             long ret = genericResponse.sendResponse(s);
114         
115             // Cleanup by destroying XML.
116             delete env;
117             return ret;
118         }
119         catch (XMLToolingException&) {
120             // A bit weird...we have to "revert" things so that the response is isolated
121             // so the caller can free it.
122             if (response->getParent()) {
123                 response->getParent()->detach();
124                 response->detach();
125             }
126             throw;
127         }
128     }
129
130     Fault* fault = dynamic_cast<Fault*>(xmlObject);
131     if (fault) {
132         try {
133             log.debug("building Envelope and marshalling Fault");
134             Envelope* env = EnvelopeBuilder::buildEnvelope();
135             Body* body = BodyBuilder::buildBody();
136             env->setBody(body);
137             body->getXMLObjects().push_back(fault);
138             rootElement = env->marshall();
139     
140             string xmlbuf;
141             XMLHelper::serialize(rootElement, xmlbuf);
142             istringstream s(xmlbuf);
143             log.debug("sending serialized fault");
144             long ret = genericResponse.sendError(s);
145         
146             // Cleanup by destroying XML.
147             delete env;
148             return ret;
149         }
150         catch (XMLToolingException&) {
151             // A bit weird...we have to "revert" things so that the fault is isolated
152             // so the caller can free it.
153             if (fault->getParent()) {
154                 fault->getParent()->detach();
155                 fault->detach();
156             }
157             throw;
158         }
159     }
160
161     Envelope* env = dynamic_cast<Envelope*>(xmlObject);
162     if (env) {
163         log.debug("marshalling envelope");
164         rootElement = env->marshall();
165
166         bool error =
167             (env->getBody() &&
168                 env->getBody()->hasChildren() &&
169                     dynamic_cast<Fault*>(env->getBody()->getXMLObjects().front()));
170
171         string xmlbuf;
172         XMLHelper::serialize(rootElement, xmlbuf);
173         istringstream s(xmlbuf);
174         log.debug("sending serialized envelope");
175         long ret = error ? genericResponse.sendError(s) : genericResponse.sendResponse(s);
176     
177         // Cleanup by destroying XML.
178         delete env;
179         return ret;
180     }
181
182     throw BindingException("XML content for SAML 2.0 SOAP Encoder must be a SAML 2.0 response or SOAP Fault/Envelope.");
183 }