Decode the Base64 AUTHENTICATE challenge

This commit is contained in:
Kim Minh Kaplan 2018-11-09 22:33:05 +00:00
parent 1dd55ff066
commit 892fe49a68
3 changed files with 16 additions and 5 deletions

View file

@ -13,7 +13,7 @@ struct GmailOAuth2 {
impl Authenticator for GmailOAuth2 { impl Authenticator for GmailOAuth2 {
type Response = String; type Response = String;
#[allow(unused_variables)] #[allow(unused_variables)]
fn process(&self, data: String) -> Self::Response { fn process(&self, data: Vec<u8>) -> Self::Response {
format!( format!(
"user={}\x01auth=Bearer {}\x01\x01", "user={}\x01auth=Bearer {}\x01\x01",
self.user, self.access_token self.user, self.access_token

View file

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

View file

@ -395,7 +395,14 @@ 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 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); 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()),
@ -940,7 +947,7 @@ mod tests {
#[test] #[test]
fn authenticate() { fn authenticate() {
let response = b"+\r\n\ let response = b"+YmFy\r\n\
a1 OK Logged in\r\n".to_vec(); a1 OK Logged in\r\n".to_vec();
let command = "a1 AUTHENTICATE PLAIN\r\n\ let command = "a1 AUTHENTICATE PLAIN\r\n\
Zm9v\r\n"; Zm9v\r\n";
@ -949,7 +956,11 @@ mod tests {
enum Authenticate { Auth }; enum Authenticate { Auth };
impl Authenticator for Authenticate { impl Authenticator for Authenticate {
type Response = Vec<u8>; type Response = Vec<u8>;
fn process(&self, _: String) -> Self::Response { fn process(&self, challenge: Vec<u8>) -> Self::Response {
assert!(
challenge == b"bar".to_vec(),
"Invalid authenticate challenge"
);
b"foo".to_vec() b"foo".to_vec()
} }
} }