Skip to content

Commit d689bfa

Browse files
author
Florian Friedrich
committed
Add basic 2FA (code from pcloudcom/console-client#163)
1 parent 8737bec commit d689bfa

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

main.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,30 @@ int main(int argc, char **argv) {
4545
std::cout << "pCloud console client (" << version << ")" << std::endl;
4646
std::string username = "";
4747
std::string password = "";
48+
std::string tfa_code = "";
4849
bool daemon = false;
4950
bool commands = false;
5051
bool commands_only = false;
5152
bool newuser = false;
5253
bool passwordsw = false;
5354
bool save_pass = false;
5455
bool crypto = false;
56+
bool trusted_device = false;
5557
po::variables_map vm;
5658

5759
try {
5860
po::options_description desc("Allowed options");
5961
desc.add_options()("help,h", "Show this help message.")(
6062
"username,u", po::value<std::string>(&username),
61-
"pCloud account name.")("password,p", po::bool_switch(&passwordsw),
62-
"Ask for pCloud account password.")(
63-
"crypto,c", po::bool_switch(&crypto), "Ask for crypto password.")(
63+
"pCloud account name.")(
64+
"password,p", po::bool_switch(&passwordsw),
65+
"Ask for pCloud account password.")(
66+
"tfa_code,t", po::value<std::string>(&tfa_code),
67+
"pCloud tfa code")(
68+
"trusted_device,r", po::bool_switch(&trusted_device),
69+
"Trust this device.")(
70+
"crypto,c", po::bool_switch(&crypto),
71+
"Ask for crypto password.")(
6472
"passascrypto,y", po::value<std::string>(),
6573
"User password is the same as crypto password.")(
6674
"daemonize,d", po::bool_switch(&daemon),
@@ -113,6 +121,8 @@ int main(int argc, char **argv) {
113121
if (passwordsw) {
114122
cc::clibrary::pclsync_lib::get_lib().get_pass_from_console();
115123
}
124+
cc::clibrary::pclsync_lib::get_lib().set_tfa_code(tfa_code);
125+
cc::clibrary::pclsync_lib::get_lib().set_trusted_device(trusted_device);
116126
if (crypto) {
117127
cc::clibrary::pclsync_lib::get_lib().setup_crypto_ = true;
118128
if (vm.count("passascrypto")) {

pclsync_lib.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ clib::pclsync_lib::pclsync_lib()
5454

5555
clib::pclsync_lib::~pclsync_lib() {}
5656

57+
const bool clib::pclsync_lib::get_trusted_device() { return trusted_device_; };
58+
59+
const std::string &clib::pclsync_lib::get_tfa_code() { return tfa_code_; }
60+
5761
const std::string &clib::pclsync_lib::get_username() { return username_; }
5862

5963
const std::string &clib::pclsync_lib::get_password() { return password_; }
@@ -64,6 +68,12 @@ const std::string &clib::pclsync_lib::get_crypto_pass() {
6468

6569
const std::string &clib::pclsync_lib::get_mount() { return mount_; }
6670

71+
void clib::pclsync_lib::set_trusted_device(bool arg) {
72+
trusted_device_ = arg;
73+
}
74+
void clib::pclsync_lib::set_tfa_code(const std::string &arg) {
75+
tfa_code_ = arg;
76+
}
6777
void clib::pclsync_lib::set_username(const std::string &arg) {
6878
username_ = arg;
6979
}
@@ -93,6 +103,18 @@ void clib::pclsync_lib::get_pass_from_console() {
93103
do_get_pass_from_console(password_);
94104
}
95105

106+
void clib::pclsync_lib::get_tfa_code_from_console()
107+
{
108+
if (daemon_) {
109+
std::cout << "Not able to read 2fa code when started as daemon."
110+
<< std::endl;
111+
exit(1);
112+
}
113+
std::cout << "Please enter 2fa code"
114+
<< std::endl;
115+
getline(std::cin, tfa_code_);
116+
}
117+
96118
void clib::pclsync_lib::get_cryptopass_from_console() {
97119
do_get_pass_from_console(crypto_pass_);
98120
}
@@ -233,6 +255,10 @@ static const char *status2string(uint32_t status) {
233255
return "USER_MISMATCH";
234256
case PSTATUS_ACCOUT_EXPIRED:
235257
return "ACCOUT_EXPIRED";
258+
case PSTATUS_TFA_REQUIRED:
259+
return "TFA_REQUIRED";
260+
case PSTATUS_BAD_TFA_CODE:
261+
return "BAD_TFA_CODE";
236262
default:
237263
return "Unrecognized status";
238264
}
@@ -256,6 +282,14 @@ static void status_change(pstatus_t *status) {
256282
clib::pclsync_lib::get_lib().get_password().c_str(),
257283
(int)clib::pclsync_lib::get_lib().save_pass_);
258284
std::cout << "logging in" << std::endl;
285+
} else if (status->status == PSTATUS_TFA_REQUIRED) {
286+
if (clib::pclsync_lib::get_lib().get_tfa_code().empty()) {
287+
clib::pclsync_lib::get_lib().get_tfa_code_from_console();
288+
}
289+
290+
psync_tfa_set_code(clib::pclsync_lib::get_lib().get_tfa_code().c_str(),
291+
clib::pclsync_lib::get_lib().get_trusted_device(),
292+
0);
259293
} else if (status->status == PSTATUS_BAD_LOGIN_DATA) {
260294
if (!clib::pclsync_lib::get_lib().newuser_) {
261295
clib::pclsync_lib::get_lib().get_pass_from_console();

pclsync_lib.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class pclsync_lib {
4646
~pclsync_lib();
4747
pclsync_lib();
4848

49+
bool trusted_device_;
4950
bool crypto_on_;
5051
bool save_pass_;
5152
bool setup_crypto_;
@@ -55,12 +56,16 @@ class pclsync_lib {
5556
bool was_init_;
5657

5758
// Getters
59+
const bool get_trusted_device();
60+
const std::string &get_tfa_code();
5861
const std::string &get_username();
5962
const std::string &get_password();
6063
const std::string &get_crypto_pass();
6164
const std::string &get_mount();
6265

6366
// Setters
67+
void set_trusted_device(bool arg);
68+
void set_tfa_code(const std::string& arg);
6469
void set_username(const std::string &arg);
6570
void set_password(const std::string &arg);
6671
void set_crypto_pass(const std::string &arg);
@@ -75,6 +80,7 @@ class pclsync_lib {
7580
static pclsync_lib &get_lib();
7681

7782
// Console
83+
void get_tfa_code_from_console();
7884
void get_pass_from_console();
7985
void get_cryptopass_from_console();
8086

@@ -97,6 +103,7 @@ class pclsync_lib {
97103
private:
98104
std::string username_;
99105
std::string password_;
106+
std::string tfa_code_;
100107
std::string crypto_pass_;
101108
std::string mount_;
102109

0 commit comments

Comments
 (0)