Renames and fixes typo from mecanism to mechanism

This commit is contained in:
Ashkan Kiani
2015-10-27 06:20:04 -07:00
parent 09f61a9fc9
commit 8d9877233d
5 changed files with 64 additions and 64 deletions

View File

@@ -12,7 +12,7 @@
//!
//! * 8BITMIME ([RFC 6152](https://tools.ietf.org/html/rfc6152))
//! * AUTH ([RFC 4954](http://tools.ietf.org/html/rfc4954)) with PLAIN and
//! CRAM-MD5 mecanisms
//! CRAM-MD5 mechanisms
//! * STARTTLS ([RFC 2487](http://tools.ietf.org/html/rfc2487))
//! * SMTPUTF8 ([RFC 6531](http://tools.ietf.org/html/rfc6531))
//!
@@ -60,7 +60,7 @@
//! use lettre::email::EmailBuilder;
//! use lettre::transport::smtp::{SecurityLevel, SmtpTransport,
//! SmtpTransportBuilder};
//! use lettre::transport::smtp::authentication::Mecanism;
//! use lettre::transport::smtp::authentication::Mechanism;
//! use lettre::transport::smtp::SUBMISSION_PORT;
//! use lettre::transport::EmailTransport;
//!
@@ -89,8 +89,8 @@
//! .security_level(SecurityLevel::AlwaysEncrypt)
//! // Enable SMTPUTF8 is the server supports it
//! .smtp_utf8(true)
//! // Configure accepted authetication mecanisms
//! .authentication_mecanisms(vec![Mecanism::CramMd5])
//! // Configure accepted authetication mechanisms
//! .authentication_mechanisms(vec![Mechanism::CramMd5])
//! // Enable connection reuse
//! .connection_reuse(true).build();
//!

View File

