From daa4a249419aedd4a323d7f51cdccee5e5ef4c40 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 10 Jan 2020 15:31:07 +0100 Subject: [PATCH] Can set login tokens from command line --- helpers/accounthelper.cpp | 27 +++++++++++++++++++++++---- helpers/accounthelper.h | 4 ++++ main.cpp | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/helpers/accounthelper.cpp b/helpers/accounthelper.cpp index 3d3dce3..4613488 100644 --- a/helpers/accounthelper.cpp +++ b/helpers/accounthelper.cpp @@ -39,20 +39,39 @@ LoginResult AccountHelper::Login(const std::string &email, const std::string &pa AccountHelper::mToken1 = tokens.at("token1").as_string(); AccountHelper::mToken2 = tokens.at("token2").as_string(); + if(!refreshUserID()) + return ERROR; - // Get current user ID - auto response = ApiRequest("user/getCurrentUserID", true).exec(); - if(response.code() != 200) return ERROR; - AccountHelper::mUserID = response.object().at("userID").as_number().to_int32(); return SUCCESS; } +bool AccountHelper::signedIn() +{ + return mToken1.length() > 0 && mToken2.length() > 0 && mUserID > 0; +} + +bool AccountHelper::refreshUserID() +{ + // Get current user ID + auto response = ApiRequest("user/getCurrentUserID", true).exec(); + if(response.code() != 200) return false; + AccountHelper::mUserID = response.object().at("userID").as_number().to_int32(); + + return true; +} + int AccountHelper::userID() { return mUserID; } +void AccountHelper::setLoginTokens(std::vector tokens) +{ + AccountHelper::mToken1 = tokens[0]; + AccountHelper::mToken2 = tokens[1]; +} + vector AccountHelper::loginTokens() { auto tokens = vector(); diff --git a/helpers/accounthelper.h b/helpers/accounthelper.h index 1b7d8a1..db2b2aa 100644 --- a/helpers/accounthelper.h +++ b/helpers/accounthelper.h @@ -25,8 +25,12 @@ public: const std::string &pass); + static bool signedIn(); + + static bool refreshUserID(); static int userID(); + static void setLoginTokens(std::vector tokens); static std::vector loginTokens(); private: diff --git a/main.cpp b/main.cpp index 2520cab..077bef2 100644 --- a/main.cpp +++ b/main.cpp @@ -21,19 +21,50 @@ int main(int argc, char **argv) cout << "Usage: " << endl; cout << "* help : Show this help" << endl; cout << "* getTokens : Get login token ONLY" << endl; + cout << "* useTokens : Input login tokens (do not show login form)" << endl; return 0; } // Get login tokens - if(arg == "getTokens") { + else if(arg == "getTokens") { cout << "Getting only login tokens..." << endl; onlyGetTokens = true; } + // Use existing login tokens + else if(arg == "useTokens") { + if(argc != 4) { + cerr << "Need to specify tokens!" << endl; + return -2; + } + + // Set tokens + vector tokens; + tokens.push_back(argv[2]); + tokens.push_back(argv[3]); + AccountHelper::setLoginTokens(tokens); + + // Validate them + if(!AccountHelper::refreshUserID()) { + cerr << "Could not use login tokens!" << endl; + return -3; + } + + // Start directly main main menu + + return 0; + } + + else { + cerr << "Unrecognized argument!" << endl; + return -1; + } + + } // First, we need to sign in user - if(!LoginScreen().exec(!onlyGetTokens)) { + if(!AccountHelper::signedIn() && !LoginScreen().exec(!onlyGetTokens)) { cerr << "Could not sign in user!" << endl; return -1; }