upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/lib/list.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/list.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/list.rs')
-rw-r--r--src/lib/list.rs37
1 files changed, 31 insertions, 6 deletions
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 @@
1use std::collections::HashMap; 1use std::{collections::HashMap, path::PathBuf, str::FromStr};
2 2
3use anyhow::{Result, anyhow}; 3use anyhow::{Result, anyhow};
4use auth_git2::GitAuthenticator; 4use auth_git2::GitAuthenticator;
@@ -59,10 +59,12 @@ pub fn list_from_remote(
59 .as_str(), 59 .as_str(),
60 )?; 60 )?;
61 61
62 let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?; 62 let formatted_url = server_url.format_as(protocol)?;
63
63 let res = list_from_remote_url( 64 let res = list_from_remote_url(
64 git_repo, 65 git_repo,
65 &formatted_url, 66 &formatted_url,
67 decoded_nostr_url.ssh_key_file_path().as_ref(),
66 [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol), 68 [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol),
67 term, 69 term,
68 ); 70 );
@@ -86,9 +88,18 @@ pub fn list_from_remote(
86 } 88 }
87 Err(error) => { 89 Err(error) => {
88 term.clear_last_lines(1)?; 90 term.clear_last_lines(1)?;
89 term.write_line( 91 term.write_line(&format!(
90 format!("list: {formatted_url} failed over {protocol}: {error}").as_str(), 92 "list: {formatted_url} failed over {protocol}{}: {error}",
91 )?; 93 if protocol == &ServerProtocol::Ssh {
94 if let Some(ssh_key_file) = &decoded_nostr_url.ssh_key_file_path() {
95 format!(" with ssh key from {ssh_key_file}")
96 } else {
97 String::new()
98 }
99 } else {
100 String::new()
101 }
102 ))?;
92 failed_protocols.push(protocol); 103 failed_protocols.push(protocol);
93 } 104 }
94 } 105 }
@@ -117,6 +128,7 @@ pub fn list_from_remote(
117fn list_from_remote_url( 128fn list_from_remote_url(
118 git_repo: &Repo, 129 git_repo: &Repo,
119 git_server_remote_url: &str, 130 git_server_remote_url: &str,
131 ssh_key_file: Option<&String>,
120 dont_authenticate: bool, 132 dont_authenticate: bool,
121 term: &console::Term, 133 term: &console::Term,
122) -> Result<HashMap<String, String>> { 134) -> Result<HashMap<String, String>> {
@@ -124,7 +136,20 @@ fn list_from_remote_url(
124 136
125 let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_remote_url)?; 137 let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_remote_url)?;
126 // authentication may be required 138 // authentication may be required
127 let auth = GitAuthenticator::default(); 139 let auth = {
140 if dont_authenticate {
141 GitAuthenticator::default()
142 } else if git_server_remote_url.contains("git@") {
143 if let Some(ssh_key_file) = ssh_key_file {
144 GitAuthenticator::default()
145 .add_ssh_key_from_file(PathBuf::from_str(ssh_key_file)?, None)
146 } else {
147 GitAuthenticator::default()
148 }
149 } else {
150 GitAuthenticator::default()
151 }
152 };
128 let mut remote_callbacks = git2::RemoteCallbacks::new(); 153 let mut remote_callbacks = git2::RemoteCallbacks::new();
129 if !dont_authenticate { 154 if !dont_authenticate {
130 remote_callbacks.credentials(auth.credentials(&git_config)); 155 remote_callbacks.credentials(auth.credentials(&git_config));