Implement axum-login
All checks were successful
Push Workflows / rustfmt (push) Successful in 5s
Push Workflows / tailwind-build (push) Successful in 7s
Push Workflows / docs (push) Successful in 40s
Push Workflows / clippy (push) Successful in 46s
Push Workflows / test (push) Successful in 1m2s
Push Workflows / build (push) Successful in 1m54s
Push Workflows / nix-build (push) Successful in 5m20s
All checks were successful
Push Workflows / rustfmt (push) Successful in 5s
Push Workflows / tailwind-build (push) Successful in 7s
Push Workflows / docs (push) Successful in 40s
Push Workflows / clippy (push) Successful in 46s
Push Workflows / test (push) Successful in 1m2s
Push Workflows / build (push) Successful in 1m54s
Push Workflows / nix-build (push) Successful in 5m20s
This commit is contained in:
91
src/server/auth.rs
Normal file
91
src/server/auth.rs
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
use axum_login::{AuthUser, AuthnBackend, UserId};
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use diesel_async::RunQueryDsl;
|
||||||
|
|
||||||
|
use crate::models::user::{DbUser, UserCredentials};
|
||||||
|
use crate::server::database::{DbConn, DbPool};
|
||||||
|
use crate::util::error::{Contextualize, Error, Result};
|
||||||
|
|
||||||
|
impl AuthUser for DbUser {
|
||||||
|
type Id = i32;
|
||||||
|
|
||||||
|
fn id(&self) -> Self::Id {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
|
||||||
|
fn session_auth_hash(&self) -> &[u8] {
|
||||||
|
self.hashed_password.auth_hash()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct AuthBackend {
|
||||||
|
pub db_pool: DbPool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AuthnBackend for AuthBackend {
|
||||||
|
type User = DbUser;
|
||||||
|
type Credentials = UserCredentials;
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
async fn authenticate(
|
||||||
|
&self,
|
||||||
|
attempt_creds: Self::Credentials,
|
||||||
|
) -> Result<Option<Self::User>, Self::Error> {
|
||||||
|
let mut db_conn = self
|
||||||
|
.db_pool
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.err_context("Failed to get database pool connection")?;
|
||||||
|
|
||||||
|
let user = get_user_by_username(&mut db_conn, attempt_creds.username)
|
||||||
|
.await
|
||||||
|
.err_context("Error fetching user for authentication check")?;
|
||||||
|
|
||||||
|
let Some(user) = user else { return Ok(None) };
|
||||||
|
|
||||||
|
let password_result = user
|
||||||
|
.hashed_password
|
||||||
|
.check(attempt_creds.password)
|
||||||
|
.err_context("Error checking user password attempt")?;
|
||||||
|
|
||||||
|
if password_result {
|
||||||
|
Ok(Some(user))
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_user(&self, user_id: &UserId<Self>) -> Result<Option<Self::User>, Self::Error> {
|
||||||
|
let mut db_conn = self
|
||||||
|
.db_pool
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.err_context("Failed to get database pool connection")?;
|
||||||
|
|
||||||
|
get_user_by_id(&mut db_conn, *user_id)
|
||||||
|
.await
|
||||||
|
.err_context("Failed fetching user for session")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_user_by_id(db_conn: &mut DbConn, id: i32) -> Result<Option<DbUser>> {
|
||||||
|
crate::schema::users::table
|
||||||
|
.find(id)
|
||||||
|
.first(db_conn)
|
||||||
|
.await
|
||||||
|
.optional()
|
||||||
|
.err_context("Error fetching user from database by id")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_user_by_username(
|
||||||
|
db_conn: &mut DbConn,
|
||||||
|
username: String,
|
||||||
|
) -> Result<Option<DbUser>> {
|
||||||
|
crate::schema::users::table
|
||||||
|
.filter(crate::schema::users::username.eq(username))
|
||||||
|
.first(db_conn)
|
||||||
|
.await
|
||||||
|
.optional()
|
||||||
|
.err_context("Error fetching user from database by username")
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
pub mod auth;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod database;
|
pub mod database;
|
||||||
pub mod key_val_store;
|
pub mod key_val_store;
|
||||||
|
|||||||
Reference in New Issue
Block a user