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/MessageEncoder.h"
27 #include "binding/SAMLArtifact.h"
28 #include "saml1/core/Assertions.h"
29 #include "saml1/core/Protocols.h"
30 #include "saml2/metadata/Metadata.h"
32 #include <xmltooling/logging.h>
33 #include <xmltooling/XMLToolingConfig.h>
34 #include <xmltooling/io/HTTPResponse.h>
35 #include <xmltooling/util/NDC.h>
36 #include <xmltooling/util/URLEncoder.h>
38 using namespace opensaml::saml1;
39 using namespace opensaml::saml1p;
40 using namespace opensaml::saml2md;
41 using namespace opensaml;
42 using namespace xmlsignature;
43 using namespace xmltooling::logging;
44 using namespace xmltooling;
49 class SAML_DLLLOCAL SAML1ArtifactEncoder : public MessageEncoder
52 SAML1ArtifactEncoder() {}
53 virtual ~SAML1ArtifactEncoder() {}
56 GenericResponse& genericResponse,
58 const char* destination,
59 const EntityDescriptor* recipient=NULL,
60 const char* relayState=NULL,
61 const ArtifactGenerator* artifactGenerator=NULL,
62 const Credential* credential=NULL,
63 const XMLCh* signatureAlg=NULL,
64 const XMLCh* digestAlg=NULL
68 MessageEncoder* SAML_DLLLOCAL SAML1ArtifactEncoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
70 return new SAML1ArtifactEncoder();
75 long SAML1ArtifactEncoder::encode(
76 GenericResponse& genericResponse,
78 const char* destination,
79 const EntityDescriptor* recipient,
80 const char* relayState,
81 const ArtifactGenerator* artifactGenerator,
82 const Credential* credential,
83 const XMLCh* signatureAlg,
84 const XMLCh* digestAlg
88 xmltooling::NDC ndc("encode");
90 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1Artifact");
92 log.debug("validating input");
93 HTTPResponse* httpResponse=dynamic_cast<HTTPResponse*>(&genericResponse);
95 throw BindingException("Unable to cast response interface to HTTPResponse type.");
96 if (xmlObject->getParent())
97 throw BindingException("Cannot encode XML content with parent.");
98 Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);
100 throw BindingException("XML content for SAML 1.x Artifact Encoder must be a SAML 1.x <Assertion>.");
102 throw BindingException("SAML 1.x Artifact Encoder requires relay state (TARGET) value.");
104 // Signing is a protocol level issue, so no signing here...
106 ArtifactMap* mapper = SAMLConfig::getConfig().getArtifactMap();
108 throw BindingException("SAML 1.x Artifact Encoder requires ArtifactMap be set in configuration.");
110 // Obtain a fresh artifact.
111 if (!artifactGenerator)
112 throw BindingException("SAML 1.x Artifact Encoder requires an ArtifactGenerator instance.");
114 if (log.isDebugEnabled())
115 log.debugStream() << "marshalled assertion:" << logging::eol << *xmlObject << logging::eol;
117 auto_ptr_char recipientID(recipient ? recipient->getEntityID() : NULL);
118 log.debug("obtaining new artifact for relying party (%s)", recipientID.get() ? recipientID.get() : "unknown");
119 auto_ptr<SAMLArtifact> artifact(artifactGenerator->generateSAML1Artifact(recipient));
121 // Store the assertion. Last step in storage will be to delete the XML.
122 log.debug("storing artifact and content in map");
123 mapper->storeContent(xmlObject, artifact.get(), recipientID.get());
125 // Generate redirect.
126 string loc = destination;
127 loc += (strchr(destination,'?') ? '&' : '?');
128 const URLEncoder* escaper = XMLToolingConfig::getConfig().getURLEncoder();
129 loc = loc + "SAMLart=" + escaper->encode(artifact->encode().c_str()) + "&TARGET=" + escaper->encode(relayState);
130 log.debug("message encoded, sending redirect to client");
131 return httpResponse->sendRedirect(loc.c_str());