lib.rs

  1//#![deny(missing_docs)]
  2
  3//! This crate provides a framework for SASL authentication and a few authentication mechanisms.
  4//!
  5//! # Examples
  6//!
  7//! ## Simple client-sided usage
  8//!
  9//! ```rust
 10//! use sasl::client::Mechanism;
 11//! use sasl::common::Credentials;
 12//! use sasl::client::mechanisms::Plain;
 13//!
 14//! let creds = Credentials::default()
 15//!                         .with_username("user")
 16//!                         .with_password("pencil");
 17//!
 18//! let mut mechanism = Plain::from_credentials(creds).unwrap();
 19//!
 20//! let initial_data = mechanism.initial().unwrap();
 21//!
 22//! assert_eq!(initial_data, b"\0user\0pencil");
 23//! ```
 24//!
 25//! ## More complex usage
 26//!
 27//! ```rust
 28//! #[macro_use] extern crate sasl;
 29//!
 30//! use sasl::server::{Validator, Provider, Mechanism as ServerMechanism, Response};
 31//! use sasl::server::mechanisms::{Plain as ServerPlain, Scram as ServerScram};
 32//! use sasl::client::Mechanism as ClientMechanism;
 33//! use sasl::client::mechanisms::{Plain as ClientPlain, Scram as ClientScram};
 34//! use sasl::common::{Identity, Credentials, Password, ChannelBinding};
 35//! use sasl::common::scram::{ScramProvider, Sha1, Sha256};
 36//! use sasl::secret;
 37//!
 38//! const USERNAME: &'static str = "user";
 39//! const PASSWORD: &'static str = "pencil";
 40//! const SALT: [u8; 8] = [35, 71, 92, 105, 212, 219, 114, 93];
 41//! const ITERATIONS: usize = 4096;
 42//!
 43//! struct MyValidator;
 44//!
 45//! impl Validator<secret::Plain> for MyValidator {
 46//!     fn validate(&self, identity: &Identity, value: &secret::Plain) -> Result<(), String> {
 47//!         let &secret::Plain(ref password) = value;
 48//!         if identity != &Identity::Username(USERNAME.to_owned()) {
 49//!             Err("authentication failed".to_owned())
 50//!         }
 51//!         else if password != PASSWORD {
 52//!             Err("authentication failed".to_owned())
 53//!         }
 54//!         else {
 55//!             Ok(())
 56//!         }
 57//!     }
 58//! }
 59//!
 60//! impl Provider<secret::Pbkdf2Sha1> for MyValidator {
 61//!     fn provide(&self, identity: &Identity) -> Result<secret::Pbkdf2Sha1, String> {
 62//!         if identity != &Identity::Username(USERNAME.to_owned()) {
 63//!             Err("authentication failed".to_owned())
 64//!         }
 65//!         else {
 66//!             let digest = sasl::common::scram::Sha1::derive
 67//!                 ( &Password::Plain((PASSWORD.to_owned()))
 68//!                 , &SALT[..]
 69//!                 , ITERATIONS )?;
 70//!             Ok(secret::Pbkdf2Sha1 {
 71//!                 salt: SALT.to_vec(),
 72//!                 iterations: ITERATIONS,
 73//!                 digest: digest,
 74//!             })
 75//!         }
 76//!     }
 77//! }
 78//!
 79//! impl_validator_using_provider!(MyValidator, secret::Pbkdf2Sha1);
 80//!
 81//! impl Provider<secret::Pbkdf2Sha256> for MyValidator {
 82//!     fn provide(&self, identity: &Identity) -> Result<secret::Pbkdf2Sha256, String> {
 83//!         if identity != &Identity::Username(USERNAME.to_owned()) {
 84//!             Err("authentication failed".to_owned())
 85//!         }
 86//!         else {
 87//!             let digest = sasl::common::scram::Sha256::derive
 88//!                 ( &Password::Plain((PASSWORD.to_owned()))
 89//!                 , &SALT[..]
 90//!                 , ITERATIONS )?;
 91//!             Ok(secret::Pbkdf2Sha256 {
 92//!                 salt: SALT.to_vec(),
 93//!                 iterations: ITERATIONS,
 94//!                 digest: digest,
 95//!             })
 96//!         }
 97//!     }
 98//! }
 99//!
100//! impl_validator_using_provider!(MyValidator, secret::Pbkdf2Sha256);
101//!
102//! fn finish<CM, SM>(cm: &mut CM, sm: &mut SM) -> Result<Identity, String>
103//!     where CM: ClientMechanism,
104//!           SM: ServerMechanism {
105//!     let init = cm.initial()?;
106//!     println!("C: {}", String::from_utf8_lossy(&init));
107//!     let mut resp = sm.respond(&init)?;
108//!     loop {
109//!         let msg;
110//!         match resp {
111//!             Response::Proceed(ref data) => {
112//!                 println!("S: {}", String::from_utf8_lossy(&data));
113//!                 msg = cm.response(data)?;
114//!                 println!("C: {}", String::from_utf8_lossy(&msg));
115//!             },
116//!             _ => break,
117//!         }
118//!         resp = sm.respond(&msg)?;
119//!     }
120//!     if let Response::Success(ret, fin) = resp {
121//!         println!("S: {}", String::from_utf8_lossy(&fin));
122//!         cm.success(&fin)?;
123//!         Ok(ret)
124//!     }
125//!     else {
126//!         unreachable!();
127//!     }
128//! }
129//!
130//! fn main() {
131//!     let mut mech = ServerPlain::new(MyValidator);
132//!     let expected_response = Response::Success(Identity::Username("user".to_owned()), Vec::new());
133//!     assert_eq!(mech.respond(b"\0user\0pencil"), Ok(expected_response));
134//!
135//!     let mut mech = ServerPlain::new(MyValidator);
136//!     assert_eq!(mech.respond(b"\0user\0marker"), Err("authentication failed".to_owned()));
137//!
138//!     let creds = Credentials::default()
139//!                             .with_username(USERNAME)
140//!                             .with_password(PASSWORD);
141//!     let mut client_mech = ClientPlain::from_credentials(creds.clone()).unwrap();
142//!     let mut server_mech = ServerPlain::new(MyValidator);
143//!
144//!     assert_eq!(finish(&mut client_mech, &mut server_mech), Ok(Identity::Username(USERNAME.to_owned())));
145//!
146//!     let mut client_mech = ClientScram::<Sha1>::from_credentials(creds.clone()).unwrap();
147//!     let mut server_mech = ServerScram::<Sha1, _>::new(MyValidator, ChannelBinding::Unsupported);
148//!
149//!     assert_eq!(finish(&mut client_mech, &mut server_mech), Ok(Identity::Username(USERNAME.to_owned())));
150//!
151//!     let mut client_mech = ClientScram::<Sha256>::from_credentials(creds.clone()).unwrap();
152//!     let mut server_mech = ServerScram::<Sha256, _>::new(MyValidator, ChannelBinding::Unsupported);
153//!
154//!     assert_eq!(finish(&mut client_mech, &mut server_mech), Ok(Identity::Username(USERNAME.to_owned())));
155//! }
156//! ```
157//!
158//! # Usage
159//!
160//! You can use this in your crate by adding this under `dependencies` in your `Cargo.toml`:
161//!
162//! ```toml,ignore
163//! sasl = "*"
164//! ```
165
166mod error;
167
168pub mod client;
169#[macro_use]
170pub mod server;
171pub mod common;
172pub mod secret;
173
174pub use crate::error::Error;