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