Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 3 | #pragma once |
| 4 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 5 | #include "bmcweb_config.h" |
| 6 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 7 | #include "logging.hpp" |
Ed Tanous | 2c6ffdb | 2023-06-28 11:28:38 -0700 | [diff] [blame] | 8 | #include "ossl_random.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 9 | #include "utils/ip_utils.hpp" |
Ed Tanous | fc76b8a | 2020-09-28 17:21:52 -0700 | [diff] [blame] | 10 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 11 | #include <boost/asio/ip/address.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 12 | #include <nlohmann/json.hpp> |
Ratan Gupta | 12c04ef | 2019-04-03 10:08:11 +0530 | [diff] [blame] | 13 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 14 | #include <chrono> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 15 | #include <csignal> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 16 | #include <cstddef> |
| 17 | #include <cstdint> |
| 18 | #include <functional> |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 19 | #include <memory> |
Ed Tanous | bb759e3 | 2022-08-02 17:07:54 -0700 | [diff] [blame] | 20 | #include <optional> |
Ed Tanous | b7f3a82 | 2024-06-05 08:45:25 -0700 | [diff] [blame] | 21 | #include <string> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 22 | #include <string_view> |
| 23 | #include <unordered_map> |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 24 | #include <vector> |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 25 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 26 | namespace persistent_data |
| 27 | { |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 28 | |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 29 | // 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 |
| 32 | constexpr std::size_t sessionTokenSize = 20; |
| 33 | |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 34 | enum class SessionType |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 35 | { |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 36 | None, |
| 37 | Basic, |
| 38 | Session, |
| 39 | Cookie, |
| 40 | MutualTLS |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 41 | }; |
| 42 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | struct UserSession |
| 44 | { |
| 45 | std::string uniqueId; |
| 46 | std::string sessionToken; |
| 47 | std::string username; |
| 48 | std::string csrfToken; |
Ed Tanous | bb759e3 | 2022-08-02 17:07:54 -0700 | [diff] [blame] | 49 | std::optional<std::string> clientId; |
Sunitha Harish | 92f6822 | 2020-05-28 05:09:09 -0500 | [diff] [blame] | 50 | std::string clientIp; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 51 | std::chrono::time_point<std::chrono::steady_clock> lastUpdated; |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 52 | SessionType sessionType{SessionType::None}; |
Ed Tanous | 7e9c08e | 2023-06-16 11:29:37 -0700 | [diff] [blame] | 53 | bool cookieAuth = false; |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 54 | bool isConfigureSelfOnly = false; |
Ed Tanous | 47f2934 | 2024-03-19 12:18:06 -0700 | [diff] [blame] | 55 | std::string userRole; |
| 56 | std::vector<std::string> userGroups; |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 57 | |
| 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, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 65 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | /** |
| 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 | */ |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 76 | static std::shared_ptr<UserSession> |
| 77 | fromJson(const nlohmann::json::object_t& j) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 78 | { |
| 79 | std::shared_ptr<UserSession> userSession = |
| 80 | std::make_shared<UserSession>(); |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 81 | for (const auto& element : j) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 82 | { |
| 83 | const std::string* thisValue = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 84 | element.second.get_ptr<const std::string*>(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 85 | if (thisValue == nullptr) |
| 86 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 87 | BMCWEB_LOG_ERROR( |
| 88 | "Error reading persistent store. Property {} was not of type string", |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 89 | element.first); |
Ed Tanous | dc511aa | 2020-10-21 12:33:42 -0700 | [diff] [blame] | 90 | continue; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 91 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 92 | if (element.first == "unique_id") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 93 | { |
| 94 | userSession->uniqueId = *thisValue; |
| 95 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 96 | else if (element.first == "session_token") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 97 | { |
| 98 | userSession->sessionToken = *thisValue; |
| 99 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 100 | else if (element.first == "csrf_token") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 101 | { |
| 102 | userSession->csrfToken = *thisValue; |
| 103 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 104 | else if (element.first == "username") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 105 | { |
| 106 | userSession->username = *thisValue; |
| 107 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 108 | else if (element.first == "client_id") |
Sunitha Harish | 08bdcc7 | 2020-05-12 05:17:57 -0500 | [diff] [blame] | 109 | { |
| 110 | userSession->clientId = *thisValue; |
| 111 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 112 | else if (element.first == "client_ip") |
Sunitha Harish | 92f6822 | 2020-05-28 05:09:09 -0500 | [diff] [blame] | 113 | { |
| 114 | userSession->clientIp = *thisValue; |
| 115 | } |
| 116 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 117 | else |
| 118 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 119 | BMCWEB_LOG_ERROR( |
| 120 | "Got unexpected property reading persistent file: {}", |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 121 | element.first); |
Ed Tanous | dc511aa | 2020-10-21 12:33:42 -0700 | [diff] [blame] | 122 | continue; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
Ed Tanous | dc511aa | 2020-10-21 12:33:42 -0700 | [diff] [blame] | 125 | // 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 Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 132 | BMCWEB_LOG_DEBUG("Session missing required security " |
| 133 | "information, refusing to restore"); |
Ed Tanous | dc511aa | 2020-10-21 12:33:42 -0700 | [diff] [blame] | 134 | return nullptr; |
| 135 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 136 | |
| 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 Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 144 | userSession->sessionType = SessionType::Session; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 145 | |
| 146 | return userSession; |
Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 147 | } |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 148 | }; |
| 149 | |
Ed Tanous | 3ce3688 | 2024-06-09 10:58:16 -0700 | [diff] [blame] | 150 | enum 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 | |
| 167 | inline 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 | { |
| 180 | // Not yet supported |
| 181 | // return MTLSCommonNameParseMode::UserPrincipalName; |
| 182 | } |
| 183 | if constexpr (BMCWEB_META_TLS_COMMON_NAME_PARSING) |
| 184 | { |
| 185 | if (name == "Meta") |
| 186 | { |
| 187 | return MTLSCommonNameParseMode::Meta; |
| 188 | } |
| 189 | } |
| 190 | return MTLSCommonNameParseMode::Invalid; |
| 191 | } |
| 192 | |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 193 | struct AuthConfigMethods |
| 194 | { |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 195 | // Authentication paths |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 196 | bool basic = BMCWEB_BASIC_AUTH; |
| 197 | bool sessionToken = BMCWEB_SESSION_AUTH; |
| 198 | bool xtoken = BMCWEB_XTOKEN_AUTH; |
| 199 | bool cookie = BMCWEB_COOKIE_AUTH; |
| 200 | bool tls = BMCWEB_MUTUAL_TLS_AUTH; |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 201 | |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 202 | // Whether or not unauthenticated TLS should be accepted |
| 203 | // true = reject connections if mutual tls is not provided |
| 204 | // false = allow connection, and allow user to use other auth method |
| 205 | // Always default to false, because root certificates will not |
| 206 | // be provisioned at startup |
| 207 | bool tlsStrict = false; |
| 208 | |
Ed Tanous | 3ce3688 | 2024-06-09 10:58:16 -0700 | [diff] [blame] | 209 | MTLSCommonNameParseMode mTLSCommonNameParsingMode = |
| 210 | getMTLSCommonNameParseMode( |
| 211 | BMCWEB_MUTUAL_TLS_COMMON_NAME_PARSING_DEFAULT); |
| 212 | |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 213 | void fromJson(const nlohmann::json::object_t& j) |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 214 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 215 | for (const auto& element : j) |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 216 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 217 | const bool* value = element.second.get_ptr<const bool*>(); |
Ed Tanous | 3ce3688 | 2024-06-09 10:58:16 -0700 | [diff] [blame] | 218 | if (value != nullptr) |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 219 | { |
Ed Tanous | 3ce3688 | 2024-06-09 10:58:16 -0700 | [diff] [blame] | 220 | if (element.first == "XToken") |
| 221 | { |
| 222 | xtoken = *value; |
| 223 | } |
| 224 | else if (element.first == "Cookie") |
| 225 | { |
| 226 | cookie = *value; |
| 227 | } |
| 228 | else if (element.first == "SessionToken") |
| 229 | { |
| 230 | sessionToken = *value; |
| 231 | } |
| 232 | else if (element.first == "BasicAuth") |
| 233 | { |
| 234 | basic = *value; |
| 235 | } |
| 236 | else if (element.first == "TLS") |
| 237 | { |
| 238 | tls = *value; |
| 239 | } |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 240 | else if (element.first == "TLSStrict") |
| 241 | { |
| 242 | tlsStrict = *value; |
| 243 | } |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 244 | } |
Ed Tanous | 3ce3688 | 2024-06-09 10:58:16 -0700 | [diff] [blame] | 245 | const uint64_t* intValue = |
| 246 | element.second.get_ptr<const uint64_t*>(); |
| 247 | if (intValue != nullptr) |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 248 | { |
Ed Tanous | 3ce3688 | 2024-06-09 10:58:16 -0700 | [diff] [blame] | 249 | if (element.first == "MTLSCommonNameParseMode") |
| 250 | { |
| 251 | if (*intValue <= 2 || *intValue == 100) |
| 252 | { |
| 253 | mTLSCommonNameParsingMode = |
| 254 | static_cast<MTLSCommonNameParseMode>(*intValue); |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | BMCWEB_LOG_ERROR( |
| 259 | "Json value of {} was out of range of the enum. Ignoring", |
| 260 | *intValue); |
| 261 | } |
| 262 | } |
Zbigniew Kurzynski | 501f1e5 | 2019-10-02 11:22:11 +0200 | [diff] [blame] | 263 | } |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | }; |
| 267 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 268 | class SessionStore |
| 269 | { |
| 270 | public: |
| 271 | std::shared_ptr<UserSession> generateUserSession( |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 272 | std::string_view username, const boost::asio::ip::address& clientIp, |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 273 | const std::optional<std::string>& clientId, SessionType sessionType, |
Sunitha Harish | d323922 | 2021-02-24 15:33:29 +0530 | [diff] [blame] | 274 | bool isConfigureSelfOnly = false) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 275 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 276 | // Only need csrf tokens for cookie based auth, token doesn't matter |
Ed Tanous | b7f3a82 | 2024-06-05 08:45:25 -0700 | [diff] [blame] | 277 | std::string sessionToken = |
| 278 | bmcweb::getRandomIdOfLength(sessionTokenSize); |
| 279 | std::string csrfToken = bmcweb::getRandomIdOfLength(sessionTokenSize); |
| 280 | std::string uniqueId = bmcweb::getRandomIdOfLength(10); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 281 | |
Ed Tanous | b7f3a82 | 2024-06-05 08:45:25 -0700 | [diff] [blame] | 282 | // |
| 283 | if (sessionToken.empty() || csrfToken.empty() || uniqueId.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 284 | { |
Ed Tanous | b7f3a82 | 2024-06-05 08:45:25 -0700 | [diff] [blame] | 285 | BMCWEB_LOG_ERROR("Failed to generate session tokens"); |
| 286 | return nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 287 | } |
Jiaqing Zhao | 41d61c8 | 2021-12-07 13:21:47 +0800 | [diff] [blame] | 288 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 289 | auto session = std::make_shared<UserSession>(UserSession{ |
| 290 | uniqueId, |
| 291 | sessionToken, |
| 292 | std::string(username), |
| 293 | csrfToken, |
| 294 | clientId, |
| 295 | redfish::ip_util::toString(clientIp), |
| 296 | std::chrono::steady_clock::now(), |
| 297 | sessionType, |
| 298 | false, |
| 299 | isConfigureSelfOnly, |
| 300 | "", |
| 301 | {}}); |
Patrick Williams | 41713dd | 2022-09-28 06:48:07 -0500 | [diff] [blame] | 302 | auto it = authTokens.emplace(sessionToken, session); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 303 | // Only need to write to disk if session isn't about to be destroyed. |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 304 | needWrite = sessionType != SessionType::Basic && |
| 305 | sessionType != SessionType::MutualTLS; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 306 | return it.first->second; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 307 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 308 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 309 | std::shared_ptr<UserSession> loginSessionByToken(std::string_view token) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 310 | { |
| 311 | applySessionTimeouts(); |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 312 | if (token.size() != sessionTokenSize) |
| 313 | { |
| 314 | return nullptr; |
| 315 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 316 | auto sessionIt = authTokens.find(std::string(token)); |
| 317 | if (sessionIt == authTokens.end()) |
| 318 | { |
| 319 | return nullptr; |
| 320 | } |
| 321 | std::shared_ptr<UserSession> userSession = sessionIt->second; |
| 322 | userSession->lastUpdated = std::chrono::steady_clock::now(); |
| 323 | return userSession; |
| 324 | } |
| 325 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 326 | std::shared_ptr<UserSession> getSessionByUid(std::string_view uid) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 327 | { |
| 328 | applySessionTimeouts(); |
| 329 | // TODO(Ed) this is inefficient |
| 330 | auto sessionIt = authTokens.begin(); |
| 331 | while (sessionIt != authTokens.end()) |
| 332 | { |
| 333 | if (sessionIt->second->uniqueId == uid) |
| 334 | { |
| 335 | return sessionIt->second; |
| 336 | } |
| 337 | sessionIt++; |
| 338 | } |
| 339 | return nullptr; |
| 340 | } |
| 341 | |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 342 | void removeSession(const std::shared_ptr<UserSession>& session) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 343 | { |
| 344 | authTokens.erase(session->sessionToken); |
| 345 | needWrite = true; |
| 346 | } |
| 347 | |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 348 | std::vector<std::string> getAllUniqueIds() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 349 | { |
| 350 | applySessionTimeouts(); |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 351 | std::vector<std::string> ret; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 352 | ret.reserve(authTokens.size()); |
| 353 | for (auto& session : authTokens) |
| 354 | { |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 355 | ret.push_back(session.second->uniqueId); |
| 356 | } |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | std::vector<std::string> getUniqueIdsBySessionType(SessionType type) |
| 361 | { |
| 362 | applySessionTimeouts(); |
| 363 | |
| 364 | std::vector<std::string> ret; |
| 365 | ret.reserve(authTokens.size()); |
| 366 | for (auto& session : authTokens) |
| 367 | { |
| 368 | if (type == session.second->sessionType) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 369 | { |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 370 | ret.push_back(session.second->uniqueId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | return ret; |
| 374 | } |
| 375 | |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 376 | std::vector<std::shared_ptr<UserSession>> getSessions() |
| 377 | { |
| 378 | std::vector<std::shared_ptr<UserSession>> sessions; |
| 379 | sessions.reserve(authTokens.size()); |
| 380 | for (auto& session : authTokens) |
| 381 | { |
| 382 | sessions.push_back(session.second); |
| 383 | } |
| 384 | return sessions; |
| 385 | } |
| 386 | |
Xie Ning | 9fa06f1 | 2022-06-29 18:27:47 +0800 | [diff] [blame] | 387 | void removeSessionsByUsername(std::string_view username) |
| 388 | { |
| 389 | std::erase_if(authTokens, [username](const auto& value) { |
| 390 | if (value.second == nullptr) |
| 391 | { |
| 392 | return false; |
| 393 | } |
| 394 | return value.second->username == username; |
| 395 | }); |
| 396 | } |
| 397 | |
Ravi Teja | e518ef3 | 2024-05-16 10:33:08 -0500 | [diff] [blame] | 398 | void removeSessionsByUsernameExceptSession( |
| 399 | std::string_view username, const std::shared_ptr<UserSession>& session) |
| 400 | { |
| 401 | std::erase_if(authTokens, [username, session](const auto& value) { |
| 402 | if (value.second == nullptr) |
| 403 | { |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | return value.second->username == username && |
| 408 | value.second->uniqueId != session->uniqueId; |
| 409 | }); |
| 410 | } |
| 411 | |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 412 | void updateAuthMethodsConfig(const AuthConfigMethods& config) |
| 413 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 414 | bool isTLSchanged = (authMethodsConfig.tls != config.tls); |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 415 | authMethodsConfig = config; |
| 416 | needWrite = true; |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 417 | if (isTLSchanged) |
| 418 | { |
| 419 | // recreate socket connections with new settings |
Myung Bae | 92e11bf | 2025-01-31 09:22:23 -0500 | [diff] [blame^] | 420 | // NOLINTNEXTLINE(misc-include-cleaner) |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 421 | std::raise(SIGHUP); |
| 422 | } |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | AuthConfigMethods& getAuthMethodsConfig() |
| 426 | { |
| 427 | return authMethodsConfig; |
| 428 | } |
| 429 | |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 430 | bool needsWrite() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 431 | { |
| 432 | return needWrite; |
| 433 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 434 | int64_t getTimeoutInSeconds() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 435 | { |
Manojkiran Eda | f2a4a60 | 2020-08-27 16:04:26 +0530 | [diff] [blame] | 436 | return std::chrono::seconds(timeoutInSeconds).count(); |
| 437 | } |
| 438 | |
| 439 | void updateSessionTimeout(std::chrono::seconds newTimeoutInSeconds) |
| 440 | { |
| 441 | timeoutInSeconds = newTimeoutInSeconds; |
| 442 | needWrite = true; |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 443 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 444 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 445 | static SessionStore& getInstance() |
| 446 | { |
| 447 | static SessionStore sessionStore; |
| 448 | return sessionStore; |
| 449 | } |
| 450 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 451 | void applySessionTimeouts() |
| 452 | { |
| 453 | auto timeNow = std::chrono::steady_clock::now(); |
Manojkiran Eda | f2a4a60 | 2020-08-27 16:04:26 +0530 | [diff] [blame] | 454 | if (timeNow - lastTimeoutUpdate > std::chrono::seconds(1)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 455 | { |
| 456 | lastTimeoutUpdate = timeNow; |
| 457 | auto authTokensIt = authTokens.begin(); |
| 458 | while (authTokensIt != authTokens.end()) |
| 459 | { |
| 460 | if (timeNow - authTokensIt->second->lastUpdated >= |
Manojkiran Eda | f2a4a60 | 2020-08-27 16:04:26 +0530 | [diff] [blame] | 461 | timeoutInSeconds) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 462 | { |
| 463 | authTokensIt = authTokens.erase(authTokensIt); |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 464 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 465 | needWrite = true; |
| 466 | } |
| 467 | else |
| 468 | { |
| 469 | authTokensIt++; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | } |
Gunnar Mills | 83cf818 | 2020-11-11 15:37:34 -0600 | [diff] [blame] | 474 | |
| 475 | SessionStore(const SessionStore&) = delete; |
| 476 | SessionStore& operator=(const SessionStore&) = delete; |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 477 | SessionStore(SessionStore&&) = delete; |
| 478 | SessionStore& operator=(const SessionStore&&) = delete; |
| 479 | ~SessionStore() = default; |
Gunnar Mills | 83cf818 | 2020-11-11 15:37:34 -0600 | [diff] [blame] | 480 | |
| 481 | std::unordered_map<std::string, std::shared_ptr<UserSession>, |
Ed Tanous | 724985f | 2024-06-05 09:19:06 -0700 | [diff] [blame] | 482 | std::hash<std::string>, bmcweb::ConstantTimeCompare> |
Gunnar Mills | 83cf818 | 2020-11-11 15:37:34 -0600 | [diff] [blame] | 483 | authTokens; |
| 484 | |
| 485 | std::chrono::time_point<std::chrono::steady_clock> lastTimeoutUpdate; |
| 486 | bool needWrite{false}; |
| 487 | std::chrono::seconds timeoutInSeconds; |
| 488 | AuthConfigMethods authMethodsConfig; |
| 489 | |
| 490 | private: |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 491 | SessionStore() : timeoutInSeconds(1800) {} |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 492 | }; |
| 493 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 494 | } // namespace persistent_data |