First SOAP encoder.
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1POSTEncoder.cpp
1 /*
2  *  Copyright 2001-2006 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  * SAML1POSTEncoder.cpp
19  * 
20  * SAML 1.x POST binding/profile message encoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "saml1/binding/SAML1POSTEncoder.h"
26 #include "saml1/core/Protocols.h"
27
28 #include <fstream>
29 #include <sstream>
30 #include <log4cpp/Category.hh>
31 #include <xercesc/util/Base64.hpp>
32 #include <xmltooling/util/NDC.h>
33 #include <xmltooling/util/TemplateEngine.h>
34
35 using namespace opensaml::saml1p;
36 using namespace opensaml;
37 using namespace xmlsignature;
38 using namespace xmltooling;
39 using namespace log4cpp;
40 using namespace std;
41
42 namespace opensaml {
43     namespace saml1p {              
44         MessageEncoder* SAML_DLLLOCAL SAML1POSTEncoderFactory(const DOMElement* const & e)
45         {
46             return new SAML1POSTEncoder(e);
47         }
48     };
49 };
50
51 static const XMLCh templat[] = UNICODE_LITERAL_8(t,e,m,p,l,a,t,e);
52
53 SAML1POSTEncoder::SAML1POSTEncoder(const DOMElement* e)
54 {
55     if (e) {
56         auto_ptr_char t(e->getAttributeNS(NULL, templat));
57         if (t.get())
58             m_template = t.get();
59     }
60     if (m_template.empty())
61         throw XMLToolingException("SAML1POSTEncoder requires template attribute.");
62 }
63
64 long SAML1POSTEncoder::encode(
65     GenericResponse& genericResponse,
66     XMLObject* xmlObject,
67     const char* destination,
68     const char* recipientID,
69     const char* relayState,
70     const CredentialResolver* credResolver,
71     const XMLCh* sigAlgorithm
72     ) const
73 {
74 #ifdef _DEBUG
75     xmltooling::NDC ndc("encode");
76 #endif
77     Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1POST");
78
79     log.debug("validating input");
80     if (xmlObject->getParent())
81         throw BindingException("Cannot encode XML content with parent.");
82     Response* response = dynamic_cast<Response*>(xmlObject);
83     if (!response)
84         throw BindingException("XML content for SAML 1.x POST Encoder must be a SAML 1.x <Response>.");
85     if (!relayState)
86         throw BindingException("SAML 1.x POST Encoder requires relay state (TARGET) value.");
87     
88     DOMElement* rootElement = NULL;
89     if (credResolver) {
90         // Signature based on native XML signing.
91         if (response->getSignature()) {
92             log.debug("response already signed, skipping signature operation");
93         }
94         else {
95             log.debug("signing and marshalling the response");
96
97             // Build a Signature.
98             Signature* sig = buildSignature(credResolver, sigAlgorithm);
99             response->setSignature(sig);
100     
101             // Sign response while marshalling.
102             vector<Signature*> sigs(1,sig);
103             rootElement = response->marshall((DOMDocument*)NULL,&sigs);
104         }
105     }
106     else {
107         log.debug("marshalling the response");
108         rootElement = response->marshall();
109     }
110     
111     string xmlbuf;
112     XMLHelper::serialize(rootElement, xmlbuf);
113     unsigned int len=0;
114     XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(xmlbuf.data()),xmlbuf.size(),&len);
115     if (out) {
116         xmlbuf.erase();
117         xmlbuf.append(reinterpret_cast<char*>(out),len);
118         XMLString::release(&out);
119     }
120     else {
121         throw BindingException("Base64 encoding of XML failed.");
122     }
123
124     // Push message into template and send result to client.
125     log.debug("message encoded, sending HTML form template to client");
126     TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine();
127     if (!engine)
128         throw BindingException("Encoding response using POST requires a TemplateEngine instance.");
129     ifstream infile(m_template.c_str());
130     if (!infile)
131         throw BindingException("Failed to open HTML template for POST response ($1).", params(1,m_template.c_str()));
132     map<string,string> params;
133     params["action"] = destination;
134     params["SAMLResponse"] = xmlbuf;
135     params["TARGET"] = relayState;
136     stringstream s;
137     engine->run(infile, s, params);
138     genericResponse.setContentType("text/html");
139     long ret = genericResponse.sendResponse(s);
140
141     // Cleanup by destroying XML.
142     delete xmlObject;
143     return ret;
144 }