2 * Copyright 2001-2007 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"
28 #include "saml2/metadata/MetadataProvider.h"
30 #include <xmltooling/logging.h>
31 #include <xmltooling/soap/SOAP.h>
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;
42 void SAML2SOAPClient::sendSAML(RequestAbstractType* request, const char* from, MetadataCredentialCriteria& to, const char* endpoint)
44 auto_ptr<Envelope> env(EnvelopeBuilder::buildEnvelope());
45 Body* body = BodyBuilder::buildBody();
47 body->getUnknownXMLObjects().push_back(request);
48 m_soaper.send(*env.get(), from, to, endpoint);
49 m_correlate = XMLString::replicate(request->getID());
52 StatusResponseType* SAML2SOAPClient::receiveSAML()
54 auto_ptr<Envelope> env(m_soaper.receive());
56 Body* body = env->getBody();
57 if (body && body->hasChildren()) {
58 // Check for SAML Response.
59 StatusResponseType* response = dynamic_cast<StatusResponseType*>(body->getUnknownXMLObjects().front());
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.");
65 SecurityPolicy& policy = m_soaper.getPolicy();
68 // Extract Response details.
69 policy.setMessageID(response->getID());
70 policy.setIssueInstant(response->getIssueInstantEpoch());
72 // Extract and re-verify Issuer if present.
73 const Issuer* issuer = response->getIssuer();
75 policy.setIssuer(issuer); // This will throw if it conflicts with the known peer identity.
77 // Now run the policy.
78 policy.evaluate(*response);
81 Status* status = response->getStatus();
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
91 body->detach(); // frees Envelope
92 response->detach(); // frees Body
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
106 bool SAML2SOAPClient::handleError(const Status& status)
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")