Allow message-only policy rules, basic SAML SOAP client.
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1SOAPDecoder.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  * SAML1SOAPDecoder.cpp
19  * 
20  * SAML 1.x SOAP binding message decoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "saml1/binding/SAML1SOAPDecoder.h"
26
27 #include <log4cpp/Category.hh>
28 #include <xmltooling/soap/SOAP.h>
29 #include <xmltooling/util/NDC.h>
30 #include <xmltooling/validation/ValidatorSuite.h>
31
32 using namespace opensaml::saml1p;
33 using namespace opensaml;
34 using namespace soap11;
35 using namespace xmltooling;
36 using namespace log4cpp;
37 using namespace std;
38
39 namespace opensaml {
40     namespace saml1p {              
41         MessageDecoder* SAML_DLLLOCAL SAML1SOAPDecoderFactory(const DOMElement* const & e)
42         {
43             return new SAML1SOAPDecoder(e);
44         }
45     };
46 };
47
48 SAML1SOAPDecoder::SAML1SOAPDecoder(const DOMElement* e) {}
49
50 XMLObject* SAML1SOAPDecoder::decode(
51     string& relayState,
52     const GenericRequest& genericRequest,
53     SecurityPolicy& policy
54     ) const
55 {
56 #ifdef _DEBUG
57     xmltooling::NDC ndc("decode");
58 #endif
59     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1SOAP");
60
61     log.debug("validating input");
62     string s = genericRequest.getContentType();
63     if (s != "text/xml") {
64         log.warn("ignoring incorrect Content Type (%s)", s.c_str() ? s.c_str() : "none");
65         return NULL;
66     }
67
68     const char* data = genericRequest.getRequestBody();
69     if (!data) {
70         log.warn("empty request body");
71         return NULL;
72     }
73     istringstream is(data);
74     
75     // Parse and bind the document into an XMLObject.
76     DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
77         : XMLToolingConfig::getConfig().getParser()).parse(is); 
78     XercesJanitor<DOMDocument> janitor(doc);
79     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
80     janitor.release();
81
82     Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
83     if (!env)
84         throw BindingException("Decoded message was not a SOAP 1.1 Envelope.");
85
86     if (!m_validate)
87         SchemaValidators.validate(env);
88     
89     Body* body = env->getBody();
90     if (body && body->hasChildren()) {
91         Request* request = dynamic_cast<Request*>(body->getXMLObjects().front());
92         if (request) {
93             // Run through the policy at two layers.
94             policy.evaluate(*env, &genericRequest);
95             policy.evaluate(*request, &genericRequest);
96             xmlObject.release();
97             body->detach(); // frees Envelope
98             request->detach();   // frees Body
99             return request;
100         }
101     }
102     
103     throw BindingException("SOAP Envelope did not contain a SAML Request.");
104 }