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