|
10 | 10 |
|
11 | 11 | use std::collections::HashMap;
|
12 | 12 | use std::fs::File;
|
| 13 | +use std::io::{BufRead, BufReader}; |
13 | 14 | use std::iter::FromIterator;
|
14 | 15 | use std::path::PathBuf;
|
15 | 16 | use std::{fmt, result};
|
@@ -201,19 +202,16 @@ pub enum Auth {
|
201 | 202 | impl Auth {
|
202 | 203 | /// Convert into the arguments that jsonrpc::Client needs.
|
203 | 204 | pub fn get_user_pass(self) -> Result<(Option<String>, Option<String>)> {
|
204 |
| - use std::io::Read; |
205 | 205 | match self {
|
206 | 206 | Auth::None => Ok((None, None)),
|
207 | 207 | Auth::UserPass(u, p) => Ok((Some(u), Some(p))),
|
208 | 208 | Auth::CookieFile(path) => {
|
209 |
| - let mut file = File::open(path)?; |
210 |
| - let mut contents = String::new(); |
211 |
| - file.read_to_string(&mut contents)?; |
212 |
| - let mut split = contents.splitn(2, ":"); |
213 |
| - Ok(( |
214 |
| - Some(split.next().ok_or(Error::InvalidCookieFile)?.into()), |
215 |
| - Some(split.next().ok_or(Error::InvalidCookieFile)?.into()), |
216 |
| - )) |
| 209 | + let line = BufReader::new(File::open(path)?) |
| 210 | + .lines() |
| 211 | + .next() |
| 212 | + .ok_or(Error::InvalidCookieFile)??; |
| 213 | + let colon = line.find(':').ok_or(Error::InvalidCookieFile)?; |
| 214 | + Ok((Some(line[..colon].into()), Some(line[colon + 1..].into()))) |
217 | 215 | }
|
218 | 216 | }
|
219 | 217 | }
|
@@ -1429,4 +1427,34 @@ mod tests {
|
1429 | 1427 | fn test_handle_defaults() {
|
1430 | 1428 | test_handle_defaults_inner().unwrap();
|
1431 | 1429 | }
|
| 1430 | + |
| 1431 | + #[test] |
| 1432 | + fn auth_cookie_file_ignores_newline() { |
| 1433 | + let tempdir = tempfile::tempdir().unwrap(); |
| 1434 | + let path = tempdir.path().join("cookie"); |
| 1435 | + std::fs::write(&path, "foo:bar\n").unwrap(); |
| 1436 | + assert_eq!( |
| 1437 | + Auth::CookieFile(path).get_user_pass().unwrap(), |
| 1438 | + (Some("foo".into()), Some("bar".into())), |
| 1439 | + ); |
| 1440 | + } |
| 1441 | + |
| 1442 | + #[test] |
| 1443 | + fn auth_cookie_file_ignores_additional_lines() { |
| 1444 | + let tempdir = tempfile::tempdir().unwrap(); |
| 1445 | + let path = tempdir.path().join("cookie"); |
| 1446 | + std::fs::write(&path, "foo:bar\nbaz").unwrap(); |
| 1447 | + assert_eq!( |
| 1448 | + Auth::CookieFile(path).get_user_pass().unwrap(), |
| 1449 | + (Some("foo".into()), Some("bar".into())), |
| 1450 | + ); |
| 1451 | + } |
| 1452 | + |
| 1453 | + #[test] |
| 1454 | + fn auth_cookie_file_fails_if_colon_isnt_present() { |
| 1455 | + let tempdir = tempfile::tempdir().unwrap(); |
| 1456 | + let path = tempdir.path().join("cookie"); |
| 1457 | + std::fs::write(&path, "foobar").unwrap(); |
| 1458 | + assert!(matches!(Auth::CookieFile(path).get_user_pass(), Err(Error::InvalidCookieFile))); |
| 1459 | + } |
1432 | 1460 | }
|
0 commit comments