Add a helper for completing authentication via the command line

master
Paul Woolcock 6 years ago
parent e284894d40
commit 034bd4e6d1
  1. 13
      README.md
  2. 8
      examples/register.rs
  3. 27
      src/helpers/cli.rs
  4. 3
      src/helpers/mod.rs

@ -35,11 +35,11 @@ extern crate elefren;
```rust,no_run ```rust,no_run
extern crate elefren; extern crate elefren;
use std::io;
use std::error::Error; use std::error::Error;
use elefren::prelude::*; use elefren::prelude::*;
use elefren::helpers::toml; // requires `features = ["toml"]` use elefren::helpers::toml; // requires `features = ["toml"]`
use elefren::helpers::cli;
fn main() -> Result<(), Box<Error>> { fn main() -> Result<(), Box<Error>> {
let mastodon = if let Ok(data) = toml::from_file("mastodon-data.toml") { let mastodon = if let Ok(data) = toml::from_file("mastodon-data.toml") {
@ -59,16 +59,7 @@ fn register() -> Result<Mastodon, Box<Error>> {
let registration = Registration::new("https://mastodon.social") let registration = Registration::new("https://mastodon.social")
.client_name("elefren-examples") .client_name("elefren-examples")
.build()?; .build()?;
let url = registration.authorize_url()?; let mastodon = cli::authenticate(registration)?;
println!("Click this link to authorize on Mastodon: {}", url);
println!("Paste the returned authorization code: ");
let mut input = String::new();
io::stdin().read_line(&mut input)?;
let code = input.trim().to_string();
let mastodon = registration.complete(&code)?;
// Save app data for using on the next run. // Save app data for using on the next run.
toml::to_file(&*mastodon, "mastodon-data.toml")?; toml::to_file(&*mastodon, "mastodon-data.toml")?;

@ -6,6 +6,7 @@ pub use self::elefren::prelude::*;
use std::{error::Error, io}; use std::{error::Error, io};
use self::elefren::helpers::cli;
#[cfg(feature = "toml")] #[cfg(feature = "toml")]
use self::elefren::helpers::toml; use self::elefren::helpers::toml;
@ -34,12 +35,7 @@ pub fn register() -> Result<Mastodon, Box<Error>> {
.scopes(Scopes::all()) .scopes(Scopes::all())
.website("https://github.com/pwoolcoc/elefren") .website("https://github.com/pwoolcoc/elefren")
.build()?; .build()?;
let url = registration.authorize_url()?; let mastodon = cli::authenticate(registration)?;
println!("Click this link to authorize on Mastodon: {}", url);
let code = read_line("Paste the returned authorization code: ")?;
let mastodon = registration.complete(&code)?;
// Save app data for using on the next run. // Save app data for using on the next run.
toml::to_file(&*mastodon, "mastodon-data.toml")?; toml::to_file(&*mastodon, "mastodon-data.toml")?;

@ -0,0 +1,27 @@
use std::io::{self, BufRead, Write};
use errors::Result;
use http_send::HttpSend;
use registration::Registered;
use 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)?)
}

@ -21,3 +21,6 @@ pub mod toml;
/// features = ["json"] /// features = ["json"]
/// ``` /// ```
pub mod json; pub mod json;
/// Helpers for working with the command line
pub mod cli;

Loading…
Cancel
Save