2 * Copyright 2001-2007 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/MessageDecoder.h"
26 #include "saml2/core/Protocols.h"
28 #include <log4cpp/Category.hh>
29 #include <xmltooling/soap/SOAP.h>
30 #include <xmltooling/util/NDC.h>
31 #include <xmltooling/validation/ValidatorSuite.h>
33 using namespace opensaml::saml2p;
34 using namespace opensaml;
35 using namespace soap11;
36 using namespace xmltooling;
37 using namespace log4cpp;
42 class SAML_DLLLOCAL SAML2SOAPDecoder : public MessageDecoder
45 SAML2SOAPDecoder(const DOMElement* e);
46 virtual ~SAML2SOAPDecoder() {}
48 bool isUserAgentPresent() const {
52 xmltooling::XMLObject* decode(
53 std::string& relayState,
54 const GenericRequest& genericRequest,
55 SecurityPolicy& policy
59 MessageDecoder* SAML_DLLLOCAL SAML2SOAPDecoderFactory(const DOMElement* const & e)
61 return new SAML2SOAPDecoder(e);
66 SAML2SOAPDecoder::SAML2SOAPDecoder(const DOMElement* e) {}
68 XMLObject* SAML2SOAPDecoder::decode(
70 const GenericRequest& genericRequest,
71 SecurityPolicy& policy
75 xmltooling::NDC ndc("decode");
77 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2SOAP");
79 log.debug("validating input");
80 string s = genericRequest.getContentType();
81 if (s.find("text/xml") == string::npos) {
82 log.warn("ignoring incorrect content type (%s)", s.c_str() ? s.c_str() : "none");
83 throw BindingException("Invalid content type for SOAP message.");
86 const char* data = genericRequest.getRequestBody();
88 throw BindingException("SOAP message had an empty request body.");
89 istringstream is(data);
91 // Parse and bind the document into an XMLObject.
92 DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
93 : XMLToolingConfig::getConfig().getParser()).parse(is);
94 XercesJanitor<DOMDocument> janitor(doc);
95 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
98 Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
100 throw BindingException("Decoded message was not a SOAP 1.1 Envelope.");
102 if (!policy.getValidating())
103 SchemaValidators.validate(env);
105 Body* body = env->getBody();
106 if (body && body->hasChildren()) {
107 RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(body->getUnknownXMLObjects().front());
109 // Run through the policy at two layers.
110 policy.evaluate(*env, &genericRequest);
111 policy.evaluate(*request, &genericRequest);
113 body->detach(); // frees Envelope
114 request->detach(); // frees Body
119 throw BindingException("SOAP Envelope did not contain a SAML Request.");