pub struct BasicAuthentication { /* private fields */ }Expand description
Basic Authentication backend
Passwords are hashed with Argon2id before storage. Verification uses the constant-time comparison built into Argon2.
Implementations§
Source§impl BasicAuthentication
impl BasicAuthentication
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new BasicAuthentication backend with no users.
§Examples
use reinhardt_auth::{HttpBasicAuth, AuthenticationBackend};
use bytes::Bytes;
use hyper::{HeaderMap, Method, Uri, Version};
use reinhardt_http::Request;
let auth = HttpBasicAuth::new();
let request = Request::builder()
.method(Method::GET)
.uri("/")
.body(Bytes::new())
.build()
.unwrap();
let result = auth.authenticate(&request).await.unwrap();
assert!(result.is_none());Sourcepub fn add_user(
&mut self,
username: impl Into<String>,
password: impl Into<String>,
)
pub fn add_user( &mut self, username: impl Into<String>, password: impl Into<String>, )
Adds a user with the given username and password.
The password is hashed with Argon2id before storage.
§Panics
Panics if password hashing fails (should not happen in practice).
§Examples
use reinhardt_auth::{HttpBasicAuth, AuthenticationBackend};
use bytes::Bytes;
use hyper::{HeaderMap, Method, Uri, Version};
use reinhardt_http::Request;
let mut auth = HttpBasicAuth::new();
auth.add_user("alice", "secret123");
auth.add_user("bob", "password456");
// Create a request with valid Basic auth credentials
// "alice:secret123" in base64 is "YWxpY2U6c2VjcmV0MTIz"
let mut headers = HeaderMap::new();
headers.insert("Authorization", "Basic YWxpY2U6c2VjcmV0MTIz".parse().unwrap());
let request = Request::builder()
.method(Method::GET)
.uri("/")
.headers(headers)
.body(Bytes::new())
.build()
.unwrap();
// Authentication should succeed
let result = auth.authenticate(&request).await.unwrap();
assert!(result.is_some());
assert_eq!(result.unwrap().get_username(), "alice");Trait Implementations§
Source§impl AuthenticationBackend for BasicAuthentication
impl AuthenticationBackend for BasicAuthentication
Source§fn authenticate<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Option<Box<dyn User>>, AuthenticationError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn authenticate<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Option<Box<dyn User>>, AuthenticationError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Authenticate a request and return a user if successful Read more
Source§impl Default for BasicAuthentication
impl Default for BasicAuthentication
Source§impl RestAuthentication for BasicAuthentication
impl RestAuthentication for BasicAuthentication
Source§fn authenticate<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Option<Box<dyn User>>, AuthenticationError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn authenticate<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Option<Box<dyn User>>, AuthenticationError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Authenticate a request and return a user if successful
Auto Trait Implementations§
impl Freeze for BasicAuthentication
impl RefUnwindSafe for BasicAuthentication
impl Send for BasicAuthentication
impl Sync for BasicAuthentication
impl Unpin for BasicAuthentication
impl UnsafeUnpin for BasicAuthentication
impl UnwindSafe for BasicAuthentication
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian().