bff2e554ca4c6eb0903e31dd7a2af096108f639d
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1POSTDecoder.cpp
1 /*
2  *  Copyright 2001-2006 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  * SAML1POSTDecoder.cpp
19  * 
20  * SAML 1.x POST binding/profile message decoder
21  */
22
23 #include "internal.h"
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"
30
31 #include <log4cpp/Category.hh>
32 #include <xercesc/util/Base64.hpp>
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/validation/ValidatorSuite.h>
35
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;
42 using namespace std;
43
44 namespace opensaml {
45     namespace saml1p {              
46         MessageDecoder* SAML_DLLLOCAL SAML1POSTDecoderFactory(const DOMElement* const & e)
47         {
48             return new SAML1POSTDecoder(e);
49         }
50     };
51 };
52
53 SAML1POSTDecoder::SAML1POSTDecoder(const DOMElement* e) {}
54
55 SAML1POSTDecoder::~SAML1POSTDecoder() {}
56
57 Response* SAML1POSTDecoder::decode(
58     string& relayState,
59     const GenericRequest& genericRequest,
60     SecurityPolicy& policy
61     ) const
62 {
63 #ifdef _DEBUG
64     xmltooling::NDC ndc("decode");
65 #endif
66     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1POST");
67
68     log.debug("validating input");
69     const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
70     if (!httpRequest) {
71         log.error("unable to cast request to HTTPRequest type");
72         return NULL;
73     }
74     if (strcmp(httpRequest->getMethod(),"POST"))
75         return NULL;
76     const char* samlResponse = httpRequest->getParameter("SAMLResponse");
77     const char* TARGET = httpRequest->getParameter("TARGET");
78     if (!samlResponse || !TARGET)
79         return NULL;
80     relayState = TARGET;
81
82     // Decode the base64 into SAML.
83     unsigned int x;
84     XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(samlResponse),&x);
85     if (!decoded)
86         throw BindingException("Unable to decode base64 in POST profile response.");
87     log.debug("decoded SAML response:\n%s", decoded);
88     istringstream is(reinterpret_cast<char*>(decoded));
89     XMLString::release(&decoded);
90     
91     // Parse and bind the document into an XMLObject.
92     DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
93         : XMLToolingConfig::getConfig().getParser()).parse(is); 
94     XercesJanitor<DOMDocument> janitor(doc);
95     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
96     janitor.release();
97
98     Response* response = dynamic_cast<Response*>(xmlObject.get());
99     if (!response)
100         throw BindingException("Decoded message was not a SAML 1.x Response.");
101
102     try {
103         if (!m_validate)
104             SchemaValidators.validate(xmlObject.get());
105         
106         // Run through the policy.
107         policy.evaluate(genericRequest, *response);
108     }
109     catch (XMLToolingException& ex) {
110         // This is just to maximize the likelihood of attaching a source to the message for support purposes.
111         if (policy.getIssuerMetadata())
112             annotateException(&ex,policy.getIssuerMetadata()); // throws it
113           
114         // Check for an Issuer.
115         const EntityDescriptor* provider=NULL;
116         const vector<Assertion*>& assertions=const_cast<const Response*>(response)->getAssertions();
117         if (!assertions.empty() || !policy.getMetadataProvider() ||
118                 !(provider=policy.getMetadataProvider()->getEntityDescriptor(assertions.front()->getIssuer(), false))) {
119             // Just record it.
120             auto_ptr_char iname(assertions.front()->getIssuer());
121             if (iname.get())
122                 ex.addProperty("entityID", iname.get());
123             throw;
124         }
125         if (policy.getRole()) {
126             pair<bool,int> minor = response->getMinorVersion();
127             const RoleDescriptor* roledesc=provider->getRoleDescriptor(
128                 *(policy.getRole()),
129                 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM
130                 );
131             if (roledesc) annotateException(&ex,roledesc); // throws it
132         }
133         annotateException(&ex,provider);  // throws it
134     }
135
136     xmlObject.release();
137     return response;
138 }