pub struct AuthProvider<T>
where T: AuthDatabase,
{ /* private fields */ }
Expand description

Represents an authentication provider. Contains a database of users and provides methods for managing authentication.

If the database needs to be used from elsewhere in the program, it is advisable to put the database behind an Arc and Mutex/RwLock and store a cloned reference to the database in the users field of this struct.

Implementations§

source§

impl<T> AuthProvider<T>
where T: AuthDatabase,

source

pub fn new(users: T) -> Self

Create a new authentication provider with the given database.

source

pub fn with_config(self, config: AuthConfig) -> Self

Use the given configuration for this authentication provider.

source

pub fn create_user( &mut self, password: impl AsRef<str> ) -> Result<String, AuthError>

Create a user with the given password. Returns the UID of the new user.

source

pub fn exists(&mut self, uid: impl AsRef<str>) -> bool

Returns true if the user with the given UID exists.

source

pub fn verify(&self, uid: impl AsRef<str>, password: impl AsRef<str>) -> bool

Verifies that the given password matches the password of the user with the given UID.

source

pub fn remove_user(&mut self, uid: impl AsRef<str>) -> Result<(), AuthError>

Removes the user with the given UID.

source

pub fn create_session( &mut self, uid: impl AsRef<str> ) -> Result<String, AuthError>

Creates a new session for the user with the given UID, returning the token.

The session will expire after the configured duration.

source

pub fn create_session_with_lifetime( &mut self, uid: impl AsRef<str>, lifetime: u64 ) -> Result<String, AuthError>

Creates a new session for the user with the given UID, returning the token.

The session will expire after the given lifetime (in seconds).

source

pub fn refresh_session( &mut self, token: impl AsRef<str> ) -> Result<(), AuthError>

Refreshes the session with the given token. If successful, the token will be set to expire after the configured duration.

source

pub fn invalidate_session(&mut self, token: impl AsRef<str>)

Invalidates the given token, if it exists.

source

pub fn invalidate_user_session(&mut self, uid: impl AsRef<str>)

Invalidates the session of the user with the given UID, if they have one.

source

pub fn get_uid_by_token( &self, token: impl AsRef<str> ) -> Result<String, AuthError>

Gets the UID of the user with the given token.

Trait Implementations§

source§

impl<T> Default for AuthProvider<T>
where T: AuthDatabase + Default,

source§

fn default() -> AuthProvider<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for AuthProvider<T>
where T: RefUnwindSafe,

§

impl<T> Send for AuthProvider<T>
where T: Send,

§

impl<T> Sync for AuthProvider<T>
where T: Sync,

§

impl<T> Unpin for AuthProvider<T>
where T: Unpin,

§

impl<T> UnwindSafe for AuthProvider<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.