Files
spark/src/lib/utils/AuthSessionManager.cpp
T
John Colvin 884a79e13a refactor: Clean up protobuf files
* Rename files to snake_case (per Google style guide)
  https://developers.google.com/protocol-buffers/docs/style
* Prohibit single line functions in clang-format for proto files
* Run clang-format
2022-02-14 14:01:52 -05:00

151 lines
5.1 KiB
C++

#ifndef _WIN32
#include "AuthSessionManager.h"
#include "lib/utils/crypto/CryptoInit.h"
#include "lib/utils/crypto/KeyCodeHelper.h"
#include "lib/utils/crypto/PrivateKey.h"
#include "lib/utils/crypto/PublicKey.h"
#include "lib/utils/datetime.h"
#include "lib/config/config.h"
#include "lib/config/protos/ports.pb.h"
#include "lib/config/protos/keycode.pb.h"
#include "lib/logging/logging.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/helper_macros.h"
#include <google/protobuf/text_format.h>
#include <google/protobuf/util/message_differencer.h>
#include <sstream>
#include <string>
// ip/port stuff
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <sys/types.h>
namespace bts {
namespace api {
namespace auth {
const std::string AuthSessionManager::PUBLIC_KEY =
"-----BEGIN PUBLIC KEY-----\n"
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAseguRgLD88QdhK0agy8f"
"gz3SXp/hmp377+Q+z3OQqGG5kWyKnQfkAQDFeSv8o1iwSQupN0D1QOXyDnV1jmue"
"rqTitasGeq4AwKLhY89O/VuXc+URAO+HZ7EqVWfb3Nv4EOZRiiVsxmVBkld73Rcy"
"UAS4OXh4jDtE8IzOoXB7HhlMPBIAkueqdyy/v9zamzKrxPzWd+IW/IYEdCrUS8ES"
"mxOkeadSHVeH2nlHVREdktE3wKRd3fZ3OkyGaHnUPxBoUW1Ex57BpwdMGUqLZ3W2"
"92yQa/32dsLocoTri4iiImvQ/0JvfhnY9MDTZnmIgb8bPWSvPblQN0RR9OkRDFT9"
"3QIDAQAB"
"\n-----END PUBLIC KEY-----\n";
AuthSessionManager::AuthSessionManager()
: _map(), _nusers(), _expiration(), _concurrent(false) {
const auto &ekc = config::get<config::EncryptedKeyCode>();
auto pub = bts::crypto::PublicKey::read_string(PUBLIC_KEY);
bts::config::KeyCode kc;
auto valid = bts::crypto::decrypt_keycode(*pub, ekc, &kc);
bts_assert_msg(valid, "Your BTS Keycode is invalid");
_nusers = kc.nusers();
_expiration = kc.expiration();
_concurrent = kc.allow_concurrent();
bts_assert_msg(validate_ip_port(kc), "invalid ip or port in keycode");
}
bool AuthSessionManager::validate_ip_port(const bts::config::KeyCode &kc) {
bool portgood = true;
if (kc.has_port() && kc.port() != "") {
const auto &ports = config::get<config::Ports>();
portgood = std::to_string(ports.config_port()) == kc.port();
}
bool ipgood = true;
if (kc.has_ip() && kc.ip() != "") {
auto ipmap = bts::os::host_ips();
for (const auto &x : ipmap) {
if (x.second == kc.ip()) {
ipgood = true;
break;
} else
ipgood = false;
}
}
return portgood && ipgood;
}
bool AuthSessionManager::add_or_replace_session(
const api::user::TokenPayload &tok,
const std::string &username,
std::string *msg) {
if (tok.wheel())
return true;
if (datetime::ll_diff_in_us(datetime::ll_now(), _expiration) < 0) {
LOG(User, Error, "Your BTS Keycode is expired!");
if (msg)
*msg = "Your BTS Keycode is EXPIRED! Please contact "
"sales@bluetradesys.com";
return false;
}
if (msg) {
if (datetime::ll_diff_in_seconds(tok.date(), _expiration) < 86400) // 1d
*msg = "*** YOUR BTS KEYCODE WILL EXPIRE TOMORROW!!, PLEASE CONTACT "
"sales@bluetradesys.com ***";
else if (
datetime::ll_diff_in_seconds(tok.date(), _expiration) < 259200) // 3d
*msg = "Your BTS Keycode EXPIRES IN LESS THAN 3 DAYS, please contact "
"sales@bluetradesys.com";
else if (
datetime::ll_diff_in_seconds(tok.date(), _expiration) < 432000) // 5d
*msg = "Your BTS Keycode expires in less than one week, please "
"contact sales@bluetradesys.com";
}
auto it = _map.find(tok.id());
if (it != _map.end()) {
_map[tok.id()] = Session{tok};
LOG(User, Warn, "KEYCODE: Replacing session {} user:{} ", tok.id(),
username);
return true;
} else if (_map.size() < _nusers) {
_map[tok.id()] = Session{tok};
LOG(User, Warn, "KEYCODE: Creating session {} user:{}", tok.id(),
username);
return true;
}
if (msg)
*msg = "KEYCODES: Not Enough Logins available for session";
LOG(User, Error,
"KEYCODES: Not Enough Logins available for session {} user:{}", tok.id(),
username);
return false;
}
bool AuthSessionManager::check_session(const api::user::TokenPayload &tok) {
if (tok.wheel())
return true;
if (datetime::ll_diff_in_us(datetime::ll_now(), _expiration) > 0) {
auto it = _map.find(tok.id());
if (it != _map.end()) {
// return google::protobuf::util::MessageDifferencer::Equivalent(tok,
// it->second.token) //this is probably slow
bool not_expired =
datetime::ll_diff_in_us(tok.date(), _expiration) > 0;
bool same_token =
_concurrent || (tok.date() == it->second.token.date());
return not_expired && same_token;
} else // not in map, try to login in case server booted
{
return add_or_replace_session(tok);
}
}
return false;
}
void AuthSessionManager::clear_sessions() { _map.clear(); }
} // namespace auth
} // namespace api
} // namespace bts
#endif