@@ -1,4 +1,4 @@
//! Provides authentication mecanisms
//! Provides authentication mechanisms
use std::fmt;
use std::fmt::{Display, Formatter};
@@ -12,34 +12,34 @@ use crypto::mac::Mac;
use transport::smtp::NUL;
use transport::error::Error;
/// Represents authentication mecanisms
/// Represents authentication mechanisms
#[derive(PartialEq,Eq,Copy,Clone,Hash,Debug)]
pub enum Mecanism {
/// PLAIN authentication mecanism
pub enum Mechanism {
/// PLAIN authentication mechanism
/// RFC 4616: https://tools.ietf.org/html/rfc4616
Plain,
/// CRAM-MD5 authentication mecanism
/// CRAM-MD5 authentication mechanism
/// RFC 2195: https://tools.ietf.org/html/rfc2195
CramMd5,
}
impl Display for Mecanism {
impl Display for Mechanism {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f,
"{}",
match *self {
Mecanism::Plain => "PLAIN",
Mecanism::CramMd5 => "CRAM-MD5",
Mechanism::Plain => "PLAIN",
Mechanism::CramMd5 => "CRAM-MD5",
})
}
}
impl Mecanism {
/// Does the mecanism supports initial response
impl Mechanism {
/// Does the mechanism supports initial response
pub fn supports_initial_response(&self) -> bool {
match *self {
Mecanism::Plain => true,
Mecanism::CramMd5 => false,
Mechanism::Plain => true,
Mechanism::CramMd5 => false,
}
}
@@ -51,18 +51,18 @@ impl Mecanism {
challenge: Option<&str>)
-> Result<String, Error> {
match *self {
Mecanism::Plain => {
Mechanism::Plain => {
match challenge {
Some(_) => Err(Error::ClientError("This mecanism does not expect a challenge")),
Some(_) => Err(Error::ClientError("This mechanism does not expect a challenge")),
None => Ok(format!("{}{}{}{}", NUL, username, NUL, password)
.as_bytes()
.to_base64(base64::STANDARD)),
}
}
Mecanism::CramMd5 => {
Mechanism::CramMd5 => {
let encoded_challenge = match challenge {
Some(challenge) => challenge,
None => return Err(Error::ClientError("This mecanism does expect a challenge")),
None => return Err(Error::ClientError("This mechanism does expect a challenge")),
};
let decoded_challenge = match encoded_challenge.from_base64() {
@@ -83,27 +83,27 @@ impl Mecanism {
#[cfg(test)]
mod test {
use super::Mecanism;
use super::Mechanism;
#[test]
fn test_plain() {
let mecanism = Mecanism::Plain;
let mechanism = Mechanism::Plain;
assert_eq!(mecanism.response("username", "password", None).unwrap(),
assert_eq!(mechanism.response("username", "password", None).unwrap(),
"AHVzZXJuYW1lAHBhc3N3b3Jk");
assert!(mecanism.response("username", "password", Some("test")).is_err());
assert!(mechanism.response("username", "password", Some("test")).is_err());
}
#[test]
fn test_cram_md5() {
let mecanism = Mecanism::CramMd5;
let mechanism = Mechanism::CramMd5;
assert_eq!(mecanism.response("alice",
assert_eq!(mechanism.response("alice",
"wonderland",
Some("PDE3ODkzLjEzMjA2NzkxMjNAdGVzc2VyYWN0LnN1c2FtLmluPg=="))
.unwrap(),
"YWxpY2UgNjRiMmE0M2MxZjZlZDY4MDZhOTgwOTE0ZTIzZTc1ZjA=");
assert!(mecanism.response("alice", "wonderland", Some("tést")).is_err());
assert!(mecanism.response("alice", "wonderland", None).is_err());
assert!(mechanism.response("alice", "wonderland", Some("tést")).is_err());
assert!(mechanism.response("alice", "wonderland", None).is_err());
}
}

View File

@@ -11,7 +11,7 @@ use openssl::ssl::SslContext;
use transport::error::{EmailResult, Error};
use transport::smtp::response::ResponseParser;
use transport::smtp::authentication::Mecanism;
use transport::smtp::authentication::Mechanism;
use transport::smtp::client::net::{Connector, NetworkStream};
use transport::smtp::{CRLF, MESSAGE_ENDING};
@@ -173,13 +173,13 @@ impl<S: Connector + Write + Read + Debug + Clone = NetworkStream> Client<S> {
self.command("RSET")
}
/// Sends an AUTH command with the given mecanism
pub fn auth(&mut self, mecanism: Mecanism, username: &str, password: &str) -> EmailResult {
/// Sends an AUTH command with the given mechanism
pub fn auth(&mut self, mechanism: Mechanism, username: &str, password: &str) -> EmailResult {
if mecanism.supports_initial_response() {
if mechanism.supports_initial_response() {
self.command(&format!("AUTH {} {}",
mecanism,
try!(mecanism.response(username, password, None))))
mechanism,
try!(mechanism.response(username, password, None))))
} else {
let encoded_challenge = match try!(self.command("AUTH CRAM-MD5")).first_word() {
Some(challenge) => challenge,
@@ -188,7 +188,7 @@ impl<S: Connector + Write + Read + Debug + Clone = NetworkStream> Client<S> {
debug!("CRAM challenge: {}", encoded_challenge);
let cram_response = try!(mecanism.response(username,
let cram_response = try!(mechanism.response(username,
password,
Some(&encoded_challenge)));

View File

@@ -7,7 +7,7 @@ use std::collections::HashSet;
use transport::error::Error;
use transport::smtp::response::Response;
use transport::smtp::authentication::Mecanism;
use transport::smtp::authentication::Mechanism;
/// Supported ESMTP keywords
#[derive(PartialEq,Eq,Hash,Clone,Debug)]
@@ -24,8 +24,8 @@ pub enum Extension {
///
/// RFC 2487: https://tools.ietf.org/html/rfc2487
StartTls,
/// AUTH mecanism
Authentication(Mecanism),
/// AUTH mechanism
Authentication(Mechanism),
}
impl Display for Extension {
@@ -34,7 +34,7 @@ impl Display for Extension {
Extension::EightBitMime => write!(f, "{}", "8BITMIME"),
Extension::SmtpUtfEight => write!(f, "{}", "SMTPUTF8"),
Extension::StartTls => write!(f, "{}", "STARTTLS"),
Extension::Authentication(ref mecanism) => write!(f, "{} {}", "AUTH", mecanism),
Extension::Authentication(ref mechanism) => write!(f, "{} {}", "AUTH", mechanism),
}
}
}
@@ -88,13 +88,13 @@ impl ServerInfo {
features.insert(Extension::StartTls);
}
"AUTH" => {
for &mecanism in &splitted[1..] {
match mecanism {
for &mechanism in &splitted[1..] {
match mechanism {
"PLAIN" => {
features.insert(Extension::Authentication(Mecanism::Plain));
features.insert(Extension::Authentication(Mechanism::Plain));
}
"CRAM-MD5" => {
features.insert(Extension::Authentication(Mecanism::CramMd5));
features.insert(Extension::Authentication(Mechanism::CramMd5));
}
_ => (),
}
@@ -116,8 +116,8 @@ impl ServerInfo {
}
/// Checks if the server supports an ESMTP feature
pub fn supports_auth_mecanism(&self, mecanism: Mecanism) -> bool {
self.features.contains(&Extension::Authentication(mecanism))
pub fn supports_auth_mechanism(&self, mechanism: Mechanism) -> bool {
self.features.contains(&Extension::Authentication(mechanism))
}
}
@@ -126,14 +126,14 @@ mod test {
use std::collections::HashSet;
use super::{Extension, ServerInfo};
use transport::smtp::authentication::Mecanism;
use transport::smtp::authentication::Mechanism;
use transport::smtp::response::{Category, Code, Response, Severity};
#[test]
fn test_extension_fmt() {
assert_eq!(format!("{}", Extension::EightBitMime),
"8BITMIME".to_string());
assert_eq!(format!("{}", Extension::Authentication(Mecanism::Plain)),
assert_eq!(format!("{}", Extension::Authentication(Mechanism::Plain)),
"AUTH PLAIN".to_string());
}
@@ -159,7 +159,7 @@ mod test {
"name with no supported features".to_string());
let mut plain = HashSet::new();
assert!(plain.insert(Extension::Authentication(Mecanism::Plain)));
assert!(plain.insert(Extension::Authentication(Mechanism::Plain)));
assert_eq!(format!("{}",
ServerInfo {
@@ -190,7 +190,7 @@ mod test {
assert!(server_info.supports_feature(&Extension::EightBitMime));
assert!(!server_info.supports_feature(&Extension::StartTls));
assert!(!server_info.supports_auth_mecanism(Mecanism::CramMd5));
assert!(!server_info.supports_auth_mechanism(Mechanism::CramMd5));
let response2 = Response::new(Code::new(Severity::PositiveCompletion,
Category::Unspecified4,
@@ -202,8 +202,8 @@ mod test {
let mut features2 = HashSet::new();
assert!(features2.insert(Extension::EightBitMime));
assert!(features2.insert(Extension::Authentication(Mecanism::Plain)));
assert!(features2.insert(Extension::Authentication(Mecanism::CramMd5)));
assert!(features2.insert(Extension::Authentication(Mechanism::Plain)));
assert!(features2.insert(Extension::Authentication(Mechanism::CramMd5)));
let server_info2 = ServerInfo {
name: "me".to_string(),
@@ -213,8 +213,8 @@ mod test {
assert_eq!(ServerInfo::from_response(&response2).unwrap(), server_info2);
assert!(server_info2.supports_feature(&Extension::EightBitMime));
assert!(server_info2.supports_auth_mecanism(Mecanism::Plain));
assert!(server_info2.supports_auth_mecanism(Mecanism::CramMd5));
assert!(server_info2.supports_auth_mechanism(Mechanism::Plain));
assert!(server_info2.supports_auth_mechanism(Mechanism::CramMd5));
assert!(!server_info2.supports_feature(&Extension::StartTls));
}
}

View File

@@ -8,7 +8,7 @@ use openssl::ssl::{SslContext, SslMethod};
use transport::error::{EmailResult, Error};
use transport::smtp::extension::{Extension, ServerInfo};
use transport::smtp::client::Client;
use transport::smtp::authentication::Mecanism;
use transport::smtp::authentication::Mechanism;
use transport::EmailTransport;
use email::SendableEmail;
@@ -75,8 +75,8 @@ pub struct SmtpTransportBuilder {
security_level: SecurityLevel,
/// Enable UTF8 mailboxes in enveloppe or headers
smtp_utf8: bool,
/// List of authentication mecanism, sorted by priority
authentication_mecanisms: Vec<Mecanism>,
/// List of authentication mechanism, sorted by priority
authentication_mechanisms: Vec<Mechanism>,
}
/// Builder for the SMTP SmtpTransport
@@ -95,7 +95,7 @@ impl SmtpTransportBuilder {
connection_reuse_count_limit: 100,
connection_reuse: false,
hello_name: "localhost".to_string(),
authentication_mecanisms: vec![Mecanism::CramMd5, Mecanism::Plain],
authentication_mechanisms: vec![Mechanism::CramMd5, Mechanism::Plain],
}),
None => Err(From::from("Could nor resolve hostname")),
}
@@ -148,9 +148,9 @@ impl SmtpTransportBuilder {
self
}
/// Set the authentication mecanisms
pub fn authentication_mecanisms(mut self, mecanisms: Vec<Mecanism>) -> SmtpTransportBuilder {
self.authentication_mecanisms = mecanisms;
/// Set the authentication mechanisms
pub fn authentication_mechanisms(mut self, mechanisms: Vec<Mechanism>) -> SmtpTransportBuilder {
self.authentication_mechanisms = mechanisms;
self
}
@@ -293,16 +293,16 @@ impl EmailTransport for SmtpTransport {
let mut found = false;
for mecanism in self.client_info.authentication_mecanisms.clone() {
if self.server_info.as_ref().unwrap().supports_auth_mecanism(mecanism) {
for mechanism in self.client_info.authentication_mechanisms.clone() {
if self.server_info.as_ref().unwrap().supports_auth_mechanism(mechanism) {
found = true;
try_smtp!(self.client.auth(mecanism, &username, &password), self);
try_smtp!(self.client.auth(mechanism, &username, &password), self);
break;
}
}
if !found {
info!("No supported authentication mecanisms available");
info!("No supported authentication mechanisms available");
}
}
}