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 1.x SOAP binding.
28 #include "exceptions.h"
29 #include "binding/SecurityPolicy.h"
30 #include "binding/SOAPClient.h"
31 #include "saml1/binding/SAML1SOAPClient.h"
32 #include "saml1/core/Protocols.h"
33 #include "saml2/metadata/Metadata.h"
35 #include <xmltooling/logging.h>
36 #include <xmltooling/soap/SOAP.h>
38 using namespace opensaml::saml1p;
39 using namespace opensaml::saml2md;
40 using namespace opensaml;
41 using namespace soap11;
42 using namespace xmltooling::logging;
43 using namespace xmltooling;
46 SAML1SOAPClient::SAML1SOAPClient(opensaml::SOAPClient& soaper, bool fatalSAMLErrors) : m_soaper(soaper), m_fatal(fatalSAMLErrors), m_correlate(nullptr)
50 SAML1SOAPClient::~SAML1SOAPClient()
52 XMLString::release(&m_correlate);
55 void SAML1SOAPClient::sendSAML(Request* request, const char* from, MetadataCredentialCriteria& to, const char* endpoint)
57 auto_ptr<Envelope> env(EnvelopeBuilder::buildEnvelope());
58 Body* body = BodyBuilder::buildBody();
60 body->getUnknownXMLObjects().push_back(request);
61 m_soaper.send(*env.get(), from, to, endpoint);
62 m_correlate = XMLString::replicate(request->getRequestID());
65 Response* SAML1SOAPClient::receiveSAML()
67 auto_ptr<Envelope> env(m_soaper.receive());
69 Body* body = env->getBody();
70 if (body && body->hasChildren()) {
71 // Check for SAML Response.
72 Response* response = dynamic_cast<Response*>(body->getUnknownXMLObjects().front());
75 // Check InResponseTo.
76 if (m_correlate && response->getInResponseTo() && !XMLString::equals(m_correlate, response->getInResponseTo()))
77 throw SecurityPolicyException("InResponseTo attribute did not correlate with the Request ID.");
79 m_soaper.getPolicy().reset(true);
81 // Extract Response details and run policy against it.
82 // We don't pull Issuer out of any assertions because some profiles may permit
83 // alternate issuers at that layer.
84 m_soaper.getPolicy().setMessageID(response->getResponseID());
85 m_soaper.getPolicy().setIssueInstant(response->getIssueInstantEpoch());
86 m_soaper.getPolicy().evaluate(*response);
89 Status* status = response->getStatus();
91 const xmltooling::QName* code = status->getStatusCode() ? status->getStatusCode()->getValue() : nullptr;
92 if (code && *code != StatusCode::SUCCESS && handleError(*status)) {
93 BindingException ex("SAML Response contained an error.");
94 if (m_soaper.getPolicy().getIssuerMetadata())
95 annotateException(&ex, m_soaper.getPolicy().getIssuerMetadata(), status); // throws it
102 body->detach(); // frees Envelope
103 response->detach(); // frees Body
108 BindingException ex("SOAP Envelope did not contain a SAML Response or a Fault.");
109 if (m_soaper.getPolicy().getIssuerMetadata())
110 annotateException(&ex, m_soaper.getPolicy().getIssuerMetadata()); // throws it
117 bool SAML1SOAPClient::handleError(const saml1p::Status& status)
119 const xmltooling::QName* code = status.getStatusCode() ? status.getStatusCode()->getValue() : nullptr;
120 auto_ptr_char str((status.getStatusMessage() ? status.getStatusMessage()->getMessage() : nullptr));
121 Category::getInstance(SAML_LOGCAT".SOAPClient").error(
122 "SOAP client detected a SAML error: (%s) (%s)",
123 (code ? code->toString().c_str() : "no code"),
124 (str.get() ? str.get() : "no message")