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