SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1ArtifactEncoder.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * SAML1ArtifactEncoder.cpp
23  * 
24  * SAML 1.x Artifact binding/profile message encoder
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "binding/ArtifactMap.h"
30 #include "binding/MessageEncoder.h"
31 #include "binding/SAMLArtifact.h"
32 #include "saml1/core/Assertions.h"
33 #include "saml1/core/Protocols.h"
34 #include "saml2/metadata/Metadata.h"
35
36 #include <xmltooling/logging.h>
37 #include <xmltooling/XMLToolingConfig.h>
38 #include <xmltooling/io/HTTPResponse.h>
39 #include <xmltooling/util/NDC.h>
40 #include <xmltooling/util/URLEncoder.h>
41
42 using namespace opensaml::saml1;
43 using namespace opensaml::saml1p;
44 using namespace opensaml::saml2md;
45 using namespace opensaml;
46 using namespace xmlsignature;
47 using namespace xmltooling::logging;
48 using namespace xmltooling;
49 using namespace std;
50
51 namespace opensaml {
52     namespace saml1p {              
53         class SAML_DLLLOCAL SAML1ArtifactEncoder : public MessageEncoder
54         {
55         public:
56             SAML1ArtifactEncoder() {}
57             virtual ~SAML1ArtifactEncoder() {}
58
59             const XMLCh* getProtocolFamily() const {
60                 return samlconstants::SAML11_PROTOCOL_ENUM;
61             }
62
63             long encode(
64                 GenericResponse& genericResponse,
65                 XMLObject* xmlObject,
66                 const char* destination,
67                 const EntityDescriptor* recipient=nullptr,
68                 const char* relayState=nullptr,
69                 const ArtifactGenerator* artifactGenerator=nullptr,
70                 const Credential* credential=nullptr,
71                 const XMLCh* signatureAlg=nullptr,
72                 const XMLCh* digestAlg=nullptr
73                 ) const;
74         };                
75
76         MessageEncoder* SAML_DLLLOCAL SAML1ArtifactEncoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
77         {
78             return new SAML1ArtifactEncoder();
79         }
80     };
81 };
82
83 long SAML1ArtifactEncoder::encode(
84     GenericResponse& genericResponse,
85     XMLObject* xmlObject,
86     const char* destination,
87     const EntityDescriptor* recipient,
88     const char* relayState,
89     const ArtifactGenerator* artifactGenerator,
90     const Credential* credential,
91     const XMLCh* signatureAlg,
92     const XMLCh* digestAlg
93     ) const
94 {
95 #ifdef _DEBUG
96     xmltooling::NDC ndc("encode");
97 #endif
98     Category& log = Category::getInstance(SAML_LOGCAT ".MessageEncoder.SAML1Artifact");
99
100     log.debug("validating input");
101     HTTPResponse* httpResponse=dynamic_cast<HTTPResponse*>(&genericResponse);
102     if (!httpResponse)
103         throw BindingException("Unable to cast response interface to HTTPResponse type.");
104     if (xmlObject->getParent())
105         throw BindingException("Cannot encode XML content with parent.");
106     Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);
107     if (!assertion)
108         throw BindingException("XML content for SAML 1.x Artifact Encoder must be a SAML 1.x <Assertion>.");
109     if (!relayState)
110         throw BindingException("SAML 1.x Artifact Encoder requires relay state (TARGET) value.");
111     
112     // Signing is a protocol level issue, so no signing here...
113     
114     ArtifactMap* mapper = SAMLConfig::getConfig().getArtifactMap();
115     if (!mapper)
116         throw BindingException("SAML 1.x Artifact Encoder requires ArtifactMap be set in configuration.");
117
118     // Obtain a fresh artifact.
119     if (!artifactGenerator)
120         throw BindingException("SAML 1.x Artifact Encoder requires an ArtifactGenerator instance.");
121     
122     if (log.isDebugEnabled())
123         log.debugStream() << "marshalled assertion:" << logging::eol << *xmlObject << logging::eol;
124     
125     auto_ptr_char recipientID(recipient ? recipient->getEntityID() : nullptr);
126     log.debug("obtaining new artifact for relying party (%s)", recipientID.get() ? recipientID.get() : "unknown");
127     auto_ptr<SAMLArtifact> artifact(artifactGenerator->generateSAML1Artifact(recipient));
128     
129     // Store the assertion. Last step in storage will be to delete the XML.
130     log.debug("storing artifact and content in map");
131     mapper->storeContent(xmlObject, artifact.get(), recipientID.get());
132
133     // Generate redirect.
134     string loc = destination;
135     loc += (strchr(destination,'?') ? '&' : '?');
136     const URLEncoder* escaper = XMLToolingConfig::getConfig().getURLEncoder();
137     loc = loc + "SAMLart=" + escaper->encode(artifact->encode().c_str()) + "&TARGET=" + escaper->encode(relayState);
138     log.debug("message encoded, sending redirect to client");
139     return httpResponse->sendRedirect(loc.c_str());
140 }