From 93d032181d808bf772d04a0439f5ae8f2bf35589 Mon Sep 17 00:00:00 2001 From: Kim Minh Kaplan Date: Wed, 7 Nov 2018 10:48:53 +0000 Subject: [PATCH] imap::client::Client::authenticate: Base64 encode the result of the Authenticator. Fixes issue #95. --- Cargo.toml | 2 -- src/client.rs | 25 ++++++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 33cae10..9f4ba18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,4 @@ regex = "1.0" bufstream = "0.1" imap-proto = "0.6" nom = "4.0" - -[dev-dependencies] base64 = "0.10" diff --git a/src/client.rs b/src/client.rs index dad95d4..8c1faf0 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,3 +1,4 @@ +extern crate base64; use bufstream::BufStream; use native_tls::{TlsConnector, TlsStream}; use nom; @@ -394,7 +395,7 @@ impl Client { parse_authenticate_response(String::from_utf8(line).unwrap()), self ); - let auth_response = authenticator.process(data); + let auth_response = base64::encode(authenticator.process(data).as_str()); ok_or_unauth_client_err!( self.write_line(auth_response.into_bytes().as_slice()), @@ -937,6 +938,28 @@ mod tests { ); } + #[test] + fn authenticate() { + let response = b"+\r\n\ + a1 OK Logged in\r\n".to_vec(); + let command = "a1 AUTHENTICATE PLAIN\r\n\ + Zm9v\r\n"; + let mock_stream = MockStream::new(response); + let client = Client::new(mock_stream); + enum Authenticate { Auth }; + impl Authenticator for Authenticate { + fn process(&self, _: String) -> String { + "foo".to_string() + } + } + let auth = Authenticate::Auth; + let session = client.authenticate("PLAIN", auth).unwrap(); + assert!( + session.stream.get_ref().written_buf == command.as_bytes().to_vec(), + "Invalid authenticate command" + ); + } + #[test] fn login() { let response = b"a1 OK Logged in\r\n".to_vec();