Revamped encoders to produce the actual HTTP responses.
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1ArtifactEncoder.cpp
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * SAML1ArtifactEncoder.cpp
19  * 
20  * SAML 1.x Artifact binding/profile message encoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/ArtifactMap.h"
26 #include "binding/SAMLArtifact.h"
27 #include "binding/URLEncoder.h"
28 #include "saml1/binding/SAML1ArtifactEncoder.h"
29 #include "saml1/core/Assertions.h"
30
31 #include <log4cpp/Category.hh>
32 #include <xmltooling/util/NDC.h>
33
34 using namespace opensaml::saml1;
35 using namespace opensaml::saml1p;
36 using namespace opensaml;
37 using namespace xmlsignature;
38 using namespace xmltooling;
39 using namespace log4cpp;
40 using namespace std;
41
42 namespace opensaml {
43     namespace saml1p {              
44         MessageEncoder* SAML_DLLLOCAL SAML1ArtifactEncoderFactory(const DOMElement* const & e)
45         {
46             return new SAML1ArtifactEncoder(e);
47         }
48     };
49 };
50
51 SAML1ArtifactEncoder::SAML1ArtifactEncoder(const DOMElement* e) {}
52
53 SAML1ArtifactEncoder::~SAML1ArtifactEncoder() {}
54
55 long SAML1ArtifactEncoder::encode(
56     HTTPResponse& httpResponse,
57     XMLObject* xmlObject,
58     const char* destination,
59     const char* recipientID,
60     const char* relayState,
61     const CredentialResolver* credResolver,
62     const XMLCh* sigAlgorithm
63     ) const
64 {
65 #ifdef _DEBUG
66     xmltooling::NDC ndc("encode");
67 #endif
68     Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1Artifact");
69     log.debug("validating input");
70     
71     if (xmlObject->getParent())
72         throw BindingException("Cannot encode XML content with parent.");
73     Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);
74     if (!assertion)
75         throw BindingException("XML content for SAML 1.x Artifact Encoder must be a SAML 1.x <Assertion>.");
76     if (!relayState)
77         throw BindingException("SAML 1.x Artifact Encoder requires relay state (TARGET) value.");
78     
79     // Signing is a protocol level issue, so no signing here...
80     
81     ArtifactMap* mapper = SAMLConfig::getConfig().getArtifactMap();
82     if (!mapper)
83         throw BindingException("SAML 1.x Artifact Encoder requires ArtifactMap be set in configuration.");
84
85     // Obtain a fresh artifact.
86     if (!m_artifactGenerator)
87         throw BindingException("SAML 1.x Artifact Encoder requires an ArtifactGenerator instance.");
88     log.debug("obtaining new artifact for relying party (%s)", recipientID ? recipientID : "unknown");
89     auto_ptr<SAMLArtifact> artifact(m_artifactGenerator->generateSAML1Artifact(recipientID));
90     
91     // Store the assertion. Last step in storage will be to delete the XML.
92     log.debug("storing artifact and content in map");
93     mapper->storeContent(xmlObject, artifact.get(), recipientID);
94
95     // Generate redirect.
96     string loc = destination;
97     loc += (strchr(destination,'?') ? '&' : '?');
98     URLEncoder* escaper = SAMLConfig::getConfig().getURLEncoder();
99     loc = loc + "SAMLart=" + escaper->encode(artifact->encode().c_str()) + "&TARGET=" + escaper->encode(relayState);
100     log.debug("message encoded, sending redirect to client");
101     return httpResponse.sendRedirect(loc.c_str());
102 }