Added MessageRoutingRule for destination checks.
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2RedirectDecoder.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  * SAML2RedirectDecoder.cpp
19  * 
20  * SAML 2.0 HTTP Redirect binding message encoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/HTTPRequest.h"
26 #include "saml2/binding/SAML2Redirect.h"
27 #include "saml2/binding/SAML2RedirectDecoder.h"
28 #include "saml2/core/Protocols.h"
29 #include "saml2/metadata/Metadata.h"
30 #include "saml2/metadata/MetadataProvider.h"
31
32 #include <log4cpp/Category.hh>
33 #include <xercesc/util/Base64.hpp>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/validation/ValidatorSuite.h>
36
37 using namespace opensaml::saml2md;
38 using namespace opensaml::saml2p;
39 using namespace opensaml::saml2;
40 using namespace opensaml;
41 using namespace xmlsignature;
42 using namespace xmltooling;
43 using namespace log4cpp;
44 using namespace std;
45
46 namespace opensaml {
47     namespace saml2p {              
48         MessageDecoder* SAML_DLLLOCAL SAML2RedirectDecoderFactory(const DOMElement* const & e)
49         {
50             return new SAML2RedirectDecoder(e);
51         }
52     };
53 };
54
55 SAML2RedirectDecoder::SAML2RedirectDecoder(const DOMElement* e) {}
56
57 SAML2RedirectDecoder::~SAML2RedirectDecoder() {}
58
59 XMLObject* SAML2RedirectDecoder::decode(
60     string& relayState,
61     const GenericRequest& genericRequest,
62     SecurityPolicy& policy
63     ) const
64 {
65 #ifdef _DEBUG
66     xmltooling::NDC ndc("decode");
67 #endif
68     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Redirect");
69
70     log.debug("validating input");
71     const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
72     if (!httpRequest) {
73         log.error("unable to cast request to HTTPRequest type");
74         return NULL;
75     }
76     if (strcmp(httpRequest->getMethod(),"GET"))
77         return NULL;
78     const char* msg = httpRequest->getParameter("SAMLResponse");
79     if (!msg)
80         msg = httpRequest->getParameter("SAMLRequest");
81     if (!msg)
82         return NULL;
83     const char* state = httpRequest->getParameter("RelayState");
84     if (state)
85         relayState = state;
86     else
87         relayState.erase();
88     state = httpRequest->getParameter("SAMLEncoding");
89     if (state && strcmp(state,samlconstants::SAML20_BINDING_URL_ENCODING_DEFLATE)) {
90         log.warn("SAMLEncoding (%s) was not recognized", state);
91         return NULL;
92     }
93
94     // Decode the compressed message into SAML. First we base64-decode it.
95     unsigned int x;
96     XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(msg),&x);
97     if (!decoded)
98         throw BindingException("Unable to decode base64 in Redirect binding message.");
99     
100     // Now we have to inflate it.
101     stringstream str;
102     if (inflate((char*)decoded, x, str)==0) {
103         XMLString::release(&decoded);
104         throw BindingException("Unable to inflate Redirect binding message.");
105     }
106
107     XMLString::release(&decoded);
108     
109     // Parse and bind the document into an XMLObject.
110     DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
111         : XMLToolingConfig::getConfig().getParser()).parse(str);
112     XercesJanitor<DOMDocument> janitor(doc);
113     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
114     janitor.release();
115
116     saml2::RootObject* root = dynamic_cast<saml2::RootObject*>(xmlObject.get());
117     if (!root)
118         throw BindingException("XML content for SAML 2.0 HTTP-POST Decoder must be a SAML 2.0 protocol message.");
119     
120     try {
121         if (!m_validate)
122             SchemaValidators.validate(xmlObject.get());
123         
124         // Run through the policy.
125         policy.evaluate(genericRequest, *root);
126     }
127     catch (XMLToolingException& ex) {
128         // This is just to maximize the likelihood of attaching a source to the message for support purposes.
129         if (policy.getIssuerMetadata())
130             annotateException(&ex,policy.getIssuerMetadata()); // throws it
131
132         const Issuer* claimedIssuer = root->getIssuer();
133         if (!claimedIssuer || !claimedIssuer->getName())
134             throw;
135         const EntityDescriptor* provider=NULL;
136         if (!policy.getMetadataProvider() ||
137                 !(provider=policy.getMetadataProvider()->getEntityDescriptor(claimedIssuer->getName(), false))) {
138             // Just record it.
139             auto_ptr_char iname(claimedIssuer->getName());
140             if (iname.get())
141                 ex.addProperty("entityID", iname.get());
142             throw;
143         }
144
145         if (policy.getRole()) {
146             const RoleDescriptor* roledesc=provider->getRoleDescriptor(*(policy.getRole()), samlconstants::SAML20P_NS);
147             if (roledesc) annotateException(&ex,roledesc); // throws it
148         }
149         annotateException(&ex,provider);  // throws it
150     }
151
152     xmlObject.release();
153     return root;
154 }