diff --git a/examples/gmail_oauth2.rs b/examples/gmail_oauth2.rs index 051c7e2..50805f1 100644 --- a/examples/gmail_oauth2.rs +++ b/examples/gmail_oauth2.rs @@ -2,7 +2,6 @@ extern crate base64; extern crate imap; extern crate native_tls; -use base64::encode; use imap::authenticator::Authenticator; use native_tls::TlsConnector; @@ -12,13 +11,12 @@ struct GmailOAuth2 { } impl Authenticator for GmailOAuth2 { + type Response = String; #[allow(unused_variables)] - fn process(&self, data: String) -> String { - encode( - format!( - "user={}\x01auth=Bearer {}\x01\x01", - self.user, self.access_token - ).as_bytes(), + fn process(&self, data: String) -> Self::Response { + format!( + "user={}\x01auth=Bearer {}\x01\x01", + self.user, self.access_token ) } } diff --git a/src/authenticator.rs b/src/authenticator.rs index 825ec9a..0926924 100644 --- a/src/authenticator.rs +++ b/src/authenticator.rs @@ -1,4 +1,5 @@ /// This will allow plugable authentication mechanisms. pub trait Authenticator { - fn process(&self, String) -> String; + type Response: AsRef<[u8]>; + fn process(&self, String) -> Self::Response; } diff --git a/src/client.rs b/src/client.rs index 8c1faf0..734d0e9 100644 --- a/src/client.rs +++ b/src/client.rs @@ -395,8 +395,8 @@ impl Client { parse_authenticate_response(String::from_utf8(line).unwrap()), 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!( self.write_line(auth_response.into_bytes().as_slice()), self @@ -948,8 +948,9 @@ mod tests { let client = Client::new(mock_stream); enum Authenticate { Auth }; impl Authenticator for Authenticate { - fn process(&self, _: String) -> String { - "foo".to_string() + type Response = Vec; + fn process(&self, _: String) -> Self::Response { + b"foo".to_vec() } } let auth = Authenticate::Auth;