summaryrefslogtreecommitdiffstats
path: root/src/helpers/cli.rs
blob: 25c1e03dd767ef4afb3134fbdb65408ea58fbe9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use std::io::{self, BufRead, Write};

use crate::{errors::Result, http_send::HttpSend, registration::Registered, Mastodon};

/// Finishes the authentication process for the given `Registered` object,
/// using the command-line
pub fn authenticate<H: HttpSend>(registration: Registered<H>) -> Result<Mastodon<H>> {
    let url = registration.authorize_url()?;

    let stdout = io::stdout();
    let stdin = io::stdin();

    let mut stdout = stdout.lock();
    let mut stdin = stdin.lock();

    writeln!(&mut stdout, "Click this link to authorize: {}", url)?;
    write!(&mut stdout, "Paste the returned authorization code: ")?;
    stdout.flush()?;

    let mut input = String::new();
    stdin.read_line(&mut input)?;
    let code = input.trim();
    Ok(registration.complete(code)?)
}