-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(address): change password field to Option<String> for better…
… handling of credentials
- Loading branch information
Showing
2 changed files
with
41 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,9 @@ use crate::netrc::netrc; | |
pub struct ParsedAddress { | ||
pub server: String, | ||
pub username: String, | ||
pub password: String, | ||
pub password: Option<String>, | ||
pub path_segments: Vec<String>, | ||
pub file: String, | ||
pub with_password: bool, | ||
} | ||
|
||
impl PartialEq for ParsedAddress { | ||
|
@@ -50,8 +49,13 @@ impl ParsedAddress { | |
url.username().unwrap() | ||
}; | ||
|
||
let password = url.password().unwrap_or_else(|| "anonymous".to_string()); | ||
if !silent && username != "anonymous" && password != "anonymous" { | ||
let password = if username == "anonymous" || url.password().is_none() { | ||
None | ||
} else { | ||
Some(url.password().unwrap().to_string()) | ||
}; | ||
|
||
if !silent && username != "anonymous" && password.is_some() { | ||
println!("🔑 Parsed credentials from URL."); | ||
} | ||
|
||
|
@@ -67,38 +71,36 @@ impl ParsedAddress { | |
.ok_or_else(|| panic!("got empty path segments from url: {url}")) | ||
.unwrap(); | ||
|
||
let with_password = password != "anonymous"; | ||
|
||
ParsedAddress { | ||
server, | ||
username, | ||
password, | ||
path_segments, | ||
file, | ||
with_password, | ||
} | ||
} | ||
|
||
fn mixin_netrc( | ||
netrc: &Option<netrc::Netrc>, | ||
server: &str, | ||
username: String, | ||
password: String, | ||
) -> (String, String) { | ||
password: Option<String>, | ||
) -> (String, Option<String>) { | ||
let mut user = username.clone(); | ||
let mut pass = password.clone(); | ||
if !netrc.is_none() && username == "anonymous" && password == "anonymous" { | ||
for host in netrc.as_ref().unwrap().hosts.iter().enumerate() { | ||
let (_i, (netrc_name, machine)) = host; | ||
|
||
let mut name = netrc_name.to_string(); | ||
if let Some(port) = machine.port { | ||
name = name + ":" + &port.to_string()[..]; | ||
} | ||
if server == name { | ||
user.clone_from(&machine.login); | ||
pass = machine.password.clone().unwrap(); | ||
break; | ||
let mut pass = password; | ||
|
||
if netrc.is_some() && username == "anonymous" && pass.is_none() { | ||
if let Some(netrc) = netrc.as_ref() { | ||
for (_i, (netrc_name, machine)) in netrc.hosts.iter().enumerate() { | ||
let mut name = netrc_name.to_string(); | ||
if let Some(port) = machine.port { | ||
name = name + ":" + &port.to_string()[..]; | ||
} | ||
if server == name { | ||
user = machine.login.clone(); | ||
pass = machine.password.clone(); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -111,18 +113,16 @@ async fn parseaddress_operator_equals_works_when_typical() { | |
let left = ParsedAddress { | ||
server: "do.main".to_string(), | ||
username: "user".to_string(), | ||
password: "pass".to_string(), | ||
password: Some("pass".to_string()), | ||
path_segments: vec!["my".to_string(), "path".to_string()], | ||
file: "pass".to_string(), | ||
with_password: true, | ||
}; | ||
let right = ParsedAddress { | ||
server: "do.main".to_string(), | ||
username: "user".to_string(), | ||
password: "pass".to_string(), | ||
password: Some("pass".to_string()), | ||
path_segments: vec!["my".to_string(), "path".to_string()], | ||
file: "pass".to_string(), | ||
with_password: true, | ||
}; | ||
|
||
assert!(left == right); | ||
|
@@ -133,18 +133,16 @@ async fn parseaddress_operator_equals_fails_when_not_equal() { | |
let left = ParsedAddress { | ||
server: "do.main".to_string(), | ||
username: "user".to_string(), | ||
password: "pass".to_string(), | ||
password: Some("pass".to_string()), | ||
path_segments: vec!["my".to_string(), "path".to_string()], | ||
file: "pass".to_string(), | ||
with_password: true, | ||
}; | ||
let right = ParsedAddress { | ||
server: "do".to_string(), | ||
username: "user".to_string(), | ||
password: "pass".to_string(), | ||
password: Some("pass".to_string()), | ||
path_segments: vec!["my".to_string(), "path".to_string()], | ||
file: "pass".to_string(), | ||
with_password: true, | ||
}; | ||
|
||
assert!(left != right); | ||
|
@@ -155,10 +153,9 @@ async fn parse_works() { | |
let expected = ParsedAddress { | ||
server: "do.main:21".to_string(), | ||
username: "user".to_string(), | ||
password: "pass".to_string(), | ||
password: Some("pass".to_string()), | ||
path_segments: vec!["index".to_string()], | ||
file: "file".to_string(), | ||
with_password: true, | ||
}; | ||
|
||
let actual = ParsedAddress::parse_address("ftp://user:[email protected]:21/index/file", true); | ||
|
@@ -169,12 +166,12 @@ async fn parse_works() { | |
#[tokio::test] | ||
async fn mixin_works() { | ||
let expected_username = "test"; | ||
let expected_password = "p@ssw0rd"; | ||
let expected_password = Some("p@ssw0rd".to_string()); | ||
let input = "machine example.com login test password p@ssw0rd"; | ||
let input = std::io::BufReader::new(input.as_bytes()); | ||
let netrc = netrc::Netrc::parse(input).unwrap(); | ||
let username_decoded_from_url = "anonymous".to_string(); | ||
let password_decoded_from_url = "anonymous".to_string(); | ||
let password_decoded_from_url = None; | ||
|
||
let (actual_username, actual_password) = ParsedAddress::mixin_netrc( | ||
&Some(netrc), | ||
|
@@ -190,12 +187,12 @@ async fn mixin_works() { | |
#[tokio::test] | ||
async fn mixin_works_with_port() { | ||
let expected_username = "test"; | ||
let expected_password = "p@ssw0rd"; | ||
let expected_password = Some("p@ssw0rd".to_string()); | ||
let input = "machine example.com login test password p@ssw0rd port 443"; | ||
let input = std::io::BufReader::new(input.as_bytes()); | ||
let netrc = netrc::Netrc::parse(input).unwrap(); | ||
let username_decoded_from_url = "anonymous".to_string(); | ||
let password_decoded_from_url = "anonymous".to_string(); | ||
let password_decoded_from_url = None; | ||
|
||
let (actual_username, actual_password) = ParsedAddress::mixin_netrc( | ||
&Some(netrc), | ||
|
@@ -213,10 +210,9 @@ async fn parse_works_with_netrc_mixin() { | |
let expected = ParsedAddress { | ||
server: "do.main:21".to_string(), | ||
username: "test".to_string(), | ||
password: "p@ssw0rd".to_string(), | ||
password: Some("p@ssw0rd".to_string()), | ||
path_segments: vec!["index".to_string()], | ||
file: "file".to_string(), | ||
with_password: true, | ||
}; | ||
let data = "machine do.main login test password p@ssw0rd port 21"; | ||
|
||
|
@@ -232,10 +228,9 @@ async fn parse_works_when_ssh_user() { | |
let expected = ParsedAddress { | ||
server: "localhost:2223".to_string(), | ||
username: "user".to_string(), | ||
password: "anonymous".to_string(), | ||
password: None, | ||
path_segments: vec!["".to_string()], | ||
file: "file".to_string(), | ||
with_password: false, | ||
}; | ||
|
||
let actual = ParsedAddress::parse_address("ssh://user@localhost:2223/file", true); | ||
|
@@ -248,10 +243,9 @@ async fn parse_works_when_not_silent() { | |
let expected = ParsedAddress { | ||
server: "localhost:2223".to_string(), | ||
username: "user".to_string(), | ||
password: "pass".to_string(), | ||
password: Some("pass".to_string()), | ||
path_segments: vec!["".to_string()], | ||
file: "file".to_string(), | ||
with_password: false, | ||
}; | ||
|
||
let actual = ParsedAddress::parse_address("ssh://user:pass@localhost:2223/file", false); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters