From 7abb5af40a99d451cae3c3578b964983e8bc68a6 Mon Sep 17 00:00:00 2001 From: John Lewis Date: Wed, 31 Jan 2024 14:35:18 -0600 Subject: [PATCH] scaffolded auth backend type --- engine/Cargo.lock | 4 +++ engine/Cargo.toml | 1 + engine/crates/auth/Cargo.toml | 5 ++++ engine/crates/auth/src/lib.rs | 45 +++++++++++++++++++++++++++- engine/crates/clients/src/surreal.rs | 1 + 5 files changed, 55 insertions(+), 1 deletion(-) diff --git a/engine/Cargo.lock b/engine/Cargo.lock index 8bf5b0b..f87084c 100644 --- a/engine/Cargo.lock +++ b/engine/Cargo.lock @@ -215,9 +215,13 @@ dependencies = [ name = "auth" version = "0.1.0" dependencies = [ + "async-trait", "axum-login", + "clients", + "color-eyre", "serde", "surrealdb", + "thiserror", ] [[package]] diff --git a/engine/Cargo.toml b/engine/Cargo.toml index e41ddea..0c9f2c5 100644 --- a/engine/Cargo.toml +++ b/engine/Cargo.toml @@ -7,6 +7,7 @@ members = ["crates/*"] tokio = { version = "1.35.1", features = ["full"] } color-eyre = "0.6.2" axum = "0.7.4" +thiserror = "1.0.56" tracing = "0.1.40" tracing-subscriber = "0.3.18" serde = { version = "1.0.196", features = ["derive"] } diff --git a/engine/crates/auth/Cargo.toml b/engine/crates/auth/Cargo.toml index b36b8c7..1281bd2 100644 --- a/engine/crates/auth/Cargo.toml +++ b/engine/crates/auth/Cargo.toml @@ -8,4 +8,9 @@ edition = "2021" [dependencies] axum-login = "0.13.1" serde.workspace = true +color-eyre.workspace = true +thiserror.workspace = true surrealdb.workspace = true + +clients = { path = "../clients" } +async-trait = "0.1.77" diff --git a/engine/crates/auth/src/lib.rs b/engine/crates/auth/src/lib.rs index e862042..214f233 100644 --- a/engine/crates/auth/src/lib.rs +++ b/engine/crates/auth/src/lib.rs @@ -1,4 +1,4 @@ -use axum_login::AuthUser; +use axum_login::{AuthUser, AuthnBackend, UserId}; use serde::{Deserialize, Serialize}; use surrealdb::sql::Thing; @@ -25,3 +25,46 @@ impl AuthUser for AuthenticatedUser { fn id(&self) -> Self::Id { self.id.clone() } fn session_auth_hash(&self) -> &[u8] { self.password.as_bytes() } } + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Credentials { + pub username: String, + pub password: String, + pub next: Option, +} + +#[derive(Clone)] +pub struct Backend { + surreal_client: clients::surreal::SurrealRootClient, +} + +impl Backend { + pub async fn new() -> color_eyre::Result { + Ok(Self { + surreal_client: clients::surreal::SurrealRootClient::new().await?, + }) + } +} + +#[async_trait::async_trait] +impl AuthnBackend for Backend { + type User = AuthenticatedUser; + type Credentials = Credentials; + type Error = std::convert::Infallible; + + async fn authenticate( + &self, + credentials: Self::Credentials, + ) -> Result, Self::Error> { + let surreal_client = &self.surreal_client; + + Ok(None) + } + + async fn get_user( + &self, + user_id: &UserId, + ) -> Result, Self::Error> { + Ok(None) + } +} diff --git a/engine/crates/clients/src/surreal.rs b/engine/crates/clients/src/surreal.rs index 765a3c0..a79e37d 100644 --- a/engine/crates/clients/src/surreal.rs +++ b/engine/crates/clients/src/surreal.rs @@ -10,6 +10,7 @@ use surrealdb::{ }; /// A root-level client for the SurrealDB database. +#[derive(Clone, Debug)] pub struct SurrealRootClient { client: surrealdb::Surreal, }