smbcloud-cli 0.3.37

smbCloud command line interface.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use {
    crate::{
        account::{
            lib::{authorize_github, store_token},
            signup::{do_signup, signup_with_email, SignupMethod},
        },
        cli::CommandResult,
        client,
        token::is_logged_in::is_logged_in as is_logged_in_async,
        ui::{fail_message, fail_symbol, succeed_message, succeed_symbol},
    },
    anyhow::{anyhow, Result},
    console::style,
    dialoguer::{console::Term, theme::ColorfulTheme, Confirm, Input, Password, Select},
    log::debug,
    reqwest::Client,
    smbcloud_auth::{
        check_email::check_email, login::login,
        resend_email_verification::resend_email_verification as account_resend_email_verification,
        resend_reset_password_instruction::resend_reset_password_instruction as account_resend_reset_password_instruction,
        reset_password::reset_password as account_reset_password,
    },
    smbcloud_model::{
        account::{
            ErrorCode::{
                self as AccountErrorCode, EmailNotFound, EmailUnverified, GithubNotLinked,
                PasswordNotSet,
            },
            GithubInfo, SmbAuthorization, User,
        },
        login::{AccountStatus, LoginArgs},
        signup::{GithubEmail, Provider, SignupGithubParams, SignupUserGithub},
    },
    smbcloud_network::environment::Environment,
    smbcloud_networking::{constants::PATH_LINK_GITHUB_ACCOUNT, smb_base_url_builder},
    smbcloud_utils::email_validation,
    spinners::Spinner,
};

pub async fn process_login(env: Environment, is_logged_in: Option<bool>) -> Result<CommandResult> {
    let should_continue = match is_logged_in {
        Some(is_logged_id) => !is_logged_id,
        None => {
            // Check if logged in
            let logged_in = is_logged_in_async(env).await?;
            !logged_in
        }
    };

    if !should_continue {
        return Ok(CommandResult {
            spinner: Spinner::new(
                spinners::Spinners::SimpleDotsScrolling,
                succeed_message("Loading"),
            ),
            symbol: fail_symbol(),
            msg: fail_message("You are already logged in. Please logout first."),
        });
    }

    let signup_methods = vec![SignupMethod::Email, SignupMethod::GitHub];
    let selection = match Select::with_theme(&ColorfulTheme::default())
        .items(&signup_methods)
        .default(0)
        .interact_on_opt(&Term::stderr())
        .map(|i| signup_methods[i.unwrap()])
    {
        Ok(method) => method,
        Err(_) => {
            let error = anyhow!("No selection made.");
            return Err(error);
        }
    };

    match selection {
        SignupMethod::Email => login_with_email(env).await,
        SignupMethod::GitHub => login_with_github(env).await,
    }
}

// Private functions

async fn login_with_github(env: Environment) -> Result<CommandResult> {
    match authorize_github(&env).await {
        Ok(result) => process_authorization(env, result).await,
        Err(err) => {
            let error = anyhow!("Failed to authorize your GitHub account. {}", err);
            Err(error)
        }
    }
}

async fn process_authorization(env: Environment, auth: SmbAuthorization) -> Result<CommandResult> {
    // What to do if not logged in with GitHub?
    // Check error_code first
    if let Some(error_code) = auth.error_code {
        debug!("{}", error_code);
        match error_code {
            EmailNotFound => return create_new_account(env, auth.user_email, auth.user_info).await,
            EmailUnverified => return send_email_verification(env, auth.user).await,
            PasswordNotSet => {
                // Only for email and password login
                let error = anyhow!("Password not set.");
                return Err(error);
            }
            GithubNotLinked => return connect_github_account(env, auth).await,
        }
    }

    // Logged in with GitHub!
    // Token handling is in the lib.rs account module.
    if let Some(user) = auth.user {
        let spinner = Spinner::new(
            spinners::Spinners::SimpleDotsScrolling,
            style("Logging you in...").green().bold().to_string(),
        );
        // We're logged in with GitHub.
        return Ok(CommandResult {
            spinner,
            symbol: "".to_owned(),
            msg: format!("You are logged in with GitHub as {}.", user.email),
        });
    }

    let error: anyhow::Error = anyhow!("Failed to login with GitHub.");
    Err(error)
}

