Update copyright.
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2SOAPEncoder.cpp
1 /*
2  *  Copyright 2001-2007 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.SAML2SOAP");
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->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
76         httpResponse->setResponseHeader("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->getUnknownXMLObjects().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             stringstream s;
110             s << *rootElement;
111             log.debug("sending serialized response");
112             long ret = genericResponse.sendResponse(s);
113         
114             // Cleanup by destroying XML.
115             delete env;
116             return ret;
117         }
118         catch (XMLToolingException&) {
119             // A bit weird...we have to "revert" things so that the response is isolated
120             // so the caller can free it.
121             if (response->getParent()) {
122                 response->getParent()->detach();
123                 response->detach();
124             }
125             throw;
126         }
127     }
128
129     Fault* fault = dynamic_cast<Fault*>(xmlObject);
130     if (fault) {
131         try {
132             log.debug("building Envelope and marshalling Fault");
133             Envelope* env = EnvelopeBuilder::buildEnvelope();
134             Body* body = BodyBuilder::buildBody();
135             env->setBody(body);
136             body->getUnknownXMLObjects().push_back(fault);
137             rootElement = env->marshall();
138     
139             string xmlbuf;
140             XMLHelper::serialize(rootElement, xmlbuf);
141             istringstream s(xmlbuf);
142             log.debug("sending serialized fault");
143             long ret = genericResponse.sendError(s);
144         
145             // Cleanup by destroying XML.
146             delete env;
147             return ret;
148         }
149         catch (XMLToolingException&) {
150             // A bit weird...we have to "revert" things so that the fault is isolated
151             // so the caller can free it.
152             if (fault->getParent()) {
153                 fault->getParent()->detach();
154                 fault->detach();
155             }
156             throw;
157         }
158     }
159
160     Envelope* env = dynamic_cast<Envelope*>(xmlObject);
161     if (env) {
162         log.debug("marshalling envelope");
163         rootElement = env->marshall();
164
165         bool error =
166             (env->getBody() &&
167                 env->getBody()->hasChildren() &&
168                     dynamic_cast<Fault*>(env->getBody()->getUnknownXMLObjects().front()));
169
170         string xmlbuf;
171         XMLHelper::serialize(rootElement, xmlbuf);
172         istringstream s(xmlbuf);
173         log.debug("sending serialized envelope");
174         long ret = error ? genericResponse.sendError(s) : genericResponse.sendResponse(s);
175     
176         // Cleanup by destroying XML.
177         delete env;
178         return ret;
179     }
180
181     throw BindingException("XML content for SAML 2.0 SOAP Encoder must be a SAML 2.0 response or SOAP Fault/Envelope.");
182 }