aboutsummaryrefslogtreecommitdiffstats
path: root/lib/auth-service/src/service/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/auth-service/src/service/mod.rs')
-rw-r--r--lib/auth-service/src/service/mod.rs71
1 files changed, 68 insertions, 3 deletions
diff --git a/lib/auth-service/src/service/mod.rs b/lib/auth-service/src/service/mod.rs
index 5150221..80b29de 100644
--- a/lib/auth-service/src/service/mod.rs
+++ b/lib/auth-service/src/service/mod.rs
@@ -1,15 +1,71 @@
use async_session::{Result, Session, SessionStore};
use async_trait::async_trait;
use shared_svc::cache::{CacheKey, RedisManager, redis::AsyncCommands};
-use sqlx::PgPool;
+use sqlx::{PgPool, Postgres, Transaction};
use tracing::{debug, instrument};
+use crate::Provider;
+
+#[async_trait]
+pub trait AccountMgr {
+ async fn get_apid_by_email(&self, email: &str) -> Result<Option<String>>;
+ async fn create_account(&self, provider: Provider, provider_user_id: &str, ap_id: &str);
+ async fn create_account_step(
+ &self,
+ provider: Provider,
+ provider_user_id: &str,
+ ap_id: &str,
+ email: &str,
+ transaction: &mut Transaction<'_, Postgres>,
+ ) -> Result;
+ async fn persist_session(&self);
+}
+
#[derive(Debug, Clone)]
pub struct AuthService {
cache: RedisManager,
database: PgPool,
}
+#[async_trait]
+impl AccountMgr for AuthService {
+ #[instrument(skip(self))]
+ async fn get_apid_by_email(&self, email: &str) -> Result<Option<String>> {
+ todo!()
+ }
+
+ #[instrument(skip(self))]
+ async fn create_account(&self, provider: Provider, provider_user_id: &str, ap_id: &str) {
+ todo!()
+ }
+ #[instrument(skip(self, transaction))]
+ async fn create_account_step(
+ &self,
+ provider: Provider,
+ provider_user_id: &str,
+ ap_id: &str,
+ email: &str,
+ transaction: &mut Transaction<'_, Postgres>,
+ ) -> Result {
+ sqlx::query!(
+ "insert into account
+ (provider_id, provider_user_id, email, ap_id)
+ values ($1, $2, $3, $4)
+ ",
+ "",
+ provider_user_id,
+ "",
+ ap_id
+ )
+ .execute(&mut **transaction)
+ .await?;
+ todo!()
+ }
+
+ #[instrument(skip(self))]
+ async fn persist_session(&self) {}
+}
+
impl AuthService {
pub fn new(cache: &RedisManager, database: &PgPool) -> Self {
Self {
@@ -26,9 +82,18 @@ impl SessionStore for AuthService {
#[doc = " The input is expected to be the value of an identifying"]
#[doc = " cookie. This will then be parsed by the session middleware"]
#[doc = " into a session if possible"]
- #[instrument(skip(self))]
+ #[instrument(skip(self, cookie_value))]
async fn load_session(&self, cookie_value: String) -> Result<Option<Session>> {
- todo!()
+ debug!("getting session");
+ let id = Session::id_from_cookie_value(&cookie_value)?;
+ let mut client = self.cache.get().await?;
+ let session = client
+ .get::<_, Option<Vec<u8>>>(CacheKey::Session(&id))
+ .await?;
+ match session {
+ Some(value) => Ok(Some(serde_json::from_slice(&value)?)),
+ None => Ok(None),
+ }
}
#[doc = " Store a session on the storage backend."]