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