2 * Copyright 2001-2006 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * Specialized SOAPClient for SAML 2.0 SOAP binding.
24 #include "exceptions.h"
25 #include "saml2/binding/SAML2SOAPClient.h"
26 #include "saml2/core/Protocols.h"
27 #include "saml2/metadata/Metadata.h"
29 #include <log4cpp/Category.hh>
30 #include <xmltooling/soap/SOAP.h>
32 using namespace opensaml::saml2p;
33 using namespace opensaml::saml2md;
34 using namespace opensaml;
35 using namespace soap11;
36 using namespace xmltooling;
37 using namespace log4cpp;
40 void SAML2SOAPClient::sendSAML(RequestAbstractType* request, const RoleDescriptor& peer, const char* endpoint)
42 Envelope* env = EnvelopeBuilder::buildEnvelope();
43 Body* body = BodyBuilder::buildBody();
45 body->getXMLObjects().push_back(request);
47 send(env, peer, endpoint);
48 m_correlate = XMLString::replicate(request->getID());
51 catch (XMLToolingException&) {
52 // A bit weird...we have to "revert" things so that the request is isolated
53 // so the caller can free it.
54 request->getParent()->detach();
60 StatusResponseType* SAML2SOAPClient::receiveSAML()
62 auto_ptr<Envelope> env(receive());
64 Body* body = env->getBody();
65 if (body && body->hasChildren()) {
66 // Check for SAML Response.
67 StatusResponseType* response = dynamic_cast<StatusResponseType*>(body->getXMLObjects().front());
70 // Check InResponseTo.
71 if (m_correlate && !XMLString::equals(m_correlate, response->getInResponseTo()))
72 throw BindingException("InResponseTo attribute did not correlate with the Request ID.");
75 Status* status = response->getStatus();
77 const XMLCh* code = status->getStatusCode() ? status->getStatusCode()->getValue() : NULL;
78 if (code && !XMLString::equals(code,StatusCode::SUCCESS) && handleError(*status))
79 throw BindingException("SAML Response contained an error.");
82 m_policy.evaluate(*response);
84 body->detach(); // frees Envelope
85 response->detach(); // frees Body
90 throw BindingException("SOAP Envelope did not contain a SAML Response or a Fault.");
95 bool SAML2SOAPClient::handleError(const Status& status)
97 auto_ptr_char code((status.getStatusCode() ? status.getStatusCode()->getValue() : NULL));
98 auto_ptr_char str((status.getStatusMessage() ? status.getStatusMessage()->getMessage() : NULL));
99 Category::getInstance(SAML_LOGCAT".SOAPClient").error(
100 "SOAP client detected a SAML error: (%s) (%s)",
101 (code.get() ? code.get() : "no code"),
102 (str.get() ? str.get() : "no message")