Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 3 | #include <boost/container/flat_map.hpp> |
| 4 | #include <boost/uuid/uuid.hpp> |
| 5 | #include <boost/uuid/uuid_generators.hpp> |
| 6 | #include <boost/uuid/uuid_io.hpp> |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 7 | #include <csignal> |
Ratan Gupta | 12c04ef | 2019-04-03 10:08:11 +0530 | [diff] [blame] | 8 | #include <dbus_singleton.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 9 | #include <nlohmann/json.hpp> |
| 10 | #include <pam_authenticate.hpp> |
| 11 | #include <random> |
RAJESWARAN THILLAIGOVINDAN | 7052517 | 2019-07-09 13:15:05 -0500 | [diff] [blame] | 12 | #include <sdbusplus/bus/match.hpp> |
Ratan Gupta | 12c04ef | 2019-04-03 10:08:11 +0530 | [diff] [blame] | 13 | |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 14 | #include "logging.h" |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 15 | #include "utility.h" |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 16 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 17 | namespace crow |
| 18 | { |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 19 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | namespace persistent_data |
| 21 | { |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 22 | |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 23 | // entropy: 20 characters, 62 possibilities. log2(62^20) = 119 bits of |
| 24 | // entropy. OWASP recommends at least 64 |
| 25 | // https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#session-id-entropy |
| 26 | constexpr std::size_t sessionTokenSize = 20; |
| 27 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 28 | enum class PersistenceType |
| 29 | { |
| 30 | TIMEOUT, // User session times out after a predetermined amount of time |
| 31 | SINGLE_REQUEST // User times out once this request is completed. |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 32 | }; |
| 33 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | struct UserSession |
| 35 | { |
| 36 | std::string uniqueId; |
| 37 | std::string sessionToken; |
| 38 | std::string username; |
| 39 | std::string csrfToken; |
| 40 | std::chrono::time_point<std::chrono::steady_clock> lastUpdated; |
| 41 | PersistenceType persistence; |
Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 42 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | /** |
| 44 | * @brief Fills object with data from UserSession's JSON representation |
| 45 | * |
| 46 | * This replaces nlohmann's from_json to ensure no-throw approach |
| 47 | * |
| 48 | * @param[in] j JSON object from which data should be loaded |
| 49 | * |
| 50 | * @return a shared pointer if data has been loaded properly, nullptr |
| 51 | * otherwise |
| 52 | */ |
| 53 | static std::shared_ptr<UserSession> fromJson(const nlohmann::json& j) |
| 54 | { |
| 55 | std::shared_ptr<UserSession> userSession = |
| 56 | std::make_shared<UserSession>(); |
| 57 | for (const auto& element : j.items()) |
| 58 | { |
| 59 | const std::string* thisValue = |
| 60 | element.value().get_ptr<const std::string*>(); |
| 61 | if (thisValue == nullptr) |
| 62 | { |
| 63 | BMCWEB_LOG_ERROR << "Error reading persistent store. Property " |
| 64 | << element.key() << " was not of type string"; |
| 65 | return nullptr; |
| 66 | } |
| 67 | if (element.key() == "unique_id") |
| 68 | { |
| 69 | userSession->uniqueId = *thisValue; |
| 70 | } |
| 71 | else if (element.key() == "session_token") |
| 72 | { |
| 73 | userSession->sessionToken = *thisValue; |
| 74 | } |
| 75 | else if (element.key() == "csrf_token") |
| 76 | { |
| 77 | userSession->csrfToken = *thisValue; |
| 78 | } |
| 79 | else if (element.key() == "username") |
| 80 | { |
| 81 | userSession->username = *thisValue; |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | BMCWEB_LOG_ERROR |
| 86 | << "Got unexpected property reading persistent file: " |
| 87 | << element.key(); |
| 88 | return nullptr; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // For now, sessions that were persisted through a reboot get their idle |
| 93 | // timer reset. This could probably be overcome with a better |
| 94 | // understanding of wall clock time and steady timer time, possibly |
| 95 | // persisting values with wall clock time instead of steady timer, but |
| 96 | // the tradeoffs of all the corner cases involved are non-trivial, so |
| 97 | // this is done temporarily |
| 98 | userSession->lastUpdated = std::chrono::steady_clock::now(); |
| 99 | userSession->persistence = PersistenceType::TIMEOUT; |
| 100 | |
| 101 | return userSession; |
Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 102 | } |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 103 | }; |
| 104 | |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 105 | struct AuthConfigMethods |
| 106 | { |
| 107 | bool xtoken = true; |
| 108 | bool cookie = true; |
| 109 | bool sessionToken = true; |
| 110 | bool basic = true; |
Zbigniew Kurzynski | cac94c5 | 2019-11-07 12:55:04 +0100 | [diff] [blame] | 111 | bool tls = false; |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 112 | |
| 113 | void fromJson(const nlohmann::json& j) |
| 114 | { |
| 115 | for (const auto& element : j.items()) |
| 116 | { |
| 117 | const bool* value = element.value().get_ptr<const bool*>(); |
| 118 | if (value == nullptr) |
| 119 | { |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | if (element.key() == "XToken") |
| 124 | { |
| 125 | xtoken = *value; |
| 126 | } |
| 127 | else if (element.key() == "Cookie") |
| 128 | { |
| 129 | cookie = *value; |
| 130 | } |
| 131 | else if (element.key() == "SessionToken") |
| 132 | { |
| 133 | sessionToken = *value; |
| 134 | } |
| 135 | else if (element.key() == "BasicAuth") |
| 136 | { |
| 137 | basic = *value; |
| 138 | } |
Zbigniew Kurzynski | 501f1e5 | 2019-10-02 11:22:11 +0200 | [diff] [blame] | 139 | else if (element.key() == "TLS") |
| 140 | { |
| 141 | tls = *value; |
| 142 | } |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | }; |
| 146 | |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 147 | class Middleware; |
| 148 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 149 | class SessionStore |
| 150 | { |
| 151 | public: |
| 152 | std::shared_ptr<UserSession> generateUserSession( |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 153 | const std::string_view username, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 154 | PersistenceType persistence = PersistenceType::TIMEOUT) |
| 155 | { |
| 156 | // TODO(ed) find a secure way to not generate session identifiers if |
| 157 | // persistence is set to SINGLE_REQUEST |
| 158 | static constexpr std::array<char, 62> alphanum = { |
Joseph Reynolds | 368b1d4 | 2019-08-15 15:29:06 -0500 | [diff] [blame] | 159 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', |
| 160 | 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 161 | '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] | 162 | 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', |
| 163 | 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 164 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 165 | std::string sessionToken; |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 166 | sessionToken.resize(sessionTokenSize, '0'); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 167 | std::uniform_int_distribution<size_t> dist(0, alphanum.size() - 1); |
| 168 | for (size_t i = 0; i < sessionToken.size(); ++i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 169 | { |
| 170 | sessionToken[i] = alphanum[dist(rd)]; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 171 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 172 | // Only need csrf tokens for cookie based auth, token doesn't matter |
| 173 | std::string csrfToken; |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 174 | csrfToken.resize(sessionTokenSize, '0'); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 175 | for (size_t i = 0; i < csrfToken.size(); ++i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 176 | { |
| 177 | csrfToken[i] = alphanum[dist(rd)]; |
| 178 | } |
| 179 | |
| 180 | std::string uniqueId; |
| 181 | uniqueId.resize(10, '0'); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 182 | for (size_t i = 0; i < uniqueId.size(); ++i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 183 | { |
| 184 | uniqueId[i] = alphanum[dist(rd)]; |
| 185 | } |
Ratan Gupta | 12c04ef | 2019-04-03 10:08:11 +0530 | [diff] [blame] | 186 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 187 | auto session = std::make_shared<UserSession>(UserSession{ |
Ed Tanous | ca0c93b | 2019-09-19 11:53:50 -0700 | [diff] [blame] | 188 | uniqueId, sessionToken, std::string(username), csrfToken, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 189 | std::chrono::steady_clock::now(), persistence}); |
| 190 | auto it = authTokens.emplace(std::make_pair(sessionToken, session)); |
| 191 | // Only need to write to disk if session isn't about to be destroyed. |
| 192 | needWrite = persistence == PersistenceType::TIMEOUT; |
| 193 | return it.first->second; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 194 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 195 | |
| 196 | std::shared_ptr<UserSession> |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 197 | loginSessionByToken(const std::string_view token) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 198 | { |
| 199 | applySessionTimeouts(); |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 200 | if (token.size() != sessionTokenSize) |
| 201 | { |
| 202 | return nullptr; |
| 203 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 204 | auto sessionIt = authTokens.find(std::string(token)); |
| 205 | if (sessionIt == authTokens.end()) |
| 206 | { |
| 207 | return nullptr; |
| 208 | } |
| 209 | std::shared_ptr<UserSession> userSession = sessionIt->second; |
| 210 | userSession->lastUpdated = std::chrono::steady_clock::now(); |
| 211 | return userSession; |
| 212 | } |
| 213 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 214 | std::shared_ptr<UserSession> getSessionByUid(const std::string_view uid) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 215 | { |
| 216 | applySessionTimeouts(); |
| 217 | // TODO(Ed) this is inefficient |
| 218 | auto sessionIt = authTokens.begin(); |
| 219 | while (sessionIt != authTokens.end()) |
| 220 | { |
| 221 | if (sessionIt->second->uniqueId == uid) |
| 222 | { |
| 223 | return sessionIt->second; |
| 224 | } |
| 225 | sessionIt++; |
| 226 | } |
| 227 | return nullptr; |
| 228 | } |
| 229 | |
| 230 | void removeSession(std::shared_ptr<UserSession> session) |
| 231 | { |
| 232 | authTokens.erase(session->sessionToken); |
| 233 | needWrite = true; |
| 234 | } |
| 235 | |
| 236 | std::vector<const std::string*> getUniqueIds( |
| 237 | bool getAll = true, |
| 238 | const PersistenceType& type = PersistenceType::SINGLE_REQUEST) |
| 239 | { |
| 240 | applySessionTimeouts(); |
| 241 | |
| 242 | std::vector<const std::string*> ret; |
| 243 | ret.reserve(authTokens.size()); |
| 244 | for (auto& session : authTokens) |
| 245 | { |
| 246 | if (getAll || type == session.second->persistence) |
| 247 | { |
| 248 | ret.push_back(&session.second->uniqueId); |
| 249 | } |
| 250 | } |
| 251 | return ret; |
| 252 | } |
| 253 | |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 254 | void updateAuthMethodsConfig(const AuthConfigMethods& config) |
| 255 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 256 | bool isTLSchanged = (authMethodsConfig.tls != config.tls); |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 257 | authMethodsConfig = config; |
| 258 | needWrite = true; |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 259 | if (isTLSchanged) |
| 260 | { |
| 261 | // recreate socket connections with new settings |
| 262 | std::raise(SIGHUP); |
| 263 | } |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | AuthConfigMethods& getAuthMethodsConfig() |
| 267 | { |
| 268 | return authMethodsConfig; |
| 269 | } |
| 270 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 271 | bool needsWrite() |
| 272 | { |
| 273 | return needWrite; |
| 274 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 275 | int64_t getTimeoutInSeconds() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 276 | { |
| 277 | return std::chrono::seconds(timeoutInMinutes).count(); |
| 278 | }; |
| 279 | |
| 280 | // Persistent data middleware needs to be able to serialize our authTokens |
| 281 | // structure, which is private |
| 282 | friend Middleware; |
| 283 | |
| 284 | static SessionStore& getInstance() |
| 285 | { |
| 286 | static SessionStore sessionStore; |
| 287 | return sessionStore; |
| 288 | } |
| 289 | |
| 290 | SessionStore(const SessionStore&) = delete; |
| 291 | SessionStore& operator=(const SessionStore&) = delete; |
| 292 | |
| 293 | private: |
| 294 | SessionStore() : timeoutInMinutes(60) |
| 295 | { |
| 296 | } |
| 297 | |
| 298 | void applySessionTimeouts() |
| 299 | { |
| 300 | auto timeNow = std::chrono::steady_clock::now(); |
| 301 | if (timeNow - lastTimeoutUpdate > std::chrono::minutes(1)) |
| 302 | { |
| 303 | lastTimeoutUpdate = timeNow; |
| 304 | auto authTokensIt = authTokens.begin(); |
| 305 | while (authTokensIt != authTokens.end()) |
| 306 | { |
| 307 | if (timeNow - authTokensIt->second->lastUpdated >= |
| 308 | timeoutInMinutes) |
| 309 | { |
| 310 | authTokensIt = authTokens.erase(authTokensIt); |
| 311 | needWrite = true; |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | authTokensIt++; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
Ratan Gupta | 12c04ef | 2019-04-03 10:08:11 +0530 | [diff] [blame] | 320 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 321 | std::chrono::time_point<std::chrono::steady_clock> lastTimeoutUpdate; |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 322 | std::unordered_map<std::string, std::shared_ptr<UserSession>, |
| 323 | std::hash<std::string>, |
| 324 | crow::utility::ConstantTimeCompare> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 325 | authTokens; |
| 326 | std::random_device rd; |
| 327 | bool needWrite{false}; |
| 328 | std::chrono::minutes timeoutInMinutes; |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 329 | AuthConfigMethods authMethodsConfig; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 330 | }; |
| 331 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 332 | } // namespace persistent_data |
| 333 | } // namespace crow |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 334 | |
| 335 | // to_json(...) definition for objects of UserSession type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 336 | namespace nlohmann |
| 337 | { |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 338 | template <> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 339 | struct adl_serializer<std::shared_ptr<crow::persistent_data::UserSession>> |
| 340 | { |
| 341 | static void |
| 342 | to_json(nlohmann::json& j, |
| 343 | const std::shared_ptr<crow::persistent_data::UserSession>& p) |
| 344 | { |
| 345 | if (p->persistence != |
| 346 | crow::persistent_data::PersistenceType::SINGLE_REQUEST) |
| 347 | { |
| 348 | j = nlohmann::json{{"unique_id", p->uniqueId}, |
| 349 | {"session_token", p->sessionToken}, |
| 350 | {"username", p->username}, |
| 351 | {"csrf_token", p->csrfToken}}; |
| 352 | } |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 353 | } |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 354 | }; |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 355 | |
| 356 | template <> struct adl_serializer<crow::persistent_data::AuthConfigMethods> |
| 357 | { |
| 358 | static void to_json(nlohmann::json& j, |
| 359 | const crow::persistent_data::AuthConfigMethods& c) |
| 360 | { |
| 361 | j = nlohmann::json{{"XToken", c.xtoken}, |
| 362 | {"Cookie", c.cookie}, |
| 363 | {"SessionToken", c.sessionToken}, |
Zbigniew Kurzynski | 501f1e5 | 2019-10-02 11:22:11 +0200 | [diff] [blame] | 364 | {"BasicAuth", c.basic}, |
| 365 | {"TLS", c.tls}}; |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 366 | } |
| 367 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 368 | } // namespace nlohmann |