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