Set policy role based on peer entity.
[shibboleth/cpp-opensaml.git] / saml / binding / impl / SOAPClient.cpp
1 /*
2  *  Copyright 2001-2007 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/SOAPClient.h"
27 #include "saml2/metadata/Metadata.h"
28 #include "saml2/metadata/MetadataProvider.h"
29
30 #include <xmltooling/security/X509TrustEngine.h>
31 #include <xmltooling/soap/SOAP.h>
32 #include <xmltooling/soap/HTTPSOAPTransport.h>
33
34 using namespace opensaml::saml2;
35 using namespace opensaml::saml2md;
36 using namespace opensaml;
37 using namespace xmltooling;
38 using namespace std;
39
40 void SOAPClient::send(const soap11::Envelope& env, const KeyInfoSource& peer, const char* endpoint)
41 {
42     // Clear policy.
43     m_policy.reset();
44     
45     m_peer = dynamic_cast<const RoleDescriptor*>(&peer);
46     if (m_peer) {
47         const QName& role = m_peer->getElementQName();
48         if (XMLString::equals(role.getLocalPart(),RoleDescriptor::LOCAL_NAME))
49             m_policy.setRole(m_peer->getSchemaType());
50         else
51             m_policy.setRole(&role);
52     }
53     
54     soap11::SOAPClient::send(env, peer, endpoint);
55 }
56
57 void SOAPClient::prepareTransport(const xmltooling::SOAPTransport& transport)
58 {
59     const HTTPSOAPTransport* http = dynamic_cast<const HTTPSOAPTransport*>(&transport);
60     if (http) {
61         http->setRequestHeader("SOAPAction", "http://www.oasis-open.org/committees/security");
62         http->setRequestHeader("Xerces-C", XERCES_FULLVERSIONDOT);
63         http->setRequestHeader("XML-Security-C", XSEC_VERSION);
64         http->setRequestHeader("OpenSAML-C", OPENSAML_FULLVERSIONDOT);
65     }
66     
67     const X509TrustEngine* engine = dynamic_cast<const X509TrustEngine*>(m_policy.getTrustEngine());
68     if (engine) {
69         const MetadataProvider* metadata = m_policy.getMetadataProvider();
70         if (!transport.setTrustEngine(engine, m_force, metadata ? metadata->getKeyResolver() : NULL))
71             throw BindingException("Unable to install X509TrustEngine into SOAPTransport.");
72     }
73 }
74
75 soap11::Envelope* SOAPClient::receive()
76 {
77     auto_ptr<soap11::Envelope> env(soap11::SOAPClient::receive());
78     if (env.get()) {
79         if (m_peer && m_transport->isSecure()) {
80             // Set issuer based on peer identity.
81             EntityDescriptor* parent = dynamic_cast<EntityDescriptor*>(m_peer->getParent());
82             if (parent) {
83                 Issuer* issuer = IssuerBuilder::buildIssuer();
84                 issuer->setName(parent->getEntityID());
85                 m_policy.setIssuer(issuer);
86                 m_policy.setIssuerMetadata(m_peer);
87                 m_policy.setSecure(true);
88             }
89         }
90         m_policy.evaluate(*(env.get()));
91     }
92     return env.release();
93 }
94
95 void SOAPClient::reset()
96 {
97     soap11::SOAPClient::reset();
98     m_policy.reset();
99     XMLString::release(&m_correlate);
100     m_correlate=NULL;
101 }