blob: 22aa3adf0c6ab16f0c17afd8267a2e0397f37ba8 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +01003#pragma once
4
Ed Tanousd7857202025-01-28 15:32:26 -08005#include "bmcweb_config.h"
6
Ed Tanous04e438c2020-10-03 08:06:26 -07007#include "logging.hpp"
Ed Tanous2c6ffdb2023-06-28 11:28:38 -07008#include "ossl_random.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08009#include "utils/ip_utils.hpp"
Ed Tanousfc76b8a2020-09-28 17:21:52 -070010
Ed Tanousd7857202025-01-28 15:32:26 -080011#include <boost/asio/ip/address.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070012#include <nlohmann/json.hpp>
Ratan Gupta12c04ef2019-04-03 10:08:11 +053013
Ed Tanousd7857202025-01-28 15:32:26 -080014#include <chrono>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050015#include <csignal>
Ed Tanousd7857202025-01-28 15:32:26 -080016#include <cstddef>
17#include <cstdint>
18#include <functional>
Ed Tanous89cda632024-04-16 08:45:54 -070019#include <memory>
Ed Tanousbb759e32022-08-02 17:07:54 -070020#include <optional>
Ed Tanousb7f3a822024-06-05 08:45:25 -070021#include <string>
Ed Tanousd7857202025-01-28 15:32:26 -080022#include <string_view>
23#include <unordered_map>
Ed Tanous89cda632024-04-16 08:45:54 -070024#include <vector>
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010025
Ed Tanous1abe55e2018-09-05 08:30:59 -070026namespace persistent_data
27{
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010028
Ed Tanous51dae672018-09-05 16:07:32 -070029// entropy: 20 characters, 62 possibilities. log2(62^20) = 119 bits of
30// entropy. OWASP recommends at least 64
31// https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#session-id-entropy
32constexpr std::size_t sessionTokenSize = 20;
33
Ed Tanous89cda632024-04-16 08:45:54 -070034enum class SessionType
Ed Tanous1abe55e2018-09-05 08:30:59 -070035{
Ed Tanous89cda632024-04-16 08:45:54 -070036 None,
37 Basic,
38 Session,
39 Cookie,
40 MutualTLS
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010041};
42
Ed Tanous1abe55e2018-09-05 08:30:59 -070043struct UserSession
44{
45 std::string uniqueId;
46 std::string sessionToken;
47 std::string username;
48 std::string csrfToken;
Ed Tanousbb759e32022-08-02 17:07:54 -070049 std::optional<std::string> clientId;
Sunitha Harish92f68222020-05-28 05:09:09 -050050 std::string clientIp;
Ed Tanous1abe55e2018-09-05 08:30:59 -070051 std::chrono::time_point<std::chrono::steady_clock> lastUpdated;
Ed Tanous89cda632024-04-16 08:45:54 -070052 SessionType sessionType{SessionType::None};
Ed Tanous7e9c08e2023-06-16 11:29:37 -070053 bool cookieAuth = false;
Joseph Reynolds3bf4e632020-02-06 14:44:32 -060054 bool isConfigureSelfOnly = false;
Ed Tanous47f29342024-03-19 12:18:06 -070055 std::string userRole;
56 std::vector<std::string> userGroups;
Joseph Reynolds3bf4e632020-02-06 14:44:32 -060057
58 // There are two sources of truth for isConfigureSelfOnly:
59 // 1. When pamAuthenticateUser() returns PAM_NEW_AUTHTOK_REQD.
60 // 2. D-Bus User.Manager.GetUserInfo property UserPasswordExpired.
61 // These should be in sync, but the underlying condition can change at any
62 // time. For example, a password can expire or be changed outside of
63 // bmcweb. The value stored here is updated at the start of each
64 // operation and used as the truth within bmcweb.
Kowalski, Kamil5cef0f72018-02-15 15:26:51 +010065
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 /**
67 * @brief Fills object with data from UserSession's JSON representation
68 *
69 * This replaces nlohmann's from_json to ensure no-throw approach
70 *
71 * @param[in] j JSON object from which data should be loaded
72 *
73 * @return a shared pointer if data has been loaded properly, nullptr
74 * otherwise
75 */
Patrick Williams504af5a2025-02-03 14:29:03 -050076 static std::shared_ptr<UserSession> fromJson(
77 const nlohmann::json::object_t& j)
Ed Tanous1abe55e2018-09-05 08:30:59 -070078 {
79 std::shared_ptr<UserSession> userSession =
80 std::make_shared<UserSession>();
Ed Tanous0bdda662023-08-03 17:27:34 -070081 for (const auto& element : j)
Ed Tanous1abe55e2018-09-05 08:30:59 -070082 {
83 const std::string* thisValue =
Ed Tanous0bdda662023-08-03 17:27:34 -070084 element.second.get_ptr<const std::string*>();
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 if (thisValue == nullptr)
86 {
Ed Tanous62598e32023-07-17 17:06:25 -070087 BMCWEB_LOG_ERROR(
88 "Error reading persistent store. Property {} was not of type string",
Ed Tanous0bdda662023-08-03 17:27:34 -070089 element.first);
Ed Tanousdc511aa2020-10-21 12:33:42 -070090 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -070091 }
Ed Tanous0bdda662023-08-03 17:27:34 -070092 if (element.first == "unique_id")
Ed Tanous1abe55e2018-09-05 08:30:59 -070093 {
94 userSession->uniqueId = *thisValue;
95 }
Ed Tanous0bdda662023-08-03 17:27:34 -070096 else if (element.first == "session_token")
Ed Tanous1abe55e2018-09-05 08:30:59 -070097 {
98 userSession->sessionToken = *thisValue;
99 }
Ed Tanous0bdda662023-08-03 17:27:34 -0700100 else if (element.first == "csrf_token")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700101 {
102 userSession->csrfToken = *thisValue;
103 }
Ed Tanous0bdda662023-08-03 17:27:34 -0700104 else if (element.first == "username")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700105 {
106 userSession->username = *thisValue;
107 }
Ed Tanous0bdda662023-08-03 17:27:34 -0700108 else if (element.first == "client_id")
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500109 {
110 userSession->clientId = *thisValue;
111 }
Ed Tanous0bdda662023-08-03 17:27:34 -0700112 else if (element.first == "client_ip")
Sunitha Harish92f68222020-05-28 05:09:09 -0500113 {
114 userSession->clientIp = *thisValue;
115 }
116
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117 else
118 {
Ed Tanous62598e32023-07-17 17:06:25 -0700119 BMCWEB_LOG_ERROR(
120 "Got unexpected property reading persistent file: {}",
Ed Tanous0bdda662023-08-03 17:27:34 -0700121 element.first);
Ed Tanousdc511aa2020-10-21 12:33:42 -0700122 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700123 }
124 }
Ed Tanousdc511aa2020-10-21 12:33:42 -0700125 // If any of these fields are missing, we can't restore the session, as
126 // we don't have enough information. These 4 fields have been present
127 // in every version of this file in bmcwebs history, so any file, even
128 // on upgrade, should have these present
129 if (userSession->uniqueId.empty() || userSession->username.empty() ||
130 userSession->sessionToken.empty() || userSession->csrfToken.empty())
131 {
Ed Tanous62598e32023-07-17 17:06:25 -0700132 BMCWEB_LOG_DEBUG("Session missing required security "
133 "information, refusing to restore");
Ed Tanousdc511aa2020-10-21 12:33:42 -0700134 return nullptr;
135 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136
137 // For now, sessions that were persisted through a reboot get their idle
138 // timer reset. This could probably be overcome with a better
139 // understanding of wall clock time and steady timer time, possibly
140 // persisting values with wall clock time instead of steady timer, but
141 // the tradeoffs of all the corner cases involved are non-trivial, so
142 // this is done temporarily
143 userSession->lastUpdated = std::chrono::steady_clock::now();
Ed Tanous89cda632024-04-16 08:45:54 -0700144 userSession->sessionType = SessionType::Session;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700145
146 return userSession;
Kowalski, Kamil5cef0f72018-02-15 15:26:51 +0100147 }
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100148};
149
Ed Tanous3ce36882024-06-09 10:58:16 -0700150enum class MTLSCommonNameParseMode
151{
152 Invalid = 0,
153 // This section approximately matches Redfish AccountService
154 // CertificateMappingAttribute, plus bmcweb defined OEM ones.
155 // Note, IDs in this enum must be maintained between versions, as they are
156 // persisted to disk
157 Whole = 1,
158 CommonName = 2,
159 UserPrincipalName = 3,
160
161 // Intentional gap for future DMTF-defined enums
162
163 // OEM parsing modes for various OEMs
164 Meta = 100,
165};
166
167inline MTLSCommonNameParseMode getMTLSCommonNameParseMode(std::string_view name)
168{
169 if (name == "CommonName")
170 {
171 return MTLSCommonNameParseMode::CommonName;
172 }
173 if (name == "Whole")
174 {
175 // Not yet supported
176 // return MTLSCommonNameParseMode::Whole;
177 }
178 if (name == "UserPrincipalName")
179 {
Malik Akbar Hashemi Rafsanjani4d7b5dd2025-02-26 13:14:30 -0800180 return MTLSCommonNameParseMode::UserPrincipalName;
Ed Tanous3ce36882024-06-09 10:58:16 -0700181 }
182 if constexpr (BMCWEB_META_TLS_COMMON_NAME_PARSING)
183 {
184 if (name == "Meta")
185 {
186 return MTLSCommonNameParseMode::Meta;
187 }
188 }
189 return MTLSCommonNameParseMode::Invalid;
190}
191
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100192struct AuthConfigMethods
193{
Ed Tanous3281bcf2024-06-25 16:02:05 -0700194 // Authentication paths
Ed Tanous25b54db2024-04-17 15:40:31 -0700195 bool basic = BMCWEB_BASIC_AUTH;
196 bool sessionToken = BMCWEB_SESSION_AUTH;
197 bool xtoken = BMCWEB_XTOKEN_AUTH;
198 bool cookie = BMCWEB_COOKIE_AUTH;
199 bool tls = BMCWEB_MUTUAL_TLS_AUTH;
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100200
Ed Tanous3281bcf2024-06-25 16:02:05 -0700201 // Whether or not unauthenticated TLS should be accepted
202 // true = reject connections if mutual tls is not provided
203 // false = allow connection, and allow user to use other auth method
204 // Always default to false, because root certificates will not
205 // be provisioned at startup
206 bool tlsStrict = false;
207
Ed Tanous3ce36882024-06-09 10:58:16 -0700208 MTLSCommonNameParseMode mTLSCommonNameParsingMode =
209 getMTLSCommonNameParseMode(
210 BMCWEB_MUTUAL_TLS_COMMON_NAME_PARSING_DEFAULT);
211
Ed Tanous0bdda662023-08-03 17:27:34 -0700212 void fromJson(const nlohmann::json::object_t& j)
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100213 {
Ed Tanous0bdda662023-08-03 17:27:34 -0700214 for (const auto& element : j)
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100215 {
Ed Tanous0bdda662023-08-03 17:27:34 -0700216 const bool* value = element.second.get_ptr<const bool*>();
Ed Tanous3ce36882024-06-09 10:58:16 -0700217 if (value != nullptr)
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100218 {
Ed Tanous3ce36882024-06-09 10:58:16 -0700219 if (element.first == "XToken")
220 {
221 xtoken = *value;
222 }
223 else if (element.first == "Cookie")
224 {
225 cookie = *value;
226 }
227 else if (element.first == "SessionToken")
228 {
229 sessionToken = *value;
230 }
231 else if (element.first == "BasicAuth")
232 {
233 basic = *value;
234 }
235 else if (element.first == "TLS")
236 {
237 tls = *value;
238 }
Ed Tanous3281bcf2024-06-25 16:02:05 -0700239 else if (element.first == "TLSStrict")
240 {
241 tlsStrict = *value;
242 }
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100243 }
Ed Tanous3ce36882024-06-09 10:58:16 -0700244 const uint64_t* intValue =
245 element.second.get_ptr<const uint64_t*>();
246 if (intValue != nullptr)
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100247 {
Ed Tanous3ce36882024-06-09 10:58:16 -0700248 if (element.first == "MTLSCommonNameParseMode")
249 {
Malik Akbar Hashemi Rafsanjani4d7b5dd2025-02-26 13:14:30 -0800250 MTLSCommonNameParseMode tmpMTLSCommonNameParseMode =
251 static_cast<MTLSCommonNameParseMode>(*intValue);
252 if (tmpMTLSCommonNameParseMode <=
253 MTLSCommonNameParseMode::UserPrincipalName ||
254 tmpMTLSCommonNameParseMode ==
255 MTLSCommonNameParseMode::Meta)
Ed Tanous3ce36882024-06-09 10:58:16 -0700256 {
Malik Akbar Hashemi Rafsanjani4d7b5dd2025-02-26 13:14:30 -0800257 mTLSCommonNameParsingMode = tmpMTLSCommonNameParseMode;
Ed Tanous3ce36882024-06-09 10:58:16 -0700258 }
259 else
260 {
Malik Akbar Hashemi Rafsanjani4d7b5dd2025-02-26 13:14:30 -0800261 BMCWEB_LOG_WARNING(
Ed Tanous3ce36882024-06-09 10:58:16 -0700262 "Json value of {} was out of range of the enum. Ignoring",
263 *intValue);
264 }
265 }
Zbigniew Kurzynski501f1e52019-10-02 11:22:11 +0200266 }
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100267 }
268 }
269};
270
Ed Tanous1abe55e2018-09-05 08:30:59 -0700271class SessionStore
272{
273 public:
274 std::shared_ptr<UserSession> generateUserSession(
Ed Tanous26ccae32023-02-16 10:28:44 -0800275 std::string_view username, const boost::asio::ip::address& clientIp,
Ed Tanous89cda632024-04-16 08:45:54 -0700276 const std::optional<std::string>& clientId, SessionType sessionType,
Sunitha Harishd3239222021-02-24 15:33:29 +0530277 bool isConfigureSelfOnly = false)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700278 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700279 // Only need csrf tokens for cookie based auth, token doesn't matter
Ed Tanousb7f3a822024-06-05 08:45:25 -0700280 std::string sessionToken =
281 bmcweb::getRandomIdOfLength(sessionTokenSize);
282 std::string csrfToken = bmcweb::getRandomIdOfLength(sessionTokenSize);
283 std::string uniqueId = bmcweb::getRandomIdOfLength(10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700284
Ed Tanousb7f3a822024-06-05 08:45:25 -0700285 //
286 if (sessionToken.empty() || csrfToken.empty() || uniqueId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700287 {
Ed Tanousb7f3a822024-06-05 08:45:25 -0700288 BMCWEB_LOG_ERROR("Failed to generate session tokens");
289 return nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700290 }
Jiaqing Zhao41d61c82021-12-07 13:21:47 +0800291
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400292 auto session = std::make_shared<UserSession>(UserSession{
293 uniqueId,
294 sessionToken,
295 std::string(username),
296 csrfToken,
297 clientId,
298 redfish::ip_util::toString(clientIp),
299 std::chrono::steady_clock::now(),
300 sessionType,
301 false,
302 isConfigureSelfOnly,
303 "",
304 {}});
Patrick Williams41713dd2022-09-28 06:48:07 -0500305 auto it = authTokens.emplace(sessionToken, session);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700306 // Only need to write to disk if session isn't about to be destroyed.
Ed Tanous89cda632024-04-16 08:45:54 -0700307 needWrite = sessionType != SessionType::Basic &&
308 sessionType != SessionType::MutualTLS;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700309 return it.first->second;
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100310 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700311
Ed Tanous26ccae32023-02-16 10:28:44 -0800312 std::shared_ptr<UserSession> loginSessionByToken(std::string_view token)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700313 {
314 applySessionTimeouts();
Ed Tanous51dae672018-09-05 16:07:32 -0700315 if (token.size() != sessionTokenSize)
316 {
317 return nullptr;
318 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700319 auto sessionIt = authTokens.find(std::string(token));
320 if (sessionIt == authTokens.end())
321 {
322 return nullptr;
323 }
324 std::shared_ptr<UserSession> userSession = sessionIt->second;
325 userSession->lastUpdated = std::chrono::steady_clock::now();
326 return userSession;
327 }
328
Ed Tanous26ccae32023-02-16 10:28:44 -0800329 std::shared_ptr<UserSession> getSessionByUid(std::string_view uid)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700330 {
331 applySessionTimeouts();
332 // TODO(Ed) this is inefficient
333 auto sessionIt = authTokens.begin();
334 while (sessionIt != authTokens.end())
335 {
336 if (sessionIt->second->uniqueId == uid)
337 {
338 return sessionIt->second;
339 }
340 sessionIt++;
341 }
342 return nullptr;
343 }
344
Ed Tanousb5a76932020-09-29 16:16:58 -0700345 void removeSession(const std::shared_ptr<UserSession>& session)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700346 {
347 authTokens.erase(session->sessionToken);
348 needWrite = true;
349 }
350
Ed Tanous89cda632024-04-16 08:45:54 -0700351 std::vector<std::string> getAllUniqueIds()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700352 {
353 applySessionTimeouts();
Ed Tanous89cda632024-04-16 08:45:54 -0700354 std::vector<std::string> ret;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700355 ret.reserve(authTokens.size());
356 for (auto& session : authTokens)
357 {
Ed Tanous89cda632024-04-16 08:45:54 -0700358 ret.push_back(session.second->uniqueId);
359 }
360 return ret;
361 }
362
363 std::vector<std::string> getUniqueIdsBySessionType(SessionType type)
364 {
365 applySessionTimeouts();
366
367 std::vector<std::string> ret;
368 ret.reserve(authTokens.size());
369 for (auto& session : authTokens)
370 {
371 if (type == session.second->sessionType)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700372 {
Ed Tanous89cda632024-04-16 08:45:54 -0700373 ret.push_back(session.second->uniqueId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700374 }
375 }
376 return ret;
377 }
378
Ed Tanous89cda632024-04-16 08:45:54 -0700379 std::vector<std::shared_ptr<UserSession>> getSessions()
380 {
381 std::vector<std::shared_ptr<UserSession>> sessions;
382 sessions.reserve(authTokens.size());
383 for (auto& session : authTokens)
384 {
385 sessions.push_back(session.second);
386 }
387 return sessions;
388 }
389
Xie Ning9fa06f12022-06-29 18:27:47 +0800390 void removeSessionsByUsername(std::string_view username)
391 {
392 std::erase_if(authTokens, [username](const auto& value) {
393 if (value.second == nullptr)
394 {
395 return false;
396 }
397 return value.second->username == username;
398 });
399 }
400
Ravi Tejae518ef32024-05-16 10:33:08 -0500401 void removeSessionsByUsernameExceptSession(
402 std::string_view username, const std::shared_ptr<UserSession>& session)
403 {
404 std::erase_if(authTokens, [username, session](const auto& value) {
405 if (value.second == nullptr)
406 {
407 return false;
408 }
409
410 return value.second->username == username &&
411 value.second->uniqueId != session->uniqueId;
412 });
413 }
414
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100415 void updateAuthMethodsConfig(const AuthConfigMethods& config)
416 {
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100417 bool isTLSchanged = (authMethodsConfig.tls != config.tls);
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100418 authMethodsConfig = config;
419 needWrite = true;
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100420 if (isTLSchanged)
421 {
422 // recreate socket connections with new settings
Myung Bae92e11bf2025-01-31 09:22:23 -0500423 // NOLINTNEXTLINE(misc-include-cleaner)
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100424 std::raise(SIGHUP);
425 }
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100426 }
427
428 AuthConfigMethods& getAuthMethodsConfig()
429 {
430 return authMethodsConfig;
431 }
432
Ed Tanous9eb808c2022-01-25 10:19:23 -0800433 bool needsWrite() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700434 {
435 return needWrite;
436 }
Ed Tanous271584a2019-07-09 16:24:22 -0700437 int64_t getTimeoutInSeconds() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700438 {
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530439 return std::chrono::seconds(timeoutInSeconds).count();
440 }
441
442 void updateSessionTimeout(std::chrono::seconds newTimeoutInSeconds)
443 {
444 timeoutInSeconds = newTimeoutInSeconds;
445 needWrite = true;
Ed Tanous23a21a12020-07-25 04:45:05 +0000446 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700447
Ed Tanous1abe55e2018-09-05 08:30:59 -0700448 static SessionStore& getInstance()
449 {
450 static SessionStore sessionStore;
451 return sessionStore;
452 }
453
Ed Tanous1abe55e2018-09-05 08:30:59 -0700454 void applySessionTimeouts()
455 {
456 auto timeNow = std::chrono::steady_clock::now();
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530457 if (timeNow - lastTimeoutUpdate > std::chrono::seconds(1))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700458 {
459 lastTimeoutUpdate = timeNow;
460 auto authTokensIt = authTokens.begin();
461 while (authTokensIt != authTokens.end())
462 {
463 if (timeNow - authTokensIt->second->lastUpdated >=
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530464 timeoutInSeconds)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700465 {
466 authTokensIt = authTokens.erase(authTokensIt);
Ratan Gupta07386c62019-12-14 14:06:09 +0530467
Ed Tanous1abe55e2018-09-05 08:30:59 -0700468 needWrite = true;
469 }
470 else
471 {
472 authTokensIt++;
473 }
474 }
475 }
476 }
Gunnar Mills83cf8182020-11-11 15:37:34 -0600477
478 SessionStore(const SessionStore&) = delete;
479 SessionStore& operator=(const SessionStore&) = delete;
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800480 SessionStore(SessionStore&&) = delete;
481 SessionStore& operator=(const SessionStore&&) = delete;
482 ~SessionStore() = default;
Gunnar Mills83cf8182020-11-11 15:37:34 -0600483
484 std::unordered_map<std::string, std::shared_ptr<UserSession>,
Ed Tanous724985f2024-06-05 09:19:06 -0700485 std::hash<std::string>, bmcweb::ConstantTimeCompare>
Gunnar Mills83cf8182020-11-11 15:37:34 -0600486 authTokens;
487
488 std::chrono::time_point<std::chrono::steady_clock> lastTimeoutUpdate;
489 bool needWrite{false};
490 std::chrono::seconds timeoutInSeconds;
491 AuthConfigMethods authMethodsConfig;
492
493 private:
Patrick Williams89492a12023-05-10 07:51:34 -0500494 SessionStore() : timeoutInSeconds(1800) {}
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100495};
496
Ed Tanous1abe55e2018-09-05 08:30:59 -0700497} // namespace persistent_data