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 2.0 SOAP binding.
24 #include "exceptions.h"
25 #include "binding/SecurityPolicy.h"
26 #include "binding/SOAPClient.h"
27 #include "saml2/binding/SAML2SOAPClient.h"
28 #include "saml2/core/Protocols.h"
29 #include "saml2/metadata/Metadata.h"
30 #include "saml2/metadata/MetadataProvider.h"
32 #include <xmltooling/logging.h>
33 #include <xmltooling/soap/SOAP.h>
35 using namespace opensaml::saml2;
36 using namespace opensaml::saml2p;
37 using namespace opensaml::saml2md;
38 using namespace opensaml;
39 using namespace soap11;
40 using namespace xmltooling::logging;
41 using namespace xmltooling;
44 void SAML2SOAPClient::sendSAML(RequestAbstractType* request, const char* from, MetadataCredentialCriteria& to, const char* endpoint)
46 auto_ptr<Envelope> env(EnvelopeBuilder::buildEnvelope());
47 Body* body = BodyBuilder::buildBody();
49 body->getUnknownXMLObjects().push_back(request);
50 m_soaper.send(*env.get(), from, to, endpoint);
51 m_correlate = XMLString::replicate(request->getID());
54 StatusResponseType* SAML2SOAPClient::receiveSAML()
56 auto_ptr<Envelope> env(m_soaper.receive());
58 Body* body = env->getBody();
59 if (body && body->hasChildren()) {
60 // Check for SAML Response.
61 StatusResponseType* response = dynamic_cast<StatusResponseType*>(body->getUnknownXMLObjects().front());
63 // Check InResponseTo.
64 if (m_correlate && response->getInResponseTo() && !XMLString::equals(m_correlate, response->getInResponseTo()))
65 throw SecurityPolicyException("InResponseTo attribute did not correlate with the Request ID.");
67 SecurityPolicy& policy = m_soaper.getPolicy();
70 // Extract Response details.
71 policy.setMessageID(response->getID());
72 policy.setIssueInstant(response->getIssueInstantEpoch());
74 // Extract and re-verify Issuer if present.
75 const Issuer* issuer = response->getIssuer();
77 policy.setIssuer(issuer); // This will throw if it conflicts with the known peer identity.
79 // Now run the policy.
80 policy.evaluate(*response);
83 Status* status = response->getStatus();
85 const XMLCh* code = status->getStatusCode() ? status->getStatusCode()->getValue() : NULL;
86 if (code && !XMLString::equals(code,StatusCode::SUCCESS) && handleError(*status)) {
87 BindingException ex("SAML response contained an error.");
88 annotateException(&ex, policy.getIssuerMetadata(), status); // throws it
93 body->detach(); // frees Envelope
94 response->detach(); // frees Body
99 BindingException ex("SOAP Envelope did not contain a SAML Response or a Fault.");
100 if (m_soaper.getPolicy().getIssuerMetadata())
101 annotateException(&ex, m_soaper.getPolicy().getIssuerMetadata()); // throws it
108 bool SAML2SOAPClient::handleError(const Status& status)
110 auto_ptr_char code((status.getStatusCode() ? status.getStatusCode()->getValue() : NULL));
111 auto_ptr_char str((status.getStatusMessage() ? status.getStatusMessage()->getMessage() : NULL));
112 Category::getInstance(SAML_LOGCAT".SOAPClient").error(
113 "SOAP client detected a SAML error: (%s) (%s)",
114 (code.get() ? code.get() : "no code"),
115 (str.get() ? str.get() : "no message")