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 <memory>
21 #include <sstream>
22 #include <typeinfo>
23 
24 namespace cppcms {
25 namespace http {
26  class context;
27  class request;
28  class response;
29 }
30 
31 class session_api;
32 
36 class CPPCMS_API request_forgery_error : public cppcms_error {
37 public:
40  cppcms_error("Cross site request forgery detected")
41  {
42  }
43 };
44 
69 class CPPCMS_API session_interface : private booster::noncopyable {
70 public:
71 
76 
80  bool is_set(std::string const &key);
84  void erase(std::string const &key);
88  void clear();
89 
93  bool is_exposed(std::string const &key);
98  void expose(std::string const &key,bool val=true);
102  void hide(std::string const &key);
103 
108  std::string &operator[](std::string const &key);
112  void set(std::string const &key,std::string const &v);
113 
118  std::string get(std::string const &key);
119 
123  std::string get(std::string const &key,std::string const &default_value);
124 
134  template<typename T>
135  T get(std::string const &key)
136  {
137  std::istringstream ss(get(key));
138  ss.imbue(std::locale::classic());
139  T value;
140  ss>>value;
141  if(ss.fail() || !ss.eof())
142  throw std::bad_cast();
143  return value;
144  }
145 
151  template<typename T>
152  void set(std::string const &key,T const &value)
153  {
154  std::ostringstream ss;
155  ss.imbue(std::locale::classic());
156  ss<<value;
157  set(key,ss.str());
158  }
159 
165  template<typename Serializable>
166  void store_data(std::string const &key,Serializable const &object)
167  {
168  std::string buffer;
170  set(key,buffer);
171  }
172 
181  template<typename Serializable>
182  void fetch_data(std::string const &key,Serializable &object)
183  {
184  std::string buffer=get(key);
186  }
187 
191  enum {
193  renew,
194  browser
196  };
198 
202  int age();
206  void age(int t);
210  void default_age();
211 
215  int expiration();
219  void expiration(int h);
223  void default_expiration();
224 
236  void on_server(bool srv);
237 
241  bool on_server();
242 
243 
249  void set_session_cookie(std::string const &data);
255  void clear_session_cookie();
256 
262  std::string get_session_cookie();
263 
268  bool load();
269 
275  void save();
276 
284  bool is_blocking();
285 
290  void reset_session();
291 
292 
300  bool validate_csrf_token(std::string const &str);
309  void validate_request_origin();
310 
325  void request_origin_validation_is_required(bool required);
326 
331  std::string get_csrf_token();
336  std::string get_csrf_token_cookie_name();
337 
338 private:
339  friend class http::response;
340  friend class http::request;
341 
342 
343 
344  struct entry;
345 
346  typedef std::map<std::string,entry> data_type;
347  data_type data_,data_copy_;
348  http::context *context_;
349 
350  // Cached defaults
351  int timeout_val_def_;
352  int how_def_;
353 
354  // User Values
355  int timeout_val_;
356  int how_;
357 
358  // Information from session data
359  time_t timeout_in_;
360 
361  uint32_t new_session_ : 1;
362  uint32_t saved_ : 1;
363  uint32_t on_server_ : 1;
364  uint32_t loaded_ : 1;
365  uint32_t reset_ : 1;
366  uint32_t csrf_checked_ : 1;
367  uint32_t csrf_do_validation_ : 1;
368  uint32_t csrf_validation_ : 1;
369  uint32_t reserved_ : 24;
370 
371  std::string temp_cookie_;
372 
373  // storage itself
374 
376  struct _data;
377  booster::hold_ptr<_data> d; // for future use
378 
379  int cookie_age();
380  time_t session_age();
381 
382  void check();
383  void update_exposed(bool);
384 
385 
386  void set_session_cookie(int64_t age,std::string const &data,std::string const &key=std::string());
387 
388  void save_data(std::map<std::string,entry> const &data,std::string &s);
389  void load_data(std::map<std::string,entry> &data,std::string const &s);
390  std::string generate_csrf_token();
391 };
392 
393 } // cppcms
394 
395 
396 #endif
Once the session is created it will expire in age() second from the moment it created.
Definition: session_interface.h:192
void set(std::string const &key, T const &value)
Definition: session_interface.h:152
Exception thrown by CppCMS framework.
Definition: cppcms_error.h:22
This exception is thrown when CSRF attempt is suspected:
Definition: session_interface.h:36
This class provides an access to an application for session management.
Definition: session_interface.h:69
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:45
Definition: log.h:25
this class represents all HTTP/CGI response related API, generation of output content and HTTP header...
Definition: http_response.h:31
request_forgery_error()
Create an exception object.
Definition: session_interface.h:39
void store_data(std::string const &key, Serializable const &object)
Definition: session_interface.h:166
void fetch_data(std::string const &key, Serializable &object)
Definition: session_interface.h:182
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:34