ac295699332d6f0d3c4d9af8840b6c37a0e2318f
[shibboleth/cpp-opensaml.git] / saml / binding / impl / SOAPClient.cpp
1 /*
2  *  Copyright 2001-2009 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * SOAPClient.cpp
19  * 
20  * Implements SOAP 1.1 messaging over a transport.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "version.h"
26 #include "binding/SecurityPolicy.h"
27 #include "binding/SOAPClient.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataProvider.h"
30
31 #include <xmltooling/security/X509TrustEngine.h>
32 #include <xmltooling/soap/SOAP.h>
33 #include <xmltooling/soap/HTTPSOAPTransport.h>
34
35 using namespace opensaml::saml2;
36 using namespace opensaml::saml2md;
37 using namespace opensaml;
38 using namespace xmltooling;
39 using namespace std;
40
41 SOAPClient::SOAPClient(SecurityPolicy& policy)
42     : soap11::SOAPClient(policy.getValidating()), m_policy(policy), m_force(true), m_peer(NULL), m_criteria(NULL)
43 {
44 }
45
46 void SOAPClient::send(const soap11::Envelope& env, const char* from, MetadataCredentialCriteria& to, const char* endpoint)
47 {
48     // Clear policy.
49     m_policy.reset();
50
51     m_criteria = &to;
52     m_peer = &(to.getRole());
53     
54     const xmltooling::QName& role = m_peer->getElementQName();
55     if (XMLString::equals(role.getLocalPart(),RoleDescriptor::LOCAL_NAME))
56         m_policy.setRole(m_peer->getSchemaType());
57     else
58         m_policy.setRole(&role);
59
60     // Establish the "expected" issuer identity.
61     const XMLCh* entityID = dynamic_cast<const EntityDescriptor*>(m_peer->getParent())->getEntityID();
62     m_policy.setIssuer(entityID);
63     if (!m_policy.getIssuerMetadata())
64         m_policy.setIssuerMetadata(m_peer);
65
66     // Call the base class.
67     auto_ptr_char pn(entityID);
68     soap11::SOAPClient::send(env, SOAPTransport::Address(from, pn.get(), endpoint));
69 }
70
71 void SOAPClient::prepareTransport(xmltooling::SOAPTransport& transport)
72 {
73     HTTPSOAPTransport* http = dynamic_cast<HTTPSOAPTransport*>(&transport);
74     if (http) {
75         http->setRequestHeader("SOAPAction", "http://www.oasis-open.org/committees/security");
76         http->setRequestHeader("Xerces-C", XERCES_FULLVERSIONDOT);
77         http->setRequestHeader("XML-Security-C", XSEC_FULLVERSIONDOT);
78         http->setRequestHeader("OpenSAML-C", OPENSAML_FULLVERSIONDOT);
79     }
80     
81     const X509TrustEngine* engine = dynamic_cast<const X509TrustEngine*>(m_policy.getTrustEngine());
82     if (engine) {
83         if (!transport.setTrustEngine(engine, m_policy.getMetadataProvider(), m_criteria, m_force))
84             throw BindingException("Unable to install X509TrustEngine into SOAPTransport.");
85     }
86 }
87
88 soap11::Envelope* SOAPClient::receive()
89 {
90     auto_ptr<soap11::Envelope> env(soap11::SOAPClient::receive());
91     if (env.get()) {
92         if (m_peer && m_transport->isAuthenticated()) {
93             // Set flag based on peer identity.
94             m_policy.setAuthenticated(true);
95         }
96
97         // Run policy against SOAP layer.
98         m_policy.evaluate(*(env.get()));
99     }
100     return env.release();
101 }
102
103 void SOAPClient::reset()
104 {
105     m_criteria = NULL;
106     m_peer = NULL;
107     soap11::SOAPClient::reset();
108     m_policy.reset();
109 }