async fn create_new_account(
    env: Environment,
    user_email: Option<GithubEmail>,
    user_info: Option<GithubInfo>,
) -> Result<CommandResult> {
    let confirm = match Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt("Do you want to create a new account?")
        .interact()
    {
        Ok(confirm) => confirm,
        Err(_) => {
            let error = anyhow!("Invalid input.");
            return Err(error);
        }
    };

    // Create account if user confirms
    if !confirm {
        let spinner = Spinner::new(
            spinners::Spinners::SimpleDotsScrolling,
            style("Logging you in...").green().bold().to_string(),
        );
        return Ok(CommandResult {
            spinner,
            symbol: "".to_owned(),
            msg: "Please accept to link your GitHub account.".to_owned(),
        });
    }

    if let (Some(email), Some(info)) = (user_email, user_info) {
        let params = SignupGithubParams {
            user: SignupUserGithub {
                email: email.email,
                authorizations_attributes: vec![Provider {
                    uid: info.id.to_string(),
                    provider: 0,
                }],
            },
        };

        return do_signup(env, &params).await;
    }

    Err(anyhow!("Shouldn't be here."))
}

async fn send_email_verification(env: Environment, user: Option<User>) -> Result<CommandResult> {
    // Return early if user is null
    if let Some(user) = user {
        let confirm = match Confirm::with_theme(&ColorfulTheme::default())
            .with_prompt("Do you want to send a new verification email?")
            .interact()
        {
            Ok(confirm) => confirm,
            Err(_) => {
                let error = anyhow!("Invalid input.");
                return Err(error);
            }
        };

        // Send verification email if user confirms
        if !confirm {
            let spinner = Spinner::new(
                spinners::Spinners::SimpleDotsScrolling,
                style("Cancel operation.").green().bold().to_string(),
            );
            return Ok(CommandResult {
                spinner,
                symbol: succeed_symbol(),
                msg: succeed_message("Doing nothing."),
            });
        }
        resend_email_verification(env, user).await
    } else {
        let error = anyhow!("Failed to get user.");
        Err(error)
    }
}

async fn resend_email_verification(env: Environment, user: User) -> Result<CommandResult> {
    let spinner = Spinner::new(
        spinners::Spinners::SimpleDotsScrolling,
        style("Sending verification email...")
            .green()
            .bold()
            .to_string(),
    );
    match account_resend_email_verification(env, client(), user.email).await {
        Ok(_) => Ok(CommandResult {
            spinner,
            symbol: succeed_symbol(),
            msg: succeed_message("Verification email sent!"),
        }),
        _ => {
            let error = anyhow!("Failed to send verification email.");
            Err(error)
        }
    }
}

async fn connect_github_account(env: Environment, auth: SmbAuthorization) -> Result<CommandResult> {
    let confirm = match Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt("Do you want to link your GitHub account?")
        .interact()
    {
        Ok(confirm) => confirm,
        Err(_) => {
            let error = anyhow!("Invalid input.");
            return Err(error);
        }
    };

    // Link GitHub account if user confirms
    if !confirm {
        let spinner = Spinner::new(
            spinners::Spinners::SimpleDotsScrolling,
            succeed_message("Cancel operation."),
        );
        return Ok(CommandResult {
            spinner,
            symbol: succeed_symbol(),
            msg: succeed_message("Doing nothing."),
        });
    }

    let spinner = Spinner::new(
        spinners::Spinners::SimpleDotsScrolling,
        succeed_message("Linking your GitHub account..."),
    );

    let response = Client::new()
        .post(build_smb_connect_github_url(env))
        .json(&auth)
        .header("Accept", "application/json")
        .header("Content-Type", "application/x-www-form-urlencoded")
        .send()
        .await?;

    match response.status() {
        reqwest::StatusCode::OK => Ok(CommandResult {
            spinner,
            symbol: succeed_symbol(),
            msg: succeed_message("GitHub account linked!"),
        }),
        _ => {
            let error = anyhow!("Failed to link GitHub account.");
            Err(error)
        }
    }
}

