4346ef56c6da46bdd435d14c295c022bc50b4f27
[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/MetadataCredentialCriteria.h"
30 #include "saml2/metadata/MetadataProvider.h"
31
32 #include <xmltooling/security/X509TrustEngine.h>
33 #include <xmltooling/soap/SOAP.h>
34 #include <xmltooling/soap/HTTPSOAPTransport.h>
35 #include <xsec/framework/XSECDefs.hpp>
36
37 using namespace opensaml::saml2;
38 using namespace opensaml::saml2md;
39 using namespace opensaml;
40 using namespace xmltooling;
41 using namespace std;
42
43 SOAPClient::SOAPClient(SecurityPolicy& policy)
44     : soap11::SOAPClient(policy.getValidating()), m_policy(policy), m_force(true), m_peer(NULL), m_criteria(NULL)
45 {
46 }
47
48 SOAPClient::~SOAPClient()
49 {
50 }
51
52 void SOAPClient::forceTransportAuthentication(bool force)
53 {
54     m_force = force;
55 }
56
57 void SOAPClient::send(const soap11::Envelope& env, const char* from, MetadataCredentialCriteria& to, const char* endpoint)
58 {
59     // Clear policy.
60     m_policy.reset();
61
62     m_criteria = &to;
63     m_peer = &(to.getRole());
64     
65     const xmltooling::QName& role = m_peer->getElementQName();
66     if (XMLString::equals(role.getLocalPart(),RoleDescriptor::LOCAL_NAME))
67         m_policy.setRole(m_peer->getSchemaType());
68     else
69         m_policy.setRole(&role);
70
71     // Establish the "expected" issuer identity.
72     const XMLCh* entityID = dynamic_cast<const EntityDescriptor*>(m_peer->getParent())->getEntityID();
73     m_policy.setIssuer(entityID);
74     if (!m_policy.getIssuerMetadata())
75         m_policy.setIssuerMetadata(m_peer);
76
77     // Call the base class.
78     auto_ptr_char pn(entityID);
79     soap11::SOAPClient::send(env, SOAPTransport::Address(from, pn.get(), endpoint));
80 }
81
82 void SOAPClient::prepareTransport(xmltooling::SOAPTransport& transport)
83 {
84     HTTPSOAPTransport* http = dynamic_cast<HTTPSOAPTransport*>(&transport);
85     if (http) {
86         http->setRequestHeader("SOAPAction", "http://www.oasis-open.org/committees/security");
87         http->setRequestHeader("Xerces-C", XERCES_FULLVERSIONDOT);
88         http->setRequestHeader("XML-Security-C", XSEC_FULLVERSIONDOT);
89         http->setRequestHeader("OpenSAML-C", OPENSAML_FULLVERSIONDOT);
90     }
91     
92     const X509TrustEngine* engine = dynamic_cast<const X509TrustEngine*>(m_policy.getTrustEngine());
93     if (engine) {
94         if (!transport.setTrustEngine(engine, m_policy.getMetadataProvider(), m_criteria, m_force))
95             throw BindingException("Unable to install X509TrustEngine into SOAPTransport.");
96     }
97 }
98
99 soap11::Envelope* SOAPClient::receive()
100 {
101     auto_ptr<soap11::Envelope> env(soap11::SOAPClient::receive());
102     if (env.get()) {
103         if (m_peer && m_transport->isAuthenticated()) {
104             // Set flag based on peer identity.
105             m_policy.setAuthenticated(true);
106         }
107
108         // Run policy against SOAP layer.
109         m_policy.evaluate(*(env.get()));
110     }
111     return env.release();
112 }
113
114 void SOAPClient::reset()
115 {
116     m_criteria = NULL;
117     m_peer = NULL;
118     soap11::SOAPClient::reset();
119     m_policy.reset();
120 }
121
122 SecurityPolicy& SOAPClient::getPolicy() const
123 {
124     return m_policy;
125 }