Reducing header overuse, non-inlining selected methods (CPPOST-35).
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1SOAPDecoder.cpp
1 /*
2  *  Copyright 2001-2009 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 "binding/SecurityPolicy.h"
26 #include "saml1/binding/SAML1MessageDecoder.h"
27 #include "saml1/core/Protocols.h"
28
29 #include <xmltooling/logging.h>
30 #include <xmltooling/XMLToolingConfig.h>
31 #include <xmltooling/io/GenericRequest.h>
32 #include <xmltooling/soap/SOAP.h>
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/util/ParserPool.h>
35 #include <xmltooling/validation/ValidatorSuite.h>
36
37 using namespace opensaml::saml1p;
38 using namespace opensaml;
39 using namespace soap11;
40 using namespace xmltooling::logging;
41 using namespace xmltooling;
42 using namespace std;
43
44 namespace opensaml {
45     namespace saml1p {              
46         class SAML_DLLLOCAL SAML1SOAPDecoder : public SAML1MessageDecoder
47         {
48         public:
49             SAML1SOAPDecoder() {}
50             virtual ~SAML1SOAPDecoder() {}
51
52             bool isUserAgentPresent() const {
53                 return false;
54             }
55
56             xmltooling::XMLObject* decode(
57                 std::string& relayState,
58                 const GenericRequest& genericRequest,
59                 SecurityPolicy& policy
60                 ) const;
61         };                
62
63         MessageDecoder* SAML_DLLLOCAL SAML1SOAPDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
64         {
65             return new SAML1SOAPDecoder();
66         }
67     };
68 };
69
70 XMLObject* SAML1SOAPDecoder::decode(
71     string& relayState,
72     const GenericRequest& genericRequest,
73     SecurityPolicy& policy
74     ) const
75 {
76 #ifdef _DEBUG
77     xmltooling::NDC ndc("decode");
78 #endif
79     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1SOAP");
80
81     log.debug("validating input");
82     string s = genericRequest.getContentType();
83     if (s.find("text/xml") == string::npos) {
84         log.warn("ignoring incorrect content type (%s)", s.c_str() ? s.c_str() : "none");
85         throw BindingException("Invalid content type for SOAP message.");
86     }
87
88     const char* data = genericRequest.getRequestBody();
89     if (!data)
90         throw BindingException("SOAP message had an empty request body.");
91     log.debug("received message:\n%s", data);
92     istringstream is(data);
93     
94     // Parse and bind the document into an XMLObject.
95     DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
96         : XMLToolingConfig::getConfig().getParser()).parse(is); 
97     XercesJanitor<DOMDocument> janitor(doc);
98     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
99     janitor.release();
100
101     Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
102     if (!env)
103         throw BindingException("Decoded message was not a SOAP 1.1 Envelope.");
104
105     SchemaValidators.validate(env);
106     
107     Body* body = env->getBody();
108     if (body && body->hasChildren()) {
109         Request* request = dynamic_cast<Request*>(body->getUnknownXMLObjects().front());
110         if (request) {
111             // Run through the policy at two layers.
112             pair<bool,int> minor = request->getMinorVersion();
113             extractMessageDetails(
114                 *env,
115                 genericRequest,
116                 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM,
117                 policy
118                 );
119             policy.evaluate(*env,&genericRequest);
120
121             // Reset, extract, and run again.
122             policy.reset(true);
123             extractMessageDetails(
124                 *request,
125                 genericRequest,
126                 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM,
127                 policy
128                 );
129             policy.evaluate(*request,&genericRequest);
130             xmlObject.release();
131             body->detach(); // frees Envelope
132             request->detach();   // frees Body
133             return request;
134         }
135     }
136     
137     throw BindingException("SOAP Envelope did not contain a SAML 1.x Request.");
138 }