async fn login_with_email(env: Environment) -> Result<CommandResult> {
    println!("Provide your login credentials.");
    let username = match Input::<String>::with_theme(&ColorfulTheme::default())
        .with_prompt("Email")
        .validate_with(|email: &String| email_validation(email))
        .interact()
    {
        Ok(email) => email,
        Err(_) => {
            let error = anyhow!("Invalid email.");
            return Err(error);
        }
    };

    match check_email(env, client(), &username).await {
        Ok(auth) => {
            // Only continue with password input if email is found and confirmed.
            if auth.error_code.is_some() {
                // Check if email is in the database, unconfirmed. Only presents password input if email is found and confirmed.
                let spinner = Spinner::new(
                    spinners::Spinners::SimpleDotsScrolling,
                    succeed_message("Checking email"),
                );
                after_checking_email_step(&env, spinner, auth, Some(username)).await
            } else {
                let password = match Password::with_theme(&ColorfulTheme::default())
                    .with_prompt("Password")
                    .interact()
                {
                    Ok(password) => password,
                    Err(_) => {
                        let error = anyhow!("Invalid password.");
                        return Err(error);
                    }
                };
                do_process_login(env, LoginArgs { username, password }).await
            }
        }
        Err(_) => Err(anyhow!(fail_message(
            "Server error. Please try again later."
        ))),
    }
}

async fn do_process_login(env: Environment, args: LoginArgs) -> Result<CommandResult> {
    let spinner = Spinner::new(
        spinners::Spinners::SimpleDotsScrolling,
        succeed_message("Loading"),
    );
    let account_status = match login(env, client(), args.username, args.password).await {
        Ok(response) => response,
        Err(_) => return Err(anyhow!(fail_message("Check your internet connection."))),
    };

    match account_status {
        AccountStatus::Ready { access_token } => {
            store_token(env, access_token).await?;
            Ok(CommandResult {
                spinner,
                symbol: succeed_symbol(),
                msg: succeed_message("You are logged in!"),
            })
        }
        AccountStatus::NotFound => Err(anyhow!(fail_message("Check your internet connection."))),
        AccountStatus::Incomplete { status } => {
            action_on_account_status(&env, spinner, status, None, None).await
        }
    }
}

async fn after_checking_email_step(
    env: &Environment,
    mut spinner: Spinner,
    result: SmbAuthorization,
    username: Option<String>,
) -> Result<CommandResult> {
    match result.error_code {
        Some(error_code) => {
            debug!("{}", error_code);
            match error_code {
                EmailNotFound => {
                    spinner.stop_and_persist(
                        &succeed_symbol(),
                        succeed_message(
                            "Account not found. Please continue with setting up your account.",
                        ),
                    );
                    signup_with_email(env.to_owned(), username).await
                }
                EmailUnverified => {
                    spinner.stop_and_persist(
                        &succeed_symbol(),
                        succeed_message("Email not verified. Please verify your email."),
                    );
                    send_email_verification(*env, result.user).await
                }
                PasswordNotSet => {
                    spinner.stop_and_persist(
                        &succeed_symbol(),
                        succeed_message("Password not set. Please reset your password."),
                    );
                    send_reset_password(*env, result.user).await
                }
                _ => {
                    spinner.stop_and_persist(&fail_symbol(), fail_message("An error occurred."));
                    Err(anyhow!("Idk what happened."))
                }
            }
        }
        None => Err(anyhow!("Shouldn't be here.")),
    }
}

