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]
name = "ck"
version = "0.1.0"
edition = "2021"
version.workspace = true
edition.workspace = true
publish = false
[dependencies]

View File

@ -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;

View File

@ -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<UserCacheError> for ApiError {
struct UserCache {
lifetime: TimedCache<String, ()>,
id_to_user: HashMap<String, Arc<user::Model>>,
token_to_user: HashMap<String, Arc<user::Model>>,
uri_to_user: HashMap<String, Arc<user::Model>>,
id_to_user: HashMap<String, Arc<ck::user::Model>>,
token_to_user: HashMap<String, Arc<ck::user::Model>>,
uri_to_user: HashMap<String, Arc<ck::user::Model>>,
}
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();
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.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<user::Model>) {
fn maybe_refresh(&mut self, user: &Arc<ck::user::Model>) {
if self.lifetime.cache_get(&user.id).is_none() {
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 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<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 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<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 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<user::Model>,
) -> Result<Option<Arc<user::Model>>, UserCacheError> {
user: Option<ck::user::Model>,
) -> Result<Option<Arc<ck::user::Model>>, 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<Option<Arc<user::Model>>, UserCacheError> {
) -> Result<Option<Arc<ck::user::Model>>, 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<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);
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<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);
if let Some(user) = result {

View File

@ -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::Model>,
user: Arc<ck::user::Model>,
},
AccessToken {
user: Arc<user::Model>,
access_token: Arc<access_token::Model>,
user: Arc<ck::user::Model>,
access_token: Arc<ck::access_token::Model>,
},
Anonymous,
}
impl AuthMode {
fn get_user(&self) -> Option<&Arc<user::Model>> {
fn get_user(&self) -> Option<&Arc<ck::user::Model>> {
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<user::Model>);
pub struct AuthenticatedUser(pub Arc<ck::user::Model>);
#[derive(Clone)]
pub struct MaybeUser(pub Option<Arc<user::Model>>);
pub struct MaybeUser(pub Option<Arc<ck::user::Model>>);
#[async_trait]
impl<S> FromRequestParts<S> 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
}),