Skip to content
This repository was archived by the owner on Jul 23, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@
///
/// struct RandomAuthenticator;
///
/// impl Authenticator for RandomAuthenticator {
/// fn authenticate(&self, _username: &str, _password: &str) -> Result<bool, ()> {
/// Ok(rand::random())
/// impl Authenticator<RandomUser> for RandomAuthenticator {
/// fn authenticate(&self, _username: &str, _password: &str) -> Result<RandomUser, ()> {
/// match rand::random() {
/// true => Ok(RandomUser{}),
/// _ => Err(()),
/// }
/// }
/// }
///
/// struct RandomUser;
/// ```
/// [`Server`]: ../server/struct.Server.html
pub trait Authenticator {
pub trait Authenticator<U> {
/// Authenticate the given user with the given password.
fn authenticate(&self, username: &str, password: &str) -> Result<bool, ()>;
fn authenticate(&self, username: &str, password: &str) -> Result<U, ()>;
}

/// [`Authenticator`] implementation that authenticates against [`PAM`].
Expand All @@ -35,15 +40,19 @@ pub mod pam;
/// # Example
///
/// ```rust
/// use firetrap::auth::{Authenticator, AnonymousAuthenticator};
/// use firetrap::auth::{Authenticator, AnonymousUser, AnonymousAuthenticator};
///
/// let my_auth = AnonymousAuthenticator{};
/// assert_eq!(my_auth.authenticate("Finn", "I ❤️ PB").unwrap(), true);
/// assert_eq!(my_auth.authenticate("Finn", "I ❤️ PB").unwrap(), AnonymousUser{});
/// ```
pub struct AnonymousAuthenticator;

impl Authenticator for AnonymousAuthenticator {
fn authenticate(&self, _username: &str, _password: &str) -> Result<bool, ()> {
Ok(true)
impl Authenticator<AnonymousUser> for AnonymousAuthenticator {
fn authenticate(&self, _username: &str, _password: &str) -> Result<AnonymousUser, ()> {
Ok(AnonymousUser{})
}
}

/// AnonymousUser
#[derive(Debug,PartialEq)]
pub struct AnonymousUser;
18 changes: 14 additions & 4 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub enum Command {
/// The `STAT` command
Stat {
/// The bytes making up the path about which information is requested, if given.
path: Option<Bytes>,
path: Option<String>,
},
/// The `TYPE` command
Type,
Expand Down Expand Up @@ -193,9 +193,9 @@ impl Command {
}
}
b"SYST" | b"syst" => Command::Syst,
b"STAT" => {
let params = parse_to_eol(cmd_params)?;
let path = if !params.is_empty() { Some(params) } else { None };
b"STAT" | b"stat" => {
let path = parse_to_eol(cmd_params)?;
let path = if path.is_empty() { None } else { Some(String::from_utf8_lossy(&path).to_string()) };
Command::Stat{path}
},
b"TYPE" | b"type" => {
Expand Down Expand Up @@ -700,6 +700,16 @@ mod tests {
assert_eq!(Command::parse(input).unwrap(), Command::Port);
}

#[test]
fn parse_stat() {
let input = "STAT\r\n";
assert_eq!(Command::parse(input), Ok(Command::Stat{path: None}));

let input = "STAT tmp\r\n";
let expected_path = Some("tmp".to_string());
assert_eq!(Command::parse(input), Ok(Command::Stat{path: expected_path}));
}

#[test]
fn parse_list() {
let input = "LIST\r\n";
Expand Down
Loading