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