2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
24 * Specialized SOAPClient for SAML 2.0 SOAP binding.
28 #include "exceptions.h"
29 #include "binding/SecurityPolicy.h"
30 #include "binding/SOAPClient.h"
31 #include "saml2/binding/SAML2SOAPClient.h"
32 #include "saml2/core/Protocols.h"
33 #include "saml2/metadata/Metadata.h"
34 #include "saml2/metadata/MetadataProvider.h"
36 #include <xmltooling/logging.h>
37 #include <xmltooling/soap/SOAP.h>
39 using namespace opensaml::saml2;
40 using namespace opensaml::saml2p;
41 using namespace opensaml::saml2md;
42 using namespace opensaml;
43 using namespace soap11;
44 using namespace xmltooling::logging;
45 using namespace xmltooling;
48 SAML2SOAPClient::SAML2SOAPClient(opensaml::SOAPClient& soaper, bool fatalSAMLErrors)
49 : m_soaper(soaper), m_fatal(fatalSAMLErrors), m_correlate(nullptr)
53 SAML2SOAPClient::~SAML2SOAPClient()
55 XMLString::release(&m_correlate);
58 void SAML2SOAPClient::sendSAML(RequestAbstractType* request, const char* from, MetadataCredentialCriteria& to, const char* endpoint)
60 auto_ptr<Envelope> env(EnvelopeBuilder::buildEnvelope());
61 Body* body = BodyBuilder::buildBody();
63 body->getUnknownXMLObjects().push_back(request);
64 m_soaper.send(*env.get(), from, to, endpoint);
65 m_correlate = XMLString::replicate(request->getID());
68 StatusResponseType* SAML2SOAPClient::receiveSAML()
70 auto_ptr<Envelope> env(m_soaper.receive());
72 Body* body = env->getBody();
73 if (body && body->hasChildren()) {
74 // Check for SAML Response.
75 StatusResponseType* response = dynamic_cast<StatusResponseType*>(body->getUnknownXMLObjects().front());
77 // Check InResponseTo.
78 if (m_correlate && response->getInResponseTo() && !XMLString::equals(m_correlate, response->getInResponseTo()))
79 throw SecurityPolicyException("InResponseTo attribute did not correlate with the Request ID.");
81 SecurityPolicy& policy = m_soaper.getPolicy();
84 // Extract Response details.
85 policy.setMessageID(response->getID());
86 policy.setIssueInstant(response->getIssueInstantEpoch());
88 // Extract and re-verify Issuer if present.
89 const Issuer* issuer = response->getIssuer();
91 policy.setIssuer(issuer); // This will throw if it conflicts with the known peer identity.
93 // Now run the policy.
94 policy.evaluate(*response);
97 Status* status = response->getStatus();
99 const XMLCh* code = status->getStatusCode() ? status->getStatusCode()->getValue() : nullptr;
100 if (code && !XMLString::equals(code,StatusCode::SUCCESS) && handleError(*status)) {
101 BindingException ex("SAML response contained an error.");
102 annotateException(&ex, policy.getIssuerMetadata(), status); // throws it
107 body->detach(); // frees Envelope
108 response->detach(); // frees Body
113 BindingException ex("SOAP Envelope did not contain a SAML Response or a Fault.");
114 if (m_soaper.getPolicy().getIssuerMetadata())
115 annotateException(&ex, m_soaper.getPolicy().getIssuerMetadata()); // throws it
122 bool SAML2SOAPClient::handleError(const saml2p::Status& status)
124 auto_ptr_char code((status.getStatusCode() ? status.getStatusCode()->getValue() : nullptr));
125 auto_ptr_char str((status.getStatusMessage() ? status.getStatusMessage()->getMessage() : nullptr));
126 Category::getInstance(SAML_LOGCAT".SOAPClient").error(
127 "SOAP client detected a SAML error: (%s) (%s)",
128 (code.get() ? code.get() : "no code"),
129 (str.get() ? str.get() : "no message")