2 * Copyright 2001-2007 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/MessageEncoder.h"
26 #include "signature/ContentReference.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 class SAML_DLLLOCAL SAML1POSTEncoder : public MessageEncoder
48 SAML1POSTEncoder(const DOMElement* e);
49 virtual ~SAML1POSTEncoder() {}
52 GenericResponse& genericResponse,
54 const char* destination,
55 const char* recipientID=NULL,
56 const char* relayState=NULL,
57 const Credential* credential=NULL,
58 const XMLCh* signatureAlg=NULL,
59 const XMLCh* digestAlg=NULL
63 /** Pathname of HTML template for transmission of message via POST. */
67 MessageEncoder* SAML_DLLLOCAL SAML1POSTEncoderFactory(const DOMElement* const & e)
69 return new SAML1POSTEncoder(e);
74 static const XMLCh _template[] = UNICODE_LITERAL_8(t,e,m,p,l,a,t,e);
76 SAML1POSTEncoder::SAML1POSTEncoder(const DOMElement* e)
79 auto_ptr_char t(e->getAttributeNS(NULL, _template));
83 if (m_template.empty())
84 throw XMLToolingException("SAML1POSTEncoder requires template XML attribute.");
87 long SAML1POSTEncoder::encode(
88 GenericResponse& genericResponse,
90 const char* destination,
91 const char* recipientID,
92 const char* relayState,
93 const Credential* credential,
94 const XMLCh* signatureAlg,
95 const XMLCh* digestAlg
99 xmltooling::NDC ndc("encode");
101 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1POST");
103 log.debug("validating input");
104 if (xmlObject->getParent())
105 throw BindingException("Cannot encode XML content with parent.");
106 Response* response = dynamic_cast<Response*>(xmlObject);
108 throw BindingException("XML content for SAML 1.x POST Encoder must be a SAML 1.x <Response>.");
110 throw BindingException("SAML 1.x POST Encoder requires relay state (TARGET) value.");
112 DOMElement* rootElement = NULL;
114 // Signature based on native XML signing.
115 if (response->getSignature()) {
116 log.debug("response already signed, skipping signature operation");
119 log.debug("signing and marshalling the response");
121 // Build a Signature.
122 Signature* sig = SignatureBuilder::buildSignature();
123 response->setSignature(sig);
125 sig->setSignatureAlgorithm(signatureAlg);
127 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
129 cr->setDigestAlgorithm(digestAlg);
132 // Sign response while marshalling.
133 vector<Signature*> sigs(1,sig);
134 rootElement = response->marshall((DOMDocument*)NULL,&sigs,credential);
138 log.debug("marshalling the response");
139 rootElement = response->marshall();
143 XMLHelper::serialize(rootElement, xmlbuf);
145 XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(xmlbuf.data()),xmlbuf.size(),&len);
148 xmlbuf.append(reinterpret_cast<char*>(out),len);
149 XMLString::release(&out);
152 throw BindingException("Base64 encoding of XML failed.");
155 // Push message into template and send result to client.
156 log.debug("message encoded, sending HTML form template to client");
157 TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine();
159 throw BindingException("Encoding response using POST requires a TemplateEngine instance.");
160 ifstream infile(m_template.c_str());
162 throw BindingException("Failed to open HTML template for POST response ($1).", params(1,m_template.c_str()));
163 TemplateEngine::TemplateParameters params;
164 params.m_map["action"] = destination;
165 params.m_map["SAMLResponse"] = xmlbuf;
166 params.m_map["TARGET"] = relayState;
168 engine->run(infile, s, params);
169 genericResponse.setContentType("text/html");
170 long ret = genericResponse.sendResponse(s);
172 // Cleanup by destroying XML.