9d4e2fd5bd0621339a78dafe2ad53f7597629694
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2ECPDecoder.cpp
1 /*
2  *  Copyright 2001-2007 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  * SAML2ECPDecoder.cpp
19  * 
20  * SAML 2.0 ECP profile message decoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "saml2/binding/SAML2MessageDecoder.h"
26 #include "saml2/core/Protocols.h"
27
28 #include <xmltooling/logging.h>
29 #include <xmltooling/soap/SOAP.h>
30 #include <xmltooling/util/NDC.h>
31 #include <xmltooling/validation/ValidatorSuite.h>
32
33 using namespace opensaml::saml2p;
34 using namespace opensaml;
35 using namespace soap11;
36 using namespace xmltooling::logging;
37 using namespace xmltooling;
38 using namespace std;
39
40 namespace opensaml {
41     namespace saml2p {              
42         class SAML_DLLLOCAL SAML2ECPDecoder : public SAML2MessageDecoder
43         {
44         public:
45             SAML2ECPDecoder() {}
46             virtual ~SAML2ECPDecoder() {}
47
48             xmltooling::XMLObject* decode(
49                 std::string& relayState,
50                 const GenericRequest& genericRequest,
51                 SecurityPolicy& policy
52                 ) const;
53         };                
54
55         MessageDecoder* SAML_DLLLOCAL SAML2ECPDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
56         {
57             return new SAML2ECPDecoder();
58         }
59     };
60 };
61
62 XMLObject* SAML2ECPDecoder::decode(
63     string& relayState,
64     const GenericRequest& genericRequest,
65     SecurityPolicy& policy
66     ) const
67 {
68 #ifdef _DEBUG
69     xmltooling::NDC ndc("decode");
70 #endif
71     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2ECP");
72
73     log.debug("validating input");
74     string s = genericRequest.getContentType();
75     if (s.find("application/vnd.paos+xml") == string::npos) {
76         log.warn("ignoring incorrect content type (%s)", s.c_str() ? s.c_str() : "none");
77         throw BindingException("Invalid content type for PAOS message.");
78     }
79
80     const char* data = genericRequest.getRequestBody();
81     if (!data)
82         throw BindingException("PAOS message had an empty request body.");
83     istringstream is(data);
84     
85     // Parse and bind the document into an XMLObject.
86     DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
87         : XMLToolingConfig::getConfig().getParser()).parse(is); 
88     XercesJanitor<DOMDocument> janitor(doc);
89     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
90     janitor.release();
91
92     Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
93     if (!env)
94         throw BindingException("Decoded message was not a SOAP 1.1 Envelope.");
95
96     if (!policy.getValidating())
97         SchemaValidators.validate(env);
98     
99     Body* body = env->getBody();
100     if (body && body->hasChildren()) {
101         Response* response = dynamic_cast<Response*>(body->getUnknownXMLObjects().front());
102         if (response) {
103             // Run through the policy at two layers.
104             extractMessageDetails(*env, genericRequest, samlconstants::SAML20P_NS, policy);
105             policy.evaluate(*env, &genericRequest);
106             policy.reset(true);
107             extractMessageDetails(*response, genericRequest, samlconstants::SAML20P_NS, policy);
108             policy.evaluate(*response, &genericRequest);
109             
110             // Check for RelayState header.
111             if (env->getHeader()) {
112                 const vector<XMLObject*>& blocks = const_cast<const Header*>(env->getHeader())->getUnknownXMLObjects();
113                 for (vector<XMLObject*>::const_iterator h = blocks.begin(); h != blocks.end(); ++h) {
114                     static const XMLCh RelayState[] = UNICODE_LITERAL_10(R,e,l,a,y,S,t,a,t,e);
115                     if (XMLString::equals((*h)->getElementQName().getLocalPart(), RelayState) &&
116                             XMLString::equals((*h)->getElementQName().getNamespaceURI(), samlconstants::SAML20ECP_NS)) {
117                         const ElementProxy* ep = dynamic_cast<const ElementProxy*>(*h);
118                         if (ep) {
119                             auto_ptr_char rs(ep->getTextContent());
120                             if (rs.get()) {
121                                 relayState = rs.get();
122                                 break;
123                             }
124                         }
125                     }
126                 }
127             }
128             
129             xmlObject.release();
130             body->detach(); // frees Envelope
131             response->detach();   // frees Body
132             return response;
133         }
134     }
135     
136     throw BindingException("SOAP Envelope did not contain a SAML Response.");
137 }