2 * Copyright 2001-2009 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SAML1POSTDecoder.cpp
20 * SAML 1.x POST binding/profile message decoder
24 #include "exceptions.h"
25 #include "binding/SecurityPolicy.h"
26 #include "saml1/binding/SAML1MessageDecoder.h"
27 #include "saml1/core/Assertions.h"
28 #include "saml1/core/Protocols.h"
29 #include "saml2/metadata/Metadata.h"
30 #include "saml2/metadata/MetadataProvider.h"
32 #include <xercesc/framework/MemBufInputSource.hpp>
33 #include <xercesc/framework/Wrapper4InputSource.hpp>
34 #include <xercesc/util/Base64.hpp>
35 #include <xmltooling/logging.h>
36 #include <xmltooling/XMLToolingConfig.h>
37 #include <xmltooling/io/HTTPRequest.h>
38 #include <xmltooling/util/NDC.h>
39 #include <xmltooling/validation/ValidatorSuite.h>
41 using namespace opensaml::saml2md;
42 using namespace opensaml::saml1p;
43 using namespace opensaml::saml1;
44 using namespace opensaml;
45 using namespace xmltooling::logging;
46 using namespace xmltooling;
51 class SAML_DLLLOCAL SAML1POSTDecoder : public SAML1MessageDecoder
55 virtual ~SAML1POSTDecoder() {}
57 xmltooling::XMLObject* decode(
58 std::string& relayState,
59 const GenericRequest& genericRequest,
60 SecurityPolicy& policy
64 MessageDecoder* SAML_DLLLOCAL SAML1POSTDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
66 return new SAML1POSTDecoder();
71 XMLObject* SAML1POSTDecoder::decode(
73 const GenericRequest& genericRequest,
74 SecurityPolicy& policy
78 xmltooling::NDC ndc("decode");
80 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1POST");
82 log.debug("validating input");
83 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
85 throw BindingException("Unable to cast request object to HTTPRequest type.");
86 if (strcmp(httpRequest->getMethod(),"POST"))
87 throw BindingException("Invalid HTTP method ($1).", params(1, httpRequest->getMethod()));
88 const char* samlResponse = httpRequest->getParameter("SAMLResponse");
89 const char* TARGET = httpRequest->getParameter("TARGET");
90 if (!samlResponse || !TARGET)
91 throw BindingException("Request missing SAMLResponse or TARGET form parameters.");
94 // Decode the base64 into XML.
96 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(samlResponse),&x);
98 throw BindingException("Unable to decode base64 in POST profile response.");
99 log.debugStream() << "decoded SAML response:\n" << decoded << logging::eol;
101 // Parse and bind the document into an XMLObject.
102 MemBufInputSource src(decoded, x, "SAMLResponse", true);
103 Wrapper4InputSource dsrc(&src, false);
104 DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
105 : XMLToolingConfig::getConfig().getParser()).parse(dsrc);
106 XercesJanitor<DOMDocument> janitor(doc);
107 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
110 Response* response = dynamic_cast<Response*>(xmlObject.get());
112 throw BindingException("Decoded message was not a SAML 1.x Response.");
114 SchemaValidators.validate(response);
116 pair<bool,int> minor = response->getMinorVersion();
117 extractMessageDetails(
120 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM,
124 // Run through the policy.
125 policy.evaluate(*response,&genericRequest);
127 // Check recipient URL.
128 auto_ptr_char recipient(response->getRecipient());
129 const char* recipient2 = httpRequest->getRequestURL();
130 const char* delim = strchr(recipient2, '?');
131 if (!recipient.get() || !*(recipient.get())) {
132 log.error("response missing Recipient attribute");
133 throw BindingException("SAML response did not contain Recipient attribute identifying intended destination.");
135 else if ((delim && strncmp(recipient.get(), recipient2, delim - recipient2)) || (!delim && strcmp(recipient.get(),recipient2))) {
136 log.error("POST targeted at (%s), but delivered to (%s)", recipient.get(), recipient2);
137 throw BindingException("SAML message delivered with POST to incorrect server URL.");
140 return xmlObject.release();