#auth #github #location

github_auth

Authenticate with GitHub from the command line

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

#443 in Authentication

Download history 11/week @ 2023-10-18 24/week @ 2023-10-25 13/week @ 2023-11-01 8/week @ 2023-11-08 6/week @ 2023-11-15 23/week @ 2023-11-22 41/week @ 2023-11-29 3/week @ 2023-12-06 14/week @ 2023-12-13 13/week @ 2023-12-20 13/week @ 2023-12-27 13/week @ 2024-01-03 5/week @ 2024-01-10 30/week @ 2024-01-17 11/week @ 2024-01-24 30/week @ 2024-01-31

76 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

~8–20MB
~268K SLoC