Decode the Base64 AUTHENTICATE challenge
This commit is contained in:
parent
1dd55ff066
commit
892fe49a68
3 changed files with 16 additions and 5 deletions
|
|
@ -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<u8>) -> Self::Response {
|
||||
format!(
|
||||
"user={}\x01auth=Bearer {}\x01\x01",
|
||||
self.user, self.access_token
|
||||
|
|
|
|||
|
|
@ -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<u8>) -> Self::Response;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -395,7 +395,14 @@ impl<T: Read + Write> Client<T> {
|
|||
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<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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue