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