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

144 lines
4.0 KiB
C++

#include "AuthToken.h"
#ifndef _WIN32
#include "lib/utils/base64.hpp"
#include "lib/utils/bts_assert.h"
#include "datetime.h"
#include "base64.hpp"
#include "AuthSessionManager.h"
// cryptopp
#include <cryptopp/osrng.h>//temp
#include <cryptopp/cryptlib.h>
#include <cryptopp/hex.h>
#include <cryptopp/filters.h>
#include <cryptopp/twofish.h>
#include <cryptopp/eax.h>
#include <cryptopp/secblock.h>
#include <map>
namespace bts {
namespace api {
namespace auth {
using namespace CryptoPP;
// chosen by fair dice roll.
// guaranteed to be random.
constexpr byte raw_key[AES::DEFAULT_KEYLENGTH] = { 75, 22, 52, 77, 93, 194, 234, 176, 155, 181, 215, 7, 185, 211, 20, 43 };
const SecByteBlock bts_key(raw_key, AES::DEFAULT_KEYLENGTH);
constexpr byte raw_ivector[16] = { 182, 169, 236, 110, 146, 158, 187, 4, 5, 160, 205, 242, 174, 92, 127, 175 };
const SecByteBlock bts_iv(raw_ivector, sizeof(raw_ivector));
std::string encrypt(const std::string& buffer) {
CFB_Mode<AES>::Encryption enc(bts_key, bts_key.size(), bts_iv);
std::string result;
result.resize(buffer.size());
enc.ProcessData((byte*)result.c_str(), (byte*)buffer.c_str(), buffer.size());
return result;
}
std::string decrypt(const std::string& buffer) {
CFB_Mode<AES>::Decryption dec(bts_key, bts_key.size(), bts_iv);
std::string result;
result.resize(buffer.size());
dec.ProcessData((byte*)result.c_str(), (byte*)buffer.c_str(), buffer.size());
return result;
}
AuthSessionManager& session_manager()
{
static AuthSessionManager mgr;
return mgr;
}
bool requires_auth_session(const admin::BTSUser& user)
{
//if the user can trade in any account or see market data, it needs a session
if(user.has_user_data() &&
user.user_data().has_account_permissions() &&
user.user_data().account_permissions().account_permissions_size())
{
for(const auto& i: user.user_data().account_permissions().account_permissions())
{
if(i.second.has_trading_permission() && i.second.trading_permission().has_permissions())
return true;
}
}
if(user.has_user_data() &&
user.user_data().has_exchange_user_data())
{
if(user.user_data().exchange_user_data().has_cme_data())
{
for(const auto& i: user.user_data().exchange_user_data().cme_data().md_permissions())
{
if(i.enabled())
return true;
}
}
}
return false;
}
AuthToken generate_token(const admin::BTSUser& user, std::string* msg)
{
api::user::TokenPayload payload;
payload.set_date(datetime::ll_now());
payload.set_id(user.id());
if(user.name() == "bts")
payload.set_wheel(true);
if(requires_auth_session(user))
session_manager().add_or_replace_session(payload, user.name(), msg);
auto token = encrypt(payload.SerializeAsString());
bts_assert_d(token.size() < 255-8);
AuthToken result;
result.set_token(token);
return result;
}
AuthToken generate_wheel_token()
{
api::user::TokenPayload payload;
payload.set_date(datetime::ll_now());
payload.set_id(99999);
payload.set_wheel(true);
auto token = encrypt(payload.SerializeAsString());
AuthToken result;
result.set_token(token);
return result;
}
bool decode_token(const api::AuthToken& token, user::TokenPayload* payload)
{
if (token.token().empty())
return false;
std::string temp = decrypt(token.token());
if(payload)
{
return payload->ParseFromString(temp) && session_manager().check_session(*payload);
}
else
{
// validate the token but don't return it
user::TokenPayload payload;
return payload.ParseFromString(temp) && session_manager().check_session(payload);
}
}
#else
namespace bts {
namespace api {
namespace auth {
AuthToken generate_wheel_token() {
return AuthToken();
}
bool decode_token(const api::AuthToken& token, user::TokenPayload* payload) {
return false;
}
#endif
}}}