Collapse unneeded header files.
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2RedirectDecoder.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  * SAML2RedirectDecoder.cpp
19  * 
20  * SAML 2.0 HTTP Redirect binding message encoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/HTTPRequest.h"
26 #include "binding/MessageDecoder.h"
27 #include "saml2/binding/SAML2Redirect.h"
28 #include "saml2/core/Protocols.h"
29 #include "saml2/metadata/Metadata.h"
30 #include "saml2/metadata/MetadataProvider.h"
31
32 #include <log4cpp/Category.hh>
33 #include <xercesc/util/Base64.hpp>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/validation/ValidatorSuite.h>
36
37 using namespace opensaml::saml2md;
38 using namespace opensaml::saml2p;
39 using namespace opensaml::saml2;
40 using namespace opensaml;
41 using namespace xmlsignature;
42 using namespace xmltooling;
43 using namespace log4cpp;
44 using namespace std;
45
46 namespace opensaml {
47     namespace saml2p {              
48         class SAML_DLLLOCAL SAML2RedirectDecoder : public MessageDecoder
49         {
50         public:
51             SAML2RedirectDecoder(const DOMElement* e) {}
52             virtual ~SAML2RedirectDecoder() {}
53             
54             xmltooling::XMLObject* decode(
55                 std::string& relayState,
56                 const GenericRequest& genericRequest,
57                 SecurityPolicy& policy
58                 ) const;
59         };                
60
61         MessageDecoder* SAML_DLLLOCAL SAML2RedirectDecoderFactory(const DOMElement* const & e)
62         {
63             return new SAML2RedirectDecoder(e);
64         }
65     };
66 };
67
68 XMLObject* SAML2RedirectDecoder::decode(
69     string& relayState,
70     const GenericRequest& genericRequest,
71     SecurityPolicy& policy
72     ) const
73 {
74 #ifdef _DEBUG
75     xmltooling::NDC ndc("decode");
76 #endif
77     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Redirect");
78
79     log.debug("validating input");
80     const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
81     if (!httpRequest) {
82         log.error("unable to cast request to HTTPRequest type");
83         return NULL;
84     }
85     if (strcmp(httpRequest->getMethod(),"GET"))
86         return NULL;
87     const char* msg = httpRequest->getParameter("SAMLResponse");
88     if (!msg)
89         msg = httpRequest->getParameter("SAMLRequest");
90     if (!msg)
91         return NULL;
92     const char* state = httpRequest->getParameter("RelayState");
93     if (state)
94         relayState = state;
95     else
96         relayState.erase();
97     state = httpRequest->getParameter("SAMLEncoding");
98     if (state && strcmp(state,samlconstants::SAML20_BINDING_URL_ENCODING_DEFLATE)) {
99         log.warn("SAMLEncoding (%s) was not recognized", state);
100         return NULL;
101     }
102
103     // Decode the compressed message into SAML. First we base64-decode it.
104     unsigned int x;
105     XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(msg),&x);
106     if (!decoded)
107         throw BindingException("Unable to decode base64 in Redirect binding message.");
108     
109     // Now we have to inflate it.
110     stringstream s;
111     if (inflate((char*)decoded, x, s)==0) {
112         XMLString::release(&decoded);
113         throw BindingException("Unable to inflate Redirect binding message.");
114     }
115     if (log.isDebugEnabled())
116         log.debug("decoded SAML message:\n%s", s.str().c_str());
117     XMLString::release(&decoded);
118     
119     // Parse and bind the document into an XMLObject.
120     DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
121         : XMLToolingConfig::getConfig().getParser()).parse(s);
122     XercesJanitor<DOMDocument> janitor(doc);
123     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
124     janitor.release();
125
126     saml2::RootObject* root = NULL;
127     StatusResponseType* response = NULL;
128     RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(xmlObject.get());
129     if (!request) {
130         response = dynamic_cast<StatusResponseType*>(xmlObject.get());
131         if (!response)
132             throw BindingException("XML content for SAML 2.0 HTTP-POST Decoder must be a SAML 2.0 protocol message.");
133         root = static_cast<saml2::RootObject*>(response);
134     }
135     else {
136         root = static_cast<saml2::RootObject*>(request);
137     }
138     
139     if (!policy.getValidating())
140         SchemaValidators.validate(xmlObject.get());
141     
142     // Run through the policy.
143     policy.evaluate(*root, &genericRequest);
144
145     // Check destination URL.
146     auto_ptr_char dest(request ? request->getDestination() : response->getDestination());
147     const char* dest2 = httpRequest->getRequestURL();
148     if ((root->getSignature() || httpRequest->getParameter("Signature")) && !dest.get() || !*(dest.get())) {
149         log.error("signed SAML message missing Destination attribute");
150         throw BindingException("Signed SAML message missing Destination attribute identifying intended destination.");
151     }
152     else if (dest.get() && (!dest2 || !*dest2 || strcmp(dest.get(),dest2))) {
153         log.error("Redirect targeted at (%s), but delivered to (%s)", dest.get(), dest2 ? dest2 : "none");
154         throw BindingException("SAML message delivered with Redirect to incorrect server URL.");
155     }
156
157     return xmlObject.release();
158 }