Convert logging to log4shib via compile time switch.
[shibboleth/opensaml2.git] / saml / saml1 / binding / impl / SAML1POSTDecoder.cpp
1 /*
2  *  Copyright 2001-2007 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 "binding/MessageDecoder.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/util/Base64.hpp>
32 #include <xmltooling/logging.h>
33 #include <xmltooling/io/HTTPRequest.h>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/validation/ValidatorSuite.h>
36
37 using namespace opensaml::saml2md;
38 using namespace opensaml::saml1p;
39 using namespace opensaml::saml1;
40 using namespace opensaml;
41 using namespace xmltooling::logging;
42 using namespace xmltooling;
43 using namespace std;
44
45 namespace opensaml {
46     namespace saml1p {              
47         class SAML_DLLLOCAL SAML1POSTDecoder : public MessageDecoder
48         {
49         public:
50             SAML1POSTDecoder() {}
51             virtual ~SAML1POSTDecoder() {}
52             
53             xmltooling::XMLObject* decode(
54                 std::string& relayState,
55                 const GenericRequest& genericRequest,
56                 SecurityPolicy& policy
57                 ) const;
58         };                
59
60         MessageDecoder* SAML_DLLLOCAL SAML1POSTDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
61         {
62             return new SAML1POSTDecoder();
63         }
64     };
65 };
66
67 XMLObject* SAML1POSTDecoder::decode(
68     string& relayState,
69     const GenericRequest& genericRequest,
70     SecurityPolicy& policy
71     ) const
72 {
73 #ifdef _DEBUG
74     xmltooling::NDC ndc("decode");
75 #endif
76     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1POST");
77
78     log.debug("validating input");
79     const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
80     if (!httpRequest)
81         throw BindingException("Unable to cast request object to HTTPRequest type.");
82     if (strcmp(httpRequest->getMethod(),"POST"))
83         throw BindingException("Invalid HTTP method ($1).", params(1, httpRequest->getMethod()));
84     const char* samlResponse = httpRequest->getParameter("SAMLResponse");
85     const char* TARGET = httpRequest->getParameter("TARGET");
86     if (!samlResponse || !TARGET)
87         throw BindingException("Request missing SAMLResponse or TARGET parameters.");
88     relayState = TARGET;
89
90     // Decode the base64 into SAML.
91     unsigned int x;
92     XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(samlResponse),&x);
93     if (!decoded)
94         throw BindingException("Unable to decode base64 in POST profile response.");
95     log.debug("decoded SAML response:\n%s", decoded);
96     istringstream is(reinterpret_cast<char*>(decoded));
97     XMLString::release(&decoded);
98     
99     // Parse and bind the document into an XMLObject.
100     DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
101         : XMLToolingConfig::getConfig().getParser()).parse(is); 
102     XercesJanitor<DOMDocument> janitor(doc);
103     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
104     janitor.release();
105
106     Response* response = dynamic_cast<Response*>(xmlObject.get());
107     if (!response)
108         throw BindingException("Decoded message was not a SAML 1.x Response.");
109
110     if (!policy.getValidating())
111         SchemaValidators.validate(xmlObject.get());
112
113     // Run through the policy.
114     policy.evaluate(*response, &genericRequest);
115     
116     // Check recipient URL.
117     auto_ptr_char recipient(response->getRecipient());
118     const char* recipient2 = httpRequest->getRequestURL();
119     const char* delim = strchr(recipient2, '?');
120     if (!recipient.get() || !*(recipient.get())) {
121         log.error("response missing Recipient attribute");
122         throw BindingException("SAML response did not contain Recipient attribute identifying intended destination.");
123     }
124     else if ((delim && strncmp(recipient.get(), recipient2, delim - recipient2)) || (!delim && strcmp(recipient.get(),recipient2))) {
125         log.error("POST targeted at (%s), but delivered to (%s)", recipient.get(), recipient2);
126         throw BindingException("SAML message delivered with POST to incorrect server URL.");
127     }
128     
129     return xmlObject.release();
130 }