use crate::model::Config;
use anyhow::{anyhow, bail, Context, Result};
use serde::{Deserialize, Serialize};
use std::io::Write;
use std::process::{Command, ExitStatus, Stdio};
const USERNAME_KEY: &str = "credential.https://github.com.username";
#[derive(Clone, Debug, Deserialize)]
pub struct Profile {
pub login: String,
pub id: u64,
pub name: Option<String>,
pub email: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Account {
pub label: String,
pub name: String,
pub email: String,
pub github_username: String,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Original {
pub name: Option<String>,
pub email: Option<String>,
pub username: Option<String>,
}
pub fn problem(candidate: &Account, others: &[Account]) -> Option<String> {
if candidate.label.trim().is_empty() {
return Some("label is required".into());
}
if others.iter().any(|a| a.label == candidate.label) {
return Some(format!("label {} is already used", candidate.label));
}
let email = &candidate.email;
if email.trim().is_empty() {
return Some("email is required".into());
}
if email.matches('@').count() != 1 || email.chars().any(char::is_whitespace) {
return Some("email must contain one '@' and no spaces".into());
}
if let Some(other) = others.iter().find(|a| a.email.eq_ignore_ascii_case(email)) {
return Some(format!("email already used by {}", other.label));
}
let username = &candidate.github_username;
if username.is_empty() {
return Some("GitHub username is required".into());
}
if username.len() > 39
|| !username
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-')
{
return Some(
"GitHub username may only use letters, digits or '-' (max 39 characters)".into(),
);
}
if let Some(other) = others
.iter()
.find(|a| a.github_username.eq_ignore_ascii_case(username))
{
return Some(format!("GitHub username already used by {}", other.label));
}
None
}
pub fn activate(config: &mut Config, label: &str) -> Result<()> {
let account = config
.accounts
.iter()
.find(|a| a.label == label)
.cloned()
.ok_or_else(|| anyhow!("no account named {label}"))?;
let fresh = config.original.is_none();
let before = read_global();
if fresh {
config.original = Some(before.clone());
}
let name = if account.name.trim().is_empty() {
account.github_username.as_str()
} else {
account.name.as_str()
};
let applied = git_set("user.name", name)
.and_then(|_| git_set("user.email", &account.email))
.and_then(|_| git_set(USERNAME_KEY, &account.github_username));
if let Err(error) = applied {
restore("user.name", before.name.as_deref()).ok();
restore("user.email", before.email.as_deref()).ok();
restore(USERNAME_KEY, before.username.as_deref()).ok();
if fresh {
config.original = None;
}
return Err(error);
}
config.active_account = Some(label.to_string());
crate::config::save(config)
}
pub fn deactivate(config: &mut Config) -> Result<()> {
if let Some(original) = config.original.clone() {
restore("user.name", original.name.as_deref())?;
restore("user.email", original.email.as_deref())?;
restore(USERNAME_KEY, original.username.as_deref())?;
config.original = None;
}
config.active_account = None;
Ok(())
}
pub fn stored_logins() -> Vec<String> {
Command::new("git")
.args(["credential-manager", "github", "list"])
.output()
.ok()
.filter(|output| output.status.success())
.map(|output| {
String::from_utf8_lossy(&output.stdout)
.lines()
.map(|line| line.trim().to_string())
.filter(|line| !line.is_empty())
.collect()
})
.unwrap_or_default()
}
pub fn login(username: &str) -> Result<()> {
if !browser_login(&["--username", username])?.success() {
bail!("GitHub sign-in for {username} did not finish");
}
Ok(())
}
pub fn sign_in_new(label: &str) -> Result<Account> {
let before = stored_logins();
if !browser_login(&[])?.success() {
bail!("GitHub sign-in did not finish");
}
let username = stored_logins()
.into_iter()
.find(|user| !before.contains(user))
.ok_or_else(|| {
anyhow!("Sign-in finished, but no new GitHub account was found. If this account was already added, press L on it instead.")
})?;
Ok(account_from(label, &fetch_profile(&username)?))
}
pub fn verify_login(username: &str) -> Result<()> {
fetch_profile(username).map(|_| ())
}
pub fn refresh(account: &mut Account) -> Result<()> {
let fresh = account_from(&account.label, &fetch_profile(&account.github_username)?);
account.name = fresh.name;
account.email = fresh.email;
Ok(())
}
pub fn fetch_profile(username: &str) -> Result<Profile> {
let token = token_for(username)?;
ureq::get("https://api.github.com/user")
.set("Authorization", &format!("Bearer {token}"))
.set("User-Agent", "gct")
.set("Accept", "application/vnd.github+json")
.set("X-GitHub-Api-Version", "2022-11-28")
.call()
.context("GitHub profile request failed")?
.into_string()
.context("GitHub profile could not be read")
.and_then(|body| {
let profile: Profile =
serde_json::from_str(&body).context("GitHub returned an unexpected profile")?;
if !profile.login.eq_ignore_ascii_case(username) {
bail!(
"GitHub returned {} but {username} was requested; sign in again with the right account",
profile.login
);
}
Ok(profile)
})
}
fn account_from(label: &str, profile: &Profile) -> Account {
let name = profile.name.clone().filter(|name| !name.trim().is_empty());
let email = profile
.email
.clone()
.filter(|email| !email.trim().is_empty());
Account {
label: label.into(),
name: name.unwrap_or_else(|| profile.login.clone()),
email: email.unwrap_or_else(|| {
format!("{}+{}@users.noreply.github.com", profile.id, profile.login)
}),
github_username: profile.login.clone(),
}
}
fn browser_login(extra: &[&str]) -> Result<ExitStatus> {
Command::new("git")
.args(["credential-manager", "github", "login"])
.args(extra)
.arg("--browser")
.status()
.context("could not start Git Credential Manager")
}
fn token_for(username: &str) -> Result<String> {
let mut child = Command::new("git")
.args(["credential", "fill"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.context("could not start git credential")?;
let request = format!("protocol=https\nhost=github.com\nusername={username}\n\n");
child
.stdin
.take()
.context("could not reach git credential")?
.write_all(request.as_bytes())
.context("could not reach git credential")?;
let output = child
.wait_with_output()
.context("could not read git credential")?;
let missing = || anyhow!("no saved GitHub login for {username}");
if !output.status.success() {
return Err(missing());
}
String::from_utf8_lossy(&output.stdout)
.lines()
.find_map(|line| line.strip_prefix("password="))
.map(|token| token.trim().to_string())
.filter(|token| !token.is_empty())
.ok_or_else(missing)
}
fn read_global() -> Original {
Original {
name: git_get("user.name"),
email: git_get("user.email"),
username: git_get(USERNAME_KEY),
}
}
fn git_get(key: &str) -> Option<String> {
let output = Command::new("git")
.args(["config", "--global", "--get", key])
.output()
.ok()?;
if !output.status.success() {
return None;
}
let value = String::from_utf8_lossy(&output.stdout).trim().to_string();
(!value.is_empty()).then_some(value)
}
fn git_set(key: &str, value: &str) -> Result<()> {
let status = Command::new("git")
.args(["config", "--global", key, value])
.status()
.with_context(|| format!("could not set {key}"))?;
if !status.success() {
bail!("could not set {key}");
}
Ok(())
}
fn restore(key: &str, value: Option<&str>) -> Result<()> {
match value {
Some(value) => {
let status = Command::new("git")
.args(["config", "--global", "--replace-all", key, value])
.status()
.with_context(|| format!("could not set {key}"))?;
if !status.success() {
bail!("could not set {key}");
}
Ok(())
}
None => {
let status = Command::new("git")
.args(["config", "--global", "--unset", key])
.status()
.with_context(|| format!("could not unset {key}"))?;
if status.success() || status.code() == Some(5) {
Ok(())
} else {
bail!("could not unset {key}")
}
}
}
}