d01027523fece8dc527f3f77e809f1716c020086
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2SOAPClient.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * SAML2SOAPClient.cpp
23  * 
24  * Specialized SOAPClient for SAML 2.0 SOAP binding.
25  */
26
27 #include "internal.h"
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"
35
36 #include <xmltooling/logging.h>
37 #include <xmltooling/soap/SOAP.h>
38
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;
46 using namespace std;
47
48 SAML2SOAPClient::SAML2SOAPClient(opensaml::SOAPClient& soaper, bool fatalSAMLErrors)
49     : m_soaper(soaper), m_fatal(fatalSAMLErrors), m_correlate(nullptr)
50 {
51 }
52
53 SAML2SOAPClient::~SAML2SOAPClient()
54 {
55     XMLString::release(&m_correlate);
56 }
57
58 void SAML2SOAPClient::sendSAML(RequestAbstractType* request, const char* from, MetadataCredentialCriteria& to, const char* endpoint)
59 {
60     auto_ptr<Envelope> env(EnvelopeBuilder::buildEnvelope());
61     Body* body = BodyBuilder::buildBody();
62     env->setBody(body);
63     body->getUnknownXMLObjects().push_back(request);
64     m_soaper.send(*env.get(), from, to, endpoint);
65     m_correlate = XMLString::replicate(request->getID());
66 }
67
68 StatusResponseType* SAML2SOAPClient::receiveSAML()
69 {
70     auto_ptr<Envelope> env(m_soaper.receive());
71     if (env.get()) {
72         Body* body = env->getBody();
73         if (body && body->hasChildren()) {
74             // Check for SAML Response.
75             StatusResponseType* response = dynamic_cast<StatusResponseType*>(body->getUnknownXMLObjects().front());
76             if (response) {
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.");
80
81                 SecurityPolicy& policy = m_soaper.getPolicy();
82                 policy.reset(true);
83
84                 // Extract Response details.
85                 policy.setMessageID(response->getID());
86                 policy.setIssueInstant(response->getIssueInstantEpoch());
87
88                 // Extract and re-verify Issuer if present.
89                 const Issuer* issuer = response->getIssuer();
90                 if (issuer)
91                     policy.setIssuer(issuer);   // This will throw if it conflicts with the known peer identity.
92
93                 // Now run the policy.
94                 policy.evaluate(*response);
95
96                 // Check Status.
97                 Status* status = response->getStatus();
98                 if (status) {
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
103                     }
104                 }
105                 
106                 env.release();
107                 body->detach(); // frees Envelope
108                 response->detach();   // frees Body
109                 return response;
110             }
111         }
112         
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
116         else
117             ex.raise();
118     }
119     return nullptr;
120 }
121
122 bool SAML2SOAPClient::handleError(const saml2p::Status& status)
123 {
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")
130         );
131     return m_fatal;
132 }