Add short name to encoders/decoders for endpoint generation.
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1SOAPDecoder.cpp
1 /*
2  *  Copyright 2001-2009 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 "binding/SecurityPolicy.h"
26 #include "saml1/binding/SAML1MessageDecoder.h"
27 #include "saml1/core/Protocols.h"
28
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>
36
37 using namespace opensaml::saml1p;
38 using namespace opensaml;
39 using namespace soap11;
40 using namespace xmltooling::logging;
41 using namespace xmltooling;
42 using namespace std;
43
44 namespace opensaml {
45     namespace saml1p {              
46         class SAML_DLLLOCAL SAML1SOAPDecoder : public SAML1MessageDecoder
47         {
48         public:
49             SAML1SOAPDecoder() {}
50             virtual ~SAML1SOAPDecoder() {}
51
52             bool isUserAgentPresent() const {
53                 return false;
54             }
55
56             const char* getShortName() const {
57                 return "SOAP";
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 SAML1SOAPDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
68         {
69             return new SAML1SOAPDecoder();
70         }
71     };
72 };
73
74 XMLObject* SAML1SOAPDecoder::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.SAML1SOAP");
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         Request* request = dynamic_cast<Request*>(body->getUnknownXMLObjects().front());
114         if (request) {
115             // Run through the policy at two layers.
116             pair<bool,int> minor = request->getMinorVersion();
117             extractMessageDetails(
118                 *env,
119                 genericRequest,
120                 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM,
121                 policy
122                 );
123             policy.evaluate(*env,&genericRequest);
124
125             // Reset, extract, and run again.
126             policy.reset(true);
127             extractMessageDetails(
128                 *request,
129                 genericRequest,
130                 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM,
131                 policy
132                 );
133             policy.evaluate(*request,&genericRequest);
134             xmlObject.release();
135             body->detach(); // frees Envelope
136             request->detach();   // frees Body
137             return request;
138         }
139     }
140     
141     throw BindingException("SOAP Envelope did not contain a SAML 1.x Request.");
142 }