From b126e5b7acfce55bd101b06cb5baf7f251cd0fda Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 16 Jan 2025 09:23:05 +0000 Subject: feat!(nostr_url): replace user with ssh_key_file replaces the "user" in the nostr_url format with "ssh_key_file", to support the original intent, which was to allow users to specify different authentication credentials. most git servers always expect the ssh user to be 'git'. the idiumatic way of specifying logging in as a different user is to specify a different ssh key. the idiomatic way of storing non-default ssh keys is in the location `~/.ssh/key_name`. "ssh_key_file" can be specified as `key_name`, for keys in the default location, or as a relative or absolute custom location eg. `/other_keys/.ssh/nym1` or `../.ssh/nym1`. BREAKING CHANGE: in nostr git url nym1@ssh/npub123/identifer, nym1 is now treated as ssh key file location rather than a ssh user. it can be specified as a file within `~/.ssh` eg `~/.ssh/nym1` or a full or relative path. --- src/lib/list.rs | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'src/lib/list.rs') diff --git a/src/lib/list.rs b/src/lib/list.rs index b867858..639140e 100644 --- a/src/lib/list.rs +++ b/src/lib/list.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::{collections::HashMap, path::PathBuf, str::FromStr}; use anyhow::{Result, anyhow}; use auth_git2::GitAuthenticator; @@ -59,10 +59,12 @@ pub fn list_from_remote( .as_str(), )?; - let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?; + let formatted_url = server_url.format_as(protocol)?; + let res = list_from_remote_url( git_repo, &formatted_url, + decoded_nostr_url.ssh_key_file_path().as_ref(), [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol), term, ); @@ -86,9 +88,18 @@ pub fn list_from_remote( } Err(error) => { term.clear_last_lines(1)?; - term.write_line( - format!("list: {formatted_url} failed over {protocol}: {error}").as_str(), - )?; + term.write_line(&format!( + "list: {formatted_url} failed over {protocol}{}: {error}", + if protocol == &ServerProtocol::Ssh { + if let Some(ssh_key_file) = &decoded_nostr_url.ssh_key_file_path() { + format!(" with ssh key from {ssh_key_file}") + } else { + String::new() + } + } else { + String::new() + } + ))?; failed_protocols.push(protocol); } } @@ -117,6 +128,7 @@ pub fn list_from_remote( fn list_from_remote_url( git_repo: &Repo, git_server_remote_url: &str, + ssh_key_file: Option<&String>, dont_authenticate: bool, term: &console::Term, ) -> Result> { @@ -124,7 +136,20 @@ fn list_from_remote_url( let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_remote_url)?; // authentication may be required - let auth = GitAuthenticator::default(); + let auth = { + if dont_authenticate { + GitAuthenticator::default() + } else if git_server_remote_url.contains("git@") { + if let Some(ssh_key_file) = ssh_key_file { + GitAuthenticator::default() + .add_ssh_key_from_file(PathBuf::from_str(ssh_key_file)?, None) + } else { + GitAuthenticator::default() + } + } else { + GitAuthenticator::default() + } + }; let mut remote_callbacks = git2::RemoteCallbacks::new(); if !dont_authenticate { remote_callbacks.credentials(auth.credentials(&git_config)); -- cgit v1.2.3