92 lines
2.8 KiB
C++
92 lines
2.8 KiB
C++
#ifndef _WIN32
|
|
#include "AuthSessionManager.h"
|
|
|
|
#include "lib/utils/datetime.h"
|
|
#include "lib/utils/crypto/KeyCodeHelper.h"
|
|
#include "lib/utils/crypto/CryptoInit.h"
|
|
#include "lib/utils/crypto/PrivateKey.h"
|
|
#include "lib/utils/crypto/PublicKey.h"
|
|
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/utils/helper_macros.h"
|
|
#include "lib/config/config.h"
|
|
#include "lib/config/protos/keycode.pb.h"
|
|
|
|
#include <google/protobuf/text_format.h>
|
|
#include <google/protobuf/util/message_differencer.h>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
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()
|
|
{
|
|
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();
|
|
|
|
auto current = datetime::ll_diff_in_us(datetime::ll_now(), _expiration) > 0;
|
|
if(!current)
|
|
LOG(Gen,Error, "Your BTS Keycode is expired");
|
|
bts_assert_msg(current, "Your BTS Keycode is EXPIRED");
|
|
}
|
|
|
|
|
|
|
|
bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok)
|
|
{
|
|
if(tok.wheel())
|
|
return true;
|
|
auto it = _map.find(tok.id());
|
|
if(it != _map.end())
|
|
{
|
|
_map[tok.id()] = Session{tok};
|
|
return true;
|
|
}
|
|
else if(_map.size() < _nusers)
|
|
{
|
|
_map[tok.id()] = Session{tok};
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool AuthSessionManager::check_session(const api::user::TokenPayload& tok)
|
|
{
|
|
auto now = datetime::ll_now();
|
|
if(datetime::ll_diff_in_us(now, _expiration) > 0)
|
|
{
|
|
if(tok.wheel())
|
|
return true;
|
|
auto it = _map.find(tok.id());
|
|
if(it != _map.end())
|
|
{
|
|
//return google::protobuf::util::MessageDifferencer::Equivalent(tok, it->second.token) //this is probably slow
|
|
return tok.date() == it->second.token.date() && //ids are equal
|
|
datetime::ll_diff_in_us(tok.date(), _expiration) > 0;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}}}
|
|
#endif |