Multi-line svn commit, see body.
[shibboleth/cpp-opensaml.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 #include "saml2/metadata/MetadataProvider.h"
29
30 #include <xmltooling/logging.h>
31 #include <xmltooling/soap/SOAP.h>
32
33 using namespace opensaml::saml2;
34 using namespace opensaml::saml2p;
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 void SAML2SOAPClient::sendSAML(RequestAbstractType* request, const char* from, MetadataCredentialCriteria& to, const char* endpoint)
43 {
44     auto_ptr<Envelope> env(EnvelopeBuilder::buildEnvelope());
45     Body* body = BodyBuilder::buildBody();
46     env->setBody(body);
47     body->getUnknownXMLObjects().push_back(request);
48     m_soaper.send(*env.get(), from, to, endpoint);
49     m_correlate = XMLString::replicate(request->getID());
50 }
51
52 StatusResponseType* SAML2SOAPClient::receiveSAML()
53 {
54     auto_ptr<Envelope> env(m_soaper.receive());
55     if (env.get()) {
56         Body* body = env->getBody();
57         if (body && body->hasChildren()) {
58             // Check for SAML Response.
59             StatusResponseType* response = dynamic_cast<StatusResponseType*>(body->getUnknownXMLObjects().front());
60             if (response) {
61                 // Check InResponseTo.
62                 if (m_correlate && response->getInResponseTo() && !XMLString::equals(m_correlate, response->getInResponseTo()))
63                     throw SecurityPolicyException("InResponseTo attribute did not correlate with the Request ID.");
64
65                 SecurityPolicy& policy = m_soaper.getPolicy();
66                 policy.reset(true);
67
68                 // Extract Response details.
69                 policy.setMessageID(response->getID());
70                 policy.setIssueInstant(response->getIssueInstantEpoch());
71
72                 // Extract and re-verify Issuer if present.
73                 const Issuer* issuer = response->getIssuer();
74                 if (issuer)
75                     policy.setIssuer(issuer);   // This will throw if it conflicts with the known peer identity.
76
77                 // Now run the policy.
78                 policy.evaluate(*response);
79
80                 // Check Status.
81                 Status* status = response->getStatus();
82                 if (status) {
83                     const XMLCh* code = status->getStatusCode() ? status->getStatusCode()->getValue() : NULL;
84                     if (code && !XMLString::equals(code,StatusCode::SUCCESS) && handleError(*status)) {
85                         BindingException ex("SAML response contained an error.");
86                         annotateException(&ex, policy.getIssuerMetadata(), status);   // throws it
87                     }
88                 }
89                 
90                 env.release();
91                 body->detach(); // frees Envelope
92                 response->detach();   // frees Body
93                 return response;
94             }
95         }
96         
97         BindingException ex("SOAP Envelope did not contain a SAML Response or a Fault.");
98         if (m_soaper.getPolicy().getIssuerMetadata())
99             annotateException(&ex, m_soaper.getPolicy().getIssuerMetadata());   // throws it
100         else
101             ex.raise();
102     }
103     return NULL;
104 }
105
106 bool SAML2SOAPClient::handleError(const Status& status)
107 {
108     auto_ptr_char code((status.getStatusCode() ? status.getStatusCode()->getValue() : NULL));
109     auto_ptr_char str((status.getStatusMessage() ? status.getStatusMessage()->getMessage() : NULL));
110     Category::getInstance(SAML_LOGCAT".SOAPClient").error(
111         "SOAP client detected a SAML error: (%s) (%s)",
112         (code.get() ? code.get() : "no code"),
113         (str.get() ? str.get() : "no message")
114         );
115     return m_fatal;
116 }