Convert logging to log4shib via compile time switch.
[shibboleth/opensaml2.git] / saml / saml2 / binding / impl / SAML2SOAPClient.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  * SAML2SOAPClient.cpp
19  * 
20  * Specialized SOAPClient for SAML 2.0 SOAP binding.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "saml2/binding/SAML2SOAPClient.h"
26 #include "saml2/core/Protocols.h"
27 #include "saml2/metadata/Metadata.h"
28
29 #include <xmltooling/logging.h>
30 #include <xmltooling/soap/SOAP.h>
31
32 using namespace opensaml::saml2p;
33 using namespace opensaml::saml2md;
34 using namespace opensaml;
35 using namespace soap11;
36 using namespace xmltooling::logging;
37 using namespace xmltooling;
38 using namespace std;
39
40 void SAML2SOAPClient::sendSAML(RequestAbstractType* request, MetadataCredentialCriteria& peer, const char* endpoint)
41 {
42     auto_ptr<Envelope> env(EnvelopeBuilder::buildEnvelope());
43     Body* body = BodyBuilder::buildBody();
44     env->setBody(body);
45     body->getUnknownXMLObjects().push_back(request);
46     m_soaper.send(*env.get(), peer, endpoint);
47     m_correlate = XMLString::replicate(request->getID());
48 }
49
50 StatusResponseType* SAML2SOAPClient::receiveSAML()
51 {
52     auto_ptr<Envelope> env(m_soaper.receive());
53     if (env.get()) {
54         Body* body = env->getBody();
55         if (body && body->hasChildren()) {
56             // Check for SAML Response.
57             StatusResponseType* response = dynamic_cast<StatusResponseType*>(body->getUnknownXMLObjects().front());
58             if (response) {
59                 
60                 // Check InResponseTo.
61                 if (m_correlate && response->getInResponseTo() && !XMLString::equals(m_correlate, response->getInResponseTo()))
62                     throw SecurityPolicyException("InResponseTo attribute did not correlate with the Request ID.");
63
64                 m_soaper.getPolicy().evaluate(*response);
65                 if (!m_soaper.getPolicy().isSecure()) {
66                     SecurityPolicyException ex("Security policy could not authenticate the message.");
67                     annotateException(&ex, m_soaper.getPolicy().getIssuerMetadata(), response->getStatus());   // throws it
68                 }
69
70                 // Check Status.
71                 Status* status = response->getStatus();
72                 if (status) {
73                     const XMLCh* code = status->getStatusCode() ? status->getStatusCode()->getValue() : NULL;
74                     if (code && !XMLString::equals(code,StatusCode::SUCCESS) && handleError(*status)) {
75                         BindingException ex("SAML response contained an error.");
76                         annotateException(&ex, m_soaper.getPolicy().getIssuerMetadata(), status);   // throws it
77                     }
78                 }
79                 
80                 env.release();
81                 body->detach(); // frees Envelope
82                 response->detach();   // frees Body
83                 return response;
84             }
85         }
86         
87         BindingException ex("SOAP Envelope did not contain a SAML Response or a Fault.");
88         if (m_soaper.getPolicy().getIssuerMetadata())
89             annotateException(&ex, m_soaper.getPolicy().getIssuerMetadata());   // throws it
90         else
91             ex.raise();
92     }
93     return NULL;
94 }
95
96 bool SAML2SOAPClient::handleError(const Status& status)
97 {
98     auto_ptr_char code((status.getStatusCode() ? status.getStatusCode()->getValue() : NULL));
99     auto_ptr_char str((status.getStatusMessage() ? status.getStatusMessage()->getMessage() : NULL));
100     Category::getInstance(SAML_LOGCAT".SOAPClient").error(
101         "SOAP client detected a SAML error: (%s) (%s)",
102         (code.get() ? code.get() : "no code"),
103         (str.get() ? str.get() : "no message")
104         );
105     return m_fatal;
106 }