2 * Copyright 2001-2006 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SAML1POSTEncoder.cpp
20 * SAML 1.x POST binding/profile message encoder
24 #include "exceptions.h"
25 #include "saml1/binding/SAML1POSTEncoder.h"
26 #include "saml1/core/Protocols.h"
30 #include <log4cpp/Category.hh>
31 #include <xercesc/util/Base64.hpp>
32 #include <xmltooling/util/NDC.h>
33 #include <xmltooling/util/TemplateEngine.h>
35 using namespace opensaml::saml1p;
36 using namespace opensaml;
37 using namespace xmlsignature;
38 using namespace xmltooling;
39 using namespace log4cpp;
44 MessageEncoder* SAML_DLLLOCAL SAML1POSTEncoderFactory(const DOMElement* const & e)
46 return new SAML1POSTEncoder(e);
51 static const XMLCh templat[] = UNICODE_LITERAL_8(t,e,m,p,l,a,t,e);
53 SAML1POSTEncoder::SAML1POSTEncoder(const DOMElement* e)
56 auto_ptr_char t(e->getAttributeNS(NULL, templat));
60 if (m_template.empty())
61 throw XMLToolingException("SAML1POSTEncoder requires template attribute.");
64 SAML1POSTEncoder::~SAML1POSTEncoder() {}
66 long SAML1POSTEncoder::encode(
67 HTTPResponse& httpResponse,
69 const char* destination,
70 const char* recipientID,
71 const char* relayState,
72 const CredentialResolver* credResolver,
73 const XMLCh* sigAlgorithm
77 xmltooling::NDC ndc("encode");
79 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1POST");
80 log.debug("validating input");
82 if (xmlObject->getParent())
83 throw BindingException("Cannot encode XML content with parent.");
84 Response* response = dynamic_cast<Response*>(xmlObject);
86 throw BindingException("XML content for SAML 1.x POST Encoder must be a SAML 1.x <Response>.");
88 throw BindingException("SAML 1.x POST Encoder requires relay state (TARGET) value.");
90 DOMElement* rootElement = NULL;
92 // Signature based on native XML signing.
93 if (response->getSignature()) {
94 log.debug("response already signed, skipping signature operation");
97 log.debug("signing and marshalling the response");
100 Signature* sig = buildSignature(credResolver, sigAlgorithm);
101 response->setSignature(sig);
103 // Sign response while marshalling.
104 vector<Signature*> sigs(1,sig);
105 rootElement = response->marshall((DOMDocument*)NULL,&sigs);
109 log.debug("marshalling the response");
110 rootElement = response->marshall();
114 XMLHelper::serialize(rootElement, xmlbuf);
116 XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(xmlbuf.data()),xmlbuf.size(),&len);
119 xmlbuf.append(reinterpret_cast<char*>(out),len);
120 XMLString::release(&out);
123 throw BindingException("Base64 encoding of XML failed.");
126 // Push message into template and send result to client.
127 log.debug("message encoded, sending HTML form template to client");
128 TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine();
130 throw BindingException("Encoding response using POST requires a TemplateEngine instance.");
131 ifstream infile(m_template.c_str());
133 throw BindingException("Failed to open HTML template for POST response ($1).", params(1,m_template.c_str()));
134 map<string,string> params;
135 params["action"] = destination;
136 params["SAMLResponse"] = xmlbuf;
137 params["TARGET"] = relayState;
139 engine->run(infile, s, params);
140 long ret = httpResponse.sendResponse(s);
142 // Cleanup by destroying XML.