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