SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1SOAPClient.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  * SAML1SOAPClient.cpp
23  * 
24  * Specialized SOAPClient for SAML 1.x SOAP binding.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "binding/SecurityPolicy.h"
30 #include "binding/SOAPClient.h"
31 #include "saml1/binding/SAML1SOAPClient.h"
32 #include "saml1/core/Protocols.h"
33 #include "saml2/metadata/Metadata.h"
34
35 #include <xmltooling/logging.h>
36 #include <xmltooling/soap/SOAP.h>
37
38 using namespace opensaml::saml1p;
39 using namespace opensaml::saml2md;
40 using namespace opensaml;
41 using namespace soap11;
42 using namespace xmltooling::logging;
43 using namespace xmltooling;
44 using namespace std;
45
46 SAML1SOAPClient::SAML1SOAPClient(opensaml::SOAPClient& soaper, bool fatalSAMLErrors) : m_soaper(soaper), m_fatal(fatalSAMLErrors), m_correlate(nullptr)
47 {
48 }
49
50 SAML1SOAPClient::~SAML1SOAPClient()
51 {
52     XMLString::release(&m_correlate);
53 }
54
55 void SAML1SOAPClient::sendSAML(Request* request, const char* from, MetadataCredentialCriteria& to, const char* endpoint)
56 {
57     auto_ptr<Envelope> env(EnvelopeBuilder::buildEnvelope());
58     Body* body = BodyBuilder::buildBody();
59     env->setBody(body);
60     body->getUnknownXMLObjects().push_back(request);
61     m_soaper.send(*env.get(), from, to, endpoint);
62     m_correlate = XMLString::replicate(request->getRequestID());
63 }
64
65 Response* SAML1SOAPClient::receiveSAML()
66 {
67     auto_ptr<Envelope> env(m_soaper.receive());
68     if (env.get()) {
69         Body* body = env->getBody();
70         if (body && body->hasChildren()) {
71             // Check for SAML Response.
72             Response* response = dynamic_cast<Response*>(body->getUnknownXMLObjects().front());
73             if (response) {
74
75                 // Check InResponseTo.
76                 if (m_correlate && response->getInResponseTo() && !XMLString::equals(m_correlate, response->getInResponseTo()))
77                     throw SecurityPolicyException("InResponseTo attribute did not correlate with the Request ID.");
78                 
79                 m_soaper.getPolicy().reset(true);
80
81                 // Extract Response details and run policy against it.
82                 // We don't pull Issuer out of any assertions because some profiles may permit
83                 // alternate issuers at that layer.
84                 m_soaper.getPolicy().setMessageID(response->getResponseID());
85                 m_soaper.getPolicy().setIssueInstant(response->getIssueInstantEpoch());
86                 m_soaper.getPolicy().evaluate(*response);
87                 
88                 // Check Status.
89                 Status* status = response->getStatus();
90                 if (status) {
91                     const xmltooling::QName* code = status->getStatusCode() ? status->getStatusCode()->getValue() : nullptr;
92                     if (code && *code != StatusCode::SUCCESS && handleError(*status)) {
93                         BindingException ex("SAML Response contained an error.");
94                         if (m_soaper.getPolicy().getIssuerMetadata())
95                             annotateException(&ex, m_soaper.getPolicy().getIssuerMetadata(), status);   // throws it
96                         else
97                             ex.raise();
98                     }
99                 }
100                 
101                 env.release();
102                 body->detach(); // frees Envelope
103                 response->detach();   // frees Body
104                 return response;
105             }
106         }
107         
108         BindingException ex("SOAP Envelope did not contain a SAML Response or a Fault.");
109         if (m_soaper.getPolicy().getIssuerMetadata())
110             annotateException(&ex, m_soaper.getPolicy().getIssuerMetadata());   // throws it
111         else
112             ex.raise();
113     }
114     return nullptr;
115 }
116
117 bool SAML1SOAPClient::handleError(const saml1p::Status& status)
118 {
119     const xmltooling::QName* code = status.getStatusCode() ? status.getStatusCode()->getValue() : nullptr;
120     auto_ptr_char str((status.getStatusMessage() ? status.getStatusMessage()->getMessage() : nullptr));
121     Category::getInstance(SAML_LOGCAT ".SOAPClient").error(
122         "SOAP client detected a SAML error: (%s) (%s)",
123         (code ? code->toString().c_str() : "no code"),
124         (str.get() ? str.get() : "no message")
125         );
126     return m_fatal;
127 }