pion  5.0.6
include/pion/http/auth.hpp
00001 // ---------------------------------------------------------------------
00002 // pion:  a Boost C++ framework for building lightweight HTTP interfaces
00003 // ---------------------------------------------------------------------
00004 // Copyright (C) 2007-2014 Splunk Inc.  (https://github.com/splunk/pion)
00005 //
00006 // Distributed under the Boost Software License, Version 1.0.
00007 // See http://www.boost.org/LICENSE_1_0.txt
00008 //
00009 
00010 #ifndef __PION_HTTP_AUTH_HEADER__
00011 #define __PION_HTTP_AUTH_HEADER__
00012 
00013 #include <set>
00014 #include <map>
00015 #include <boost/noncopyable.hpp>
00016 #include <boost/shared_ptr.hpp>
00017 #include <pion/config.hpp>
00018 #include <pion/error.hpp>
00019 #include <pion/logger.hpp>
00020 #include <pion/hash_map.hpp>
00021 #include <pion/tcp/connection.hpp>
00022 #include <pion/user.hpp>
00023 #include <pion/http/request.hpp>
00024 #include <boost/date_time/posix_time/posix_time.hpp>    // order important, otherwise compiling error under win32
00025 
00026 
00027 namespace pion {    // begin namespace pion
00028 namespace http {    // begin namespace http
00029 
00030 
00034 class PION_API auth :
00035     private boost::noncopyable
00036 {
00037 public:
00038     
00040     auth(user_manager_ptr userManager) 
00041         : m_logger(PION_GET_LOGGER("pion.http.auth")),
00042         m_user_manager(userManager)
00043     {}
00044     
00046     virtual ~auth() {}
00047     
00060     virtual bool handle_request(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn) = 0;
00061     
00068     virtual void set_option(const std::string& name, const std::string& value) {
00069         BOOST_THROW_EXCEPTION( error::bad_arg() << error::errinfo_arg_name(name) );
00070     }
00071     
00077     void add_restrict(const std::string& resource);
00078     
00084     void add_permit(const std::string& resource);
00085 
00091     virtual bool add_user(std::string const &username, std::string const &password) {
00092         return m_user_manager->add_user(username, password);
00093     }
00094     
00100     virtual bool update_user(std::string const &username, std::string const &password) {
00101         return m_user_manager->update_user(username, password);
00102     }
00103     
00109     virtual bool remove_user(std::string const &username) {
00110         return m_user_manager->remove_user(username);
00111     };
00112     
00116     virtual user_ptr get_user(std::string const &username) {
00117         return m_user_manager->get_user(username);
00118     }
00119 
00120     
00121 protected:
00122 
00124     typedef std::set<std::string>   resource_set_type;
00125 
00127     typedef std::map<std::string,std::pair<boost::posix_time::ptime,user_ptr> >  user_cache_type;
00128     
00129     
00135     bool need_authentication(http::request_ptr const& http_request_ptr) const;
00136     
00145     bool find_resource(const resource_set_type& resource_set,
00146                       const std::string& resource) const;
00147 
00149     inline void set_logger(logger log_ptr) { m_logger = log_ptr; }
00150     
00151 
00153     mutable logger          m_logger;
00154     
00156     user_manager_ptr        m_user_manager;
00157     
00159     resource_set_type       m_restrict_list;
00160 
00162     resource_set_type       m_white_list;
00163 
00165     mutable boost::mutex    m_resource_mutex;
00166 };
00167 
00169 typedef boost::shared_ptr<auth> auth_ptr;
00170 
00171 
00172 }   // end namespace http
00173 }   // end namespace pion
00174 
00175 #endif