CppCMS
session_interface.h
1 //
3 // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
4 //
5 // See accompanying file COPYING.TXT file for licensing details.
6 //
8 #ifndef CPPCMS_SESSION_INTERFACE_H
9 #define CPPCMS_SESSION_INTERFACE_H
10 
11 #include <cppcms/defs.h>
12 #include <booster/noncopyable.h>
13 #include <booster/hold_ptr.h>
14 #include <booster/shared_ptr.h>
15 #include <cppcms/cstdint.h>
16 #include <cppcms/cppcms_error.h>
17 #include <cppcms/serialization_classes.h>
18 #include <string>
19 #include <map>
20 #include <set>
21 #include <booster/auto_ptr_inc.h>
22 #include <sstream>
23 #include <typeinfo>
24 
25 namespace cppcms {
26 namespace impl {
27  struct cached_settings;
28 }
29 namespace http {
30  class context;
31  class request;
32  class response;
33  class cookie;
34 }
35 
36 class session_api;
37 class session_pool;
38 
42 class CPPCMS_API request_forgery_error : public cppcms_error {
43 public:
46  cppcms_error("Cross site request forgery detected")
47  {
48  }
49 };
50 
51 
67 public:
72  virtual void set_cookie(http::cookie const &updated_cookie) = 0;
76  virtual std::string get_session_cookie(std::string const &name) = 0;
80  virtual std::set<std::string> get_cookie_names() = 0;
81 };
82 
107 class CPPCMS_API session_interface : private booster::noncopyable {
108 public:
109 
116 
122 
130  bool is_set(std::string const &key);
134  void erase(std::string const &key);
138  void clear();
139 
143  bool is_exposed(std::string const &key);
148  void expose(std::string const &key,bool val=true);
152  void hide(std::string const &key);
153 
158  std::string &operator[](std::string const &key);
162  void set(std::string const &key,std::string const &v);
163 
168  std::string get(std::string const &key);
169 
173  std::string get(std::string const &key,std::string const &default_value);
174 
184  template<typename T>
185  T get(std::string const &key)
186  {
187  std::istringstream ss(get(key));
188  ss.imbue(std::locale::classic());
189  T value;
190  ss>>value;
191  if(ss.fail() || !ss.eof())
192  throw booster::bad_cast();
193  return value;
194  }
195 
201  template<typename T>
202  void set(std::string const &key,T const &value)
203  {
204  std::ostringstream ss;
205  ss.imbue(std::locale::classic());
206  ss<<value;
207  set(key,ss.str());
208  }
209 
215  template<typename Serializable>
216  void store_data(std::string const &key,Serializable const &object)
217  {
218  std::string buffer;
220  set(key,buffer);
221  }
222 
231  template<typename Serializable>
232  void fetch_data(std::string const &key,Serializable &object)
233  {
234  std::string buffer=get(key);
236  }
237 
241  enum {
243  renew,
244  browser
246  };
248 
252  int age();
256  void age(int t);
260  void default_age();
261 
265  int expiration();
269  void expiration(int h);
273  void default_expiration();
274 
286  void on_server(bool srv);
287 
291  bool on_server();
292 
293 
299  void set_session_cookie(std::string const &data);
305  void clear_session_cookie();
306 
312  std::string get_session_cookie();
313 
318  bool load();
319 
327  bool set_cookie_adapter_and_reload(session_interface_cookie_adapter &adapter);
328 
334  void save();
335 
343  bool is_blocking();
344 
349  void reset_session();
350 
351 
359  bool validate_csrf_token(std::string const &str);
368  void validate_request_origin();
369 
384  void request_origin_validation_is_required(bool required);
385 
390  std::string get_csrf_token();
395  std::string get_csrf_token_cookie_name();
396 
401  std::string session_cookie_name();
402 
407  std::set<std::string> key_set();
408 private:
409  friend class http::response;
410  friend class http::request;
411 
412  void init();
413 
414  impl::cached_settings const &cached_settings();
415 
416  struct entry;
417 
418  typedef std::map<std::string,entry> data_type;
419  data_type data_,data_copy_;
420  http::context *context_;
421 
422  // Cached defaults
423  int timeout_val_def_;
424  int how_def_;
425 
426  // User Values
427  int timeout_val_;
428  int how_;
429 
430  // Information from session data
431  time_t timeout_in_;
432 
433  uint32_t new_session_ : 1;
434  uint32_t saved_ : 1;
435  uint32_t on_server_ : 1;
436  uint32_t loaded_ : 1;
437  uint32_t reset_ : 1;
438  uint32_t csrf_checked_ : 1;
439  uint32_t csrf_do_validation_ : 1;
440  uint32_t csrf_validation_ : 1;
441  uint32_t reserved_ : 24;
442 
443  std::string temp_cookie_;
444 
445  // storage itself
446 
448  struct _data;
449  booster::hold_ptr<_data> d; // for future use
450 
451  int cookie_age();
452  time_t session_age();
453 
454  void check();
455  void update_exposed(bool);
456 
457 
458  void set_session_cookie(int64_t age,std::string const &data,std::string const &key=std::string());
459 
460  void save_data(std::map<std::string,entry> const &data,std::string &s);
461  void load_data(std::map<std::string,entry> &data,std::string const &s);
462  std::string generate_csrf_token();
463 };
464 
465 } // cppcms
466 
467 
468 #endif
Once the session is created it will expire in age() second from the moment it created.
Definition: session_interface.h:242
Exception thrown by CppCMS framework.
Definition: cppcms_error.h:22
This exception is thrown when CSRF attempt is suspected:
Definition: session_interface.h:42
This class provides an access to an application for session management.
Definition: session_interface.h:107
This is the namespace where all CppCMS functionality is placed.
Definition: application.h:19
static void load(std::string const &serialized_object, Object &real_object)
context is a central class that holds all specific connection related information. It encapsulates CGI request and response, cache, session and locale information
Definition: http_context.h:47
Definition: log.h:25
This class provides an access to session management backends an allow customization.
Definition: session_pool.h:35
this class represents all HTTP/CGI response related API, generation of output content and HTTP header...
Definition: http_response.h:36
Same as std::bad_cast but records stack trace.
Definition: backtrace.h:151
request_forgery_error()
Create an exception object.
Definition: session_interface.h:45
void store_data(std::string const &key, Serializable const &object)
Definition: session_interface.h:216
void fetch_data(std::string const &key, Serializable &object)
Definition: session_interface.h:232
Class that represents single HTTP Cookie Generally used in context of http::request and http::respons...
Definition: http_cookie.h:27
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15
static void save(Object const &real_object, std::string &serialized_object)
This class represents all information related to the HTTP/CGI request.
Definition: http_request.h:37