Rework POST handling to avoid remoting data if handler doesn't run.
[shibboleth/cpp-sp.git] / shibsp / handler / AbstractHandler.h
index bc17689..20a480b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2007 Internet2
+ *  Copyright 2001-2009 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #define __shibsp_abshandler_h__
 
 #include <shibsp/handler/Handler.h>
+#include <shibsp/remoting/ddf.h>
 #include <shibsp/util/DOMPropertySet.h>
 
-#include <log4cpp/Category.hh>
-#include <saml/binding/HTTPRequest.h>
-#include <saml/binding/HTTPResponse.h>
+#ifndef SHIBSP_LITE
+# include <saml/binding/MessageEncoder.h>
+# include <saml/saml2/core/Protocols.h>
+#endif
+#include <xmltooling/logging.h>
 #include <xmltooling/XMLObject.h>
+#include <xmltooling/io/HTTPRequest.h>
+#include <xmltooling/io/HTTPResponse.h>
 
 namespace shibsp {
 
@@ -50,27 +55,71 @@ namespace shibsp {
         /**
          * Constructor
          * 
-         * @param e         DOM element to load as property set. 
+         * @param e         DOM element to load as property set.
+         * @param log       logging category to use
          * @param filter    optional filter controls what child elements to include as nested PropertySets
          * @param remapper  optional map of property rename rules for legacy property support
          */
         AbstractHandler(
             const xercesc::DOMElement* e,
-            log4cpp::Category& log,
+            xmltooling::logging::Category& log,
             xercesc::DOMNodeFilter* filter=NULL,
             const std::map<std::string,std::string>* remapper=NULL
             );
 
+#ifndef SHIBSP_LITE
         /**
          * Examines a protocol response message for errors and raises an annotated exception
          * if an error is found.
          * 
          * <p>The base class version understands SAML 1.x and SAML 2.0 responses.
          * 
-         * @param response      a response message of some known protocol
+         * @param response  a response message of some known protocol
+         * @param role      issuer of message
          */
-        virtual void checkError(const xmltooling::XMLObject* response) const;
-        
+        virtual void checkError(
+            const xmltooling::XMLObject* response,
+            const opensaml::saml2md::RoleDescriptor* role=NULL
+            ) const;
+
+        /**
+         * Prepares Status information in a SAML 2.0 response.
+         * 
+         * @param response  SAML 2.0 response message
+         * @param code      SAML status code
+         * @param subcode   optional SAML substatus code
+         * @param msg       optional message to pass back
+         */
+        void fillStatus(
+            opensaml::saml2p::StatusResponseType& response, const XMLCh* code, const XMLCh* subcode=NULL, const char* msg=NULL
+            ) const;
+
+        /**
+         * Encodes and sends SAML 2.0 message, optionally signing it in the process.
+         * If the method returns, the message MUST NOT be freed by the caller.
+         *
+         * @param encoder           the MessageEncoder to use
+         * @param msg               the message to send
+         * @param relayState        any RelayState to include with the message
+         * @param destination       location to send message, if not a backchannel response
+         * @param role              recipient of message, if known
+         * @param application       the Application sending the message
+         * @param httpResponse      channel for sending message
+         * @param signIfPossible    true iff signing should be attempted regardless of "signing" property
+         * @return  the result of sending the message using the encoder
+         */
+        long sendMessage(
+            const opensaml::MessageEncoder& encoder,
+            xmltooling::XMLObject* msg,
+            const char* relayState,
+            const char* destination,
+            const opensaml::saml2md::RoleDescriptor* role,
+            const Application& application,
+            xmltooling::HTTPResponse& httpResponse,
+            bool signIfPossible=false
+            ) const;
+#endif
+
         /**
          * Implements various mechanisms to preserve RelayState,
          * such as cookies or StorageService-backed keys.
@@ -82,7 +131,9 @@ namespace shibsp {
          * @param response      outgoing HTTP response
          * @param relayState    RelayState token to supply with message
          */
-        virtual void preserveRelayState(const Application& application, opensaml::HTTPResponse& response, std::string& relayState) const;
+        virtual void preserveRelayState(
+            const Application& application, xmltooling::HTTPResponse& response, std::string& relayState
+            ) const;
 
         /**
          * Implements various mechanisms to recover RelayState,
@@ -93,18 +144,80 @@ namespace shibsp {
          * 
          * @param application   the associated Application
          * @param request       incoming HTTP request
+         * @param response      outgoing HTTP response
          * @param relayState    RelayState token supplied with message
          * @param clear         true iff the token state should be cleared
          */
         virtual void recoverRelayState(
-            const Application& application, opensaml::HTTPRequest& request, std::string& relayState, bool clear=true
+            const Application& application,
+            const xmltooling::HTTPRequest& request,
+            xmltooling::HTTPResponse& response,
+            std::string& relayState,
+            bool clear=true
             ) const;
         
+        /**
+         * Implements a mechanism to preserve form post data.
+         *
+         * @param application   the associated Application
+         * @param request       incoming HTTP request
+         * @param response      outgoing HTTP response
+         * @param relayState    relay state information attached to current sequence, if any
+         */
+        virtual void preservePostData(
+            const Application& application,
+            const xmltooling::HTTPRequest& request,
+            xmltooling::HTTPResponse& response,
+            const char* relayState
+            ) const;
+
+        /**
+         * Implements storage service and cookie mechanism to recover PostData.
+         *
+         * <p>If a supported mechanism can be identified, the return value will be
+         * the recovered state information.
+         *
+         * @param application   the associated Application
+         * @param request       incoming HTTP request
+         * @param response      outgoing HTTP response
+         * @param relayState    relay state information attached to current sequence, if any
+         * @return  recovered form post data associated with request as a DDF list of string members
+         */
+        virtual DDF recoverPostData(
+            const Application& application,
+            const xmltooling::HTTPRequest& request,
+            xmltooling::HTTPResponse& response,
+            const char* relayState
+            ) const;
+
+        /**
+         * Post a redirect response with post data.
+         * 
+         * @param application   the associated Application
+         * @param response      outgoing HTTP response
+         * @param request       incoming HTTP request
+         * @param url           action url for the form
+         * @param postData      list of parameters to load into the form, as DDF string members
+         */
+        virtual long sendPostResponse(
+            const Application& application,
+            xmltooling::HTTPResponse& httpResponse,
+            const char* url,
+            DDF& postData
+            ) const;
+
         /** Logging object. */
-        log4cpp::Category& m_log;
+        xmltooling::logging::Category& m_log;
         
+        /** Configuration namespace for custom properties. */
+        xmltooling::auto_ptr_char m_configNS;
+
     public:
         virtual ~AbstractHandler() {}
+
+    private:
+        std::pair<std::string,const char*> getPostCookieNameProps(const Application& app, const char* relayState) const;
+        DDF getPostData(const Application& application, const xmltooling::HTTPRequest& request) const;
     };
 
 #if defined (_MSC_VER)