sasl: Make this crate no_std
We mostly had to import from alloc some structs that are part of the std prelude, such as Vec and String.
This commit is contained in:
parent
0267383803
commit
dc842c44d1
14 changed files with 57 additions and 19 deletions
|
|
@ -1,3 +1,7 @@
|
||||||
|
Version NEXT, released 20??-??-??:
|
||||||
|
* Improvements
|
||||||
|
- This crate is now `no_std`, you can use it even on platforms which don’t provide the `std` crate.
|
||||||
|
|
||||||
Version 0.5.2, released 2024-07-22:
|
Version 0.5.2, released 2024-07-22:
|
||||||
* Improvements
|
* Improvements
|
||||||
- Add SCRAM client extensions support (thanks to Lucas Kent)
|
- Add SCRAM client extensions support (thanks to Lucas Kent)
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ What's this?
|
||||||
|
|
||||||
A crate which handles SASL authentication. Still unstable until 1.0.0.
|
A crate which handles SASL authentication. Still unstable until 1.0.0.
|
||||||
|
|
||||||
|
It can be used in `no_std` environments.
|
||||||
|
|
||||||
Can I see an example?
|
Can I see an example?
|
||||||
---------------------
|
---------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
use crate::client::{Mechanism, MechanismError};
|
use crate::client::{Mechanism, MechanismError};
|
||||||
use crate::common::{Credentials, Identity, Password, Secret};
|
use crate::common::{Credentials, Identity, Password, Secret};
|
||||||
|
use alloc::string::String;
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
/// A struct for the SASL PLAIN mechanism.
|
/// A struct for the SASL PLAIN mechanism.
|
||||||
pub struct Plain {
|
pub struct Plain {
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,10 @@ use crate::common::{parse_frame, xor, ChannelBinding, Credentials, Identity, Pas
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
|
|
||||||
use std::marker::PhantomData;
|
use alloc::format;
|
||||||
|
use alloc::string::String;
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
use core::marker::PhantomData;
|
||||||
|
|
||||||
enum ScramState {
|
enum ScramState {
|
||||||
Init,
|
Init,
|
||||||
|
|
@ -226,6 +229,8 @@ mod tests {
|
||||||
use crate::client::mechanisms::Scram;
|
use crate::client::mechanisms::Scram;
|
||||||
use crate::client::Mechanism;
|
use crate::client::Mechanism;
|
||||||
use crate::common::scram::{Sha1, Sha256};
|
use crate::common::scram::{Sha1, Sha256};
|
||||||
|
use alloc::borrow::ToOwned;
|
||||||
|
use alloc::string::String;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn scram_sha1_works() {
|
fn scram_sha1_works() {
|
||||||
|
|
@ -293,13 +298,13 @@ mod tests {
|
||||||
.with_first_extensions("tokenauth=true".to_owned());
|
.with_first_extensions("tokenauth=true".to_owned());
|
||||||
let init = mechanism.initial();
|
let init = mechanism.initial();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
std::str::from_utf8(&init).unwrap(),
|
core::str::from_utf8(&init).unwrap(),
|
||||||
std::str::from_utf8(client_init).unwrap()
|
core::str::from_utf8(client_init).unwrap()
|
||||||
); // depends on ordering…
|
); // depends on ordering…
|
||||||
let resp = mechanism.response(server_init).unwrap();
|
let resp = mechanism.response(server_init).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
std::str::from_utf8(&resp).unwrap(),
|
core::str::from_utf8(&resp).unwrap(),
|
||||||
std::str::from_utf8(client_final).unwrap()
|
core::str::from_utf8(client_final).unwrap()
|
||||||
); // again, depends on ordering…
|
); // again, depends on ordering…
|
||||||
mechanism.success(server_final).unwrap();
|
mechanism.success(server_final).unwrap();
|
||||||
}
|
}
|
||||||
|
|
@ -318,13 +323,13 @@ mod tests {
|
||||||
.with_final_extensions("foo=true".to_owned());
|
.with_final_extensions("foo=true".to_owned());
|
||||||
let init = mechanism.initial();
|
let init = mechanism.initial();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
std::str::from_utf8(&init).unwrap(),
|
core::str::from_utf8(&init).unwrap(),
|
||||||
std::str::from_utf8(client_init).unwrap()
|
core::str::from_utf8(client_init).unwrap()
|
||||||
); // depends on ordering…
|
); // depends on ordering…
|
||||||
let resp = mechanism.response(server_init).unwrap();
|
let resp = mechanism.response(server_init).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
std::str::from_utf8(&resp).unwrap(),
|
core::str::from_utf8(&resp).unwrap(),
|
||||||
std::str::from_utf8(client_final).unwrap()
|
core::str::from_utf8(client_final).unwrap()
|
||||||
); // again, depends on ordering…
|
); // again, depends on ordering…
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
use std::fmt;
|
use alloc::vec::Vec;
|
||||||
|
use core::fmt;
|
||||||
|
|
||||||
use crate::common::Credentials;
|
use crate::common::Credentials;
|
||||||
|
|
||||||
|
|
@ -84,7 +85,7 @@ impl fmt::Display for MechanismError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for MechanismError {}
|
impl core::error::Error for MechanismError {}
|
||||||
|
|
||||||
/// A trait which defines SASL mechanisms.
|
/// A trait which defines SASL mechanisms.
|
||||||
pub trait Mechanism {
|
pub trait Mechanism {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
use std::collections::BTreeMap;
|
use alloc::borrow::ToOwned;
|
||||||
use std::string::FromUtf8Error;
|
use alloc::collections::BTreeMap;
|
||||||
|
use alloc::string::{FromUtf8Error, String};
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
#[cfg(feature = "scram")]
|
#[cfg(feature = "scram")]
|
||||||
pub mod scram;
|
pub mod scram;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,7 @@
|
||||||
|
use alloc::string::{String, ToString};
|
||||||
|
use alloc::vec;
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
use core::fmt;
|
||||||
use getrandom::{getrandom, Error as RngError};
|
use getrandom::{getrandom, Error as RngError};
|
||||||
use hmac::{digest::InvalidLength, Hmac, Mac};
|
use hmac::{digest::InvalidLength, Hmac, Mac};
|
||||||
use pbkdf2::pbkdf2;
|
use pbkdf2::pbkdf2;
|
||||||
|
|
@ -25,8 +29,8 @@ pub enum DeriveError {
|
||||||
IncompatibleIterationCount(u32, u32),
|
IncompatibleIterationCount(u32, u32),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for DeriveError {
|
impl fmt::Display for DeriveError {
|
||||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
DeriveError::IncompatibleHashingMethod(one, two) => {
|
DeriveError::IncompatibleHashingMethod(one, two) => {
|
||||||
write!(fmt, "incompatible hashing method, {} is not {}", one, two)
|
write!(fmt, "incompatible hashing method, {} is not {}", one, two)
|
||||||
|
|
@ -40,7 +44,7 @@ impl std::fmt::Display for DeriveError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for DeriveError {}
|
impl core::error::Error for DeriveError {}
|
||||||
|
|
||||||
impl From<hmac::digest::InvalidLength> for DeriveError {
|
impl From<hmac::digest::InvalidLength> for DeriveError {
|
||||||
fn from(_err: hmac::digest::InvalidLength) -> DeriveError {
|
fn from(_err: hmac::digest::InvalidLength) -> DeriveError {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use alloc::string::String;
|
||||||
#[cfg(feature = "scram")]
|
#[cfg(feature = "scram")]
|
||||||
use getrandom::Error as RngError;
|
use getrandom::Error as RngError;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
//#![deny(missing_docs)]
|
//#![deny(missing_docs)]
|
||||||
|
#![no_std]
|
||||||
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||||
|
|
||||||
//! This crate provides a framework for SASL authentication and a few authentication mechanisms.
|
//! This crate provides a framework for SASL authentication and a few authentication mechanisms.
|
||||||
//!
|
//!
|
||||||
|
//! It can be used in `no_std` environments.
|
||||||
|
//!
|
||||||
//! # Examples
|
//! # Examples
|
||||||
//!
|
//!
|
||||||
//! ## Simple client-sided usage
|
//! ## Simple client-sided usage
|
||||||
|
|
@ -184,6 +187,8 @@
|
||||||
//! sasl = "*"
|
//! sasl = "*"
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
|
|
||||||
pub mod client;
|
pub mod client;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
#[cfg(feature = "scram")]
|
#[cfg(feature = "scram")]
|
||||||
use crate::common::scram::DeriveError;
|
use crate::common::scram::DeriveError;
|
||||||
|
use alloc::borrow::ToOwned;
|
||||||
|
use alloc::string::String;
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
pub trait Secret {}
|
pub trait Secret {}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
use crate::common::Identity;
|
use crate::common::Identity;
|
||||||
use crate::server::{Mechanism, MechanismError, Response};
|
use crate::server::{Mechanism, MechanismError, Response};
|
||||||
|
use alloc::format;
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
use getrandom::getrandom;
|
use getrandom::getrandom;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
use crate::common::Identity;
|
use crate::common::Identity;
|
||||||
use crate::secret;
|
use crate::secret;
|
||||||
use crate::server::{Mechanism, MechanismError, Response, Validator};
|
use crate::server::{Mechanism, MechanismError, Response, Validator};
|
||||||
|
use alloc::string::String;
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
pub struct Plain<V: Validator<secret::Plain>> {
|
pub struct Plain<V: Validator<secret::Plain>> {
|
||||||
validator: V,
|
validator: V,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,8 @@
|
||||||
use std::marker::PhantomData;
|
use alloc::borrow::ToOwned;
|
||||||
|
use alloc::format;
|
||||||
|
use alloc::string::{String, ToString};
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
use core::marker::PhantomData;
|
||||||
|
|
||||||
use base64::{engine::general_purpose::STANDARD as Base64, Engine};
|
use base64::{engine::general_purpose::STANDARD as Base64, Engine};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
use crate::common::Identity;
|
use crate::common::Identity;
|
||||||
use crate::secret::Secret;
|
use crate::secret::Secret;
|
||||||
use std::fmt;
|
use alloc::vec::Vec;
|
||||||
|
use core::error::Error;
|
||||||
|
use core::fmt;
|
||||||
|
|
||||||
#[cfg(feature = "scram")]
|
#[cfg(feature = "scram")]
|
||||||
use crate::common::scram::DeriveError;
|
use crate::common::scram::DeriveError;
|
||||||
|
|
@ -171,7 +173,6 @@ impl Error for ProviderError {}
|
||||||
|
|
||||||
impl Error for ValidatorError {}
|
impl Error for ValidatorError {}
|
||||||
|
|
||||||
use std::error::Error;
|
|
||||||
impl Error for MechanismError {
|
impl Error for MechanismError {
|
||||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||||
match self {
|
match self {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue