2 * Copyright 2001-2009 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SAML2SOAPDecoder.cpp
20 * SAML 2.0 SOAP binding message decoder.
24 #include "exceptions.h"
25 #include "binding/SecurityPolicy.h"
26 #include "saml2/binding/SAML2MessageDecoder.h"
27 #include "saml2/core/Protocols.h"
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>
37 using namespace opensaml::saml2p;
38 using namespace opensaml;
39 using namespace soap11;
40 using namespace xmltooling::logging;
41 using namespace xmltooling;
46 class SAML_DLLLOCAL SAML2SOAPDecoder : public SAML2MessageDecoder
50 virtual ~SAML2SOAPDecoder() {}
52 bool isUserAgentPresent() const {
56 xmltooling::XMLObject* decode(
57 std::string& relayState,
58 const GenericRequest& genericRequest,
59 SecurityPolicy& policy
63 MessageDecoder* SAML_DLLLOCAL SAML2SOAPDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
65 return new SAML2SOAPDecoder();
70 XMLObject* SAML2SOAPDecoder::decode(
72 const GenericRequest& genericRequest,
73 SecurityPolicy& policy
77 xmltooling::NDC ndc("decode");
79 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2SOAP");
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.");
88 const char* data = genericRequest.getRequestBody();
90 throw BindingException("SOAP message had an empty request body.");
91 log.debug("received message:\n%s", data);
92 istringstream is(data);
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));
101 Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
103 throw BindingException("Decoded message was not a SOAP 1.1 Envelope.");
105 SchemaValidators.validate(env);
107 Body* body = env->getBody();
108 if (body && body->hasChildren()) {
109 RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(body->getUnknownXMLObjects().front());
111 // Run through the policy at two layers.
112 extractMessageDetails(*env, genericRequest, samlconstants::SAML20P_NS, policy);
113 policy.evaluate(*env, &genericRequest);
115 extractMessageDetails(*request, genericRequest, samlconstants::SAML20P_NS, policy);
116 policy.evaluate(*request, &genericRequest);
118 body->detach(); // frees Envelope
119 request->detach(); // frees Body
124 throw BindingException("SOAP Envelope did not contain a SAML Request.");