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 * SAML2ArtifactEncoder.cpp
20 * SAML 2.0 HTTP-Artifact binding message encoder
24 #include "exceptions.h"
25 #include "binding/ArtifactMap.h"
26 #include "binding/MessageEncoder.h"
27 #include "saml2/binding/SAML2Artifact.h"
28 #include "saml2/core/Protocols.h"
32 #include <log4cpp/Category.hh>
33 #include <xmltooling/io/HTTPResponse.h>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/util/TemplateEngine.h>
36 #include <xmltooling/util/URLEncoder.h>
38 using namespace opensaml::saml2p;
39 using namespace opensaml;
40 using namespace xmlsignature;
41 using namespace xmltooling;
42 using namespace log4cpp;
47 class SAML_DLLLOCAL SAML2ArtifactEncoder : public MessageEncoder
50 SAML2ArtifactEncoder(const DOMElement* e);
51 virtual ~SAML2ArtifactEncoder() {}
54 GenericResponse& genericResponse,
56 const char* destination,
57 const char* recipientID=NULL,
58 const char* relayState=NULL,
59 const Credential* credential=NULL,
60 const XMLCh* signatureAlg=NULL,
61 const XMLCh* digestAlg=NULL
69 MessageEncoder* SAML_DLLLOCAL SAML2ArtifactEncoderFactory(const DOMElement* const & e)
71 return new SAML2ArtifactEncoder(e);
75 static const XMLCh _template[] = UNICODE_LITERAL_8(t,e,m,p,l,a,t,e);
76 static const XMLCh postArtifact[] = UNICODE_LITERAL_12(p,o,s,t,A,r,t,i,f,a,c,t);
79 SAML2ArtifactEncoder::SAML2ArtifactEncoder(const DOMElement* e) : m_post(false)
82 const XMLCh* flag = e->getAttributeNS(NULL, postArtifact);
83 m_post = (flag && (*flag==chLatin_t || *flag==chDigit_1));
85 auto_ptr_char t(e->getAttributeNS(NULL, _template));
92 long SAML2ArtifactEncoder::encode(
93 GenericResponse& genericResponse,
95 const char* destination,
96 const char* recipientID,
97 const char* relayState,
98 const Credential* credential,
99 const XMLCh* signatureAlg,
100 const XMLCh* digestAlg
104 xmltooling::NDC ndc("encode");
106 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2Artifact");
108 log.debug("validating input");
109 HTTPResponse* httpResponse=dynamic_cast<HTTPResponse*>(&genericResponse);
111 throw BindingException("Unable to cast response interface to HTTPResponse type.");
112 if (relayState && strlen(relayState)>80)
113 throw BindingException("RelayState cannot exceed 80 bytes in length.");
115 if (xmlObject->getParent())
116 throw BindingException("Cannot encode XML content with parent.");
118 StatusResponseType* response = NULL;
119 RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(xmlObject);
121 response = dynamic_cast<StatusResponseType*>(xmlObject);
123 throw BindingException("XML content for SAML 2.0 HTTP-Artifact Encoder must be a SAML 2.0 protocol message.");
125 ArtifactMap* mapper = SAMLConfig::getConfig().getArtifactMap();
127 throw BindingException("SAML 2.0 HTTP-Artifact Encoder requires ArtifactMap be set in configuration.");
129 // Obtain a fresh artifact.
130 if (!m_artifactGenerator)
131 throw BindingException("SAML 2.0 HTTP-Artifact Encoder requires an ArtifactGenerator instance.");
132 log.debug("obtaining new artifact for relying party (%s)", recipientID ? recipientID : "unknown");
133 auto_ptr<SAMLArtifact> artifact(m_artifactGenerator->generateSAML2Artifact(recipientID));
136 // Signature based on native XML signing.
137 if (request ? request->getSignature() : response->getSignature()) {
138 log.debug("message already signed, skipping signature operation");
141 log.debug("signing the message");
143 // Build a Signature.
144 Signature* sig = SignatureBuilder::buildSignature();
145 request ? request->setSignature(sig) : response->setSignature(sig);
147 sig->setSignatureAlgorithm(signatureAlg);
149 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
151 cr->setDigestAlgorithm(digestAlg);
154 // Sign response while marshalling.
155 vector<Signature*> sigs(1,sig);
156 xmlObject->marshall((DOMDocument*)NULL,&sigs,credential);
160 // Store the message. Last step in storage will be to delete the XML.
161 log.debug("storing artifact and content in map");
162 mapper->storeContent(xmlObject, artifact.get(), recipientID);
164 if (m_template.empty()) {
165 // Generate redirect.
166 string loc = destination;
167 loc += (strchr(destination,'?') ? '&' : '?');
168 const URLEncoder* escaper = XMLToolingConfig::getConfig().getURLEncoder();
169 loc = loc + "SAMLart=" + escaper->encode(artifact->encode().c_str());
170 if (relayState && *relayState)
171 loc = loc + "&RelayState=" + escaper->encode(relayState);
172 log.debug("message encoded, sending redirect to client");
173 return httpResponse->sendRedirect(loc.c_str());
176 // Push message into template and send result to client.
177 log.debug("message encoded, sending HTML form template to client");
178 TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine();
180 throw BindingException("Encoding artifact using POST requires a TemplateEngine instance.");
181 ifstream infile(m_template.c_str());
183 throw BindingException("Failed to open HTML template for POST response ($1).", params(1,m_template.c_str()));
184 TemplateEngine::TemplateParameters params;
185 params.m_map["action"] = destination;
186 params.m_map["SAMLart"] = artifact->encode();
187 if (relayState && *relayState)
188 params.m_map["RelayState"] = relayState;
190 engine->run(infile, s, params);
191 httpResponse->setContentType("text/html");
192 return httpResponse->sendResponse(s);