upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/lib/fetch.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2025-01-16 09:23:05 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2025-10-17 12:18:24 +0100
commitb126e5b7acfce55bd101b06cb5baf7f251cd0fda (patch)
treedd3eb721a49496ce2a686a43ce4d007b26ee7d7c /src/lib/fetch.rs
parent6cb9e1f5071a6dee8f8e5b98c3c2e699cafd2921 (diff)
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.
Diffstat (limited to 'src/lib/fetch.rs')
-rw-r--r--src/lib/fetch.rs36
1 files changed, 31 insertions, 5 deletions
diff --git a/src/lib/fetch.rs b/src/lib/fetch.rs
index 89001d4..eeed8f4 100644
--- a/src/lib/fetch.rs
+++ b/src/lib/fetch.rs
@@ -1,4 +1,6 @@
1use std::{ 1use std::{
2 path::PathBuf,
3 str::FromStr,
2 sync::{Arc, Mutex}, 4 sync::{Arc, Mutex},
3 time::Instant, 5 time::Instant,
4}; 6};
@@ -44,18 +46,28 @@ pub fn fetch_from_git_server(
44 format!("fetching {} over {protocol}...", server_url.short_name(),).as_str(), 46 format!("fetching {} over {protocol}...", server_url.short_name(),).as_str(),
45 )?; 47 )?;
46 48
47 let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?; 49 let formatted_url = server_url.format_as(protocol)?;
48 let res = fetch_from_git_server_url( 50 let res = fetch_from_git_server_url(
49 &git_repo.git_repo, 51 &git_repo.git_repo,
50 oids, 52 oids,
51 &formatted_url, 53 &formatted_url,
52 [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol), 54 [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol),
55 decoded_nostr_url.ssh_key_file_path().as_ref(),
53 term, 56 term,
54 ); 57 );
55 if let Err(error) = res { 58 if let Err(error) = res {
56 term.write_line( 59 term.write_line(&format!(
57 format!("fetch: {formatted_url} failed over {protocol}: {error}").as_str(), 60 "fetch: {formatted_url} failed over {protocol}{}: {error}",
58 )?; 61 if protocol == &ServerProtocol::Ssh {
62 if let Some(ssh_key_file) = &decoded_nostr_url.ssh_key_file_path() {
63 format!(" with ssh key from {ssh_key_file}")
64 } else {
65 String::new()
66 }
67 } else {
68 String::new()
69 }
70 ))?;
59 failed_protocols.push(protocol); 71 failed_protocols.push(protocol);
60 } else { 72 } else {
61 success = true; 73 success = true;
@@ -89,6 +101,7 @@ fn fetch_from_git_server_url(
89 oids: &[String], 101 oids: &[String],
90 git_server_url: &str, 102 git_server_url: &str,
91 dont_authenticate: bool, 103 dont_authenticate: bool,
104 ssh_key_file: Option<&String>,
92 term: &console::Term, 105 term: &console::Term,
93) -> Result<()> { 106) -> Result<()> {
94 if git_server_url.parse::<CloneUrl>()?.protocol() == ServerProtocol::Ssh && !check_ssh_keys() { 107 if git_server_url.parse::<CloneUrl>()?.protocol() == ServerProtocol::Ssh && !check_ssh_keys() {
@@ -96,7 +109,20 @@ fn fetch_from_git_server_url(
96 } 109 }
97 let git_config = git_repo.config()?; 110 let git_config = git_repo.config()?;
98 let mut git_server_remote = git_repo.remote_anonymous(git_server_url)?; 111 let mut git_server_remote = git_repo.remote_anonymous(git_server_url)?;
99 let auth = GitAuthenticator::default(); 112 let auth = {
113 if dont_authenticate {
114 GitAuthenticator::default()
115 } else if git_server_url.contains("git@") {
116 if let Some(ssh_key_file) = ssh_key_file {
117 GitAuthenticator::default()
118 .add_ssh_key_from_file(PathBuf::from_str(ssh_key_file)?, None)
119 } else {
120 GitAuthenticator::default()
121 }
122 } else {
123 GitAuthenticator::default()
124 }
125 };
100 let mut fetch_options = git2::FetchOptions::new(); 126 let mut fetch_options = git2::FetchOptions::new();
101 let mut remote_callbacks = git2::RemoteCallbacks::new(); 127 let mut remote_callbacks = git2::RemoteCallbacks::new();
102 let fetch_reporter = Arc::new(Mutex::new(FetchReporter::new(term))); 128 let fetch_reporter = Arc::new(Mutex::new(FetchReporter::new(term)));