From 30e3da71e99a4bc34811fa2c2123ca73695cc33c Mon Sep 17 00:00:00 2001 From: Natty Date: Fri, 22 Sep 2023 19:57:53 +0200 Subject: [PATCH] Changed the Calckey model export to be a separate module --- ext_calckey_model/entity_ck/Cargo.toml | 4 +-- ext_calckey_model/src/lib.rs | 4 ++- src/service/user_cache.rs | 36 +++++++++++++++----------- src/web/auth.rs | 16 ++++++------ 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/ext_calckey_model/entity_ck/Cargo.toml b/ext_calckey_model/entity_ck/Cargo.toml index 3c5ad76..a1a7b7e 100644 --- a/ext_calckey_model/entity_ck/Cargo.toml +++ b/ext_calckey_model/entity_ck/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ck" -version = "0.1.0" -edition = "2021" +version.workspace = true +edition.workspace = true publish = false [dependencies] diff --git a/ext_calckey_model/src/lib.rs b/ext_calckey_model/src/lib.rs index ee74e8e..1fb0704 100644 --- a/ext_calckey_model/src/lib.rs +++ b/ext_calckey_model/src/lib.rs @@ -1,5 +1,7 @@ +pub use ck; +use ck::*; + use chrono::Utc; -pub use ck::*; use ext_calckey_model_migration::{Migrator, MigratorTrait}; use futures_util::StreamExt; use redis::IntoConnectionInfo; diff --git a/src/service/user_cache.rs b/src/service/user_cache.rs index 3f8816a..10bc576 100644 --- a/src/service/user_cache.rs +++ b/src/service/user_cache.rs @@ -1,7 +1,7 @@ use crate::web::ApiError; use cached::{Cached, TimedCache}; use magnetar_calckey_model::{ - user, CalckeyCache, CalckeyCacheError, CalckeyDbError, CalckeyModel, CalckeySub, + ck, CalckeyCache, CalckeyCacheError, CalckeyDbError, CalckeyModel, CalckeySub, InternalStreamMessage, SubMessage, }; use magnetar_common::config::MagnetarConfig; @@ -35,13 +35,13 @@ impl From for ApiError { struct UserCache { lifetime: TimedCache, - id_to_user: HashMap>, - token_to_user: HashMap>, - uri_to_user: HashMap>, + id_to_user: HashMap>, + token_to_user: HashMap>, + uri_to_user: HashMap>, } impl UserCache { - fn purge(&mut self, user: impl AsRef) { + fn purge(&mut self, user: impl AsRef) { let user = user.as_ref(); self.lifetime.cache_remove(&user.id); @@ -56,7 +56,7 @@ impl UserCache { } } - fn refresh(&mut self, user: Arc) { + fn refresh(&mut self, user: Arc) { self.purge(&user); self.lifetime.cache_set(user.id.clone(), ()); @@ -74,13 +74,13 @@ impl UserCache { /// Low-priority refresh. Only refreshes the cache if the user is not there. /// Used mostly for getters that would otherwise data race with more important refreshes. - fn maybe_refresh(&mut self, user: &Arc) { + fn maybe_refresh(&mut self, user: &Arc) { if self.lifetime.cache_get(&user.id).is_none() { self.refresh(user.clone()); } } - fn get_by_id(&mut self, id: &str) -> Option> { + fn get_by_id(&mut self, id: &str) -> Option> { if let Some(user) = self.id_to_user.get(id).cloned() { if self.lifetime.cache_get(id).is_none() { self.purge(&user); @@ -93,7 +93,7 @@ impl UserCache { None } - fn get_by_token(&mut self, token: &str) -> Option> { + fn get_by_token(&mut self, token: &str) -> Option> { if let Some(user) = self.token_to_user.get(token).cloned() { if self.lifetime.cache_get(&user.id).is_none() { self.purge(&user); @@ -106,7 +106,7 @@ impl UserCache { None } - fn get_by_uri(&mut self, uri: &str) -> Option> { + fn get_by_uri(&mut self, uri: &str) -> Option> { if let Some(user) = self.uri_to_user.get(uri).cloned() { if self.lifetime.cache_get(&user.id).is_none() { self.purge(&user); @@ -188,8 +188,8 @@ impl UserCacheService { async fn map_cache_user( &self, - user: Option, - ) -> Result>, UserCacheError> { + user: Option, + ) -> Result>, UserCacheError> { if let Some(user) = user { let user = Arc::new(user); self.cache.lock().await.maybe_refresh(&user); @@ -202,7 +202,7 @@ impl UserCacheService { pub async fn get_by_token( &self, token: &str, - ) -> Result>, UserCacheError> { + ) -> Result>, UserCacheError> { let result = self.cache.lock().await.get_by_token(token); if let Some(user) = result { @@ -213,7 +213,10 @@ impl UserCacheService { .await } - pub async fn get_by_uri(&self, uri: &str) -> Result>, UserCacheError> { + pub async fn get_by_uri( + &self, + uri: &str, + ) -> Result>, UserCacheError> { let result = self.cache.lock().await.get_by_uri(uri); if let Some(user) = result { @@ -225,7 +228,10 @@ impl UserCacheService { self.map_cache_user(user).await } - pub async fn get_by_id(&self, id: &str) -> Result>, UserCacheError> { + pub async fn get_by_id( + &self, + id: &str, + ) -> Result>, UserCacheError> { let result = self.cache.lock().await.get_by_id(id); if let Some(user) = result { diff --git a/src/web/auth.rs b/src/web/auth.rs index 1073f19..7483cb5 100644 --- a/src/web/auth.rs +++ b/src/web/auth.rs @@ -10,7 +10,7 @@ use axum::middleware::Next; use axum::response::{IntoResponse, Response}; use headers::authorization::Bearer; use headers::{Authorization, HeaderMapExt}; -use magnetar_calckey_model::{access_token, user, CalckeyDbError}; +use magnetar_calckey_model::{ck, CalckeyDbError}; use std::convert::Infallible; use std::sync::Arc; use strum::IntoStaticStr; @@ -20,17 +20,17 @@ use tracing::error; #[derive(Debug)] pub enum AuthMode { User { - user: Arc, + user: Arc, }, AccessToken { - user: Arc, - access_token: Arc, + user: Arc, + access_token: Arc, }, Anonymous, } impl AuthMode { - fn get_user(&self) -> Option<&Arc> { + fn get_user(&self) -> Option<&Arc> { match self { AuthMode::User { user } | AuthMode::AccessToken { user, .. } => Some(user), AuthMode::Anonymous => None, @@ -62,10 +62,10 @@ impl IntoResponse for AuthUserRejection { #[derive(Clone, FromRequestParts)] #[from_request(via(axum::Extension), rejection(AuthUserRejection))] -pub struct AuthenticatedUser(pub Arc); +pub struct AuthenticatedUser(pub Arc); #[derive(Clone)] -pub struct MaybeUser(pub Option>); +pub struct MaybeUser(pub Option>); #[async_trait] impl FromRequestParts for MaybeUser { @@ -211,7 +211,7 @@ impl AuthState { return match self.service.db.get_app_by_id(app_id).await? { Some(app) => Ok(AuthMode::AccessToken { user, - access_token: Arc::new(access_token::Model { + access_token: Arc::new(ck::access_token::Model { permission: app.permission, ..access_token }),