chore: Started authentication process with axum-login

This commit is contained in:
Cyper committed 2026-03-01 03:09:10 -05:00
1 parent dc47f52e2e
commit 5e79129cde
4 files changed
+378

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
use std::collections::HashMap;
use axum_login::{AuthUser, AuthnBackend, UserId};
// User model - whatever type for `id` is used here needs to be reflected
// in `AuthUser::Id` type value as well
#[derive(Debug, Clone)]
pub struct User {
id: i32,
pw_hash: Vec<u8>,
}
impl AuthUser for User {
type Id = i32;
fn id(&self) -> Self::Id {
self.id
}
// TODO: In the future, this would also be based off any other security
// factors, like email, phone number, 2FA/MFA, etc., so changing any of
// those factors would invalidate the session
fn session_auth_hash(&self) -> &[u8] {
&self.pw_hash
}
}
#[derive(Clone)]
struct Backend {
// Placeholder for a list of sessions that could/would come from database
users: HashMap<i32, User>,
}
// This is what is used to attempt to login. In this case, if someone attempts
// to login with just a user_id, they will succeed if it is a valid ID. To make
// this a user/password login, the properties could be `username: String` and
// `password: String`, then update the authentication function to get a user_id
// from that to return from the `users.get(&User::Id)`
#[derive(Clone)]
struct Credentials {
user_id: i32,
}
impl AuthnBackend for Backend {
type User = User;
type Credentials = Credentials;
type Error = std::convert::Infallible;
async fn authenticate(
&self,
Credentials { user_id }: Self::Credentials,
) -> Result<Option<Self::User>, Self::Error> {
// ex:
// if let Some(user) = self.find_user_by_username(&creds.username) {
// if verify_password(&creds.password, &user.password_hash) {
// return Ok(Some(user));
// }
// }
// Ok(None)
Ok(self.users.get(&user_id).cloned())
}
async fn get_user(&self, user_id: &UserId<Self>) -> Result<Option<Self::User>, Self::Error> {
Ok(self.users.get(user_id).cloned())
}
}
+1
View File
@@ -1,2 +1,3 @@
pub mod auth;
pub mod config;
pub mod state;