diff --git a/src/client/spark_api/api/ServiceContext.cs b/src/client/spark_api/api/ServiceContext.cs index 89d53f482..eb30f7726 100644 --- a/src/client/spark_api/api/ServiceContext.cs +++ b/src/client/spark_api/api/ServiceContext.cs @@ -48,7 +48,7 @@ namespace bts.api.core { _token = result.value.Token; user = username; user_id = result.value.UserId; - return new LoginResult(); + return new LoginResult(result.value.Reason, true); } else { return new LoginResult(result.value.Reason); @@ -89,6 +89,7 @@ namespace bts.api.core { public class LoginResult { public LoginResult() { success = true; } public LoginResult(string reason) { success = false; this.reason = reason; } + public LoginResult(string reason, bool success) { this.success = success; this.reason = reason; } public bool success { get; } public string reason { get; } } diff --git a/src/lib/config/defaults/keycode.conf b/src/lib/config/defaults/keycode.conf index 7bd2b5a67..49832fb86 100644 --- a/src/lib/config/defaults/keycode.conf +++ b/src/lib/config/defaults/keycode.conf @@ -1,8 +1,9 @@ #nusers: 1 +#allow concurrent : false #expires: 2019-Sep-30 00:00:00 637053984000000000 -#generation_time: 2019-Jun-06 19:57:36.339686 5248640496990784764 +#generation_time: 2019-Jun-18 18:25:44.333280 5248650809870720704 EncryptedKeyCode { value { - keycode: "JeCTIruFKQ79vsp7EciymRICr+Hl89z53lFT6+KZGkRRFl0+M0NiXwU9xA3pN9wDya1CfLUHIZtJ0vlH3FmJGknD+AzXeRM97teuyjRAEOxiKtoDTU/vVlPTOdsB2vTv4cdkwDn3mKdaz2uxDs8HPNyJGBCJSv30I/BN/IhfuHMNUo7AfNl0tuCduuqu+ZdMyDuKm1cGXKzORMpOWSHH+XgPleL3ms/tndCtk0eMpNS+sxZ7EHaqT6gNbnxwfDGeifDdGxWKHqmO3mPS34+VW/njdre3tXOZMVi90N9dkoAIrToV0wpRk7JPT6BcQL5+Y45bGWqFktWqH3dp2X3doQ==" + keycode: "pgtv/OmievnpmNlgxTYuN801Hbetgu7ZGkHnlGWtK+DBrKSEgKxr5lGvCW1ld/jsztkWhKHDh0iediMDjrPm4+C5qmXlt4JJlAFvKolMN8HVC59m82fECvakcqKq28WSEjf6/DuPJNP1ZwxjcmKouxx1LZ15VlNkECtNrC9pkCnifazbfQZGV3kD08zMEyw/RPmOaVLFFeav8lwI5UpcjPYBIK1rJowXdGSvm1CJViwG5Z2JbdHR39QbKjVx112d5VbVtBq9nO3i0FEKvobjN/Gzcz/5x4TgQtEShKsbmgEN+MBVL3AItQlcFzZvE/05JLmfACUFmk5Eu0a3DlLs3A==" } } \ No newline at end of file diff --git a/src/lib/config/protos/keycode.proto b/src/lib/config/protos/keycode.proto index 3243e1c99..e4301a6df 100644 --- a/src/lib/config/protos/keycode.proto +++ b/src/lib/config/protos/keycode.proto @@ -9,6 +9,7 @@ message KeyCode optional string ip = 40; optional string port = 50; + optional bool allow_concurrent = 60; } message EncryptedKeyCode diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index 05ea6f422..cefcc24ff 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -40,7 +40,7 @@ const std::string AuthSessionManager::PUBLIC_KEY = "3QIDAQAB" "\n-----END PUBLIC KEY-----\n"; -AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() +AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration(), _concurrent(false) { const auto& ekc = config::get(); auto pub = bts::crypto::PublicKey::read_string(PUBLIC_KEY); @@ -50,7 +50,7 @@ AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() _nusers = kc.nusers(); _expiration = kc.expiration(); - + _concurrent = kc.allow_concurrent(); bts_assert_msg(validate_ip_port(kc), "invalid ip or port in keycode"); } @@ -80,15 +80,23 @@ bool AuthSessionManager::validate_ip_port(const bts::config::KeyCode& kc) return portgood && ipgood; } -bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok) +bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok, std::string* msg) { if(tok.wheel()) return true; if(datetime::ll_diff_in_us(datetime::ll_now(), _expiration) < 0) { LOG(Gen,Error, "Your BTS Keycode is expired!"); + *msg = "Your BTS Keycode is EXPIRED! Please contact sales@bluetradesys.com"; return false; - } + } + 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()) { @@ -113,8 +121,9 @@ bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) 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; + 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; } } return false; diff --git a/src/lib/utils/AuthSessionManager.h b/src/lib/utils/AuthSessionManager.h index d353bef6f..08e9d03cd 100644 --- a/src/lib/utils/AuthSessionManager.h +++ b/src/lib/utils/AuthSessionManager.h @@ -19,7 +19,7 @@ class AuthSessionManager public: AuthSessionManager(); - bool add_or_replace_session(const api::user::TokenPayload& tok); + bool add_or_replace_session(const api::user::TokenPayload& tok, std::string* msg = nullptr); bool check_session(const api::user::TokenPayload& tok); private: @@ -30,6 +30,7 @@ private: SessionMap _map; uint32_t _nusers; datetime::LLDateTime _expiration; + bool _concurrent; }; diff --git a/src/lib/utils/AuthToken.cpp b/src/lib/utils/AuthToken.cpp index 5b4be4dbd..e1292b595 100644 --- a/src/lib/utils/AuthToken.cpp +++ b/src/lib/utils/AuthToken.cpp @@ -51,14 +51,14 @@ AuthSessionManager& session_manager() return mgr; } -AuthToken generate_token(const admin::BTSUser& user) +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); - session_manager().add_or_replace_session(payload); + session_manager().add_or_replace_session(payload, msg); auto token = encrypt(payload.SerializeAsString()); bts_assert_d(token.size() < 255-8); AuthToken result; diff --git a/src/lib/utils/AuthToken.h b/src/lib/utils/AuthToken.h index 71b1c4da0..a85605a0b 100644 --- a/src/lib/utils/AuthToken.h +++ b/src/lib/utils/AuthToken.h @@ -7,7 +7,7 @@ namespace bts { namespace api { namespace auth { -AuthToken generate_token(const admin::BTSUser&); +AuthToken generate_token(const admin::BTSUser&, std::string* msg = nullptr); AuthToken generate_wheel_token(); bool decode_token(const api::AuthToken& token, user::TokenPayload* payload); diff --git a/src/lib/utils/datetime.cpp b/src/lib/utils/datetime.cpp index 4e9d52b3d..9fb4db93f 100755 --- a/src/lib/utils/datetime.cpp +++ b/src/lib/utils/datetime.cpp @@ -61,6 +61,17 @@ long long ll_diff_in_us (unsigned long long ll1, unsigned long long ll2) #endif } +long long ll_diff_in_seconds(unsigned long long ll1, unsigned long long ll2) +{ +#ifndef NO_CPP0X + auto p1 = ll_to_ptime (ll1); + auto p2 = ll_to_ptime (ll2); // NOLINT + return (p2 - p1).total_seconds(); +#else + return 0; +#endif +} + long long ptime_diff_in_us (PTime const &t1, PTime const &t2) { diff --git a/src/lib/utils/datetime.h b/src/lib/utils/datetime.h index b9ba82dca..62384ed69 100755 --- a/src/lib/utils/datetime.h +++ b/src/lib/utils/datetime.h @@ -51,6 +51,7 @@ BTS_EXPORT LLDateTime ptime_to_ll (PTime const &t); BTS_EXPORT PTime ll_to_ptime (LLDateTime ll); extern "C" BTS_EXPORT int ll_to_cstr (LLDateTime ll, char*buffer, int buffer_sz); BTS_EXPORT long long ll_diff_in_us (unsigned long long start, unsigned long long stop); +BTS_EXPORT long long ll_diff_in_seconds(unsigned long long start, unsigned long long stop); BTS_EXPORT long long ptime_diff_in_us (PTime const &t1, PTime const &t2); BTS_EXPORT long long ptime_diff_in_ms (PTime const &t1, PTime const &t2); BTS_EXPORT long long ptime_diff_in_secs (PTime const &t1, PTime const &t2); diff --git a/src/server/sparkd/cmds/KeyCode.cpp b/src/server/sparkd/cmds/KeyCode.cpp index baccaa12b..380370659 100644 --- a/src/server/sparkd/cmds/KeyCode.cpp +++ b/src/server/sparkd/cmds/KeyCode.cpp @@ -48,7 +48,7 @@ std::string KeyCode::helpstr () { std::ostringstream oss; oss << "\n" - << "ENCODE -keyfile:[keyfile] -pass:password -users: -expiration:<2020-05-18> [-ip:] [-port:]\n " + << "ENCODE -keyfile:[keyfile] -pass:password -users: -expiration:<2020-05-18> [-ip:] [-port:] [-allow_concurrent]\n " << "DECODE -keyfile:[keyfile] -pass:password -source:[file] \n " << "KEYGEN -keyfile:[keyfile_prefix]\n " << "CHECKIPS \n "; @@ -73,7 +73,7 @@ std::string parse_date (std::string const &date) void get_cmd(const std::vector& args, std::string& keyfile, std::string& pass, std::string& users, std::string& expiration, std::string* source = nullptr, - std::string* ip = nullptr, std::string* port = nullptr) + std::string* ip = nullptr, std::string* port = nullptr, bool* allow_concurrent = nullptr) { for(size_t i = 1; i < args.size(); i++) { @@ -103,7 +103,11 @@ void get_cmd(const std::vector& args, std::string& keyfile, std::st size_t p_pos = args[i].find("-port:"); if(port && p_pos != args[i].npos) - *port = args[i].substr(p_pos+6); + *port = args[i].substr(p_pos+6); + + size_t c_pos = args[i].find("-allow_concurrent"); + if(allow_concurrent && c_pos != args[i].npos) + *allow_concurrent = true; } } @@ -159,7 +163,7 @@ int keygen(const std::vector& args) // } void write_keycode(const std::string& users, const std::string& expiration, - const std::string& ip, const std::string& port, bts::config::KeyCode& kc) + const std::string& ip, const std::string& port, bool concurrent, bts::config::KeyCode& kc) { std::ostringstream out_stream; @@ -174,6 +178,7 @@ void write_keycode(const std::string& users, const std::string& expiration, bts_assert_msg(ip != "127.0.0.1", "you can't generate a keycode for loopback, just leave the parameter blank"); kc.set_ip(ip); kc.set_port(port); + kc.set_allow_concurrent(concurrent); } void eraseAllSubStr(std::string& mainStr, const std::string& toErase) @@ -189,7 +194,7 @@ void eraseAllSubStr(std::string& mainStr, const std::string& toErase) } int encrypt(const std::string& keyfile, const std::string& pass, const std::string& users, - const std::string& expiration, const std::string& ip, const std::string& port) + const std::string& expiration, const std::string& ip, const std::string& port, bool concurrent) { std::string protobuf; @@ -206,7 +211,7 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri PrivateKeyPtr key = PrivateKey::read_file(keyfile, &cache); bts::config::KeyCode kc; - write_keycode(users, expiration, ip, port, kc); + write_keycode(users, expiration, ip, port, concurrent, kc); if(google::protobuf::TextFormat::PrintToString(kc, &kc_string)) { oss << kc_string; @@ -227,7 +232,8 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri google::protobuf::TextFormat::PrintToString(ekc, &pb); std::cout << std::endl << std::endl; std::cout << "#nusers: " << users << std::endl; - std::cout << "#expires: " << datetime::simple_date_time_string(datetime::ll_to_ptime(kc.expiration())) << " " << std::to_string(kc.expiration()) << std::endl; + std::cout << "#allow concurrent : " << (kc.allow_concurrent() ? "true" : "false") << std::endl; + std::cout << "#expires: " << datetime::simple_date_time_string(datetime::ll_to_ptime(kc.expiration())) << " " << std::to_string(kc.expiration()) << std::endl; std::cout << "#generation_time: " << datetime::simple_date_time_string(datetime::ll_to_ptime(kc.generation_time())) << " " << std::to_string(kc.generation_time()) << std::endl; std::cout << "EncryptedKeyCode {" << std::endl; std::cout << " value {" << std::endl; @@ -242,10 +248,10 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri int encode(const std::vector& args) { - std::string keyfile, pass, users, expiration, ip, port; - get_cmd(args, keyfile, pass, users, expiration, nullptr, &ip, &port); + std::string keyfile, pass, users, expiration, ip, port; bool concurrent = false; + get_cmd(args, keyfile, pass, users, expiration, nullptr, &ip, &port, &concurrent); - return encrypt(keyfile, pass, users, expiration, ip, port); + return encrypt(keyfile, pass, users, expiration, ip, port, concurrent); } int decode(const std::vector& args) diff --git a/src/server/sparkd/user/AuthServiceServer.cpp b/src/server/sparkd/user/AuthServiceServer.cpp index 13c05abf7..357a683c9 100644 --- a/src/server/sparkd/user/AuthServiceServer.cpp +++ b/src/server/sparkd/user/AuthServiceServer.cpp @@ -63,7 +63,8 @@ void AuthServiceServer::authenticate_login(const api::user::AuthenticateLoginReq if (admin_data.success()) { for (const auto& user : admin_data.value()->users()) { if (user.name() == req.login() && user.password() == req.password() && user.is_present()) { - resp.mutable_token()->CopyFrom(api::auth::generate_token(user)); + std::string msg; + resp.mutable_token()->CopyFrom(api::auth::generate_token(user, &msg)); api::user::TokenPayload payload; api::auth::decode_token(resp.token(), &payload); @@ -72,7 +73,8 @@ void AuthServiceServer::authenticate_login(const api::user::AuthenticateLoginReq if(!resp.is_valid()) resp.set_reason("User login privledges restricted, please contact your risk manager"); resp.set_user_id(user.id()); - + if(msg != "") + resp.set_reason(msg); respond(resp); return; } diff --git a/testing/manual/keycodes/keycode.txt b/testing/manual/keycodes/keycode.txt index 8c955d293..ffd573a26 100644 --- a/testing/manual/keycodes/keycode.txt +++ b/testing/manual/keycodes/keycode.txt @@ -25,6 +25,7 @@ You should see keycode details printed to screen d)Kill your sparkd +4.5) For extra credit generate a keycode with -allow_concurrent and make sure we don't zero the market data for concurrent logins 5) Test expiration. Generate a keycode in the past: