Files
spark/src/lib/utils/AuthSessionManager.cpp
T

156 lines
5.1 KiB
C++

#ifndef _WIN32
#include "AuthSessionManager.h"
#include "lib/utils/FileUtils.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/keycode.pb.h"
#include "lib/config/protos/ports.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 {
AuthSessionManager::AuthSessionManager()
: _map(), _nusers(), _expiration(), _concurrent(false) {
LOG(Gen, Info,
"AuthSessionManager::cons _nusers: {} _expiration: {} _concurrent: {}",
_nusers, _expiration, _concurrent);
// const auto &ekc = config::get<config::EncryptedKeyCode>();
// auto pub = bts::crypto::PublicKey::read(config::get<config::PublicKey>());
// 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.port().empty()) {
const auto &ports = config::get<config::Ports>();
portgood = std::to_string(ports.config_port()) == kc.port();
}
bool ipgood = true;
if (!kc.ip().empty()) {
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;
return true;
}
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;
return true;
}
void AuthSessionManager::clear_sessions() { _map.clear(); }
} // namespace auth
} // namespace api
} // namespace bts
#endif