async fn action_on_account_status(
    env: &Environment,
    mut spinner: Spinner,
    error_code: AccountErrorCode,
    username: Option<String>,
    user: Option<User>,
) -> Result<CommandResult> {
    match error_code {
        EmailNotFound => {
            spinner.stop_and_persist(
                &succeed_symbol(),
                succeed_message("Account not found. Please continue with setting up your account."),
            );
            signup_with_email(env.to_owned(), username).await
        }
        EmailUnverified => {
            spinner.stop_and_persist(
                &succeed_symbol(),
                succeed_message("Email not verified. Please verify your email."),
            );
            send_email_verification(*env, user).await
        }
        PasswordNotSet => {
            spinner.stop_and_persist(
                &succeed_symbol(),
                succeed_message("Password not set. Please reset your password."),
            );
            send_reset_password(*env, user).await
        }
        _ => {
            spinner.stop_and_persist(&fail_symbol(), fail_message("An error occurred."));
            Err(anyhow!("Idk what happened."))
        }
    }
}

async fn send_reset_password(env: Environment, user: Option<User>) -> Result<CommandResult> {
    // Return early if user is null
    if let Some(user) = user {
        let confirm = match Confirm::with_theme(&ColorfulTheme::default())
            .with_prompt("Do you want to reset your password?")
            .interact()
        {
            Ok(confirm) => confirm,
            Err(_) => {
                let error = anyhow!("Invalid input.");
                return Err(error);
            }
        };

        // Send verification email if user confirms
        if !confirm {
            let spinner = Spinner::new(
                spinners::Spinners::SimpleDotsScrolling,
                style("Cancel operation.").green().bold().to_string(),
            );
            return Ok(CommandResult {
                spinner,
                symbol: succeed_symbol(),
                msg: succeed_message("Doing nothing."),
            });
        }
        resend_reset_password_instruction(env, user).await
    } else {
        let error = anyhow!("Failed to get user.");
        Err(error)
    }
}

async fn resend_reset_password_instruction(env: Environment, user: User) -> Result<CommandResult> {
    let mut spinner = Spinner::new(
        spinners::Spinners::SimpleDotsScrolling,
        succeed_message("Sending reset password instruction..."),
    );

    match account_resend_reset_password_instruction(env, client(), user.email).await {
        Ok(_) => {
            spinner.stop_and_persist(
                "",
                "Reset password instruction sent! Please check your email.".to_owned(),
            );
            input_reset_password_token(env).await
        }
        _ => {
            let error = anyhow!("Failed to send reset password instruction.");
            Err(error)
        }
    }
}

async fn input_reset_password_token(env: Environment) -> Result<CommandResult> {
    let token = match Input::<String>::with_theme(&ColorfulTheme::default())
        .with_prompt("Input reset password token")
        .interact()
    {
        Ok(token) => token,
        Err(_) => {
            let error = anyhow!("Invalid token.");
            return Err(error);
        }
    };
    let password = match Password::with_theme(&ColorfulTheme::default())
        .with_prompt("New password.")
        .with_confirmation("Repeat password.", "Error: the passwords don't match.")
        .interact()
    {
        Ok(password) => password,
        Err(_) => {
            let error = anyhow!("Invalid password.");
            return Err(error);
        }
    };

    let spinner = Spinner::new(
        spinners::Spinners::SimpleDotsScrolling,
        style("Resetting password...").green().bold().to_string(),
    );

    match account_reset_password(env, client(), token, password).await {
        Ok(_) => Ok(CommandResult {
            spinner,
            symbol: succeed_symbol(),
            msg: succeed_message("Password reset!"),
        }),
        _ => Err(anyhow!(fail_message("Failed to reset password."))),
    }
}

fn build_smb_connect_github_url(env: Environment) -> String {
    let mut url_builder = smb_base_url_builder(env, client());
    url_builder.add_route(PATH_LINK_GITHUB_ACCOUNT);
    url_builder.build()
}