Menu

[r607]: / framework / trunk / session_cookies.cpp  Maximize  Restore  History

Download this file

91 lines (77 with data), 1.8 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "config.h"
#include "session_interface.h"
#include "session_cookies.h"
#include "hmac_encryptor.h"
#include "worker_thread.h"
#include "manager.h"
#include "session_backend_factory.h"
#ifdef EN_ENCR_SESSIONS
#include "aes_encryptor.h"
#endif
using namespace std;
namespace cppcms {
namespace {
struct builder {
shared_ptr<session_api> operator()(worker_thread &w)
{
return shared_ptr<session_api>(new session_cookies(w));
}
};
}
session_backend_factory session_cookies::factory()
{
return builder();
}
session_cookies::session_cookies(worker_thread &w,auto_ptr<encryptor> enc) :
worker(w),
encr(enc)
{
}
session_cookies::session_cookies(worker_thread &w) :
worker(w)
{
#ifdef EN_ENCR_SESSIONS
string default_type="aes";
#else
string default_type="hmac";
#endif
string type=w.app.config.sval("session.cookies_encryptor",default_type);
string key=w.app.config.sval("session.cookies_key");
if(type=="hmac") {
encr.reset(new hmac::cipher(key));
return;
}
#ifdef EN_ENCR_SESSIONS
if(type=="aes") {
encr.reset(new aes::cipher(key));
return;
}
#endif
throw cppcms_error("Unknown encryptor "+type);
}
void session_cookies::save(session_interface *session,string const &data,time_t timeout,bool not_used)
{
string cdata=encr->encrypt(data,timeout);
session->set_session_cookie(cdata);
}
bool session_cookies::load(session_interface *session,string &data,time_t &timeout_out)
{
string cdata=session->get_session_cookie();
if(cdata.empty()) return false;
time_t timeout;
string tmp;
if(!encr->decrypt(cdata,tmp,&timeout))
return false;
time_t now;
time(&now);
if(timeout < now)
return false;
data.swap(tmp);
timeout_out=timeout;
return true;
}
void session_cookies::clear(session_interface *session)
{
session->clear_session_cookie();
}
};
MongoDB Logo MongoDB