AuthSession in progress

This commit is contained in:
Joshua Perry 2024-06-07 02:24:42 +01:00
parent 1644c9c51c
commit 56ef6e3df9
4 changed files with 76 additions and 15 deletions

View File

@ -1,9 +1,15 @@
pub mod db;
use axum::{
Router,
routing::{get, post}
};
use axum_session_auth::AuthSession;
use axum_session_mongo::SessionMongoPool;
use db::User;
use http::method::Method;
use mongodb::{Client, bson::oid::ObjectId};
mod db;
pub fn router() -> Router {
Router::new()
@ -35,3 +41,7 @@ pub async fn post_sign_up() {}
pub async fn post_backup() {}
pub async fn get_restore() {}
async fn auth(method: Method, auth: AuthSession<User, ObjectId, SessionMongoPool, Client>) {
//TODO: Auth loop (get from crate example)
}

View File

@ -1,15 +1,12 @@
use anyhow::Result;
use axum::async_trait;
use axum_session_auth::Authentication;
use serde::{Deserialize, Serialize};
use mongodb::{
bson::{DateTime, oid::ObjectId},
bson::{doc, oid::ObjectId, DateTime},
Client,
Database,
error::Error,
};
pub async fn get_database_client() -> Result<Database, Error> {
Ok(Client::with_uri_str("mongodb:://localhost:27017").await?.database("dermy"))
}
#[derive(Serialize, Deserialize)]
pub struct Mole {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
@ -87,26 +84,51 @@ impl LogEntry {
}
}
#[derive(Serialize, Deserialize)]
#[derive(Clone, Serialize, Deserialize)]
pub struct User {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<ObjectId>,
#[serde(rename="_auth")]
pub auth: Auth,
pub username: String,
#[serde(skip_serializing)]
logged_in: bool,
}
impl User {
pub fn new(username: String) -> Self {
let id = None;
let auth = Default::default();
let logged_in = false;
Self {
id,
auth,
username,
logged_in,
}
}
}
#[derive(Default, Serialize, Deserialize)]
#[async_trait]
impl Authentication<User, ObjectId, Client> for User {
async fn load_user(user_id: ObjectId, db: Option<&Client>) -> Result<User> {
Ok(db.unwrap()
.database("dermy")
.collection::<User>("users")
.find_one(doc! {"_id": user_id}, None).await?.unwrap())
}
fn is_authenticated(&self) -> bool {
self.logged_in
}
fn is_active(&self) -> bool {
self.logged_in
}
fn is_anonymous(&self) -> bool {
!self.logged_in
}
}
#[derive(Clone, Default, Serialize, Deserialize)]
pub struct Auth {
#[serde(rename = "_hash", skip_serializing_if = "Option::is_none")]
pub hash: Option<String>,

View File

@ -1,11 +1,29 @@
mod account;
mod model;
pub async fn run() {
let app = router();
use account::db::User;
use anyhow::Result;
use axum_session::{SessionConfig, SessionLayer, SessionStore};
use axum_session_auth::{AuthConfig, AuthSessionLayer};
use axum_session_mongo::SessionMongoPool;
use mongodb::{bson::oid::ObjectId, Client};
pub async fn run() -> Result<()> {
let db = Client::with_uri_str("mongodb://localhost:27017").await?;
let session_store = session(db.clone()).await?;
let auth_config = AuthConfig::<ObjectId>::default();
let app = router()
.layer(SessionLayer::new(session_store))
.layer(AuthSessionLayer::<User, ObjectId, SessionMongoPool, Client>
::new(Some(db)).with_config(auth_config)
);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
axum::serve(listener, app).await?;
Ok(())
}
fn router() -> axum::Router {
@ -13,3 +31,11 @@ fn router() -> axum::Router {
.nest("/account", account::router())
.nest("/predict", model::router())
}
async fn session(db: Client) -> Result<SessionStore<SessionMongoPool>> {
let session_config = SessionConfig::default()
.with_table_name("sessions");
Ok(SessionStore::<SessionMongoPool>
::new(Some(db.clone().into()), session_config).await?)
}

View File

@ -1,4 +1,7 @@
use anyhow::Result;
#[tokio::main]
async fn main() {
dermy_server::run().await;
async fn main() -> Result<()> {
dermy_server::run().await?;
Ok(())
}