Struct humphrey_auth::AuthProvider
source · 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,
impl<T> AuthProvider<T>where
T: AuthDatabase,
sourcepub fn with_config(self, config: AuthConfig) -> Self
pub fn with_config(self, config: AuthConfig) -> Self
Use the given configuration for this authentication provider.
sourcepub fn create_user(
&mut self,
password: impl AsRef<str>
) -> Result<String, AuthError>
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.
sourcepub fn exists(&mut self, uid: impl AsRef<str>) -> bool
pub fn exists(&mut self, uid: impl AsRef<str>) -> bool
Returns true if the user with the given UID exists.
sourcepub fn verify(&self, uid: impl AsRef<str>, password: impl AsRef<str>) -> bool
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.
sourcepub fn remove_user(&mut self, uid: impl AsRef<str>) -> Result<(), AuthError>
pub fn remove_user(&mut self, uid: impl AsRef<str>) -> Result<(), AuthError>
Removes the user with the given UID.
sourcepub fn create_session(
&mut self,
uid: impl AsRef<str>
) -> Result<String, AuthError>
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.
sourcepub fn create_session_with_lifetime(
&mut self,
uid: impl AsRef<str>,
lifetime: u64
) -> Result<String, AuthError>
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).
sourcepub fn refresh_session(
&mut self,
token: impl AsRef<str>
) -> Result<(), AuthError>
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.
sourcepub fn invalidate_session(&mut self, token: impl AsRef<str>)
pub fn invalidate_session(&mut self, token: impl AsRef<str>)
Invalidates the given token, if it exists.
sourcepub fn invalidate_user_session(&mut self, uid: impl AsRef<str>)
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.