01b79a5fd20a3cf4b363438cdef2fb250dcf158b
[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/MessageEncoder.h"
28 #include "binding/SAMLArtifact.h"
29 #include "binding/URLEncoder.h"
30 #include "saml1/core/Assertions.h"
31 #include "saml1/core/Protocols.h"
32
33 #include <log4cpp/Category.hh>
34 #include <xmltooling/util/NDC.h>
35
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;
42 using namespace std;
43
44 namespace opensaml {
45     namespace saml1p {              
46         class SAML_DLLLOCAL SAML1ArtifactEncoder : public MessageEncoder
47         {
48         public:
49             SAML1ArtifactEncoder(const DOMElement* e) {}
50             virtual ~SAML1ArtifactEncoder() {}
51             
52             long encode(
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
60                 ) const;
61         };                
62
63         MessageEncoder* SAML_DLLLOCAL SAML1ArtifactEncoderFactory(const DOMElement* const & e)
64         {
65             return new SAML1ArtifactEncoder(e);
66         }
67     };
68 };
69
70 long SAML1ArtifactEncoder::encode(
71     GenericResponse& genericResponse,
72     XMLObject* xmlObject,
73     const char* destination,
74     const char* recipientID,
75     const char* relayState,
76     const CredentialResolver* credResolver,
77     const XMLCh* sigAlgorithm
78     ) const
79 {
80 #ifdef _DEBUG
81     xmltooling::NDC ndc("encode");
82 #endif
83     Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1Artifact");
84
85     log.debug("validating input");
86     HTTPResponse* httpResponse=dynamic_cast<HTTPResponse*>(&genericResponse);
87     if (!httpResponse)
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);
92     if (!assertion)
93         throw BindingException("XML content for SAML 1.x Artifact Encoder must be a SAML 1.x <Assertion>.");
94     if (!relayState)
95         throw BindingException("SAML 1.x Artifact Encoder requires relay state (TARGET) value.");
96     
97     // Signing is a protocol level issue, so no signing here...
98     
99     ArtifactMap* mapper = SAMLConfig::getConfig().getArtifactMap();
100     if (!mapper)
101         throw BindingException("SAML 1.x Artifact Encoder requires ArtifactMap be set in configuration.");
102
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));
108     
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);
112
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());
120 }