Changed the Calckey model export to be a separate module

This commit is contained in:
Natty 2023-09-22 19:57:53 +02:00
parent 3bf0d16e8a
commit 30e3da71e9
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
4 changed files with 34 additions and 26 deletions

View File

@ -1,7 +1,7 @@
[package] [package]
name = "ck" name = "ck"
version = "0.1.0" version.workspace = true
edition = "2021" edition.workspace = true
publish = false publish = false
[dependencies] [dependencies]

View File

@ -1,5 +1,7 @@
pub use ck;
use ck::*;
use chrono::Utc; use chrono::Utc;
pub use ck::*;
use ext_calckey_model_migration::{Migrator, MigratorTrait}; use ext_calckey_model_migration::{Migrator, MigratorTrait};
use futures_util::StreamExt; use futures_util::StreamExt;
use redis::IntoConnectionInfo; use redis::IntoConnectionInfo;

View File

@ -1,7 +1,7 @@
use crate::web::ApiError; use crate::web::ApiError;
use cached::{Cached, TimedCache}; use cached::{Cached, TimedCache};
use magnetar_calckey_model::{ use magnetar_calckey_model::{
user, CalckeyCache, CalckeyCacheError, CalckeyDbError, CalckeyModel, CalckeySub, ck, CalckeyCache, CalckeyCacheError, CalckeyDbError, CalckeyModel, CalckeySub,
InternalStreamMessage, SubMessage, InternalStreamMessage, SubMessage,
}; };
use magnetar_common::config::MagnetarConfig; use magnetar_common::config::MagnetarConfig;
@ -35,13 +35,13 @@ impl From<UserCacheError> for ApiError {
struct UserCache { struct UserCache {
lifetime: TimedCache<String, ()>, lifetime: TimedCache<String, ()>,
id_to_user: HashMap<String, Arc<user::Model>>, id_to_user: HashMap<String, Arc<ck::user::Model>>,
token_to_user: HashMap<String, Arc<user::Model>>, token_to_user: HashMap<String, Arc<ck::user::Model>>,
uri_to_user: HashMap<String, Arc<user::Model>>, uri_to_user: HashMap<String, Arc<ck::user::Model>>,
} }
impl UserCache { impl UserCache {
fn purge(&mut self, user: impl AsRef<user::Model>) { fn purge(&mut self, user: impl AsRef<ck::user::Model>) {
let user = user.as_ref(); let user = user.as_ref();
self.lifetime.cache_remove(&user.id); self.lifetime.cache_remove(&user.id);
@ -56,7 +56,7 @@ impl UserCache {
} }
} }
fn refresh(&mut self, user: Arc<user::Model>) { fn refresh(&mut self, user: Arc<ck::user::Model>) {
self.purge(&user); self.purge(&user);
self.lifetime.cache_set(user.id.clone(), ()); 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. /// 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. /// Used mostly for getters that would otherwise data race with more important refreshes.
fn maybe_refresh(&mut self, user: &Arc<user::Model>) { fn maybe_refresh(&mut self, user: &Arc<ck::user::Model>) {
if self.lifetime.cache_get(&user.id).is_none() { if self.lifetime.cache_get(&user.id).is_none() {
self.refresh(user.clone()); self.refresh(user.clone());
} }
} }
fn get_by_id(&mut self, id: &str) -> Option<Arc<user::Model>> { fn get_by_id(&mut self, id: &str) -> Option<Arc<ck::user::Model>> {
if let Some(user) = self.id_to_user.get(id).cloned() { if let Some(user) = self.id_to_user.get(id).cloned() {
if self.lifetime.cache_get(id).is_none() { if self.lifetime.cache_get(id).is_none() {
self.purge(&user); self.purge(&user);
@ -93,7 +93,7 @@ impl UserCache {
None None
} }
fn get_by_token(&mut self, token: &str) -> Option<Arc<user::Model>> { fn get_by_token(&mut self, token: &str) -> Option<Arc<ck::user::Model>> {
if let Some(user) = self.token_to_user.get(token).cloned() { if let Some(user) = self.token_to_user.get(token).cloned() {
if self.lifetime.cache_get(&user.id).is_none() { if self.lifetime.cache_get(&user.id).is_none() {
self.purge(&user); self.purge(&user);
@ -106,7 +106,7 @@ impl UserCache {
None None
} }
fn get_by_uri(&mut self, uri: &str) -> Option<Arc<user::Model>> { fn get_by_uri(&mut self, uri: &str) -> Option<Arc<ck::user::Model>> {
if let Some(user) = self.uri_to_user.get(uri).cloned() { if let Some(user) = self.uri_to_user.get(uri).cloned() {
if self.lifetime.cache_get(&user.id).is_none() { if self.lifetime.cache_get(&user.id).is_none() {
self.purge(&user); self.purge(&user);
@ -188,8 +188,8 @@ impl UserCacheService {
async fn map_cache_user( async fn map_cache_user(
&self, &self,
user: Option<user::Model>, user: Option<ck::user::Model>,
) -> Result<Option<Arc<user::Model>>, UserCacheError> { ) -> Result<Option<Arc<ck::user::Model>>, UserCacheError> {
if let Some(user) = user { if let Some(user) = user {
let user = Arc::new(user); let user = Arc::new(user);
self.cache.lock().await.maybe_refresh(&user); self.cache.lock().await.maybe_refresh(&user);
@ -202,7 +202,7 @@ impl UserCacheService {
pub async fn get_by_token( pub async fn get_by_token(
&self, &self,
token: &str, token: &str,
) -> Result<Option<Arc<user::Model>>, UserCacheError> { ) -> Result<Option<Arc<ck::user::Model>>, UserCacheError> {
let result = self.cache.lock().await.get_by_token(token); let result = self.cache.lock().await.get_by_token(token);
if let Some(user) = result { if let Some(user) = result {
@ -213,7 +213,10 @@ impl UserCacheService {
.await .await
} }
pub async fn get_by_uri(&self, uri: &str) -> Result<Option<Arc<user::Model>>, UserCacheError> { pub async fn get_by_uri(
&self,
uri: &str,
) -> Result<Option<Arc<ck::user::Model>>, UserCacheError> {
let result = self.cache.lock().await.get_by_uri(uri); let result = self.cache.lock().await.get_by_uri(uri);
if let Some(user) = result { if let Some(user) = result {
@ -225,7 +228,10 @@ impl UserCacheService {
self.map_cache_user(user).await self.map_cache_user(user).await
} }
pub async fn get_by_id(&self, id: &str) -> Result<Option<Arc<user::Model>>, UserCacheError> { pub async fn get_by_id(
&self,
id: &str,
) -> Result<Option<Arc<ck::user::Model>>, UserCacheError> {
let result = self.cache.lock().await.get_by_id(id); let result = self.cache.lock().await.get_by_id(id);
if let Some(user) = result { if let Some(user) = result {

View File

@ -10,7 +10,7 @@ use axum::middleware::Next;
use axum::response::{IntoResponse, Response}; use axum::response::{IntoResponse, Response};
use headers::authorization::Bearer; use headers::authorization::Bearer;
use headers::{Authorization, HeaderMapExt}; use headers::{Authorization, HeaderMapExt};
use magnetar_calckey_model::{access_token, user, CalckeyDbError}; use magnetar_calckey_model::{ck, CalckeyDbError};
use std::convert::Infallible; use std::convert::Infallible;
use std::sync::Arc; use std::sync::Arc;
use strum::IntoStaticStr; use strum::IntoStaticStr;
@ -20,17 +20,17 @@ use tracing::error;
#[derive(Debug)] #[derive(Debug)]
pub enum AuthMode { pub enum AuthMode {
User { User {
user: Arc<user::Model>, user: Arc<ck::user::Model>,
}, },
AccessToken { AccessToken {
user: Arc<user::Model>, user: Arc<ck::user::Model>,
access_token: Arc<access_token::Model>, access_token: Arc<ck::access_token::Model>,
}, },
Anonymous, Anonymous,
} }
impl AuthMode { impl AuthMode {
fn get_user(&self) -> Option<&Arc<user::Model>> { fn get_user(&self) -> Option<&Arc<ck::user::Model>> {
match self { match self {
AuthMode::User { user } | AuthMode::AccessToken { user, .. } => Some(user), AuthMode::User { user } | AuthMode::AccessToken { user, .. } => Some(user),
AuthMode::Anonymous => None, AuthMode::Anonymous => None,
@ -62,10 +62,10 @@ impl IntoResponse for AuthUserRejection {
#[derive(Clone, FromRequestParts)] #[derive(Clone, FromRequestParts)]
#[from_request(via(axum::Extension), rejection(AuthUserRejection))] #[from_request(via(axum::Extension), rejection(AuthUserRejection))]
pub struct AuthenticatedUser(pub Arc<user::Model>); pub struct AuthenticatedUser(pub Arc<ck::user::Model>);
#[derive(Clone)] #[derive(Clone)]
pub struct MaybeUser(pub Option<Arc<user::Model>>); pub struct MaybeUser(pub Option<Arc<ck::user::Model>>);
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for MaybeUser { impl<S> FromRequestParts<S> for MaybeUser {
@ -211,7 +211,7 @@ impl AuthState {
return match self.service.db.get_app_by_id(app_id).await? { return match self.service.db.get_app_by_id(app_id).await? {
Some(app) => Ok(AuthMode::AccessToken { Some(app) => Ok(AuthMode::AccessToken {
user, user,
access_token: Arc::new(access_token::Model { access_token: Arc::new(ck::access_token::Model {
permission: app.permission, permission: app.permission,
..access_token ..access_token
}), }),