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/validation/ValidatorSuite.h>
35
36 using namespace opensaml::saml1p;
37 using namespace opensaml;
38 using namespace soap11;
39 using namespace xmltooling::logging;
40 using namespace xmltooling;
41 using namespace std;
42
43 namespace opensaml {
44     namespace saml1p {              
45         class SAML_DLLLOCAL SAML1SOAPDecoder : public SAML1MessageDecoder
46         {
47         public:
48             SAML1SOAPDecoder() {}
49             virtual ~SAML1SOAPDecoder() {}
50
51             bool isUserAgentPresent() const {
52                 return false;
53             }
54
55             xmltooling::XMLObject* decode(
56                 std::string& relayState,
57                 const GenericRequest& genericRequest,
58                 SecurityPolicy& policy
59                 ) const;
60         };                
61
62         MessageDecoder* SAML_DLLLOCAL SAML1SOAPDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
63         {
64             return new SAML1SOAPDecoder();
65         }
66     };
67 };
68
69 XMLObject* SAML1SOAPDecoder::decode(
70     string& relayState,
71     const GenericRequest& genericRequest,
72     SecurityPolicy& policy
73     ) const
74 {
75 #ifdef _DEBUG
76     xmltooling::NDC ndc("decode");
77 #endif
78     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1SOAP");
79
80     log.debug("validating input");
81     string s = genericRequest.getContentType();
82     if (s.find("text/xml") == string::npos) {
83         log.warn("ignoring incorrect content type (%s)", s.c_str() ? s.c_str() : "none");
84         throw BindingException("Invalid content type for SOAP message.");
85     }
86
87     const char* data = genericRequest.getRequestBody();
88     if (!data)
89         throw BindingException("SOAP message had an empty request body.");
90     log.debug("received message:\n%s", data);
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     SchemaValidators.validate(env);
105     
106     Body* body = env->getBody();
107     if (body && body->hasChildren()) {
108         Request* request = dynamic_cast<Request*>(body->getUnknownXMLObjects().front());
109         if (request) {
110             // Run through the policy at two layers.
111             pair<bool,int> minor = request->getMinorVersion();
112             extractMessageDetails(
113                 *env,
114                 genericRequest,
115                 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM,
116                 policy
117                 );
118             policy.evaluate(*env,&genericRequest);
119
120             // Reset, extract, and run again.
121             policy.reset(true);
122             extractMessageDetails(
123                 *request,
124                 genericRequest,
125                 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM,
126                 policy
127                 );
128             policy.evaluate(*request,&genericRequest);
129             xmlObject.release();
130             body->detach(); // frees Envelope
131             request->detach();   // frees Body
132             return request;
133         }
134     }
135     
136     throw BindingException("SOAP Envelope did not contain a SAML 1.x Request.");
137 }