Allow message-only policy rules, basic SAML SOAP client.
[shibboleth/cpp-opensaml.git] / saml / binding / impl / SOAPClient.cpp
1 /*
2  *  Copyright 2001-2006 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 "binding/SOAPClient.h"
26 #include "saml2/metadata/Metadata.h"
27 #include "saml2/metadata/MetadataProvider.h"
28
29 #include <xmltooling/security/X509TrustEngine.h>
30 #include <xmltooling/soap/SOAP.h>
31
32 using namespace opensaml::saml2;
33 using namespace opensaml::saml2md;
34 using namespace opensaml;
35 using namespace xmltooling;
36 using namespace std;
37
38 void SOAPClient::send(const soap11::Envelope* env, const KeyInfoSource& peer, const char* endpoint)
39 {
40     // Clear policy.
41     m_policy.setIssuer(NULL);
42     m_policy.setIssuerMetadata(NULL);
43     
44     m_peer = dynamic_cast<const RoleDescriptor*>(&peer);
45     
46     soap11::SOAPClient::send(env, peer, endpoint);
47 }
48
49 void SOAPClient::prepareTransport(const xmltooling::SOAPTransport& transport)
50 {
51     const X509TrustEngine* engine = dynamic_cast<const X509TrustEngine*>(m_policy.getTrustEngine());
52     if (engine) {
53         const MetadataProvider* metadata = m_policy.getMetadataProvider();
54         if (!transport.setTrustEngine(engine, m_force, metadata ? metadata->getKeyResolver() : NULL))
55             throw BindingException("Unable to install X509TrustEngine into SOAPTransport.");
56     }
57 }
58
59 soap11::Envelope* SOAPClient::receive()
60 {
61     auto_ptr<soap11::Envelope> env(soap11::SOAPClient::receive());
62     if (env.get()) {
63         if (m_peer && m_transport->isSecure()) {
64             // Set issuer based on peer identity.
65             EntityDescriptor* parent = dynamic_cast<EntityDescriptor*>(m_peer->getParent());
66             if (parent) {
67                 Issuer* issuer = IssuerBuilder::buildIssuer();
68                 issuer->setName(parent->getEntityID());
69                 m_policy.setIssuer(issuer);
70                 m_policy.setIssuerMetadata(m_peer);
71             }
72         }
73         m_policy.evaluate(*(env.get()));
74     }
75     return env.release();
76 }