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 "binding/HTTPResponse.h"
26 #include "saml1/binding/SAML1POSTEncoder.h"
27 #include "saml1/core/Protocols.h"
31 #include <log4cpp/Category.hh>
32 #include <xercesc/util/Base64.hpp>
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/util/TemplateEngine.h>
36 using namespace opensaml::saml1p;
37 using namespace opensaml;
38 using namespace xmlsignature;
39 using namespace xmltooling;
40 using namespace log4cpp;
45 MessageEncoder* SAML_DLLLOCAL SAML1POSTEncoderFactory(const DOMElement* const & e)
47 return new SAML1POSTEncoder(e);
52 static const XMLCh templat[] = UNICODE_LITERAL_8(t,e,m,p,l,a,t,e);
54 SAML1POSTEncoder::SAML1POSTEncoder(const DOMElement* e)
57 auto_ptr_char t(e->getAttributeNS(NULL, templat));
61 if (m_template.empty())
62 throw XMLToolingException("SAML1POSTEncoder requires template attribute.");
65 SAML1POSTEncoder::~SAML1POSTEncoder() {}
67 long SAML1POSTEncoder::encode(
68 GenericResponse& genericResponse,
70 const char* destination,
71 const char* recipientID,
72 const char* relayState,
73 const CredentialResolver* credResolver,
74 const XMLCh* sigAlgorithm
78 xmltooling::NDC ndc("encode");
80 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1POST");
82 log.debug("validating input");
83 HTTPResponse* httpResponse=dynamic_cast<HTTPResponse*>(&genericResponse);
85 throw BindingException("Unable to cast response interface to HTTPResponse type.");
86 if (xmlObject->getParent())
87 throw BindingException("Cannot encode XML content with parent.");
88 Response* response = dynamic_cast<Response*>(xmlObject);
90 throw BindingException("XML content for SAML 1.x POST Encoder must be a SAML 1.x <Response>.");
92 throw BindingException("SAML 1.x POST Encoder requires relay state (TARGET) value.");
94 DOMElement* rootElement = NULL;
96 // Signature based on native XML signing.
97 if (response->getSignature()) {
98 log.debug("response already signed, skipping signature operation");
101 log.debug("signing and marshalling the response");
103 // Build a Signature.
104 Signature* sig = buildSignature(credResolver, sigAlgorithm);
105 response->setSignature(sig);
107 // Sign response while marshalling.
108 vector<Signature*> sigs(1,sig);
109 rootElement = response->marshall((DOMDocument*)NULL,&sigs);
113 log.debug("marshalling the response");
114 rootElement = response->marshall();
118 XMLHelper::serialize(rootElement, xmlbuf);
120 XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(xmlbuf.data()),xmlbuf.size(),&len);
123 xmlbuf.append(reinterpret_cast<char*>(out),len);
124 XMLString::release(&out);
127 throw BindingException("Base64 encoding of XML failed.");
130 // Push message into template and send result to client.
131 log.debug("message encoded, sending HTML form template to client");
132 TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine();
134 throw BindingException("Encoding response using POST requires a TemplateEngine instance.");
135 ifstream infile(m_template.c_str());
137 throw BindingException("Failed to open HTML template for POST response ($1).", params(1,m_template.c_str()));
138 map<string,string> params;
139 params["action"] = destination;
140 params["SAMLResponse"] = xmlbuf;
141 params["TARGET"] = relayState;
143 engine->run(infile, s, params);
144 httpResponse->setContentType("text/html");
145 long ret = httpResponse->sendResponse(s, HTTPResponse::SAML_HTTP_STATUS_OK);
147 // Cleanup by destroying XML.