2 * Copyright 2001-2006 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 "saml1/core/Assertions.h"
27 #include "saml1/binding/SAML1POSTDecoder.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataProvider.h"
31 #include <log4cpp/Category.hh>
32 #include <xercesc/util/Base64.hpp>
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/validation/ValidatorSuite.h>
36 using namespace opensaml::saml2md;
37 using namespace opensaml::saml1p;
38 using namespace opensaml::saml1;
39 using namespace opensaml;
40 using namespace xmltooling;
41 using namespace log4cpp;
46 MessageDecoder* SAML_DLLLOCAL SAML1POSTDecoderFactory(const DOMElement* const & e)
48 return new SAML1POSTDecoder(e);
53 SAML1POSTDecoder::SAML1POSTDecoder(const DOMElement* e) {}
55 Response* SAML1POSTDecoder::decode(
57 const GenericRequest& genericRequest,
58 SecurityPolicy& policy
62 xmltooling::NDC ndc("decode");
64 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1POST");
66 log.debug("validating input");
67 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
69 log.error("unable to cast request to HTTPRequest type");
72 if (strcmp(httpRequest->getMethod(),"POST"))
74 const char* samlResponse = httpRequest->getParameter("SAMLResponse");
75 const char* TARGET = httpRequest->getParameter("TARGET");
76 if (!samlResponse || !TARGET)
80 // Decode the base64 into SAML.
82 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(samlResponse),&x);
84 throw BindingException("Unable to decode base64 in POST profile response.");
85 log.debug("decoded SAML response:\n%s", decoded);
86 istringstream is(reinterpret_cast<char*>(decoded));
87 XMLString::release(&decoded);
89 // Parse and bind the document into an XMLObject.
90 DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
91 : XMLToolingConfig::getConfig().getParser()).parse(is);
92 XercesJanitor<DOMDocument> janitor(doc);
93 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
96 Response* response = dynamic_cast<Response*>(xmlObject.get());
98 throw BindingException("Decoded message was not a SAML 1.x Response.");
102 SchemaValidators.validate(xmlObject.get());
104 // Check recipient URL.
105 auto_ptr_char recipient(response->getRecipient());
106 const char* recipient2 = httpRequest->getRequestURL();
107 if (!recipient.get() || !*(recipient.get())) {
108 log.error("response missing Recipient attribute");
109 throw BindingException("SAML response did not contain Recipient attribute identifying intended destination.");
111 else if (!recipient2 || !*recipient2 || strcmp(recipient.get(),recipient2)) {
112 log.error("POST targeted at (%s), but delivered to (%s)", recipient.get(), recipient2 ? recipient2 : "none");
113 throw BindingException("SAML message delivered with POST to incorrect server URL.");
116 // Run through the policy.
117 policy.evaluate(genericRequest, *response);
119 catch (XMLToolingException& ex) {
120 // This is just to maximize the likelihood of attaching a source to the message for support purposes.
121 if (policy.getIssuerMetadata())
122 annotateException(&ex,policy.getIssuerMetadata()); // throws it
124 // Check for an Issuer.
125 const EntityDescriptor* provider=NULL;
126 const vector<Assertion*>& assertions=const_cast<const Response*>(response)->getAssertions();
127 if (!assertions.empty() || !policy.getMetadataProvider() ||
128 !(provider=policy.getMetadataProvider()->getEntityDescriptor(assertions.front()->getIssuer(), false))) {
130 auto_ptr_char iname(assertions.front()->getIssuer());
132 ex.addProperty("entityID", iname.get());
135 if (policy.getRole()) {
136 pair<bool,int> minor = response->getMinorVersion();
137 const RoleDescriptor* roledesc=provider->getRoleDescriptor(
139 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM
141 if (roledesc) annotateException(&ex,roledesc); // throws it
143 annotateException(&ex,provider); // throws it