Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add very basic 2fa support #163

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pCloudCC/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ int main(int argc, char **argv) {
std::cout << "pCloud console client v."<< version << std::endl;
std::string username;
std::string password;
std::string tfa_code;
bool daemon = false;
bool commands = false;
bool commands_only = false;
bool newuser = false;
bool passwordsw = false;
bool save_pass = false;
bool crypto = false;

bool trusted_device = false;

try {
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message")
("username,u", po::value<std::string>(&username), "pCloud account name")
("password,p", po::bool_switch(&passwordsw), "Ask pCloud account password")
("tfa_code,t", po::value(&tfa_code), "pCloud tfa code")
("trusted_device,r", po::bool_switch(&trusted_device), "Trust this device.")
("crypto,c", po::bool_switch(&crypto), "Ask crypto password")
("passascrypto,y", po::value<std::string>(), "Use user password as crypto password also.")
("daemonize,d", po::bool_switch(&daemon), "Daemonize the process.")
Expand Down Expand Up @@ -63,7 +67,8 @@ int main(int argc, char **argv) {
return 1;
}
console_client::clibrary::pclsync_lib::get_lib().set_username(username);

console_client::clibrary::pclsync_lib::get_lib().set_tfa_code(tfa_code);
console_client::clibrary::pclsync_lib::get_lib().set_trusted_device(trusted_device);
if (passwordsw) {
console_client::clibrary::pclsync_lib::get_lib().get_pass_from_console();
}
Expand Down
18 changes: 18 additions & 0 deletions pCloudCC/pclsync_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ void clib::pclsync_lib::get_pass_from_console()
do_get_pass_from_console(password_);
}

void clib::pclsync_lib::get_tfa_code_from_console()
{
if (daemon_) {
std::cout << "Not able to read 2fa code when started as daemon." << std::endl;
exit(1);
}
std::cout << "Please enter 2fa code" << std::endl;
getline(std::cin, tfa_code_);
}

void clib::pclsync_lib::get_cryptopass_from_console()
{
do_get_pass_from_console(crypto_pass_);
Expand Down Expand Up @@ -163,6 +173,8 @@ static char const * status2string (uint32_t status){
case PSTATUS_SCANNING: return "SCANNING";
case PSTATUS_USER_MISMATCH: return "USER_MISMATCH";
case PSTATUS_ACCOUT_EXPIRED: return "ACCOUT_EXPIRED";
case PSTATUS_TFA_REQUIRED: return "TFA_REQUIRED";
case PSTATUS_BAD_TFA_CODE: return "BAD_TFA_CODE";
default :return "Unrecognized status";
}
}
Expand All @@ -183,6 +195,12 @@ static void status_change(pstatus_t* status) {
psync_set_user_pass(clib::pclsync_lib::get_lib().get_username().c_str(), clib::pclsync_lib::get_lib().get_password().c_str(), (int) clib::pclsync_lib::get_lib().save_pass_);
std::cout << "logging in" << std::endl;
}
else if (status->status == PSTATUS_TFA_REQUIRED)
{
if (clib::pclsync_lib::get_lib().get_tfa_code().empty())
clib::pclsync_lib::get_lib().get_tfa_code_from_console();
psync_tfa_set_code(clib::pclsync_lib::get_lib().get_tfa_code().c_str(), clib::pclsync_lib::get_lib().get_trusted_device(), 0);
}
else if (status->status==PSTATUS_BAD_LOGIN_DATA){
if (!clib::pclsync_lib::get_lib().newuser_) {
clib::pclsync_lib::get_lib().get_pass_from_console();
Expand Down
7 changes: 7 additions & 0 deletions pCloudCC/pclsync_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ namespace console_client {
//Getters
const std::string& get_username() {return username_;}
const std::string& get_password() {return password_;}
const std::string& get_tfa_code() {return tfa_code_;}
const std::string& get_crypto_pass() {return crypto_pass_;};
const std::string& get_mount() {return mount_;}
const bool get_trusted_device() {return trusted_device_;};
//Setters
void set_trusted_device(bool arg) { trusted_device_ = arg;}
void set_tfa_code(const std::string& arg) { tfa_code_ = arg;}
void set_username(const std::string& arg) { username_ = arg;}
void set_password(const std::string& arg) { password_ = arg;}
void set_crypto_pass(const std::string& arg) { crypto_pass_ = arg;};
Expand All @@ -59,6 +63,7 @@ namespace console_client {
void set_daemon(bool p) {daemon_ = p;}
void set_status_callback(status_callback_t p) {status_callback_ = p;}
//Console
void get_tfa_code_from_console();
void get_pass_from_console();
void get_cryptopass_from_console();
//API calls
Expand All @@ -74,6 +79,7 @@ namespace console_client {
int unlink();
int login(const char* user, const char* pass, int save);

bool trusted_device_;
bool crypto_on_;
bool save_pass_;
bool setup_crypto_;
Expand All @@ -85,6 +91,7 @@ namespace console_client {
private:
std::string username_;
std::string password_;
std::string tfa_code_;
std::string crypto_pass_;
std::string mount_;

Expand Down