2 * Copyright 2001-2006 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 "saml2/binding/SAML2SOAPDecoder.h"
27 #include <log4cpp/Category.hh>
28 #include <xmltooling/soap/SOAP.h>
29 #include <xmltooling/util/NDC.h>
30 #include <xmltooling/validation/ValidatorSuite.h>
32 using namespace opensaml::saml2p;
33 using namespace opensaml;
34 using namespace soap11;
35 using namespace xmltooling;
36 using namespace log4cpp;
41 MessageDecoder* SAML_DLLLOCAL SAML2SOAPDecoderFactory(const DOMElement* const & e)
43 return new SAML2SOAPDecoder(e);
48 SAML2SOAPDecoder::SAML2SOAPDecoder(const DOMElement* e) {}
50 XMLObject* SAML2SOAPDecoder::decode(
52 const GenericRequest& genericRequest,
53 SecurityPolicy& policy
57 xmltooling::NDC ndc("decode");
59 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2SOAP");
61 log.debug("validating input");
62 string s = genericRequest.getContentType();
63 if (s != "text/xml") {
64 log.warn("ignoring incorrect Content Type (%s)", s.c_str() ? s.c_str() : "none");
68 const char* data = genericRequest.getRequestBody();
70 log.warn("empty request body");
73 istringstream is(data);
75 // Parse and bind the document into an XMLObject.
76 DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
77 : XMLToolingConfig::getConfig().getParser()).parse(is);
78 XercesJanitor<DOMDocument> janitor(doc);
79 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
82 Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
84 throw BindingException("Decoded message was not a SOAP 1.1 Envelope.");
87 SchemaValidators.validate(env);
89 Body* body = env->getBody();
90 if (body && body->hasChildren()) {
91 RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(body->getXMLObjects().front());
93 // Run through the policy at two layers.
94 policy.evaluate(*env, &genericRequest);
95 policy.evaluate(*request, &genericRequest);
97 body->detach(); // frees Envelope
98 request->detach(); // frees Body
103 throw BindingException("SOAP Envelope did not contain a SAML Request.");