pion-net  4.0.9
HTTPCookieAuth.hpp
1 // ------------------------------------------------------------------
2 // pion-net: a C++ framework for building lightweight HTTP interfaces
3 // ------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
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_HTTPCOOKIEAUTH_HEADER__
11 #define __PION_HTTPCOOKIEAUTH_HEADER__
12 
13 #include <map>
14 #include <string>
15 #include <boost/random.hpp>
16 #include <pion/PionConfig.hpp>
17 #include <pion/net/HTTPAuth.hpp>
18 #include <pion/PionDateTime.hpp> // order important, otherwise compiling error under win32
19 
20 
21 namespace pion { // begin namespace pion
22 namespace net { // begin namespace net (Pion Network Library)
23 
28 class PION_NET_API HTTPCookieAuth :
29  public HTTPAuth
30 {
31 public:
32 
44  HTTPCookieAuth(PionUserManagerPtr userManager,
45  const std::string& login="/login",
46  const std::string& logout="/logout",
47  const std::string& redirect="");
48 
50  virtual ~HTTPCookieAuth() {}
51 
69  virtual bool handleRequest(HTTPRequestPtr& request, TCPConnectionPtr& tcp_conn);
70 
84  virtual void setOption(const std::string& name, const std::string& value);
85 
86 
87 protected:
88 
97  bool processLogin(HTTPRequestPtr& http_request, TCPConnectionPtr& tcp_conn);
98 
105  void handleUnauthorized(HTTPRequestPtr& http_request, TCPConnectionPtr& tcp_conn);
106 
113  void handleRedirection(HTTPRequestPtr& http_request, TCPConnectionPtr& tcp_conn,
114  const std::string &redirection_url, const std::string &new_cookie="", bool delete_cookie=false);
115 
122  void handleOk(HTTPRequestPtr& http_request, TCPConnectionPtr& tcp_conn,
123  const std::string &new_cookie="", bool delete_cookie=false);
124 
128  void expireCache(const PionDateTime &time_now);
129 
130 
131 private:
132 
134  typedef std::map<std::string, std::pair<PionDateTime, PionUserPtr> > PionUserCache;
135 
137  static const unsigned int CACHE_EXPIRATION;
138 
140  static const unsigned int RANDOM_COOKIE_BYTES;
141 
143  static const std::string AUTH_COOKIE_NAME;
144 
146  std::string m_login;
147 
149  std::string m_logout;
150 
152  std::string m_redirect;
153 
155  boost::mt19937 m_random_gen;
156 
158  boost::uniform_int<> m_random_range;
159 
161  boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_random_die;
162 
164  PionDateTime m_cache_cleanup_time;
165 
167  PionUserCache m_user_cache;
168 
170  mutable boost::mutex m_cache_mutex;
171 };
172 
173 
174 } // end namespace net
175 } // end namespace pion
176 
177 #endif