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 * SAML1ArtifactEncoder.cpp
20 * SAML 1.x Artifact binding/profile message encoder
24 #include "exceptions.h"
25 #include "binding/ArtifactMap.h"
26 #include "binding/HTTPResponse.h"
27 #include "binding/MessageEncoder.h"
28 #include "binding/SAMLArtifact.h"
29 #include "binding/URLEncoder.h"
30 #include "saml1/core/Assertions.h"
31 #include "saml1/core/Protocols.h"
33 #include <log4cpp/Category.hh>
34 #include <xmltooling/util/NDC.h>
36 using namespace opensaml::saml1;
37 using namespace opensaml::saml1p;
38 using namespace opensaml;
39 using namespace xmlsignature;
40 using namespace xmltooling;
41 using namespace log4cpp;
46 class SAML_DLLLOCAL SAML1ArtifactEncoder : public MessageEncoder
49 SAML1ArtifactEncoder(const DOMElement* e) {}
50 virtual ~SAML1ArtifactEncoder() {}
53 GenericResponse& genericResponse,
54 xmltooling::XMLObject* xmlObject,
55 const char* destination,
56 const char* recipientID=NULL,
57 const char* relayState=NULL,
58 const xmltooling::CredentialResolver* credResolver=NULL,
59 const XMLCh* sigAlgorithm=NULL
63 MessageEncoder* SAML_DLLLOCAL SAML1ArtifactEncoderFactory(const DOMElement* const & e)
65 return new SAML1ArtifactEncoder(e);
70 long SAML1ArtifactEncoder::encode(
71 GenericResponse& genericResponse,
73 const char* destination,
74 const char* recipientID,
75 const char* relayState,
76 const CredentialResolver* credResolver,
77 const XMLCh* sigAlgorithm
81 xmltooling::NDC ndc("encode");
83 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1Artifact");
85 log.debug("validating input");
86 HTTPResponse* httpResponse=dynamic_cast<HTTPResponse*>(&genericResponse);
88 throw BindingException("Unable to cast response interface to HTTPResponse type.");
89 if (xmlObject->getParent())
90 throw BindingException("Cannot encode XML content with parent.");
91 Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);
93 throw BindingException("XML content for SAML 1.x Artifact Encoder must be a SAML 1.x <Assertion>.");
95 throw BindingException("SAML 1.x Artifact Encoder requires relay state (TARGET) value.");
97 // Signing is a protocol level issue, so no signing here...
99 ArtifactMap* mapper = SAMLConfig::getConfig().getArtifactMap();
101 throw BindingException("SAML 1.x Artifact Encoder requires ArtifactMap be set in configuration.");
103 // Obtain a fresh artifact.
104 if (!m_artifactGenerator)
105 throw BindingException("SAML 1.x Artifact Encoder requires an ArtifactGenerator instance.");
106 log.debug("obtaining new artifact for relying party (%s)", recipientID ? recipientID : "unknown");
107 auto_ptr<SAMLArtifact> artifact(m_artifactGenerator->generateSAML1Artifact(recipientID));
109 // Store the assertion. Last step in storage will be to delete the XML.
110 log.debug("storing artifact and content in map");
111 mapper->storeContent(xmlObject, artifact.get(), recipientID);
113 // Generate redirect.
114 string loc = destination;
115 loc += (strchr(destination,'?') ? '&' : '?');
116 URLEncoder* escaper = SAMLConfig::getConfig().getURLEncoder();
117 loc = loc + "SAMLart=" + escaper->encode(artifact->encode().c_str()) + "&TARGET=" + escaper->encode(relayState);
118 log.debug("message encoded, sending redirect to client");
119 return httpResponse->sendRedirect(loc.c_str());