Move Authenticator to returning an AsRef<u8>.

This commit is contained in:
Kim Minh Kaplan 2018-11-09 18:35:07 +00:00
parent 93d032181d
commit 1dd55ff066
3 changed files with 12 additions and 12 deletions

View file

@ -2,7 +2,6 @@ extern crate base64;
extern crate imap; extern crate imap;
extern crate native_tls; extern crate native_tls;
use base64::encode;
use imap::authenticator::Authenticator; use imap::authenticator::Authenticator;
use native_tls::TlsConnector; use native_tls::TlsConnector;
@ -12,13 +11,12 @@ struct GmailOAuth2 {
} }
impl Authenticator for GmailOAuth2 { impl Authenticator for GmailOAuth2 {
type Response = String;
#[allow(unused_variables)] #[allow(unused_variables)]
fn process(&self, data: String) -> String { fn process(&self, data: String) -> Self::Response {
encode( format!(
format!( "user={}\x01auth=Bearer {}\x01\x01",
"user={}\x01auth=Bearer {}\x01\x01", self.user, self.access_token
self.user, self.access_token
).as_bytes(),
) )
} }
} }

View file

@ -1,4 +1,5 @@
/// This will allow plugable authentication mechanisms. /// This will allow plugable authentication mechanisms.
pub trait Authenticator { pub trait Authenticator {
fn process(&self, String) -> String; type Response: AsRef<[u8]>;
fn process(&self, String) -> Self::Response;
} }

View file

@ -395,8 +395,8 @@ impl<T: Read + Write> Client<T> {
parse_authenticate_response(String::from_utf8(line).unwrap()), parse_authenticate_response(String::from_utf8(line).unwrap()),
self self
); );
let auth_response = base64::encode(authenticator.process(data).as_str()); let raw_response = &authenticator.process(data);
let auth_response = base64::encode(raw_response);
ok_or_unauth_client_err!( ok_or_unauth_client_err!(
self.write_line(auth_response.into_bytes().as_slice()), self.write_line(auth_response.into_bytes().as_slice()),
self self
@ -948,8 +948,9 @@ mod tests {
let client = Client::new(mock_stream); let client = Client::new(mock_stream);
enum Authenticate { Auth }; enum Authenticate { Auth };
impl Authenticator for Authenticate { impl Authenticator for Authenticate {
fn process(&self, _: String) -> String { type Response = Vec<u8>;
"foo".to_string() fn process(&self, _: String) -> Self::Response {
b"foo".to_vec()
} }
} }
let auth = Authenticate::Auth; let auth = Authenticate::Auth;