From 193fb6399816176e11ca375136fe93bb33c6881c Mon Sep 17 00:00:00 2001 From: Esteban Borai Date: Sat, 8 Jun 2024 15:17:07 -0400 Subject: [PATCH] fix(server): use `TownHall` core auth over in-situ --- crates/core/src/lib.rs | 2 +- .../src/graphql/modules/auth/query/me.rs | 4 +- .../user/mutation/user_avatar_update.rs | 2 +- .../modules/user/mutation/user_follow.rs | 2 +- .../modules/user/mutation/user_unfollow.rs | 2 +- .../modules/user/mutation/user_update.rs | 2 +- crates/server/src/services/auth.rs | 110 ------------------ crates/server/src/services/mod.rs | 1 - 8 files changed, 7 insertions(+), 118 deletions(-) delete mode 100644 crates/server/src/services/auth.rs diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 0effaace..4d30749b 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -8,5 +8,5 @@ use lazy_static::lazy_static; use pxid::Factory; lazy_static! { - static ref PXID_GENERATOR: Factory = { Factory::new().expect("Failed to create Pxid factory") }; + static ref PXID_GENERATOR: Factory = Factory::new().expect("Failed to create Pxid factory"); } diff --git a/crates/server/src/graphql/modules/auth/query/me.rs b/crates/server/src/graphql/modules/auth/query/me.rs index 8b8ac693..923ae7b2 100644 --- a/crates/server/src/graphql/modules/auth/query/me.rs +++ b/crates/server/src/graphql/modules/auth/query/me.rs @@ -1,11 +1,11 @@ use async_graphql::{Context, Result, SimpleObject}; use serde::{Deserialize, Serialize}; -use crate::context::SharedContext; +use townhall::auth::service::Token; +use crate::context::SharedContext; use crate::graphql::modules::auth::types::{AuthError, AuthErrorCode}; use crate::graphql::modules::user::types::User; -use crate::services::auth::Token; #[derive(Debug, Default, Deserialize, Serialize, SimpleObject)] pub struct Me { diff --git a/crates/server/src/graphql/modules/user/mutation/user_avatar_update.rs b/crates/server/src/graphql/modules/user/mutation/user_avatar_update.rs index 29045bc4..d330dd8e 100644 --- a/crates/server/src/graphql/modules/user/mutation/user_avatar_update.rs +++ b/crates/server/src/graphql/modules/user/mutation/user_avatar_update.rs @@ -5,11 +5,11 @@ use async_graphql::{Context, Result, SimpleObject}; use serde::{Deserialize, Serialize}; use tracing::instrument; +use townhall::auth::service::Token; use townhall::user::service::UploadAvatarDto; use crate::context::SharedContext; use crate::graphql::modules::user::types::{UserError, UserErrorCode}; -use crate::services::auth::Token; #[derive(Debug, Default, Deserialize, Serialize, SimpleObject)] pub struct UserAvatarUpdate { diff --git a/crates/server/src/graphql/modules/user/mutation/user_follow.rs b/crates/server/src/graphql/modules/user/mutation/user_follow.rs index 2c4e09a0..3a992d0a 100644 --- a/crates/server/src/graphql/modules/user/mutation/user_follow.rs +++ b/crates/server/src/graphql/modules/user/mutation/user_follow.rs @@ -2,11 +2,11 @@ use async_graphql::{Context, Result, SimpleObject}; use pxid::Pxid; use serde::{Deserialize, Serialize}; +use townhall::auth::service::Token; use townhall::user::service::FollowPeers; use crate::context::SharedContext; use crate::graphql::modules::user::types::{UserError, UserErrorCode}; -use crate::services::auth::Token; #[derive(Debug, Default, Deserialize, Serialize, SimpleObject)] pub struct UserFollow { diff --git a/crates/server/src/graphql/modules/user/mutation/user_unfollow.rs b/crates/server/src/graphql/modules/user/mutation/user_unfollow.rs index 45fa35ea..6936bc98 100644 --- a/crates/server/src/graphql/modules/user/mutation/user_unfollow.rs +++ b/crates/server/src/graphql/modules/user/mutation/user_unfollow.rs @@ -2,11 +2,11 @@ use async_graphql::{Context, Result, SimpleObject}; use pxid::Pxid; use serde::{Deserialize, Serialize}; +use townhall::auth::service::Token; use townhall::user::service::FollowPeers; use crate::context::SharedContext; use crate::graphql::modules::user::types::{UserError, UserErrorCode}; -use crate::services::auth::Token; #[derive(Debug, Default, Deserialize, Serialize, SimpleObject)] pub struct UserUnfollow { diff --git a/crates/server/src/graphql/modules/user/mutation/user_update.rs b/crates/server/src/graphql/modules/user/mutation/user_update.rs index 3ad1b89d..76fff918 100644 --- a/crates/server/src/graphql/modules/user/mutation/user_update.rs +++ b/crates/server/src/graphql/modules/user/mutation/user_update.rs @@ -1,11 +1,11 @@ use async_graphql::{Context, InputObject, Result, SimpleObject}; use serde::{Deserialize, Serialize}; +use townhall::auth::service::Token; use townhall::user::repository::user::UpdateUserDto; use crate::context::SharedContext; use crate::graphql::modules::user::types::{User, UserError, UserErrorCode}; -use crate::services::auth::Token; #[derive(Debug, Default, InputObject)] pub struct UserUpdateInput { diff --git a/crates/server/src/services/auth.rs b/crates/server/src/services/auth.rs deleted file mode 100644 index 044d0c67..00000000 --- a/crates/server/src/services/auth.rs +++ /dev/null @@ -1,110 +0,0 @@ -use std::fmt::Display; - -use anyhow::{Error, Result}; -use argon2::verify_encoded; -use chrono::{Duration, Utc}; -use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation}; -use pxid::Pxid; -use serde::{Deserialize, Serialize}; - -const JWT_AUDIENCE: &str = "townhall"; - -/// JWT Token Abstaction -#[derive(Debug)] -pub struct Token { - pub(crate) raw: String, - pub(crate) claims: Claims, -} - -impl Token { - /// Retrieves the token's user ID - pub fn user_id(&self) -> Pxid { - self.claims.uid - } - - /// Retrieves the internal JWT String - pub fn token_string(&self) -> String { - self.raw.to_string() - } -} - -impl Display for Token { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.raw) - } -} - -#[derive(Clone)] -pub struct AuthService { - encoding_key: EncodingKey, - decoding_key: DecodingKey, - validation: Validation, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct Claims { - pub exp: usize, - pub uid: Pxid, - pub iat: usize, -} - -impl AuthService { - pub fn new(jwt_secret: &str) -> Self { - let encoding_key = EncodingKey::from_secret(jwt_secret.as_bytes()); - let decoding_key = DecodingKey::from_secret(jwt_secret.as_bytes()); - let mut validation = Validation::new(Algorithm::HS256); - - validation.set_audience(JWT_AUDIENCE.as_bytes()); - - Self { - encoding_key, - decoding_key, - validation, - } - } - - pub fn sign_token(&self, uid: Pxid) -> Result { - let iat = Utc::now().timestamp() as usize; - let exp = Utc::now() - .checked_add_signed(Duration::days(30)) - .unwrap() - .timestamp() as usize; - let claims = Claims { exp, iat, uid }; - let jwt = encode(&Header::default(), &claims, &self.encoding_key) - .map_err(|e| Error::msg(e.to_string()))?; - - Ok(Token { raw: jwt, claims }) - } - - pub fn verify_token(&self, token: &Token) -> Result { - let token_data = decode::(&token.raw, &self.decoding_key, &self.validation) - .map_err(|e| Error::msg(e.to_string()))?; - - Ok(token_data.claims) - } - - pub fn validate_password(&self, encoded: &str, raw: &str) -> bool { - let raw = raw.as_bytes(); - - verify_encoded(encoded, raw).unwrap() - } - - pub fn parse_jwt(&self, jwt: &str) -> Result { - let claims = Self::decode_token(jwt, &self.decoding_key, &self.validation)?; - - Ok(Token { - raw: jwt.to_string(), - claims, - }) - } - - pub(crate) fn decode_token( - token: &str, - decoding_key: &DecodingKey, - validation: &Validation, - ) -> Result { - let token_data = decode::(token, decoding_key, validation)?; - - Ok(token_data.claims) - } -} diff --git a/crates/server/src/services/mod.rs b/crates/server/src/services/mod.rs index 506471b1..9b86da78 100644 --- a/crates/server/src/services/mod.rs +++ b/crates/server/src/services/mod.rs @@ -1,4 +1,3 @@ -pub mod auth; pub mod image; use std::sync::Arc;