2 * Copyright 2001-2009 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 1.x SOAP binding.
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"
31 #include <xmltooling/logging.h>
32 #include <xmltooling/soap/SOAP.h>
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;
42 void SAML1SOAPClient::sendSAML(Request* 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->getRequestID());
52 Response* SAML1SOAPClient::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 Response* response = dynamic_cast<Response*>(body->getUnknownXMLObjects().front());
62 // Check InResponseTo.
63 if (m_correlate && response->getInResponseTo() && !XMLString::equals(m_correlate, response->getInResponseTo()))
64 throw SecurityPolicyException("InResponseTo attribute did not correlate with the Request ID.");
66 m_soaper.getPolicy().reset(true);
68 // Extract Response details and run policy against it.
69 // We don't pull Issuer out of any assertions because some profiles may permit
70 // alternate issuers at that layer.
71 m_soaper.getPolicy().setMessageID(response->getResponseID());
72 m_soaper.getPolicy().setIssueInstant(response->getIssueInstantEpoch());
73 m_soaper.getPolicy().evaluate(*response);
76 Status* status = response->getStatus();
78 const xmltooling::QName* code = status->getStatusCode() ? status->getStatusCode()->getValue() : NULL;
79 if (code && *code != StatusCode::SUCCESS && handleError(*status)) {
80 BindingException ex("SAML Response contained an error.");
81 if (m_soaper.getPolicy().getIssuerMetadata())
82 annotateException(&ex, m_soaper.getPolicy().getIssuerMetadata()); // throws it
89 body->detach(); // frees Envelope
90 response->detach(); // frees Body
95 BindingException ex("SOAP Envelope did not contain a SAML Response or a Fault.");
96 if (m_soaper.getPolicy().getIssuerMetadata())
97 annotateException(&ex, m_soaper.getPolicy().getIssuerMetadata()); // throws it
104 bool SAML1SOAPClient::handleError(const Status& status)
106 const xmltooling::QName* code = status.getStatusCode() ? status.getStatusCode()->getValue() : NULL;
107 auto_ptr_char str((status.getStatusMessage() ? status.getStatusMessage()->getMessage() : NULL));
108 Category::getInstance(SAML_LOGCAT".SOAPClient").error(
109 "SOAP client detected a SAML error: (%s) (%s)",
110 (code ? code->toString().c_str() : "no code"),
111 (str.get() ? str.get() : "no message")