9 releases (breaking)

0.8.0 Sep 27, 2020
0.7.1 Oct 11, 2018
0.7.0 Aug 30, 2018
0.6.0 Aug 30, 2018
0.1.1 May 3, 2018

#576 in Authentication

38 downloads per month
Used in 2 crates

MIT/Apache

28KB
222 lines

github_auth

crates.io version build status downloads docs.rs docs

Authenticate with GitHub from the command line. Caches the authentication token so that future interactions just work.

Usage

extern crate github_auth;

use github_auth::{Authenticator, Scope};

let auth = Authenticator::builder("github_auth main example".into())
  .scope(Scope::PublicRepo)
  .build();

let token = auth.auth().unwrap();
println!("{:?}", token);

let location = auth.location();
println!("Token stored at: {:?}", location);

Example Output

This dialog is only required to generate a valid token. Once a valid token is created, it will no longer be shown.

GitHub username: my_name
GitHub password:
GitHub OTP (optional): 5678

Authenticating with the token

Once you've acquired an access token, you can use it to authenticate. Here's how to authenticate with the reqwest crate.

extern crate github_auth;
extern crate reqwest;

use github_auth::Authenticator;
use reqwest::{
  header::{Authorization, Headers, UserAgent},
  Client,
};

let auth = Authenticator::new("my_example_app");
let token = auth.auth().unwrap();

let mut headers = Headers::new();
headers.set(Authorization(format!("token {}", token.as_str())).to_owned());
headers.set(UserAgent::new("my_app"));

let url = "https://api.github.com/user";
let mut res = client.get(&url).headers(headers).send()?;
println!("{:?}", res.status());

Installation

$ cargo add github_auth

License

MIT OR Apache-2.0

Dependencies

~7–18MB
~246K SLoC