mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
session.h
1 /*
2 Copyright (c) 2016 Alexei Novakov
3 https://github.com/novalexei
4 
5 Distributed under the Boost Software License, Version 1.0.
6 http://boost.org/LICENSE_1_0.txt
7 */
8 #ifndef MOD_SERVLET_SESSION_H
9 #define MOD_SERVLET_SESSION_H
10 
11 #include <string>
12 #include <chrono>
13 #include <experimental/string_view>
14 
15 #include <servlet/lib/any_map.h>
16 
17 namespace servlet
18 {
19 
20 std::string generate_session_id();
21 
22 using std::experimental::string_view;
23 using std::experimental::any;
24 
25 class principal;
26 
56 class http_session : public tree_any_map
57 {
58 public:
62  typedef std::chrono::time_point<std::chrono::system_clock, typename std::chrono::system_clock::duration> time_type;
63 
71  const std::string& get_id() const { return _session_id; }
72 
79  time_type get_creation_time() const { return _created; }
80 
93 
103  bool is_new() const { return _new; }
104 
113  void set_principal(principal* p) { _principal.reset(p); }
114 
124  void set_principal(std::shared_ptr<principal> p) { _principal = p; }
125 
135  void set_principal(std::unique_ptr<principal>&& p) { _principal = std::move(p); }
136 
146  std::shared_ptr<principal> get_principal() const { return _principal; }
147 
148 protected:
154  http_session(const string_view &client_ip, const string_view &user_agent);
155 
165  virtual void validate(const string_view &client_ip, const string_view &user_agent) = 0;
166 
173  virtual void reset_session_id();
174 
178  std::string _client_ip;
182  std::string _user_agent;
187  bool _new = true;
193 
194 private:
195  std::string _session_id;
196  time_type _created;
197  std::shared_ptr<principal> _principal;
198 };
199 
206 {
207 public:
208  virtual ~principal() noexcept = default;
209 
215  virtual string_view get_name() noexcept = 0;
216 };
217 
223 {
224 public:
229  named_principal(const std::string& name) : _name{name} {}
234  named_principal(std::string&& name) : _name{std::move(name)} {}
235 
236  ~named_principal() noexcept override = default;
237 
238  string_view get_name() noexcept override { return _name; }
239 private:
240  std::string _name;
241 };
242 
243 } // end of servlet namespace
244 
245 #endif // MOD_SERVLET_SESSION_H
std::string _client_ip
Client IP string.
Definition: session.h:178
void set_principal(principal *p)
Set the authenticated principal that is associated with this session.
Definition: session.h:113
virtual string_view get_name() noexcept=0
Returns the name of this principal.
named_principal(const std::string &name)
Constructs principal with a given name.
Definition: session.h:229
string_view get_name() noexceptoverride
Returns the name of this principal.
Definition: session.h:238
const std::string & get_id() const
Returns a string containing the unique identifier assigned to this session.
Definition: session.h:71
virtual void reset_session_id()
Resets session_id for this session.
bool _new
New flag for this session.
Definition: session.h:187
std::string _user_agent
User agent string.
Definition: session.h:182
std::chrono::time_point< std::chrono::system_clock, typename std::chrono::system_clock::duration > time_type
Type to be returned with get_creation_time and get_last_accessed_time.
Definition: session.h:62
Covenience class on top of associative container to facilitate work with value type std::any...
Definition: any_map.h:84
Simple inplementation of servlet::principal with a name only.
Definition: session.h:222
This interface represents the abstract notion of a principal, which can be used to represent any enti...
Definition: session.h:205
time_type get_last_accessed_time() const
Returns the last time the client sent a request associated with this session, as recorded by std::chr...
Definition: session.h:92
bool is_new() const
Returns true if the client does not yet know about the session or if the client chooses not to join t...
Definition: session.h:103
named_principal(std::string &&name)
Constructs principal with a given name.
Definition: session.h:234
time_type get_creation_time() const
Returns the time when this session was created as recorded by std::chrono::system_clock.
Definition: session.h:79
void set_principal(std::shared_ptr< principal > p)
Set the authenticated principal that is associated with this session.
Definition: session.h:124
Provides a way to identify a user across more than one page request or visit to a Web site and to sto...
Definition: session.h:56
std::shared_ptr< principal > get_principal() const
Return the authenticated principal that is associated with this session.
Definition: session.h:146
time_type _last_accessed
Last accessed timestamp.
Definition: session.h:192
virtual void validate(const string_view &client_ip, const string_view &user_agent)=0
Validates client IP and user agent against this session ones.
void set_principal(std::unique_ptr< principal > &&p)
Set the authenticated principal that is associated with this session.
Definition: session.h:135
Containes the implementation of any_map class and related type definitions.
http_session(const string_view &client_ip, const string_view &user_agent)
Protected constructor.