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