pion-net  4.0.9
net/include/pion/net/HTTPAuth.hpp
00001 // ------------------------------------------------------------------
00002 // pion-net: a C++ framework for building lightweight HTTP interfaces
00003 // ------------------------------------------------------------------
00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
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_HTTPAUTH_HEADER__
00011 #define __PION_HTTPAUTH_HEADER__
00012 
00013 #include <set>
00014 #include <boost/noncopyable.hpp>
00015 #include <boost/shared_ptr.hpp>
00016 #include <pion/PionConfig.hpp>
00017 #include <pion/PionLogger.hpp>
00018 #include <pion/PionException.hpp>
00019 #include <pion/net/PionUser.hpp>
00020 #include <pion/net/TCPConnection.hpp>
00021 #include <pion/net/HTTPRequest.hpp>
00022 
00023 
00024 namespace pion {    // begin namespace pion
00025 namespace net {     // begin namespace net (Pion Network Library)
00026 
00030 class PION_NET_API HTTPAuth :
00031     private boost::noncopyable
00032 {
00033 public:
00034     
00036     class UnknownOptionException : public PionException {
00037     public:
00038         UnknownOptionException(const std::string& name)
00039             : PionException("Option not recognized by authentication service: ", name) {}
00040     };
00041     
00042     
00044     HTTPAuth(PionUserManagerPtr userManager) 
00045         : m_logger(PION_GET_LOGGER("pion.net.HTTPAuth")),
00046         m_user_manager(userManager)
00047     {}
00048     
00050     virtual ~HTTPAuth() {}
00051     
00064     virtual bool handleRequest(HTTPRequestPtr& request, TCPConnectionPtr& tcp_conn) = 0;
00065     
00072     virtual void setOption(const std::string& name, const std::string& value) {
00073         throw UnknownOptionException(name);
00074     }
00075     
00081     void addRestrict(const std::string& resource);
00082     
00088     void addPermit(const std::string& resource);
00089 
00095     virtual bool addUser(std::string const &username, std::string const &password) {
00096         return m_user_manager->addUser(username, password);
00097     }
00098     
00104     virtual bool updateUser(std::string const &username, std::string const &password) {
00105         return m_user_manager->updateUser(username, password);
00106     }
00107     
00113     virtual bool removeUser(std::string const &username) {
00114         return m_user_manager->removeUser(username);
00115     };
00116     
00120     virtual PionUserPtr getUser(std::string const &username) {
00121         return m_user_manager->getUser(username);
00122     }
00123 
00124     
00125 protected:
00126 
00128     typedef std::set<std::string>   AuthResourceSet;
00129 
00130     
00136     bool needAuthentication(HTTPRequestPtr const& http_request) const;
00137     
00146     bool findResource(const AuthResourceSet& resource_set,
00147                       const std::string& resource) const;
00148 
00150     inline void setLogger(PionLogger log_ptr) { m_logger = log_ptr; }
00151     
00152 
00154     mutable PionLogger              m_logger;
00155     
00157     PionUserManagerPtr          m_user_manager;
00158     
00160     AuthResourceSet             m_restrict_list;
00161 
00163     AuthResourceSet             m_white_list;
00164 
00166     mutable boost::mutex        m_resource_mutex;
00167 };
00168 
00170 typedef boost::shared_ptr<HTTPAuth> HTTPAuthPtr;
00171 
00172 
00173 }   // end namespace net
00174 }   // end namespace pion
00175 
00176 #endif