Apply manual validators even when schema was used.
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1POSTDecoder.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  * SAML1POSTDecoder.cpp
19  * 
20  * SAML 1.x POST binding/profile message decoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "saml1/binding/SAML1MessageDecoder.h"
26 #include "saml1/core/Assertions.h"
27 #include "saml1/core/Protocols.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataProvider.h"
30
31 #include <xercesc/framework/MemBufInputSource.hpp>
32 #include <xercesc/framework/Wrapper4InputSource.hpp>
33 #include <xercesc/util/Base64.hpp>
34 #include <xmltooling/logging.h>
35 #include <xmltooling/io/HTTPRequest.h>
36 #include <xmltooling/util/NDC.h>
37 #include <xmltooling/validation/ValidatorSuite.h>
38
39 using namespace opensaml::saml2md;
40 using namespace opensaml::saml1p;
41 using namespace opensaml::saml1;
42 using namespace opensaml;
43 using namespace xmltooling::logging;
44 using namespace xmltooling;
45 using namespace std;
46
47 namespace opensaml {
48     namespace saml1p {              
49         class SAML_DLLLOCAL SAML1POSTDecoder : public SAML1MessageDecoder
50         {
51         public:
52             SAML1POSTDecoder() {}
53             virtual ~SAML1POSTDecoder() {}
54             
55             xmltooling::XMLObject* decode(
56                 std::string& relayState,
57                 const GenericRequest& genericRequest,
58                 SecurityPolicy& policy
59                 ) const;
60         };                
61
62         MessageDecoder* SAML_DLLLOCAL SAML1POSTDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
63         {
64             return new SAML1POSTDecoder();
65         }
66     };
67 };
68
69 XMLObject* SAML1POSTDecoder::decode(
70     string& relayState,
71     const GenericRequest& genericRequest,
72     SecurityPolicy& policy
73     ) const
74 {
75 #ifdef _DEBUG
76     xmltooling::NDC ndc("decode");
77 #endif
78     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1POST");
79
80     log.debug("validating input");
81     const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
82     if (!httpRequest)
83         throw BindingException("Unable to cast request object to HTTPRequest type.");
84     if (strcmp(httpRequest->getMethod(),"POST"))
85         throw BindingException("Invalid HTTP method ($1).", params(1, httpRequest->getMethod()));
86     const char* samlResponse = httpRequest->getParameter("SAMLResponse");
87     const char* TARGET = httpRequest->getParameter("TARGET");
88     if (!samlResponse || !TARGET)
89         throw BindingException("Request missing SAMLResponse or TARGET form parameters.");
90     relayState = TARGET;
91
92     // Decode the base64 into XML.
93     xsecsize_t x;
94     XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(samlResponse),&x);
95     if (!decoded)
96         throw BindingException("Unable to decode base64 in POST profile response.");
97     log.debugStream() << "decoded SAML response:\n" << decoded << logging::eol;
98
99     // Parse and bind the document into an XMLObject.
100     MemBufInputSource src(decoded, x, "SAMLResponse", true);
101     Wrapper4InputSource dsrc(&src, false);
102     DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
103         : XMLToolingConfig::getConfig().getParser()).parse(dsrc); 
104     XercesJanitor<DOMDocument> janitor(doc);
105     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
106     janitor.release();
107
108     Response* response = dynamic_cast<Response*>(xmlObject.get());
109     if (!response)
110         throw BindingException("Decoded message was not a SAML 1.x Response.");
111
112     SchemaValidators.validate(response);
113     
114     pair<bool,int> minor = response->getMinorVersion();
115     extractMessageDetails(
116         *response,
117         genericRequest,
118         (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM,
119         policy
120         );
121
122     // Run through the policy.
123     policy.evaluate(*response,&genericRequest);
124     
125     // Check recipient URL.
126     auto_ptr_char recipient(response->getRecipient());
127     const char* recipient2 = httpRequest->getRequestURL();
128     const char* delim = strchr(recipient2, '?');
129     if (!recipient.get() || !*(recipient.get())) {
130         log.error("response missing Recipient attribute");
131         throw BindingException("SAML response did not contain Recipient attribute identifying intended destination.");
132     }
133     else if ((delim && strncmp(recipient.get(), recipient2, delim - recipient2)) || (!delim && strcmp(recipient.get(),recipient2))) {
134         log.error("POST targeted at (%s), but delivered to (%s)", recipient.get(), recipient2);
135         throw BindingException("SAML message delivered with POST to incorrect server URL.");
136     }
137     
138     return xmlObject.release();
139 }