Move over to upstream c++ style
This patchset moves bmcweb over to the upstream style naming
conventions for variables, classes, and functions, as well as imposes
the latest clang-format file.
This changeset was mostly built automatically by the included
.clang-tidy file, which has the ability to autoformat and auto rename
variables. At some point in the future I would like to see this in
greater use, but for now, we will impose it on bmcweb, and see how it
goes.
Tested: Code still compiles, and appears to run, although other issues
are possible and likely.
Change-Id: If422a2e36df924e897736b3feffa89f411d9dac1
Signed-off-by: Ed Tanous <ed.tanous@intel.com>
diff --git a/include/sessions.hpp b/include/sessions.hpp
index b4e86c7..f7f937d 100644
--- a/include/sessions.hpp
+++ b/include/sessions.hpp
@@ -14,7 +14,7 @@
namespace crow {
-namespace PersistentData {
+namespace persistent_data {
enum class PersistenceType {
TIMEOUT, // User session times out after a predetermined amount of time
@@ -22,11 +22,11 @@
};
struct UserSession {
- std::string unique_id;
- std::string session_token;
+ std::string uniqueId;
+ std::string sessionToken;
std::string username;
- std::string csrf_token;
- std::chrono::time_point<std::chrono::steady_clock> last_updated;
+ std::string csrfToken;
+ std::chrono::time_point<std::chrono::steady_clock> lastUpdated;
PersistenceType persistence;
/**
@@ -44,20 +44,20 @@
const std::string* thisValue =
element.value().get_ptr<const std::string*>();
if (thisValue == nullptr) {
- CROW_LOG_ERROR << "Error reading persistent store. Property "
+ BMCWEB_LOG_ERROR << "Error reading persistent store. Property "
<< element.key() << " was not of type string";
return nullptr;
}
if (element.key() == "unique_id") {
- userSession->unique_id = *thisValue;
+ userSession->uniqueId = *thisValue;
} else if (element.key() == "session_token") {
- userSession->session_token = *thisValue;
+ userSession->sessionToken = *thisValue;
} else if (element.key() == "csrf_token") {
- userSession->csrf_token = *thisValue;
+ userSession->csrfToken = *thisValue;
} else if (element.key() == "username") {
userSession->username = *thisValue;
} else {
- CROW_LOG_ERROR << "Got unexpected property reading persistent file: "
+ BMCWEB_LOG_ERROR << "Got unexpected property reading persistent file: "
<< element.key();
return nullptr;
}
@@ -68,7 +68,7 @@
// of wall clock time and steady timer time, possibly persisting values with
// wall clock time instead of steady timer, but the tradeoffs of all the
// corner cases involved are non-trivial, so this is done temporarily
- userSession->last_updated = std::chrono::steady_clock::now();
+ userSession->lastUpdated = std::chrono::steady_clock::now();
userSession->persistence = PersistenceType::TIMEOUT;
return userSession;
@@ -79,100 +79,99 @@
class SessionStore {
public:
- std::shared_ptr<UserSession> generate_user_session(
+ std::shared_ptr<UserSession> generateUserSession(
const boost::string_view username,
PersistenceType persistence = PersistenceType::TIMEOUT) {
// TODO(ed) find a secure way to not generate session identifiers if
// persistence is set to SINGLE_REQUEST
static constexpr std::array<char, 62> alphanum = {
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
- 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
- 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'b', 'C',
+ 'D', 'E', 'F', 'g', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+ 'Q', 'r', 'S', 'T', 'U', 'v', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
// entropy: 30 characters, 62 possibilities. log2(62^30) = 178 bits of
// entropy. OWASP recommends at least 60
// https://www.owasp.org/index.php/Session_Management_Cheat_Sheet#Session_ID_Entropy
- std::string session_token;
- session_token.resize(20, '0');
+ std::string sessionToken;
+ sessionToken.resize(20, '0');
std::uniform_int_distribution<int> dist(0, alphanum.size() - 1);
- for (int i = 0; i < session_token.size(); ++i) {
- session_token[i] = alphanum[dist(rd)];
+ for (int i = 0; i < sessionToken.size(); ++i) {
+ sessionToken[i] = alphanum[dist(rd)];
}
// Only need csrf tokens for cookie based auth, token doesn't matter
- std::string csrf_token;
- csrf_token.resize(20, '0');
- for (int i = 0; i < csrf_token.size(); ++i) {
- csrf_token[i] = alphanum[dist(rd)];
+ std::string csrfToken;
+ csrfToken.resize(20, '0');
+ for (int i = 0; i < csrfToken.size(); ++i) {
+ csrfToken[i] = alphanum[dist(rd)];
}
- std::string unique_id;
- unique_id.resize(10, '0');
- for (int i = 0; i < unique_id.size(); ++i) {
- unique_id[i] = alphanum[dist(rd)];
+ std::string uniqueId;
+ uniqueId.resize(10, '0');
+ for (int i = 0; i < uniqueId.size(); ++i) {
+ uniqueId[i] = alphanum[dist(rd)];
}
auto session = std::make_shared<UserSession>(
- UserSession{unique_id, session_token, std::string(username), csrf_token,
+ UserSession{uniqueId, sessionToken, std::string(username), csrfToken,
std::chrono::steady_clock::now(), persistence});
- auto it = auth_tokens.emplace(std::make_pair(session_token, session));
+ auto it = authTokens.emplace(std::make_pair(sessionToken, session));
// Only need to write to disk if session isn't about to be destroyed.
- need_write_ = persistence == PersistenceType::TIMEOUT;
+ needWrite = persistence == PersistenceType::TIMEOUT;
return it.first->second;
}
- std::shared_ptr<UserSession> login_session_by_token(
+ std::shared_ptr<UserSession> loginSessionByToken(
const boost::string_view token) {
- apply_session_timeouts();
- auto session_it = auth_tokens.find(std::string(token));
- if (session_it == auth_tokens.end()) {
+ applySessionTimeouts();
+ auto sessionIt = authTokens.find(std::string(token));
+ if (sessionIt == authTokens.end()) {
return nullptr;
}
- std::shared_ptr<UserSession> user_session = session_it->second;
- user_session->last_updated = std::chrono::steady_clock::now();
- return user_session;
+ std::shared_ptr<UserSession> userSession = sessionIt->second;
+ userSession->lastUpdated = std::chrono::steady_clock::now();
+ return userSession;
}
- std::shared_ptr<UserSession> get_session_by_uid(
- const boost::string_view uid) {
- apply_session_timeouts();
+ std::shared_ptr<UserSession> getSessionByUid(const boost::string_view uid) {
+ applySessionTimeouts();
// TODO(Ed) this is inefficient
- auto session_it = auth_tokens.begin();
- while (session_it != auth_tokens.end()) {
- if (session_it->second->unique_id == uid) {
- return session_it->second;
+ auto sessionIt = authTokens.begin();
+ while (sessionIt != authTokens.end()) {
+ if (sessionIt->second->uniqueId == uid) {
+ return sessionIt->second;
}
- session_it++;
+ sessionIt++;
}
return nullptr;
}
- void remove_session(std::shared_ptr<UserSession> session) {
- auth_tokens.erase(session->session_token);
- need_write_ = true;
+ void removeSession(std::shared_ptr<UserSession> session) {
+ authTokens.erase(session->sessionToken);
+ needWrite = true;
}
- std::vector<const std::string*> get_unique_ids(
+ std::vector<const std::string*> getUniqueIds(
bool getAll = true,
const PersistenceType& type = PersistenceType::SINGLE_REQUEST) {
- apply_session_timeouts();
+ applySessionTimeouts();
std::vector<const std::string*> ret;
- ret.reserve(auth_tokens.size());
- for (auto& session : auth_tokens) {
+ ret.reserve(authTokens.size());
+ for (auto& session : authTokens) {
if (getAll || type == session.second->persistence) {
- ret.push_back(&session.second->unique_id);
+ ret.push_back(&session.second->uniqueId);
}
}
return ret;
}
- bool needs_write() { return need_write_; }
- int get_timeout_in_seconds() const {
- return std::chrono::seconds(timeout_in_minutes).count();
+ bool needsWrite() { return needWrite; }
+ int getTimeoutInSeconds() const {
+ return std::chrono::seconds(timeoutInMinutes).count();
};
- // Persistent data middleware needs to be able to serialize our auth_tokens
+ // Persistent data middleware needs to be able to serialize our authTokens
// structure, which is private
friend Middleware;
@@ -185,48 +184,47 @@
SessionStore& operator=(const SessionStore&) = delete;
private:
- SessionStore() : timeout_in_minutes(60) {}
+ SessionStore() : timeoutInMinutes(60) {}
- void apply_session_timeouts() {
- auto time_now = std::chrono::steady_clock::now();
- if (time_now - last_timeout_update > std::chrono::minutes(1)) {
- last_timeout_update = time_now;
- auto auth_tokens_it = auth_tokens.begin();
- while (auth_tokens_it != auth_tokens.end()) {
- if (time_now - auth_tokens_it->second->last_updated >=
- timeout_in_minutes) {
- auth_tokens_it = auth_tokens.erase(auth_tokens_it);
- need_write_ = true;
+ void applySessionTimeouts() {
+ auto timeNow = std::chrono::steady_clock::now();
+ if (timeNow - lastTimeoutUpdate > std::chrono::minutes(1)) {
+ lastTimeoutUpdate = timeNow;
+ auto authTokensIt = authTokens.begin();
+ while (authTokensIt != authTokens.end()) {
+ if (timeNow - authTokensIt->second->lastUpdated >= timeoutInMinutes) {
+ authTokensIt = authTokens.erase(authTokensIt);
+ needWrite = true;
} else {
- auth_tokens_it++;
+ authTokensIt++;
}
}
}
}
- std::chrono::time_point<std::chrono::steady_clock> last_timeout_update;
+ std::chrono::time_point<std::chrono::steady_clock> lastTimeoutUpdate;
boost::container::flat_map<std::string, std::shared_ptr<UserSession>>
- auth_tokens;
+ authTokens;
std::random_device rd;
- bool need_write_{false};
- std::chrono::minutes timeout_in_minutes;
+ bool needWrite{false};
+ std::chrono::minutes timeoutInMinutes;
};
-} // namespace PersistentData
+} // namespace persistent_data
} // namespace crow
// to_json(...) definition for objects of UserSession type
namespace nlohmann {
template <>
-struct adl_serializer<std::shared_ptr<crow::PersistentData::UserSession>> {
+struct adl_serializer<std::shared_ptr<crow::persistent_data::UserSession>> {
static void to_json(
nlohmann::json& j,
- const std::shared_ptr<crow::PersistentData::UserSession>& p) {
+ const std::shared_ptr<crow::persistent_data::UserSession>& p) {
if (p->persistence !=
- crow::PersistentData::PersistenceType::SINGLE_REQUEST) {
- j = nlohmann::json{{"unique_id", p->unique_id},
- {"session_token", p->session_token},
+ crow::persistent_data::PersistenceType::SINGLE_REQUEST) {
+ j = nlohmann::json{{"unique_id", p->uniqueId},
+ {"session_token", p->sessionToken},
{"username", p->username},
- {"csrf_token", p->csrf_token}};
+ {"csrf_token", p->csrfToken}};
}
}
};