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::saml2md;
38 using namespace opensaml;
39 using namespace xmlsignature;
40 using namespace xmltooling;
41 using namespace log4cpp;
46 class SAML_DLLLOCAL SAML1POSTEncoder : public MessageEncoder
49 SAML1POSTEncoder(const DOMElement* e);
50 virtual ~SAML1POSTEncoder() {}
53 GenericResponse& genericResponse,
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
65 /** Pathname of HTML template for transmission of message via POST. */
69 MessageEncoder* SAML_DLLLOCAL SAML1POSTEncoderFactory(const DOMElement* const & e)
71 return new SAML1POSTEncoder(e);
76 static const XMLCh _template[] = UNICODE_LITERAL_8(t,e,m,p,l,a,t,e);
78 SAML1POSTEncoder::SAML1POSTEncoder(const DOMElement* e)
81 auto_ptr_char t(e->getAttributeNS(NULL, _template));
85 if (m_template.empty())
86 throw XMLToolingException("SAML1POSTEncoder requires template XML attribute.");
89 long SAML1POSTEncoder::encode(
90 GenericResponse& genericResponse,
92 const char* destination,
93 const EntityDescriptor* recipient,
94 const char* relayState,
95 const ArtifactGenerator* artifactGenerator,
96 const Credential* credential,
97 const XMLCh* signatureAlg,
98 const XMLCh* digestAlg
102 xmltooling::NDC ndc("encode");
104 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1POST");
106 log.debug("validating input");
107 if (xmlObject->getParent())
108 throw BindingException("Cannot encode XML content with parent.");
109 Response* response = dynamic_cast<Response*>(xmlObject);
111 throw BindingException("XML content for SAML 1.x POST Encoder must be a SAML 1.x <Response>.");
113 throw BindingException("SAML 1.x POST Encoder requires relay state (TARGET) value.");
115 DOMElement* rootElement = NULL;
117 // Signature based on native XML signing.
118 if (response->getSignature()) {
119 log.debug("response already signed, skipping signature operation");
122 log.debug("signing and marshalling the response");
124 // Build a Signature.
125 Signature* sig = SignatureBuilder::buildSignature();
126 response->setSignature(sig);
128 sig->setSignatureAlgorithm(signatureAlg);
130 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
132 cr->setDigestAlgorithm(digestAlg);
135 // Sign response while marshalling.
136 vector<Signature*> sigs(1,sig);
137 rootElement = response->marshall((DOMDocument*)NULL,&sigs,credential);
141 log.debug("marshalling the response");
142 rootElement = response->marshall();
146 XMLHelper::serialize(rootElement, xmlbuf);
148 XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(xmlbuf.data()),xmlbuf.size(),&len);
151 xmlbuf.append(reinterpret_cast<char*>(out),len);
152 XMLString::release(&out);
155 throw BindingException("Base64 encoding of XML failed.");
158 // Push message into template and send result to client.
159 log.debug("message encoded, sending HTML form template to client");
160 TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine();
162 throw BindingException("Encoding response using POST requires a TemplateEngine instance.");
163 ifstream infile(m_template.c_str());
165 throw BindingException("Failed to open HTML template for POST response ($1).", params(1,m_template.c_str()));
166 TemplateEngine::TemplateParameters params;
167 params.m_map["action"] = destination;
168 params.m_map["SAMLResponse"] = xmlbuf;
169 params.m_map["TARGET"] = relayState;
171 engine->run(infile, s, params);
172 genericResponse.setContentType("text/html");
173 long ret = genericResponse.sendResponse(s);
175 // Cleanup by destroying XML.