Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 3 | #include "logging.h" |
| 4 | #include "utility.h" |
| 5 | |
James Feist | a68a804 | 2020-04-15 15:46:44 -0700 | [diff] [blame] | 6 | #include <openssl/rand.h> |
| 7 | |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 8 | #include <boost/container/flat_map.hpp> |
| 9 | #include <boost/uuid/uuid.hpp> |
| 10 | #include <boost/uuid/uuid_generators.hpp> |
| 11 | #include <boost/uuid/uuid_io.hpp> |
Ratan Gupta | 12c04ef | 2019-04-03 10:08:11 +0530 | [diff] [blame] | 12 | #include <dbus_singleton.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 13 | #include <nlohmann/json.hpp> |
| 14 | #include <pam_authenticate.hpp> |
RAJESWARAN THILLAIGOVINDAN | 7052517 | 2019-07-09 13:15:05 -0500 | [diff] [blame] | 15 | #include <sdbusplus/bus/match.hpp> |
Ratan Gupta | 12c04ef | 2019-04-03 10:08:11 +0530 | [diff] [blame] | 16 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 17 | #include <csignal> |
| 18 | #include <random> |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 19 | #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE |
| 20 | #include <ibm/locks.hpp> |
| 21 | #endif |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 22 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 23 | namespace persistent_data |
| 24 | { |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 25 | |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 26 | // entropy: 20 characters, 62 possibilities. log2(62^20) = 119 bits of |
| 27 | // entropy. OWASP recommends at least 64 |
| 28 | // https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#session-id-entropy |
| 29 | constexpr std::size_t sessionTokenSize = 20; |
| 30 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 31 | enum class PersistenceType |
| 32 | { |
| 33 | TIMEOUT, // User session times out after a predetermined amount of time |
| 34 | SINGLE_REQUEST // User times out once this request is completed. |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 37 | struct UserSession |
| 38 | { |
| 39 | std::string uniqueId; |
| 40 | std::string sessionToken; |
| 41 | std::string username; |
| 42 | std::string csrfToken; |
Sunitha Harish | 08bdcc7 | 2020-05-12 05:17:57 -0500 | [diff] [blame] | 43 | std::string clientId; |
Sunitha Harish | 92f6822 | 2020-05-28 05:09:09 -0500 | [diff] [blame] | 44 | std::string clientIp; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 45 | std::chrono::time_point<std::chrono::steady_clock> lastUpdated; |
| 46 | PersistenceType persistence; |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 47 | bool cookieAuth = false; |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 48 | bool isConfigureSelfOnly = false; |
| 49 | |
| 50 | // There are two sources of truth for isConfigureSelfOnly: |
| 51 | // 1. When pamAuthenticateUser() returns PAM_NEW_AUTHTOK_REQD. |
| 52 | // 2. D-Bus User.Manager.GetUserInfo property UserPasswordExpired. |
| 53 | // These should be in sync, but the underlying condition can change at any |
| 54 | // time. For example, a password can expire or be changed outside of |
| 55 | // bmcweb. The value stored here is updated at the start of each |
| 56 | // operation and used as the truth within bmcweb. |
Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 57 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | /** |
| 59 | * @brief Fills object with data from UserSession's JSON representation |
| 60 | * |
| 61 | * This replaces nlohmann's from_json to ensure no-throw approach |
| 62 | * |
| 63 | * @param[in] j JSON object from which data should be loaded |
| 64 | * |
| 65 | * @return a shared pointer if data has been loaded properly, nullptr |
| 66 | * otherwise |
| 67 | */ |
| 68 | static std::shared_ptr<UserSession> fromJson(const nlohmann::json& j) |
| 69 | { |
| 70 | std::shared_ptr<UserSession> userSession = |
| 71 | std::make_shared<UserSession>(); |
| 72 | for (const auto& element : j.items()) |
| 73 | { |
| 74 | const std::string* thisValue = |
| 75 | element.value().get_ptr<const std::string*>(); |
| 76 | if (thisValue == nullptr) |
| 77 | { |
| 78 | BMCWEB_LOG_ERROR << "Error reading persistent store. Property " |
| 79 | << element.key() << " was not of type string"; |
| 80 | return nullptr; |
| 81 | } |
| 82 | if (element.key() == "unique_id") |
| 83 | { |
| 84 | userSession->uniqueId = *thisValue; |
| 85 | } |
| 86 | else if (element.key() == "session_token") |
| 87 | { |
| 88 | userSession->sessionToken = *thisValue; |
| 89 | } |
| 90 | else if (element.key() == "csrf_token") |
| 91 | { |
| 92 | userSession->csrfToken = *thisValue; |
| 93 | } |
| 94 | else if (element.key() == "username") |
| 95 | { |
| 96 | userSession->username = *thisValue; |
| 97 | } |
Sunitha Harish | 08bdcc7 | 2020-05-12 05:17:57 -0500 | [diff] [blame] | 98 | else if (element.key() == "client_id") |
| 99 | { |
| 100 | userSession->clientId = *thisValue; |
| 101 | } |
Sunitha Harish | 92f6822 | 2020-05-28 05:09:09 -0500 | [diff] [blame] | 102 | else if (element.key() == "client_ip") |
| 103 | { |
| 104 | userSession->clientIp = *thisValue; |
| 105 | } |
| 106 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 107 | else |
| 108 | { |
| 109 | BMCWEB_LOG_ERROR |
| 110 | << "Got unexpected property reading persistent file: " |
| 111 | << element.key(); |
| 112 | return nullptr; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // For now, sessions that were persisted through a reboot get their idle |
| 117 | // timer reset. This could probably be overcome with a better |
| 118 | // understanding of wall clock time and steady timer time, possibly |
| 119 | // persisting values with wall clock time instead of steady timer, but |
| 120 | // the tradeoffs of all the corner cases involved are non-trivial, so |
| 121 | // this is done temporarily |
| 122 | userSession->lastUpdated = std::chrono::steady_clock::now(); |
| 123 | userSession->persistence = PersistenceType::TIMEOUT; |
| 124 | |
| 125 | return userSession; |
Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 126 | } |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 127 | }; |
| 128 | |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 129 | struct AuthConfigMethods |
| 130 | { |
| 131 | bool xtoken = true; |
| 132 | bool cookie = true; |
| 133 | bool sessionToken = true; |
| 134 | bool basic = true; |
Zbigniew Kurzynski | cac94c5 | 2019-11-07 12:55:04 +0100 | [diff] [blame] | 135 | bool tls = false; |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 136 | |
| 137 | void fromJson(const nlohmann::json& j) |
| 138 | { |
| 139 | for (const auto& element : j.items()) |
| 140 | { |
| 141 | const bool* value = element.value().get_ptr<const bool*>(); |
| 142 | if (value == nullptr) |
| 143 | { |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | if (element.key() == "XToken") |
| 148 | { |
| 149 | xtoken = *value; |
| 150 | } |
| 151 | else if (element.key() == "Cookie") |
| 152 | { |
| 153 | cookie = *value; |
| 154 | } |
| 155 | else if (element.key() == "SessionToken") |
| 156 | { |
| 157 | sessionToken = *value; |
| 158 | } |
| 159 | else if (element.key() == "BasicAuth") |
| 160 | { |
| 161 | basic = *value; |
| 162 | } |
Zbigniew Kurzynski | 501f1e5 | 2019-10-02 11:22:11 +0200 | [diff] [blame] | 163 | else if (element.key() == "TLS") |
| 164 | { |
| 165 | tls = *value; |
| 166 | } |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | }; |
| 170 | |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 171 | class Middleware; |
| 172 | |
James Feist | a68a804 | 2020-04-15 15:46:44 -0700 | [diff] [blame] | 173 | struct OpenSSLGenerator |
| 174 | { |
| 175 | |
| 176 | uint8_t operator()(void) |
| 177 | { |
| 178 | uint8_t index = 0; |
| 179 | int rc = RAND_bytes(&index, sizeof(index)); |
| 180 | if (rc != opensslSuccess) |
| 181 | { |
| 182 | std::cerr << "Cannot get random number\n"; |
| 183 | err = true; |
| 184 | } |
| 185 | |
| 186 | return index; |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 187 | } |
James Feist | a68a804 | 2020-04-15 15:46:44 -0700 | [diff] [blame] | 188 | |
| 189 | uint8_t max() |
| 190 | { |
| 191 | return std::numeric_limits<uint8_t>::max(); |
| 192 | } |
| 193 | uint8_t min() |
| 194 | { |
| 195 | return std::numeric_limits<uint8_t>::min(); |
| 196 | } |
| 197 | |
| 198 | bool error() |
| 199 | { |
| 200 | return err; |
| 201 | } |
| 202 | |
| 203 | // all generators require this variable |
| 204 | using result_type = uint8_t; |
| 205 | |
| 206 | private: |
| 207 | // RAND_bytes() returns 1 on success, 0 otherwise. -1 if bad function |
| 208 | static constexpr int opensslSuccess = 1; |
| 209 | bool err = false; |
| 210 | }; |
| 211 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 212 | class SessionStore |
| 213 | { |
| 214 | public: |
| 215 | std::shared_ptr<UserSession> generateUserSession( |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 216 | const std::string_view username, |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 217 | PersistenceType persistence = PersistenceType::TIMEOUT, |
Sunitha Harish | 92f6822 | 2020-05-28 05:09:09 -0500 | [diff] [blame] | 218 | bool isConfigureSelfOnly = false, const std::string_view clientId = "", |
| 219 | const std::string_view clientIp = "") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 220 | { |
| 221 | // TODO(ed) find a secure way to not generate session identifiers if |
| 222 | // persistence is set to SINGLE_REQUEST |
| 223 | static constexpr std::array<char, 62> alphanum = { |
Joseph Reynolds | 368b1d4 | 2019-08-15 15:29:06 -0500 | [diff] [blame] | 224 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', |
| 225 | 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 226 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 227 | 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', |
| 228 | 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 229 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 230 | std::string sessionToken; |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 231 | sessionToken.resize(sessionTokenSize, '0'); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 232 | std::uniform_int_distribution<size_t> dist(0, alphanum.size() - 1); |
James Feist | a68a804 | 2020-04-15 15:46:44 -0700 | [diff] [blame] | 233 | |
| 234 | OpenSSLGenerator gen; |
| 235 | |
Ed Tanous | 0dfeda6 | 2019-10-24 11:21:38 -0700 | [diff] [blame] | 236 | for (char& sessionChar : sessionToken) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 237 | { |
Ed Tanous | 0dfeda6 | 2019-10-24 11:21:38 -0700 | [diff] [blame] | 238 | sessionChar = alphanum[dist(gen)]; |
James Feist | a68a804 | 2020-04-15 15:46:44 -0700 | [diff] [blame] | 239 | if (gen.error()) |
| 240 | { |
| 241 | return nullptr; |
| 242 | } |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 243 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 244 | // Only need csrf tokens for cookie based auth, token doesn't matter |
| 245 | std::string csrfToken; |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 246 | csrfToken.resize(sessionTokenSize, '0'); |
Ed Tanous | 0dfeda6 | 2019-10-24 11:21:38 -0700 | [diff] [blame] | 247 | for (char& csrfChar : csrfToken) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 248 | { |
Ed Tanous | 0dfeda6 | 2019-10-24 11:21:38 -0700 | [diff] [blame] | 249 | csrfChar = alphanum[dist(gen)]; |
James Feist | a68a804 | 2020-04-15 15:46:44 -0700 | [diff] [blame] | 250 | if (gen.error()) |
| 251 | { |
| 252 | return nullptr; |
| 253 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | std::string uniqueId; |
| 257 | uniqueId.resize(10, '0'); |
Ed Tanous | 0dfeda6 | 2019-10-24 11:21:38 -0700 | [diff] [blame] | 258 | for (char& uidChar : uniqueId) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 259 | { |
Ed Tanous | 0dfeda6 | 2019-10-24 11:21:38 -0700 | [diff] [blame] | 260 | uidChar = alphanum[dist(gen)]; |
James Feist | a68a804 | 2020-04-15 15:46:44 -0700 | [diff] [blame] | 261 | if (gen.error()) |
| 262 | { |
| 263 | return nullptr; |
| 264 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 265 | } |
Sunitha Harish | 92f6822 | 2020-05-28 05:09:09 -0500 | [diff] [blame] | 266 | auto session = std::make_shared<UserSession>( |
| 267 | UserSession{uniqueId, sessionToken, std::string(username), |
| 268 | csrfToken, std::string(clientId), std::string(clientIp), |
| 269 | std::chrono::steady_clock::now(), persistence, false, |
| 270 | isConfigureSelfOnly}); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 271 | auto it = authTokens.emplace(std::make_pair(sessionToken, session)); |
| 272 | // Only need to write to disk if session isn't about to be destroyed. |
| 273 | needWrite = persistence == PersistenceType::TIMEOUT; |
| 274 | return it.first->second; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 275 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 276 | |
| 277 | std::shared_ptr<UserSession> |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 278 | loginSessionByToken(const std::string_view token) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 279 | { |
| 280 | applySessionTimeouts(); |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 281 | if (token.size() != sessionTokenSize) |
| 282 | { |
| 283 | return nullptr; |
| 284 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 285 | auto sessionIt = authTokens.find(std::string(token)); |
| 286 | if (sessionIt == authTokens.end()) |
| 287 | { |
| 288 | return nullptr; |
| 289 | } |
| 290 | std::shared_ptr<UserSession> userSession = sessionIt->second; |
| 291 | userSession->lastUpdated = std::chrono::steady_clock::now(); |
| 292 | return userSession; |
| 293 | } |
| 294 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 295 | std::shared_ptr<UserSession> getSessionByUid(const std::string_view uid) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 296 | { |
| 297 | applySessionTimeouts(); |
| 298 | // TODO(Ed) this is inefficient |
| 299 | auto sessionIt = authTokens.begin(); |
| 300 | while (sessionIt != authTokens.end()) |
| 301 | { |
| 302 | if (sessionIt->second->uniqueId == uid) |
| 303 | { |
| 304 | return sessionIt->second; |
| 305 | } |
| 306 | sessionIt++; |
| 307 | } |
| 308 | return nullptr; |
| 309 | } |
| 310 | |
| 311 | void removeSession(std::shared_ptr<UserSession> session) |
| 312 | { |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 313 | #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE |
| 314 | crow::ibm_mc_lock::Lock::getInstance().releaseLock(session->uniqueId); |
| 315 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 316 | authTokens.erase(session->sessionToken); |
| 317 | needWrite = true; |
| 318 | } |
| 319 | |
| 320 | std::vector<const std::string*> getUniqueIds( |
| 321 | bool getAll = true, |
| 322 | const PersistenceType& type = PersistenceType::SINGLE_REQUEST) |
| 323 | { |
| 324 | applySessionTimeouts(); |
| 325 | |
| 326 | std::vector<const std::string*> ret; |
| 327 | ret.reserve(authTokens.size()); |
| 328 | for (auto& session : authTokens) |
| 329 | { |
| 330 | if (getAll || type == session.second->persistence) |
| 331 | { |
| 332 | ret.push_back(&session.second->uniqueId); |
| 333 | } |
| 334 | } |
| 335 | return ret; |
| 336 | } |
| 337 | |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 338 | void updateAuthMethodsConfig(const AuthConfigMethods& config) |
| 339 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 340 | bool isTLSchanged = (authMethodsConfig.tls != config.tls); |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 341 | authMethodsConfig = config; |
| 342 | needWrite = true; |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 343 | if (isTLSchanged) |
| 344 | { |
| 345 | // recreate socket connections with new settings |
| 346 | std::raise(SIGHUP); |
| 347 | } |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | AuthConfigMethods& getAuthMethodsConfig() |
| 351 | { |
| 352 | return authMethodsConfig; |
| 353 | } |
| 354 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 355 | bool needsWrite() |
| 356 | { |
| 357 | return needWrite; |
| 358 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 359 | int64_t getTimeoutInSeconds() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 360 | { |
| 361 | return std::chrono::seconds(timeoutInMinutes).count(); |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 362 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 363 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 364 | static SessionStore& getInstance() |
| 365 | { |
| 366 | static SessionStore sessionStore; |
| 367 | return sessionStore; |
| 368 | } |
| 369 | |
| 370 | SessionStore(const SessionStore&) = delete; |
| 371 | SessionStore& operator=(const SessionStore&) = delete; |
| 372 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 373 | std::unordered_map<std::string, std::shared_ptr<UserSession>, |
| 374 | std::hash<std::string>, |
| 375 | crow::utility::ConstantTimeCompare> |
| 376 | authTokens; |
| 377 | |
| 378 | std::chrono::time_point<std::chrono::steady_clock> lastTimeoutUpdate; |
| 379 | bool needWrite{false}; |
| 380 | std::chrono::minutes timeoutInMinutes; |
| 381 | AuthConfigMethods authMethodsConfig; |
| 382 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 383 | private: |
| 384 | SessionStore() : timeoutInMinutes(60) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 385 | {} |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 386 | |
| 387 | void applySessionTimeouts() |
| 388 | { |
| 389 | auto timeNow = std::chrono::steady_clock::now(); |
| 390 | if (timeNow - lastTimeoutUpdate > std::chrono::minutes(1)) |
| 391 | { |
| 392 | lastTimeoutUpdate = timeNow; |
| 393 | auto authTokensIt = authTokens.begin(); |
| 394 | while (authTokensIt != authTokens.end()) |
| 395 | { |
| 396 | if (timeNow - authTokensIt->second->lastUpdated >= |
| 397 | timeoutInMinutes) |
| 398 | { |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 399 | #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE |
| 400 | crow::ibm_mc_lock::Lock::getInstance().releaseLock( |
| 401 | authTokensIt->second->uniqueId); |
| 402 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 403 | authTokensIt = authTokens.erase(authTokensIt); |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 404 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 405 | needWrite = true; |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | authTokensIt++; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 414 | }; |
| 415 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 416 | } // namespace persistent_data |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 417 | |
| 418 | // to_json(...) definition for objects of UserSession type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 419 | namespace nlohmann |
| 420 | { |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 421 | template <> |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 422 | struct adl_serializer<std::shared_ptr<persistent_data::UserSession>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 423 | { |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 424 | static void to_json(nlohmann::json& j, |
| 425 | const std::shared_ptr<persistent_data::UserSession>& p) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 426 | { |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 427 | if (p->persistence != persistent_data::PersistenceType::SINGLE_REQUEST) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 428 | { |
Sunitha Harish | 08bdcc7 | 2020-05-12 05:17:57 -0500 | [diff] [blame] | 429 | #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE |
Sunitha Harish | 92f6822 | 2020-05-28 05:09:09 -0500 | [diff] [blame] | 430 | j = nlohmann::json{ |
| 431 | {"unique_id", p->uniqueId}, {"session_token", p->sessionToken}, |
| 432 | {"username", p->username}, {"csrf_token", p->csrfToken}, |
| 433 | {"client_id", p->clientId}, { "client_ip", p->clientIp }}; |
Sunitha Harish | 08bdcc7 | 2020-05-12 05:17:57 -0500 | [diff] [blame] | 434 | #else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 435 | j = nlohmann::json{{"unique_id", p->uniqueId}, |
| 436 | {"session_token", p->sessionToken}, |
| 437 | {"username", p->username}, |
Sunitha Harish | 92f6822 | 2020-05-28 05:09:09 -0500 | [diff] [blame] | 438 | {"csrf_token", p->csrfToken}, |
| 439 | {"client_ip", p->clientIp}}; |
Sunitha Harish | 08bdcc7 | 2020-05-12 05:17:57 -0500 | [diff] [blame] | 440 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 441 | } |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 442 | } |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 443 | }; |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 444 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 445 | template <> |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 446 | struct adl_serializer<persistent_data::AuthConfigMethods> |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 447 | { |
| 448 | static void to_json(nlohmann::json& j, |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 449 | const persistent_data::AuthConfigMethods& c) |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 450 | { |
| 451 | j = nlohmann::json{{"XToken", c.xtoken}, |
| 452 | {"Cookie", c.cookie}, |
| 453 | {"SessionToken", c.sessionToken}, |
Zbigniew Kurzynski | 501f1e5 | 2019-10-02 11:22:11 +0200 | [diff] [blame] | 454 | {"BasicAuth", c.basic}, |
| 455 | {"TLS", c.tls}}; |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 456 | } |
| 457 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 458 | } // namespace nlohmann |