summaryrefslogtreecommitdiff
path: root/include/sessions.hpp
diff options
context:
space:
mode:
authorBorawski.Lukasz <lukasz.borawski@intel.com>2018-04-04 13:50:16 +0300
committerEd Tanous <ed.tanous@intel.com>2018-07-25 20:29:23 +0300
commit4b1b8683d31260b3032bb9f9fcde1eadaed4e1e5 (patch)
treee42eb626b813a24db19bb8ed58709faa10e475b1 /include/sessions.hpp
parenteb547730ed7f37a1d7c36d9785f6b0722cb8ef3d (diff)
downloadbmcweb-4b1b8683d31260b3032bb9f9fcde1eadaed4e1e5.tar.xz
Make SessionStore a proper singleton
- SessionStore class now has a proper singleton structure - session_storage_singleton.hpp is removed - from_json(..) function for SessionStore is changed to a specialized template - minor cosmetic fixes added - Move the template class usages of Crow App over to a non-template parameter Change-Id: Ic9effd5b7bac089a84c80a0caa97bd46d4984416 Signed-off-by: Borawski.Lukasz <lukasz.borawski@intel.com> Signed-off-by: Ed Tanous <ed.tanous@intel.com>
Diffstat (limited to 'include/sessions.hpp')
-rw-r--r--include/sessions.hpp38
1 files changed, 28 insertions, 10 deletions
diff --git a/include/sessions.hpp b/include/sessions.hpp
index fdd12000f4..b0fe1122b4 100644
--- a/include/sessions.hpp
+++ b/include/sessions.hpp
@@ -73,20 +73,10 @@ struct UserSession {
}
};
-void to_json(nlohmann::json& j, const std::shared_ptr<UserSession> p) {
- if (p->persistence != PersistenceType::SINGLE_REQUEST) {
- j = {{"unique_id", p->unique_id},
- {"session_token", p->session_token},
- {"username", p->username},
- {"csrf_token", p->csrf_token}};
- }
-}
-
class Middleware;
class SessionStore {
public:
- SessionStore() : timeout_in_minutes(60) {}
std::shared_ptr<UserSession> generate_user_session(
const boost::string_view username,
PersistenceType persistence = PersistenceType::TIMEOUT) {
@@ -184,7 +174,17 @@ class SessionStore {
// structure, which is private
friend Middleware;
+ static SessionStore& getInstance() {
+ static SessionStore sessionStore;
+ return sessionStore;
+ }
+
+ SessionStore(const SessionStore&) = delete;
+ SessionStore& operator=(const SessionStore&) = delete;
+
private:
+ SessionStore() : timeout_in_minutes(60) {}
+
void apply_session_timeouts() {
auto time_now = std::chrono::steady_clock::now();
if (time_now - last_timeout_update > std::chrono::minutes(1)) {
@@ -211,3 +211,21 @@ class SessionStore {
} // namespace PersistentData
} // namespace crow
+
+// to_json(...) definition for objects of UserSession type
+namespace nlohmann {
+template <>
+struct adl_serializer<std::shared_ptr<crow::PersistentData::UserSession>> {
+ static void to_json(
+ nlohmann::json& j,
+ const std::shared_ptr<crow::PersistentData::UserSession>& p) {
+ if (p->persistence !=
+ crow::PersistentData::PersistenceType::SINGLE_REQUEST) {
+ j = nlohmann::json{{"unique_id", p->unique_id},
+ {"session_token", p->session_token},
+ {"username", p->username},
+ {"csrf_token", p->csrf_token}};
+ }
+ }
+};
+} // namespace nlohmann