768eaec263cd6b14684fbefbc16801622f8f8ba1
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2SOAPDecoder.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * SAML2SOAPDecoder.cpp
23  * 
24  * SAML 2.0 SOAP binding message decoder.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "binding/SecurityPolicy.h"
30 #include "saml2/binding/SAML2MessageDecoder.h"
31 #include "saml2/core/Protocols.h"
32
33 #include <xmltooling/logging.h>
34 #include <xmltooling/XMLToolingConfig.h>
35 #include <xmltooling/io/GenericRequest.h>
36 #include <xmltooling/soap/SOAP.h>
37 #include <xmltooling/util/NDC.h>
38 #include <xmltooling/util/ParserPool.h>
39 #include <xmltooling/validation/ValidatorSuite.h>
40
41 using namespace opensaml::saml2p;
42 using namespace opensaml;
43 using namespace soap11;
44 using namespace xmltooling::logging;
45 using namespace xmltooling;
46 using namespace std;
47
48 namespace opensaml {
49     namespace saml2p {              
50         class SAML_DLLLOCAL SAML2SOAPDecoder : public SAML2MessageDecoder
51         {
52         public:
53             SAML2SOAPDecoder() {}
54             virtual ~SAML2SOAPDecoder() {}
55
56             bool isUserAgentPresent() const {
57                 return false;
58             }
59
60             xmltooling::XMLObject* decode(
61                 std::string& relayState,
62                 const GenericRequest& genericRequest,
63                 SecurityPolicy& policy
64                 ) const;
65         };                
66
67         MessageDecoder* SAML_DLLLOCAL SAML2SOAPDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
68         {
69             return new SAML2SOAPDecoder();
70         }
71     };
72 };
73
74 XMLObject* SAML2SOAPDecoder::decode(
75     string& relayState,
76     const GenericRequest& genericRequest,
77     SecurityPolicy& policy
78     ) const
79 {
80 #ifdef _DEBUG
81     xmltooling::NDC ndc("decode");
82 #endif
83     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2SOAP");
84
85     log.debug("validating input");
86     string s = genericRequest.getContentType();
87     if (s.find("text/xml") == string::npos) {
88         log.warn("ignoring incorrect content type (%s)", s.c_str() ? s.c_str() : "none");
89         throw BindingException("Invalid content type for SOAP message.");
90     }
91
92     const char* data = genericRequest.getRequestBody();
93     if (!data)
94         throw BindingException("SOAP message had an empty request body.");
95     log.debug("received message:\n%s", data);
96     istringstream is(data);
97     
98     // Parse and bind the document into an XMLObject.
99     DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
100         : XMLToolingConfig::getConfig().getParser()).parse(is); 
101     XercesJanitor<DOMDocument> janitor(doc);
102     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
103     janitor.release();
104
105     Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
106     if (!env)
107         throw BindingException("Decoded message was not a SOAP 1.1 Envelope.");
108
109     SchemaValidators.validate(env);
110     
111     Body* body = env->getBody();
112     if (body && body->hasChildren()) {
113         RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(body->getUnknownXMLObjects().front());
114         if (request) {
115             // Run through the policy at two layers.
116             extractMessageDetails(*env, genericRequest, samlconstants::SAML20P_NS, policy);
117             policy.evaluate(*env, &genericRequest);
118             policy.reset(true);
119             extractMessageDetails(*request, genericRequest, samlconstants::SAML20P_NS, policy);
120             policy.evaluate(*request, &genericRequest);
121             xmlObject.release();
122             body->detach(); // frees Envelope
123             request->detach();   // frees Body
124             return request;
125         }
126     }
127     
128     throw BindingException("SOAP Envelope did not contain a SAML Request.");
129 }