Add short name to encoders/decoders for endpoint generation.
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2ECPDecoder.cpp
1 /*
2  *  Copyright 2001-2010 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  * SAML2ECPDecoder.cpp
19  * 
20  * SAML 2.0 ECP profile message decoder.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/SecurityPolicy.h"
26 #include "saml2/binding/SAML2MessageDecoder.h"
27 #include "saml2/core/Protocols.h"
28
29 #include <xmltooling/logging.h>
30 #include <xmltooling/XMLToolingConfig.h>
31 #include <xmltooling/io/HTTPRequest.h>
32 #include <xmltooling/soap/SOAP.h>
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/util/ParserPool.h>
35 #include <xmltooling/util/Predicates.h>
36 #include <xmltooling/validation/ValidatorSuite.h>
37
38 using namespace opensaml::saml2p;
39 using namespace opensaml;
40 using namespace soap11;
41 using namespace xmltooling::logging;
42 using namespace xmltooling;
43 using namespace std;
44
45 namespace opensaml {
46     namespace saml2p {              
47         class SAML_DLLLOCAL SAML2ECPDecoder : public SAML2MessageDecoder
48         {
49         public:
50             SAML2ECPDecoder() {}
51             virtual ~SAML2ECPDecoder() {}
52
53             const char* getShortName() const {
54                 return "ECP";
55             }
56
57             xmltooling::XMLObject* decode(
58                 std::string& relayState,
59                 const GenericRequest& genericRequest,
60                 SecurityPolicy& policy
61                 ) const;
62         };                
63
64         MessageDecoder* SAML_DLLLOCAL SAML2ECPDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
65         {
66             return new SAML2ECPDecoder();
67         }
68     };
69 };
70
71 XMLObject* SAML2ECPDecoder::decode(
72     string& relayState,
73     const GenericRequest& genericRequest,
74     SecurityPolicy& policy
75     ) const
76 {
77 #ifdef _DEBUG
78     xmltooling::NDC ndc("decode");
79 #endif
80     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2ECP");
81
82     log.debug("validating input");
83     const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
84     if (!httpRequest)
85         throw BindingException("Unable to cast request object to HTTPRequest type.");
86     string s = genericRequest.getContentType();
87     if (s.find("application/vnd.paos+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 PAOS message.");
90     }
91
92     const char* data = genericRequest.getRequestBody();
93     if (!data)
94         throw BindingException("PAOS 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         Response* response = dynamic_cast<Response*>(body->getUnknownXMLObjects().front());
114         if (response) {
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(*response, genericRequest, samlconstants::SAML20P_NS, policy);
120             policy.evaluate(*response, &genericRequest);
121
122             // Check destination URL.
123             auto_ptr_char dest(response->getDestination());
124             const char* dest2 = httpRequest->getRequestURL();
125             const char* delim = strchr(dest2, '?');
126             if (response->getSignature() && (!dest.get() || !*(dest.get()))) {
127                 log.error("signed SAML message missing Destination attribute");
128                 throw BindingException("Signed SAML message missing Destination attribute identifying intended destination.");
129             }
130             else if (dest.get() && *dest.get() && ((delim && strncmp(dest.get(), dest2, delim - dest2)) || (!delim && strcmp(dest.get(),dest2)))) {
131                 log.error("PAOS response targeted at (%s), but delivered to (%s)", dest.get(), dest2);
132                 throw BindingException("SAML message delivered with PAOS to incorrect server URL.");
133             }
134
135             // Check for RelayState header.
136             if (env->getHeader()) {
137                 static const XMLCh RelayState[] = UNICODE_LITERAL_10(R,e,l,a,y,S,t,a,t,e);
138                 const vector<XMLObject*>& blocks = const_cast<const Header*>(env->getHeader())->getUnknownXMLObjects();
139                 vector<XMLObject*>::const_iterator h =
140                     find_if(blocks.begin(), blocks.end(), hasQName(xmltooling::QName(samlconstants::SAML20ECP_NS, RelayState)));
141                 const ElementProxy* ep = dynamic_cast<const ElementProxy*>(h != blocks.end() ? *h : nullptr);
142                 if (ep) {
143                     auto_ptr_char rs(ep->getTextContent());
144                     if (rs.get())
145                         relayState = rs.get();
146                 }
147             }
148             
149             xmlObject.release();
150             body->detach(); // frees Envelope
151             response->detach();   // frees Body
152             return response;
153         }
154     }
155     
156     throw BindingException("SOAP Envelope did not contain a SAML Response.");
157 }