| Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 1 | #pragma once | 
|  | 2 |  | 
| Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 3 | #include "logging.hpp" | 
| Ed Tanous | 2c6ffdb | 2023-06-28 11:28:38 -0700 | [diff] [blame] | 4 | #include "ossl_random.hpp" | 
| Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 5 | #include "utility.hpp" | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 6 | #include "utils/ip_utils.hpp" | 
| Ed Tanous | fc76b8a | 2020-09-28 17:21:52 -0700 | [diff] [blame] | 7 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 8 | #include <nlohmann/json.hpp> | 
| Ratan Gupta | 12c04ef | 2019-04-03 10:08:11 +0530 | [diff] [blame] | 9 |  | 
| Xie Ning | 9fa06f1 | 2022-06-29 18:27:47 +0800 | [diff] [blame] | 10 | #include <algorithm> | 
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 11 | #include <csignal> | 
| Ed Tanous | bb759e3 | 2022-08-02 17:07:54 -0700 | [diff] [blame] | 12 | #include <optional> | 
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 13 | #include <random> | 
| Ed Tanous | b7f3a82 | 2024-06-05 08:45:25 -0700 | [diff] [blame] | 14 | #include <string> | 
| Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 15 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | namespace persistent_data | 
|  | 17 | { | 
| Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 18 |  | 
| Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 19 | // entropy: 20 characters, 62 possibilities.  log2(62^20) = 119 bits of | 
|  | 20 | // entropy.  OWASP recommends at least 64 | 
|  | 21 | // https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#session-id-entropy | 
|  | 22 | constexpr std::size_t sessionTokenSize = 20; | 
|  | 23 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | enum class PersistenceType | 
|  | 25 | { | 
|  | 26 | TIMEOUT, // User session times out after a predetermined amount of time | 
|  | 27 | SINGLE_REQUEST // User times out once this request is completed. | 
| Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 28 | }; | 
|  | 29 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | struct UserSession | 
|  | 31 | { | 
|  | 32 | std::string uniqueId; | 
|  | 33 | std::string sessionToken; | 
|  | 34 | std::string username; | 
|  | 35 | std::string csrfToken; | 
| Ed Tanous | bb759e3 | 2022-08-02 17:07:54 -0700 | [diff] [blame] | 36 | std::optional<std::string> clientId; | 
| Sunitha Harish | 92f6822 | 2020-05-28 05:09:09 -0500 | [diff] [blame] | 37 | std::string clientIp; | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 38 | std::chrono::time_point<std::chrono::steady_clock> lastUpdated; | 
| Ed Tanous | d3a9e08 | 2022-01-07 09:30:41 -0800 | [diff] [blame] | 39 | PersistenceType persistence{PersistenceType::TIMEOUT}; | 
| Ed Tanous | 7e9c08e | 2023-06-16 11:29:37 -0700 | [diff] [blame] | 40 | bool cookieAuth = false; | 
| Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 41 | bool isConfigureSelfOnly = false; | 
| Ed Tanous | 47f2934 | 2024-03-19 12:18:06 -0700 | [diff] [blame] | 42 | std::string userRole; | 
|  | 43 | std::vector<std::string> userGroups; | 
| Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 44 |  | 
|  | 45 | // There are two sources of truth for isConfigureSelfOnly: | 
|  | 46 | //  1. When pamAuthenticateUser() returns PAM_NEW_AUTHTOK_REQD. | 
|  | 47 | //  2. D-Bus User.Manager.GetUserInfo property UserPasswordExpired. | 
|  | 48 | // These should be in sync, but the underlying condition can change at any | 
|  | 49 | // time.  For example, a password can expire or be changed outside of | 
|  | 50 | // bmcweb.  The value stored here is updated at the start of each | 
|  | 51 | // operation and used as the truth within bmcweb. | 
| Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 52 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 53 | /** | 
|  | 54 | * @brief Fills object with data from UserSession's JSON representation | 
|  | 55 | * | 
|  | 56 | * This replaces nlohmann's from_json to ensure no-throw approach | 
|  | 57 | * | 
|  | 58 | * @param[in] j   JSON object from which data should be loaded | 
|  | 59 | * | 
|  | 60 | * @return a shared pointer if data has been loaded properly, nullptr | 
|  | 61 | * otherwise | 
|  | 62 | */ | 
|  | 63 | static std::shared_ptr<UserSession> fromJson(const nlohmann::json& j) | 
|  | 64 | { | 
|  | 65 | std::shared_ptr<UserSession> userSession = | 
|  | 66 | std::make_shared<UserSession>(); | 
|  | 67 | for (const auto& element : j.items()) | 
|  | 68 | { | 
|  | 69 | const std::string* thisValue = | 
|  | 70 | element.value().get_ptr<const std::string*>(); | 
|  | 71 | if (thisValue == nullptr) | 
|  | 72 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 73 | BMCWEB_LOG_ERROR( | 
|  | 74 | "Error reading persistent store.  Property {} was not of type string", | 
|  | 75 | element.key()); | 
| Ed Tanous | dc511aa | 2020-10-21 12:33:42 -0700 | [diff] [blame] | 76 | continue; | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 77 | } | 
|  | 78 | if (element.key() == "unique_id") | 
|  | 79 | { | 
|  | 80 | userSession->uniqueId = *thisValue; | 
|  | 81 | } | 
|  | 82 | else if (element.key() == "session_token") | 
|  | 83 | { | 
|  | 84 | userSession->sessionToken = *thisValue; | 
|  | 85 | } | 
|  | 86 | else if (element.key() == "csrf_token") | 
|  | 87 | { | 
|  | 88 | userSession->csrfToken = *thisValue; | 
|  | 89 | } | 
|  | 90 | else if (element.key() == "username") | 
|  | 91 | { | 
|  | 92 | userSession->username = *thisValue; | 
|  | 93 | } | 
| Sunitha Harish | 08bdcc7 | 2020-05-12 05:17:57 -0500 | [diff] [blame] | 94 | else if (element.key() == "client_id") | 
|  | 95 | { | 
|  | 96 | userSession->clientId = *thisValue; | 
|  | 97 | } | 
| Sunitha Harish | 92f6822 | 2020-05-28 05:09:09 -0500 | [diff] [blame] | 98 | else if (element.key() == "client_ip") | 
|  | 99 | { | 
|  | 100 | userSession->clientIp = *thisValue; | 
|  | 101 | } | 
|  | 102 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | else | 
|  | 104 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 105 | BMCWEB_LOG_ERROR( | 
|  | 106 | "Got unexpected property reading persistent file: {}", | 
|  | 107 | element.key()); | 
| Ed Tanous | dc511aa | 2020-10-21 12:33:42 -0700 | [diff] [blame] | 108 | continue; | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 109 | } | 
|  | 110 | } | 
| Ed Tanous | dc511aa | 2020-10-21 12:33:42 -0700 | [diff] [blame] | 111 | // If any of these fields are missing, we can't restore the session, as | 
|  | 112 | // we don't have enough information.  These 4 fields have been present | 
|  | 113 | // in every version of this file in bmcwebs history, so any file, even | 
|  | 114 | // on upgrade, should have these present | 
|  | 115 | if (userSession->uniqueId.empty() || userSession->username.empty() || | 
|  | 116 | userSession->sessionToken.empty() || userSession->csrfToken.empty()) | 
|  | 117 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 118 | BMCWEB_LOG_DEBUG("Session missing required security " | 
|  | 119 | "information, refusing to restore"); | 
| Ed Tanous | dc511aa | 2020-10-21 12:33:42 -0700 | [diff] [blame] | 120 | return nullptr; | 
|  | 121 | } | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 122 |  | 
|  | 123 | // For now, sessions that were persisted through a reboot get their idle | 
|  | 124 | // timer reset.  This could probably be overcome with a better | 
|  | 125 | // understanding of wall clock time and steady timer time, possibly | 
|  | 126 | // persisting values with wall clock time instead of steady timer, but | 
|  | 127 | // the tradeoffs of all the corner cases involved are non-trivial, so | 
|  | 128 | // this is done temporarily | 
|  | 129 | userSession->lastUpdated = std::chrono::steady_clock::now(); | 
|  | 130 | userSession->persistence = PersistenceType::TIMEOUT; | 
|  | 131 |  | 
|  | 132 | return userSession; | 
| Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 133 | } | 
| Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 134 | }; | 
|  | 135 |  | 
| Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 136 | struct AuthConfigMethods | 
|  | 137 | { | 
| Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 138 | bool basic = BMCWEB_BASIC_AUTH; | 
|  | 139 | bool sessionToken = BMCWEB_SESSION_AUTH; | 
|  | 140 | bool xtoken = BMCWEB_XTOKEN_AUTH; | 
|  | 141 | bool cookie = BMCWEB_COOKIE_AUTH; | 
|  | 142 | bool tls = BMCWEB_MUTUAL_TLS_AUTH; | 
| Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 143 |  | 
|  | 144 | void fromJson(const nlohmann::json& j) | 
|  | 145 | { | 
|  | 146 | for (const auto& element : j.items()) | 
|  | 147 | { | 
|  | 148 | const bool* value = element.value().get_ptr<const bool*>(); | 
|  | 149 | if (value == nullptr) | 
|  | 150 | { | 
|  | 151 | continue; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | if (element.key() == "XToken") | 
|  | 155 | { | 
|  | 156 | xtoken = *value; | 
|  | 157 | } | 
|  | 158 | else if (element.key() == "Cookie") | 
|  | 159 | { | 
|  | 160 | cookie = *value; | 
|  | 161 | } | 
|  | 162 | else if (element.key() == "SessionToken") | 
|  | 163 | { | 
|  | 164 | sessionToken = *value; | 
|  | 165 | } | 
|  | 166 | else if (element.key() == "BasicAuth") | 
|  | 167 | { | 
|  | 168 | basic = *value; | 
|  | 169 | } | 
| Zbigniew Kurzynski | 501f1e5 | 2019-10-02 11:22:11 +0200 | [diff] [blame] | 170 | else if (element.key() == "TLS") | 
|  | 171 | { | 
|  | 172 | tls = *value; | 
|  | 173 | } | 
| Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 174 | } | 
|  | 175 | } | 
|  | 176 | }; | 
|  | 177 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 178 | class SessionStore | 
|  | 179 | { | 
|  | 180 | public: | 
|  | 181 | std::shared_ptr<UserSession> generateUserSession( | 
| Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 182 | std::string_view username, const boost::asio::ip::address& clientIp, | 
| Ed Tanous | bb759e3 | 2022-08-02 17:07:54 -0700 | [diff] [blame] | 183 | const std::optional<std::string>& clientId, | 
| Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 184 | PersistenceType persistence = PersistenceType::TIMEOUT, | 
| Sunitha Harish | d323922 | 2021-02-24 15:33:29 +0530 | [diff] [blame] | 185 | bool isConfigureSelfOnly = false) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 186 | { | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 187 | // Only need csrf tokens for cookie based auth, token doesn't matter | 
| Ed Tanous | b7f3a82 | 2024-06-05 08:45:25 -0700 | [diff] [blame] | 188 | std::string sessionToken = | 
|  | 189 | bmcweb::getRandomIdOfLength(sessionTokenSize); | 
|  | 190 | std::string csrfToken = bmcweb::getRandomIdOfLength(sessionTokenSize); | 
|  | 191 | std::string uniqueId = bmcweb::getRandomIdOfLength(10); | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 192 |  | 
| Ed Tanous | b7f3a82 | 2024-06-05 08:45:25 -0700 | [diff] [blame] | 193 | // | 
|  | 194 | if (sessionToken.empty() || csrfToken.empty() || uniqueId.empty()) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 195 | { | 
| Ed Tanous | b7f3a82 | 2024-06-05 08:45:25 -0700 | [diff] [blame] | 196 | BMCWEB_LOG_ERROR("Failed to generate session tokens"); | 
|  | 197 | return nullptr; | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 198 | } | 
| Jiaqing Zhao | 41d61c8 | 2021-12-07 13:21:47 +0800 | [diff] [blame] | 199 |  | 
| Ed Tanous | 47f2934 | 2024-03-19 12:18:06 -0700 | [diff] [blame] | 200 | auto session = std::make_shared<UserSession>( | 
|  | 201 | UserSession{uniqueId, | 
|  | 202 | sessionToken, | 
|  | 203 | std::string(username), | 
|  | 204 | csrfToken, | 
|  | 205 | clientId, | 
|  | 206 | redfish::ip_util::toString(clientIp), | 
|  | 207 | std::chrono::steady_clock::now(), | 
|  | 208 | persistence, | 
|  | 209 | false, | 
|  | 210 | isConfigureSelfOnly, | 
|  | 211 | "", | 
|  | 212 | {}}); | 
| Patrick Williams | 41713dd | 2022-09-28 06:48:07 -0500 | [diff] [blame] | 213 | auto it = authTokens.emplace(sessionToken, session); | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 214 | // Only need to write to disk if session isn't about to be destroyed. | 
|  | 215 | needWrite = persistence == PersistenceType::TIMEOUT; | 
|  | 216 | return it.first->second; | 
| Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 217 | } | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 218 |  | 
| Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 219 | std::shared_ptr<UserSession> loginSessionByToken(std::string_view token) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 220 | { | 
|  | 221 | applySessionTimeouts(); | 
| Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 222 | if (token.size() != sessionTokenSize) | 
|  | 223 | { | 
|  | 224 | return nullptr; | 
|  | 225 | } | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 226 | auto sessionIt = authTokens.find(std::string(token)); | 
|  | 227 | if (sessionIt == authTokens.end()) | 
|  | 228 | { | 
|  | 229 | return nullptr; | 
|  | 230 | } | 
|  | 231 | std::shared_ptr<UserSession> userSession = sessionIt->second; | 
|  | 232 | userSession->lastUpdated = std::chrono::steady_clock::now(); | 
|  | 233 | return userSession; | 
|  | 234 | } | 
|  | 235 |  | 
| Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 236 | std::shared_ptr<UserSession> getSessionByUid(std::string_view uid) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 237 | { | 
|  | 238 | applySessionTimeouts(); | 
|  | 239 | // TODO(Ed) this is inefficient | 
|  | 240 | auto sessionIt = authTokens.begin(); | 
|  | 241 | while (sessionIt != authTokens.end()) | 
|  | 242 | { | 
|  | 243 | if (sessionIt->second->uniqueId == uid) | 
|  | 244 | { | 
|  | 245 | return sessionIt->second; | 
|  | 246 | } | 
|  | 247 | sessionIt++; | 
|  | 248 | } | 
|  | 249 | return nullptr; | 
|  | 250 | } | 
|  | 251 |  | 
| Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 252 | void removeSession(const std::shared_ptr<UserSession>& session) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 253 | { | 
|  | 254 | authTokens.erase(session->sessionToken); | 
|  | 255 | needWrite = true; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | std::vector<const std::string*> getUniqueIds( | 
|  | 259 | bool getAll = true, | 
|  | 260 | const PersistenceType& type = PersistenceType::SINGLE_REQUEST) | 
|  | 261 | { | 
|  | 262 | applySessionTimeouts(); | 
|  | 263 |  | 
|  | 264 | std::vector<const std::string*> ret; | 
|  | 265 | ret.reserve(authTokens.size()); | 
|  | 266 | for (auto& session : authTokens) | 
|  | 267 | { | 
|  | 268 | if (getAll || type == session.second->persistence) | 
|  | 269 | { | 
|  | 270 | ret.push_back(&session.second->uniqueId); | 
|  | 271 | } | 
|  | 272 | } | 
|  | 273 | return ret; | 
|  | 274 | } | 
|  | 275 |  | 
| Xie Ning | 9fa06f1 | 2022-06-29 18:27:47 +0800 | [diff] [blame] | 276 | void removeSessionsByUsername(std::string_view username) | 
|  | 277 | { | 
|  | 278 | std::erase_if(authTokens, [username](const auto& value) { | 
|  | 279 | if (value.second == nullptr) | 
|  | 280 | { | 
|  | 281 | return false; | 
|  | 282 | } | 
|  | 283 | return value.second->username == username; | 
|  | 284 | }); | 
|  | 285 | } | 
|  | 286 |  | 
| Ravi Teja | e518ef3 | 2024-05-16 10:33:08 -0500 | [diff] [blame] | 287 | void removeSessionsByUsernameExceptSession( | 
|  | 288 | std::string_view username, const std::shared_ptr<UserSession>& session) | 
|  | 289 | { | 
|  | 290 | std::erase_if(authTokens, [username, session](const auto& value) { | 
|  | 291 | if (value.second == nullptr) | 
|  | 292 | { | 
|  | 293 | return false; | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 | return value.second->username == username && | 
|  | 297 | value.second->uniqueId != session->uniqueId; | 
|  | 298 | }); | 
|  | 299 | } | 
|  | 300 |  | 
| Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 301 | void updateAuthMethodsConfig(const AuthConfigMethods& config) | 
|  | 302 | { | 
| Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 303 | bool isTLSchanged = (authMethodsConfig.tls != config.tls); | 
| Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 304 | authMethodsConfig = config; | 
|  | 305 | needWrite = true; | 
| Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 306 | if (isTLSchanged) | 
|  | 307 | { | 
|  | 308 | // recreate socket connections with new settings | 
|  | 309 | std::raise(SIGHUP); | 
|  | 310 | } | 
| Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 311 | } | 
|  | 312 |  | 
|  | 313 | AuthConfigMethods& getAuthMethodsConfig() | 
|  | 314 | { | 
|  | 315 | return authMethodsConfig; | 
|  | 316 | } | 
|  | 317 |  | 
| Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 318 | bool needsWrite() const | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 319 | { | 
|  | 320 | return needWrite; | 
|  | 321 | } | 
| Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 322 | int64_t getTimeoutInSeconds() const | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 323 | { | 
| Manojkiran Eda | f2a4a60 | 2020-08-27 16:04:26 +0530 | [diff] [blame] | 324 | return std::chrono::seconds(timeoutInSeconds).count(); | 
|  | 325 | } | 
|  | 326 |  | 
|  | 327 | void updateSessionTimeout(std::chrono::seconds newTimeoutInSeconds) | 
|  | 328 | { | 
|  | 329 | timeoutInSeconds = newTimeoutInSeconds; | 
|  | 330 | needWrite = true; | 
| Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 331 | } | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 332 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 333 | static SessionStore& getInstance() | 
|  | 334 | { | 
|  | 335 | static SessionStore sessionStore; | 
|  | 336 | return sessionStore; | 
|  | 337 | } | 
|  | 338 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 339 | void applySessionTimeouts() | 
|  | 340 | { | 
|  | 341 | auto timeNow = std::chrono::steady_clock::now(); | 
| Manojkiran Eda | f2a4a60 | 2020-08-27 16:04:26 +0530 | [diff] [blame] | 342 | if (timeNow - lastTimeoutUpdate > std::chrono::seconds(1)) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 343 | { | 
|  | 344 | lastTimeoutUpdate = timeNow; | 
|  | 345 | auto authTokensIt = authTokens.begin(); | 
|  | 346 | while (authTokensIt != authTokens.end()) | 
|  | 347 | { | 
|  | 348 | if (timeNow - authTokensIt->second->lastUpdated >= | 
| Manojkiran Eda | f2a4a60 | 2020-08-27 16:04:26 +0530 | [diff] [blame] | 349 | timeoutInSeconds) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 350 | { | 
|  | 351 | authTokensIt = authTokens.erase(authTokensIt); | 
| Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 352 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 353 | needWrite = true; | 
|  | 354 | } | 
|  | 355 | else | 
|  | 356 | { | 
|  | 357 | authTokensIt++; | 
|  | 358 | } | 
|  | 359 | } | 
|  | 360 | } | 
|  | 361 | } | 
| Gunnar Mills | 83cf818 | 2020-11-11 15:37:34 -0600 | [diff] [blame] | 362 |  | 
|  | 363 | SessionStore(const SessionStore&) = delete; | 
|  | 364 | SessionStore& operator=(const SessionStore&) = delete; | 
| Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 365 | SessionStore(SessionStore&&) = delete; | 
|  | 366 | SessionStore& operator=(const SessionStore&&) = delete; | 
|  | 367 | ~SessionStore() = default; | 
| Gunnar Mills | 83cf818 | 2020-11-11 15:37:34 -0600 | [diff] [blame] | 368 |  | 
|  | 369 | std::unordered_map<std::string, std::shared_ptr<UserSession>, | 
|  | 370 | std::hash<std::string>, | 
|  | 371 | crow::utility::ConstantTimeCompare> | 
|  | 372 | authTokens; | 
|  | 373 |  | 
|  | 374 | std::chrono::time_point<std::chrono::steady_clock> lastTimeoutUpdate; | 
|  | 375 | bool needWrite{false}; | 
|  | 376 | std::chrono::seconds timeoutInSeconds; | 
|  | 377 | AuthConfigMethods authMethodsConfig; | 
|  | 378 |  | 
|  | 379 | private: | 
| Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 380 | SessionStore() : timeoutInSeconds(1800) {} | 
| Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 381 | }; | 
|  | 382 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 383 | } // namespace persistent_data |