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 * SAML2POSTDecoder.cpp
20 * SAML 2.0 HTTP POST binding message encoder
24 #include "exceptions.h"
25 #include "binding/HTTPRequest.h"
26 #include "saml2/binding/SAML2POSTDecoder.h"
27 #include "saml2/core/Protocols.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::saml2p;
38 using namespace opensaml::saml2;
39 using namespace opensaml;
40 using namespace xmltooling;
41 using namespace log4cpp;
46 MessageDecoder* SAML_DLLLOCAL SAML2POSTDecoderFactory(const DOMElement* const & e)
48 return new SAML2POSTDecoder(e);
53 SAML2POSTDecoder::SAML2POSTDecoder(const DOMElement* e) {}
55 SAML2POSTDecoder::~SAML2POSTDecoder() {}
57 saml2::RootObject* SAML2POSTDecoder::decode(
58 std::string& relayState,
59 const GenericRequest& genericRequest,
60 SecurityPolicy& policy
64 xmltooling::NDC ndc("decode");
66 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2POST");
68 log.debug("validating input");
69 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
71 log.error("unable to cast request to HTTPRequest type");
74 if (strcmp(httpRequest->getMethod(),"POST"))
76 const char* msg = httpRequest->getParameter("SAMLResponse");
78 msg = httpRequest->getParameter("SAMLRequest");
81 const char* state = httpRequest->getParameter("RelayState");
87 // Decode the base64 into SAML.
89 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(msg),&x);
91 throw BindingException("Unable to decode base64 in POST binding message.");
92 log.debug("decoded SAML message:\n%s", decoded);
93 istringstream is(reinterpret_cast<char*>(decoded));
94 XMLString::release(&decoded);
96 // Parse and bind the document into an XMLObject.
97 DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
98 : XMLToolingConfig::getConfig().getParser()).parse(is);
99 XercesJanitor<DOMDocument> janitor(doc);
100 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
103 saml2::RootObject* root = dynamic_cast<saml2::RootObject*>(xmlObject.get());
105 throw BindingException("XML content for SAML 2.0 HTTP-POST Decoder must be a SAML 2.0 protocol message.");
109 SchemaValidators.validate(xmlObject.get());
111 // Run through the policy.
112 policy.evaluate(genericRequest, *root);
114 catch (XMLToolingException& ex) {
115 // This is just to maximize the likelihood of attaching a source to the message for support purposes.
116 if (policy.getIssuerMetadata())
117 annotateException(&ex,policy.getIssuerMetadata()); // throws it
119 const Issuer* claimedIssuer = root->getIssuer();
120 if (!claimedIssuer) {
121 // Check for assertions.
122 const Response* assbag = dynamic_cast<Response*>(root);
124 const vector<Assertion*>& assertions=assbag->getAssertions();
125 if (!assertions.empty())
126 claimedIssuer = assertions.front()->getIssuer();
130 if (!claimedIssuer || !claimedIssuer->getName())
132 const EntityDescriptor* provider=NULL;
133 if (!policy.getMetadataProvider() ||
134 !(provider=policy.getMetadataProvider()->getEntityDescriptor(claimedIssuer->getName(), false))) {
136 auto_ptr_char iname(claimedIssuer->getName());
138 ex.addProperty("entityID", iname.get());
142 if (policy.getRole()) {
143 const RoleDescriptor* roledesc=provider->getRoleDescriptor(*(policy.getRole()), samlconstants::SAML20P_NS);
144 if (roledesc) annotateException(&ex,roledesc); // throws it
146 annotateException(&ex,provider); // throws it