Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract GitHub token from file if --github-token or env variable GITHUB_TOKEN is not present #849

Merged
merged 6 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ compact_str = "0.7.0"
dirs = "4.0.0"
file-format = { version = "0.14.0", default-features = false }
fs-lock = { version = "0.1.0", path = "../fs-lock" }
gh-token = "0.1.0"
log = { version = "0.4.17", features = ["std"] }
miette = "5.5.0"
mimalloc = { version = "0.1.34", default-features = false, optional = true }
Expand Down
74 changes: 73 additions & 1 deletion crates/bin/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
env,
ffi::OsString,
fmt,
fmt, fs, iter,
num::{NonZeroU64, ParseIntError},
path::PathBuf,
str::FromStr,
Expand All @@ -14,6 +14,7 @@ use binstalk::{
};
use clap::{error::ErrorKind, CommandFactory, Parser, ValueEnum};
use compact_str::CompactString;
use dirs::home_dir;
use log::LevelFilter;
use semver::VersionReq;
use strum::EnumCount;
Expand Down Expand Up @@ -145,6 +146,14 @@ pub struct Args {
#[clap(help_heading = "Overrides", long, value_delimiter(','))]
pub disable_strategies: Vec<Strategy>,

/// If `--github-token` or environment variable `GITHUB_TOKEN` is not
/// specified, then cargo-binstall will try to extract github token from
/// `$HOME/.git-credentials` or `$HOME/.config/gh/hosts.yml` by default.
///
/// This option can be used to disable that behavior.
#[clap(help_heading = "Overrides", long)]
pub no_discover_github_token: bool,

/// Disable symlinking / versioned updates.
///
/// By default, Binstall will install a binary named `<name>-<version>` in the install path, and
Expand Down Expand Up @@ -452,9 +461,52 @@ You cannot use --{option} and specify multiple packages at the same time. Do one
.exit()
}

if opts.github_token.is_none() && !opts.no_discover_github_token {
if let Some(github_token) = try_extract_from_git_credentials() {
opts.github_token = Some(github_token);
} else if let Ok(github_token) = gh_token::get() {
opts.github_token = Some(github_token.into());
}
}

opts
}

fn try_extract_from_git_credentials() -> Option<CompactString> {
home_dir()
.map(|mut home| {
home.push(".git-credentials");
home
})
.into_iter()
.chain(iter::from_fn(|| {
let home = env::var_os("XDG_CONFIG_HOME")?;
(!home.is_empty()).then(|| {
let mut path = PathBuf::from(home);
path.push("git/credentials");
path
})
}))
.find_map(try_extract_from_git_credentials_from)
}

fn try_extract_from_git_credentials_from(path: PathBuf) -> Option<CompactString> {
fs::read_to_string(path)
.ok()?
.lines()
.find_map(extract_github_token_from_git_credentials_line)
.map(CompactString::from)
}

fn extract_github_token_from_git_credentials_line(line: &str) -> Option<&str> {
let cred = line
.trim()
.strip_prefix("https://")?
.strip_suffix("@github.com")?;

Some(cred.split_once(':')?.1)
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -463,4 +515,24 @@ mod test {
fn verify_cli() {
Args::command().debug_assert()
}

const GIT_CREDENTIALS_TEST_CASES: &[(&str, Option<&str>)] = &[
// Success
("https://NobodyXu:gho_asdc@github.com", Some("gho_asdc")),
(
"https://NobodyXu:gho_asdc12dz@github.com",
Some("gho_asdc12dz"),
),
// Failure
("http://NobodyXu:gho_asdc@github.com", None),
("https://NobodyXu:gho_asdc@gitlab.com", None),
("https://NobodyXugho_asdc@github.com", None),
];

#[test]
fn test_extract_github_token_from_git_credentials_line() {
GIT_CREDENTIALS_TEST_CASES.iter().for_each(|(line, res)| {
assert_eq!(extract_github_token_from_git_credentials_line(line), *res);
})
}
}