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