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