Add PLAIN authentication mecanism

This commit is contained in:
Alexis Mousset
2015-03-01 15:08:36 +01:00
parent e790a33ce6
commit 2a7f8531fd
5 changed files with 97 additions and 27 deletions

View File

@@ -0,0 +1,22 @@
// Copyright 2014 Alexis Mousset. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! PLAIN authentication mecanism
pub mod plain;
/// Trait representing an authentication mecanism
pub trait AuthenticationMecanism {
/// Create an authentication
fn new(username: String, password: String) -> Self;
/// Initial response if available
fn initial_response(&self) -> Option<String>;
/// Response to the given challenge
fn response(&self, challenge: &str) -> String;
}

View File

@@ -0,0 +1,39 @@
// Copyright 2014 Alexis Mousset. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Authentication mecanisms
use common::NUL;
use client::authentication::AuthenticationMecanism;
struct Plain {
identity: String,
username: String,
password: String,
}
impl AuthenticationMecanism for Plain {
fn new(username: String, password: String) -> Plain {
Plain {
identity: "".to_string(),
username: username,
password: password,
}
}
fn initial_response(&self) -> Option<String> {
Some(self.response(""))
}
fn response(&self, challenge: &str) -> String {
// We do not need a challenge in PLAIN authentication
let _ = challenge;
format!("{}{}{}{}{}", self.identity, NUL, self.username, NUL, self.password)
}
}

View File

@@ -33,6 +33,7 @@ use sendable_email::SendableEmail;
pub mod server_info;
pub mod connecter;
pub mod stream;
pub mod authentication;
/// Represents the configuration of a client
#[derive(Debug)]