diff --git a/examples/gmail_oauth2.rs b/examples/gmail_oauth2.rs index 50805f1..0fe212c 100644 --- a/examples/gmail_oauth2.rs +++ b/examples/gmail_oauth2.rs @@ -13,7 +13,7 @@ struct GmailOAuth2 { impl Authenticator for GmailOAuth2 { type Response = String; #[allow(unused_variables)] - fn process(&self, data: String) -> Self::Response { + fn process(&self, data: Vec) -> Self::Response { format!( "user={}\x01auth=Bearer {}\x01\x01", self.user, self.access_token diff --git a/src/authenticator.rs b/src/authenticator.rs index 0926924..1e5187f 100644 --- a/src/authenticator.rs +++ b/src/authenticator.rs @@ -1,5 +1,5 @@ /// This will allow plugable authentication mechanisms. pub trait Authenticator { type Response: AsRef<[u8]>; - fn process(&self, String) -> Self::Response; + fn process(&self, Vec) -> Self::Response; } diff --git a/src/client.rs b/src/client.rs index 734d0e9..017bd1d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -395,7 +395,14 @@ impl Client { parse_authenticate_response(String::from_utf8(line).unwrap()), self ); - let raw_response = &authenticator.process(data); + let challenge = ok_or_unauth_client_err!( + base64::decode(data.as_str()) + .map_err(|_| + Error::Parse(ParseError::Authentication(data)) + ), + self + ); + let raw_response = &authenticator.process(challenge); let auth_response = base64::encode(raw_response); ok_or_unauth_client_err!( self.write_line(auth_response.into_bytes().as_slice()), @@ -940,7 +947,7 @@ mod tests { #[test] fn authenticate() { - let response = b"+\r\n\ + let response = b"+YmFy\r\n\ a1 OK Logged in\r\n".to_vec(); let command = "a1 AUTHENTICATE PLAIN\r\n\ Zm9v\r\n"; @@ -949,7 +956,11 @@ mod tests { enum Authenticate { Auth }; impl Authenticator for Authenticate { type Response = Vec; - fn process(&self, _: String) -> Self::Response { + fn process(&self, challenge: Vec) -> Self::Response { + assert!( + challenge == b"bar".to_vec(), + "Invalid authenticate challenge" + ); b"foo".to_vec() } }