Switch encoders to metadata-based recipient parameter.
[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/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"
31
32 #include <log4cpp/Category.hh>
33 #include <xmltooling/XMLToolingConfig.h>
34 #include <xmltooling/io/HTTPResponse.h>
35 #include <xmltooling/util/NDC.h>
36 #include <xmltooling/util/URLEncoder.h>
37
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;
44 using namespace log4cpp;
45 using namespace std;
46
47 namespace opensaml {
48     namespace saml1p {              
49         class SAML_DLLLOCAL SAML1ArtifactEncoder : public MessageEncoder
50         {
51         public:
52             SAML1ArtifactEncoder(const DOMElement* e) {}
53             virtual ~SAML1ArtifactEncoder() {}
54             
55             long encode(
56                 GenericResponse& genericResponse,
57                 XMLObject* xmlObject,
58                 const char* destination,
59                 const EntityDescriptor* recipient=NULL,
60                 const char* relayState=NULL,
61                 const Credential* credential=NULL,
62                 const XMLCh* signatureAlg=NULL,
63                 const XMLCh* digestAlg=NULL
64                 ) const;
65         };                
66
67         MessageEncoder* SAML_DLLLOCAL SAML1ArtifactEncoderFactory(const DOMElement* const & e)
68         {
69             return new SAML1ArtifactEncoder(e);
70         }
71     };
72 };
73
74 long SAML1ArtifactEncoder::encode(
75     GenericResponse& genericResponse,
76     XMLObject* xmlObject,
77     const char* destination,
78     const EntityDescriptor* recipient,
79     const char* relayState,
80     const Credential* credential,
81     const XMLCh* signatureAlg,
82     const XMLCh* digestAlg
83     ) const
84 {
85 #ifdef _DEBUG
86     xmltooling::NDC ndc("encode");
87 #endif
88     Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1Artifact");
89
90     log.debug("validating input");
91     HTTPResponse* httpResponse=dynamic_cast<HTTPResponse*>(&genericResponse);
92     if (!httpResponse)
93         throw BindingException("Unable to cast response interface to HTTPResponse type.");
94     if (xmlObject->getParent())
95         throw BindingException("Cannot encode XML content with parent.");
96     Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);
97     if (!assertion)
98         throw BindingException("XML content for SAML 1.x Artifact Encoder must be a SAML 1.x <Assertion>.");
99     if (!relayState)
100         throw BindingException("SAML 1.x Artifact Encoder requires relay state (TARGET) value.");
101     
102     // Signing is a protocol level issue, so no signing here...
103     
104     ArtifactMap* mapper = SAMLConfig::getConfig().getArtifactMap();
105     if (!mapper)
106         throw BindingException("SAML 1.x Artifact Encoder requires ArtifactMap be set in configuration.");
107
108     // Obtain a fresh artifact.
109     if (!m_artifactGenerator)
110         throw BindingException("SAML 1.x Artifact Encoder requires an ArtifactGenerator instance.");
111     auto_ptr_char recipientID(recipient ? recipient->getEntityID() : NULL);
112     log.debug("obtaining new artifact for relying party (%s)", recipientID.get() ? recipientID.get() : "unknown");
113     auto_ptr<SAMLArtifact> artifact(m_artifactGenerator->generateSAML1Artifact(recipient));
114     
115     // Store the assertion. Last step in storage will be to delete the XML.
116     log.debug("storing artifact and content in map");
117     mapper->storeContent(xmlObject, artifact.get(), recipientID.get());
118
119     // Generate redirect.
120     string loc = destination;
121     loc += (strchr(destination,'?') ? '&' : '?');
122     const URLEncoder* escaper = XMLToolingConfig::getConfig().getURLEncoder();
123     loc = loc + "SAMLart=" + escaper->encode(artifact->encode().c_str()) + "&TARGET=" + escaper->encode(relayState);
124     log.debug("message encoded, sending redirect to client");
125     return httpResponse->sendRedirect(loc.c_str());
126 }