Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <nlohmann/json.hpp> |
| 4 | #include <pam_authenticate.hpp> |
| 5 | #include <webassets.hpp> |
| 6 | #include <random> |
| 7 | #include <crow/app.h> |
| 8 | #include <crow/http_request.h> |
| 9 | #include <crow/http_response.h> |
| 10 | #include <boost/container/flat_map.hpp> |
| 11 | #include <boost/uuid/uuid.hpp> |
| 12 | #include <boost/uuid/uuid_generators.hpp> |
| 13 | #include <boost/uuid/uuid_io.hpp> |
| 14 | |
| 15 | namespace crow { |
| 16 | |
| 17 | namespace PersistentData { |
| 18 | |
| 19 | enum class PersistenceType { |
| 20 | TIMEOUT, // User session times out after a predetermined amount of time |
| 21 | SINGLE_REQUEST // User times out once this request is completed. |
| 22 | }; |
| 23 | |
| 24 | struct UserSession { |
| 25 | std::string unique_id; |
| 26 | std::string session_token; |
| 27 | std::string username; |
| 28 | std::string csrf_token; |
| 29 | std::chrono::time_point<std::chrono::steady_clock> last_updated; |
| 30 | PersistenceType persistence; |
Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame^] | 31 | |
| 32 | /** |
| 33 | * @brief Fills object with data from UserSession's JSON representation |
| 34 | * |
| 35 | * This replaces nlohmann's from_json to ensure no-throw approach |
| 36 | * |
| 37 | * @param[in] j JSON object from which data should be loaded |
| 38 | * |
| 39 | * @return true if data has been loaded properly, false otherwise |
| 40 | */ |
| 41 | bool fromJson(const nlohmann::json& j) { |
| 42 | auto jUid = j.find("unique_id"); |
| 43 | auto jToken = j.find("session_token"); |
| 44 | auto jUsername = j.find("username"); |
| 45 | auto jCsrf = j.find("csrf_token"); |
| 46 | |
| 47 | // Verify existence |
| 48 | if (jUid == j.end() || jToken == j.end() || jUsername == j.end() || |
| 49 | jCsrf == j.end()) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | // Verify types |
| 54 | if (!jUid->is_string() || !jToken->is_string() || !jUsername->is_string() || |
| 55 | !jCsrf->is_string()) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | unique_id = jUid->get<std::string>(); |
| 60 | session_token = jToken->get<std::string>(); |
| 61 | username = jUsername->get<std::string>(); |
| 62 | csrf_token = jCsrf->get<std::string>(); |
| 63 | |
| 64 | // For now, sessions that were persisted through a reboot get their timer |
| 65 | // reset. This could probably be overcome with a better understanding of |
| 66 | // wall clock time and steady timer time, possibly persisting values with |
| 67 | // wall clock time instead of steady timer, but the tradeoffs of all the |
| 68 | // corner cases involved are non-trivial, so this is done temporarily |
| 69 | last_updated = std::chrono::steady_clock::now(); |
| 70 | persistence = PersistenceType::TIMEOUT; |
| 71 | |
| 72 | return true; |
| 73 | } |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | void to_json(nlohmann::json& j, const UserSession& p) { |
| 77 | if (p.persistence != PersistenceType::SINGLE_REQUEST) { |
| 78 | j = nlohmann::json{{"unique_id", p.unique_id}, |
| 79 | {"session_token", p.session_token}, |
| 80 | {"username", p.username}, |
| 81 | {"csrf_token", p.csrf_token}}; |
| 82 | } |
| 83 | } |
| 84 | |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 85 | class Middleware; |
| 86 | |
| 87 | class SessionStore { |
| 88 | public: |
| 89 | const UserSession& generate_user_session( |
| 90 | const std::string& username, |
| 91 | PersistenceType persistence = PersistenceType::TIMEOUT) { |
| 92 | // TODO(ed) find a secure way to not generate session identifiers if |
| 93 | // persistence is set to SINGLE_REQUEST |
| 94 | static constexpr std::array<char, 62> alphanum = { |
| 95 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', |
| 96 | 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 97 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', |
| 98 | 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', |
| 99 | 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; |
| 100 | |
| 101 | // entropy: 30 characters, 62 possibilities. log2(62^30) = 178 bits of |
| 102 | // entropy. OWASP recommends at least 60 |
| 103 | // https://www.owasp.org/index.php/Session_Management_Cheat_Sheet#Session_ID_Entropy |
| 104 | std::string session_token; |
| 105 | session_token.resize(20, '0'); |
| 106 | std::uniform_int_distribution<int> dist(0, alphanum.size() - 1); |
| 107 | for (int i = 0; i < session_token.size(); ++i) { |
| 108 | session_token[i] = alphanum[dist(rd)]; |
| 109 | } |
| 110 | // Only need csrf tokens for cookie based auth, token doesn't matter |
| 111 | std::string csrf_token; |
| 112 | csrf_token.resize(20, '0'); |
| 113 | for (int i = 0; i < csrf_token.size(); ++i) { |
| 114 | csrf_token[i] = alphanum[dist(rd)]; |
| 115 | } |
| 116 | |
| 117 | std::string unique_id; |
| 118 | unique_id.resize(10, '0'); |
| 119 | for (int i = 0; i < unique_id.size(); ++i) { |
| 120 | unique_id[i] = alphanum[dist(rd)]; |
| 121 | } |
| 122 | |
| 123 | const auto session_it = auth_tokens.emplace( |
| 124 | session_token, |
| 125 | std::move(UserSession{unique_id, session_token, username, csrf_token, |
| 126 | std::chrono::steady_clock::now(), persistence})); |
| 127 | const UserSession& user = (session_it).first->second; |
| 128 | // Only need to write to disk if session isn't about to be destroyed. |
| 129 | need_write_ = persistence == PersistenceType::TIMEOUT; |
| 130 | return user; |
| 131 | } |
| 132 | |
| 133 | const UserSession* login_session_by_token(const std::string& token) { |
| 134 | apply_session_timeouts(); |
| 135 | auto session_it = auth_tokens.find(token); |
| 136 | if (session_it == auth_tokens.end()) { |
| 137 | return nullptr; |
| 138 | } |
| 139 | UserSession& foo = session_it->second; |
| 140 | foo.last_updated = std::chrono::steady_clock::now(); |
| 141 | return &foo; |
| 142 | } |
| 143 | |
| 144 | const UserSession* get_session_by_uid(const std::string& uid) { |
| 145 | apply_session_timeouts(); |
| 146 | // TODO(Ed) this is inefficient |
| 147 | auto session_it = auth_tokens.begin(); |
| 148 | while (session_it != auth_tokens.end()) { |
| 149 | if (session_it->second.unique_id == uid) { |
| 150 | return &session_it->second; |
| 151 | } |
| 152 | session_it++; |
| 153 | } |
| 154 | return nullptr; |
| 155 | } |
| 156 | |
| 157 | void remove_session(const UserSession* session) { |
| 158 | auth_tokens.erase(session->session_token); |
| 159 | need_write_ = true; |
| 160 | } |
| 161 | |
| 162 | std::vector<const std::string*> get_unique_ids( |
| 163 | bool getAll = true, |
| 164 | const PersistenceType& type = PersistenceType::SINGLE_REQUEST) { |
| 165 | apply_session_timeouts(); |
| 166 | |
| 167 | std::vector<const std::string*> ret; |
| 168 | ret.reserve(auth_tokens.size()); |
| 169 | for (auto& session : auth_tokens) { |
| 170 | if (getAll || type == session.second.persistence) { |
| 171 | ret.push_back(&session.second.unique_id); |
| 172 | } |
| 173 | } |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | bool needs_write() { return need_write_; } |
| 178 | |
| 179 | // Persistent data middleware needs to be able to serialize our auth_tokens |
| 180 | // structure, which is private |
| 181 | friend Middleware; |
| 182 | |
| 183 | private: |
| 184 | void apply_session_timeouts() { |
| 185 | std::chrono::minutes timeout(60); |
| 186 | auto time_now = std::chrono::steady_clock::now(); |
| 187 | if (time_now - last_timeout_update > std::chrono::minutes(1)) { |
| 188 | last_timeout_update = time_now; |
| 189 | auto auth_tokens_it = auth_tokens.begin(); |
| 190 | while (auth_tokens_it != auth_tokens.end()) { |
| 191 | if (time_now - auth_tokens_it->second.last_updated >= timeout) { |
| 192 | auth_tokens_it = auth_tokens.erase(auth_tokens_it); |
| 193 | need_write_ = true; |
| 194 | } else { |
| 195 | auth_tokens_it++; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | std::chrono::time_point<std::chrono::steady_clock> last_timeout_update; |
| 201 | boost::container::flat_map<std::string, UserSession> auth_tokens; |
| 202 | std::random_device rd; |
| 203 | bool need_write_{false}; |
| 204 | }; |
| 205 | |
| 206 | } // namespaec PersistentData |
| 207 | } // namespace crow |