00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __PION_HTTPCOOKIEAUTH_HEADER__
00011 #define __PION_HTTPCOOKIEAUTH_HEADER__
00012
00013 #include <map>
00014 #include <string>
00015 #include <boost/random.hpp>
00016 #include <pion/PionConfig.hpp>
00017 #include <pion/net/HTTPAuth.hpp>
00018 #include <pion/PionDateTime.hpp>
00019
00020
00021 namespace pion {
00022 namespace net {
00023
00028 class PION_NET_API HTTPCookieAuth :
00029 public HTTPAuth
00030 {
00031 public:
00032
00044 HTTPCookieAuth(PionUserManagerPtr userManager,
00045 const std::string& login="/login",
00046 const std::string& logout="/logout",
00047 const std::string& redirect="");
00048
00050 virtual ~HTTPCookieAuth() {}
00051
00069 virtual bool handleRequest(HTTPRequestPtr& request, TCPConnectionPtr& tcp_conn);
00070
00084 virtual void setOption(const std::string& name, const std::string& value);
00085
00086
00087 protected:
00088
00097 bool processLogin(HTTPRequestPtr& http_request, TCPConnectionPtr& tcp_conn);
00098
00105 void handleUnauthorized(HTTPRequestPtr& http_request, TCPConnectionPtr& tcp_conn);
00106
00113 void handleRedirection(HTTPRequestPtr& http_request, TCPConnectionPtr& tcp_conn,
00114 const std::string &redirection_url, const std::string &new_cookie="", bool delete_cookie=false);
00115
00122 void handleOk(HTTPRequestPtr& http_request, TCPConnectionPtr& tcp_conn,
00123 const std::string &new_cookie="", bool delete_cookie=false);
00124
00128 void expireCache(const PionDateTime &time_now);
00129
00130
00131 private:
00132
00134 typedef std::map<std::string, std::pair<PionDateTime, PionUserPtr> > PionUserCache;
00135
00137 static const unsigned int CACHE_EXPIRATION;
00138
00140 static const unsigned int RANDOM_COOKIE_BYTES;
00141
00143 static const std::string AUTH_COOKIE_NAME;
00144
00146 std::string m_login;
00147
00149 std::string m_logout;
00150
00152 std::string m_redirect;
00153
00155 boost::mt19937 m_random_gen;
00156
00158 boost::uniform_int<> m_random_range;
00159
00161 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_random_die;
00162
00164 PionDateTime m_cache_cleanup_time;
00165
00167 PionUserCache m_user_cache;
00168
00170 mutable boost::mutex m_cache_mutex;
00171 };
00172
00173
00174 }
00175 }
00176
00177 #endif