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

fix credential.helper usage #1091

Merged
merged 8 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- support adding annotations to tags ([#747](https://github.com/extrawurst/gitui/issues/747))
- support inspecting annotation of tag ([#1076](https://github.com/extrawurst/gitui/issues/1076))
- support deleting tag on remote ([#1074](https://github.com/extrawurst/gitui/issues/1074))
- support git credentials helper (store) ([#800](https://github.com/extrawurst/gitui/issues/800))

### Fixed
- Keep commit message when pre-commit hook fails ([#1035](https://github.com/extrawurst/gitui/issues/1035))
Expand Down
73 changes: 73 additions & 0 deletions asyncgit/src/sync/cred.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//! credentials git helper

use std::process::Command;

use super::{
remotes::get_default_remote_in_repo, repository::repo, RepoPath,
};
use crate::error::{Error, Result};
use git2::{Config, CredentialHelper};
use scopetime::scope_time;

/// basic Authentication Credentials
#[derive(Debug, Clone, Default, PartialEq)]
Expand Down Expand Up @@ -55,13 +58,20 @@ pub fn extract_username_password(
.to_owned();
let mut helper = CredentialHelper::new(&url);

if use_credential_store(&repo.config()?) {
if let Some(cred) = git_credential_fill(&url) {
return Ok(cred);
}
}

//TODO: look at Cred::credential_helper,
//if the username is in the url we need to set it here,
//I dont think `config` will pick it up

if let Ok(config) = Config::open_default() {
helper.config(&config);
}

Ok(match helper.execute() {
Some((username, password)) => {
BasicAuthCredential::new(Some(username), Some(password))
Expand All @@ -70,6 +80,69 @@ pub fn extract_username_password(
})
}

fn use_credential_store(config: &Config) -> bool {
config
.get_entry("credential.helper")
.ok()
.as_ref()
.and_then(git2::ConfigEntry::value)
.map(|val| val == "store")
.unwrap_or_default()
}

// tries calling:
// printf "protocol=https\nhost=github.com\n" | git credential fill
// see https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage
// TODO: use input stream
fn git_credential_fill(url: &str) -> Option<BasicAuthCredential> {
scope_time!("git_credential_fill");

let url = url::Url::parse(url).ok()?;

let host = url.domain()?;
let protocol = url.scheme();

let cmd = format!("protocol={}\nhost={}\n", protocol, host);
let cmd =
format!("printf \"{}\" | git credential-store get", cmd);
extrawurst marked this conversation as resolved.
Show resolved Hide resolved

let bash_args = vec!["-c".to_string(), cmd];

let res = Command::new("bash").args(bash_args).output();
log::debug!("out: {:?}", res);
let res = res.ok()?;
let output = String::from_utf8_lossy(res.stdout.as_slice());

let mut res = BasicAuthCredential::default();
for line in output.lines() {
if let Some(tuple) = split_once(line, "=") {
if tuple.0 == "username" {
res.username = Some(tuple.1.to_string());
} else if tuple.0 == "password" {
res.password = Some(tuple.1.to_string());
}
}
}

if res.username.is_some() && res.password.is_some() {
return Some(res);
}

None
}

fn split_once<'a>(
v: &'a str,
splitter: &str,
) -> Option<(&'a str, &'a str)> {
let mut split = v.split(splitter);

let key = split.next();
let val = split.next();

key.zip(val)
}

/// extract credentials from url
pub fn extract_cred_from_url(url: &str) -> BasicAuthCredential {
if let Ok(url) = url::Url::parse(url) {
Expand Down