keycodes. boost stil can't handle dates

This commit is contained in:
Kevin Darby committed 2019-05-30 18:24:24 -04:00
1 parent 7d75d87e6b
commit 8a965d41ff
24 files changed
+747 -132

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
#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 =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAseguRgLD88QdhK0agy8f"
"gz3SXp/hmp377+Q+z3OQqGG5kWyKnQfkAQDFeSv8o1iwSQupN0D1QOXyDnV1jmue"
"rqTitasGeq4AwKLhY89O/VuXc+URAO+HZ7EqVWfb3Nv4EOZRiiVsxmVBkld73Rcy"
"UAS4OXh4jDtE8IzOoXB7HhlMPBIAkueqdyy/v9zamzKrxPzWd+IW/IYEdCrUS8ES"
"mxOkeadSHVeH2nlHVREdktE3wKRd3fZ3OkyGaHnUPxBoUW1Ex57BpwdMGUqLZ3W2"
"92yQa/32dsLocoTri4iiImvQ/0JvfhnY9MDTZnmIgb8bPWSvPblQN0RR9OkRDFT9"
"3QIDAQAB";
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();
}
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)
{
if(datetime::ll_now() < _expiration)
{
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)
&& (tok.date() < _expiration);
}
}
return false;
}
}}}