2 * Copyright 2001-2007 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/HTTPRequest.h"
26 #include "binding/MessageDecoder.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 <log4cpp/Category.hh>
33 #include <xercesc/util/Base64.hpp>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/validation/ValidatorSuite.h>
37 using namespace opensaml::saml2md;
38 using namespace opensaml::saml1p;
39 using namespace opensaml::saml1;
40 using namespace opensaml;
41 using namespace xmltooling;
42 using namespace log4cpp;
47 class SAML_DLLLOCAL SAML1POSTDecoder : public MessageDecoder
50 SAML1POSTDecoder(const DOMElement* e) {}
51 virtual ~SAML1POSTDecoder() {}
53 xmltooling::XMLObject* decode(
54 std::string& relayState,
55 const GenericRequest& genericRequest,
56 SecurityPolicy& policy
60 MessageDecoder* SAML_DLLLOCAL SAML1POSTDecoderFactory(const DOMElement* const & e)
62 return new SAML1POSTDecoder(e);
67 XMLObject* SAML1POSTDecoder::decode(
69 const GenericRequest& genericRequest,
70 SecurityPolicy& policy
74 xmltooling::NDC ndc("decode");
76 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1POST");
78 log.debug("validating input");
79 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
81 log.error("unable to cast request to HTTPRequest type");
84 if (strcmp(httpRequest->getMethod(),"POST"))
86 const char* samlResponse = httpRequest->getParameter("SAMLResponse");
87 const char* TARGET = httpRequest->getParameter("TARGET");
88 if (!samlResponse || !TARGET)
92 // Decode the base64 into SAML.
94 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(samlResponse),&x);
96 throw BindingException("Unable to decode base64 in POST profile response.");
97 log.debug("decoded SAML response:\n%s", decoded);
98 istringstream is(reinterpret_cast<char*>(decoded));
99 XMLString::release(&decoded);
101 // Parse and bind the document into an XMLObject.
102 DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
103 : XMLToolingConfig::getConfig().getParser()).parse(is);
104 XercesJanitor<DOMDocument> janitor(doc);
105 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
108 Response* response = dynamic_cast<Response*>(xmlObject.get());
110 throw BindingException("Decoded message was not a SAML 1.x Response.");
112 if (!policy.getValidating())
113 SchemaValidators.validate(xmlObject.get());
115 // Run through the policy.
116 policy.evaluate(*response, &genericRequest);
118 // Check recipient URL.
119 auto_ptr_char recipient(response->getRecipient());
120 const char* recipient2 = httpRequest->getRequestURL();
121 if (!recipient.get() || !*(recipient.get())) {
122 log.error("response missing Recipient attribute");
123 throw BindingException("SAML response did not contain Recipient attribute identifying intended destination.");
125 else if (!recipient2 || !*recipient2 || strcmp(recipient.get(),recipient2)) {
126 log.error("POST targeted at (%s), but delivered to (%s)", recipient.get(), recipient2 ? recipient2 : "none");
127 throw BindingException("SAML message delivered with POST to incorrect server URL.");
130 return xmlObject.release();