pion  5.0.6
auth.hpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_HTTP_AUTH_HEADER__
11 #define __PION_HTTP_AUTH_HEADER__
12 
13 #include <set>
14 #include <map>
15 #include <boost/noncopyable.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <pion/config.hpp>
18 #include <pion/error.hpp>
19 #include <pion/logger.hpp>
20 #include <pion/hash_map.hpp>
21 #include <pion/tcp/connection.hpp>
22 #include <pion/user.hpp>
23 #include <pion/http/request.hpp>
24 #include <boost/date_time/posix_time/posix_time.hpp> // order important, otherwise compiling error under win32
25 
26 
27 namespace pion { // begin namespace pion
28 namespace http { // begin namespace http
29 
30 
34 class PION_API auth :
35  private boost::noncopyable
36 {
37 public:
38 
40  auth(user_manager_ptr userManager)
41  : m_logger(PION_GET_LOGGER("pion.http.auth")),
42  m_user_manager(userManager)
43  {}
44 
46  virtual ~auth() {}
47 
60  virtual bool handle_request(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn) = 0;
61 
68  virtual void set_option(const std::string& name, const std::string& value) {
69  BOOST_THROW_EXCEPTION( error::bad_arg() << error::errinfo_arg_name(name) );
70  }
71 
77  void add_restrict(const std::string& resource);
78 
84  void add_permit(const std::string& resource);
85 
91  virtual bool add_user(std::string const &username, std::string const &password) {
92  return m_user_manager->add_user(username, password);
93  }
94 
100  virtual bool update_user(std::string const &username, std::string const &password) {
101  return m_user_manager->update_user(username, password);
102  }
103 
109  virtual bool remove_user(std::string const &username) {
110  return m_user_manager->remove_user(username);
111  };
112 
116  virtual user_ptr get_user(std::string const &username) {
117  return m_user_manager->get_user(username);
118  }
119 
120 
121 protected:
122 
124  typedef std::set<std::string> resource_set_type;
125 
127  typedef std::map<std::string,std::pair<boost::posix_time::ptime,user_ptr> > user_cache_type;
128 
129 
135  bool need_authentication(http::request_ptr const& http_request_ptr) const;
136 
145  bool find_resource(const resource_set_type& resource_set,
146  const std::string& resource) const;
147 
149  inline void set_logger(logger log_ptr) { m_logger = log_ptr; }
150 
151 
153  mutable logger m_logger;
154 
156  user_manager_ptr m_user_manager;
157 
159  resource_set_type m_restrict_list;
160 
162  resource_set_type m_white_list;
163 
165  mutable boost::mutex m_resource_mutex;
166 };
167 
169 typedef boost::shared_ptr<auth> auth_ptr;
170 
171 
172 } // end namespace http
173 } // end namespace pion
174 
175 #endif
virtual bool update_user(std::string const &username, std::string const &password)
Definition: auth.hpp:100
virtual bool remove_user(std::string const &username)
Definition: auth.hpp:109
virtual ~auth()
virtual destructor
Definition: auth.hpp:46
virtual bool add_user(std::string const &username, std::string const &password)
Definition: auth.hpp:91
user_manager_ptr m_user_manager
container used to manager user objects
Definition: auth.hpp:156
resource_set_type m_white_list
collection of resources that do NOT require authentication
Definition: auth.hpp:162
boost::mutex m_resource_mutex
mutex used to protect access to the resources
Definition: auth.hpp:165
std::map< std::string, std::pair< boost::posix_time::ptime, user_ptr > > user_cache_type
data type used to map authentication credentials to user objects
Definition: auth.hpp:127
virtual void set_option(const std::string &name, const std::string &value)
Definition: auth.hpp:68
void set_logger(logger log_ptr)
sets the logger to be used
Definition: auth.hpp:149
logger m_logger
primary logging interface used by this class
Definition: auth.hpp:153
virtual user_ptr get_user(std::string const &username)
Definition: auth.hpp:116
exception thrown for an invalid configuration argument or option
Definition: error.hpp:132
auth(user_manager_ptr userManager)
default constructor
Definition: auth.hpp:40
std::set< std::string > resource_set_type
data type for a set of resources to be authenticated
Definition: auth.hpp:124
resource_set_type m_restrict_list
collection of resources that require authentication
Definition: auth.hpp:159