pion  5.0.6
cookie_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_COOKIE_AUTH_HEADER__
11 #define __PION_HTTP_COOKIE_AUTH_HEADER__
12 
13 #include <map>
14 #include <string>
15 #include <boost/random.hpp>
16 #include <pion/config.hpp>
17 #include <pion/http/auth.hpp>
18 
19 
20 namespace pion { // begin namespace pion
21 namespace http { // begin namespace http
22 
23 
28 class PION_API cookie_auth :
29  public http::auth
30 {
31 public:
32 
44  cookie_auth(user_manager_ptr userManager,
45  const std::string& login="/login",
46  const std::string& logout="/logout",
47  const std::string& redirect="");
48 
50  virtual ~cookie_auth() {}
51 
69  virtual bool handle_request(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn);
70 
84  virtual void set_option(const std::string& name, const std::string& value);
85 
86 
87 protected:
88 
97  bool process_login(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn);
98 
105  void handle_unauthorized(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn);
106 
113  void handle_redirection(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn,
114  const std::string &redirection_url, const std::string &new_cookie="", bool delete_cookie=false);
115 
122  void handle_ok(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn,
123  const std::string &new_cookie="", bool delete_cookie=false);
124 
128  void expire_cache(const boost::posix_time::ptime &time_now);
129 
130 
131 private:
132 
134  static const unsigned int CACHE_EXPIRATION;
135 
137  static const unsigned int RANDOM_COOKIE_BYTES;
138 
140  static const std::string AUTH_COOKIE_NAME;
141 
143  std::string m_login;
144 
146  std::string m_logout;
147 
149  std::string m_redirect;
150 
152  boost::mt19937 m_random_gen;
153 
155  boost::uniform_int<> m_random_range;
156 
158  boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_random_die;
159 
161  boost::posix_time::ptime m_cache_cleanup_time;
162 
164  user_cache_type m_user_cache;
165 
167  mutable boost::mutex m_cache_mutex;
168 };
169 
170 
171 } // end namespace http
172 } // end namespace pion
173 
174 #endif
virtual ~cookie_auth()
virtual destructor
Definition: cookie_auth.hpp:50