From 55cfb8187cb814e17a2a99d02bfd9296fc01dcc2 Mon Sep 17 00:00:00 2001 From: mrw1593 Date: Fri, 30 Jun 2023 19:27:33 -0400 Subject: Added config file --- src/services/config.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/services/jwt.rs | 20 +++++++------- src/services/mod.rs | 1 + 3 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 src/services/config.rs (limited to 'src/services') diff --git a/src/services/config.rs b/src/services/config.rs new file mode 100644 index 0000000..6468126 --- /dev/null +++ b/src/services/config.rs @@ -0,0 +1,74 @@ +use std::{ + fmt::{self, Display}, + str::FromStr, +}; + +use exun::RawUnexpected; +use parking_lot::RwLock; +use serde::Deserialize; +use thiserror::Error; +use url::Url; + +static ENVIRONMENT: RwLock = RwLock::new(Environment::Local); + +#[derive(Debug, Clone, Deserialize)] +pub struct Config { + pub id: Box, + pub url: Url, +} + +pub fn get_config() -> Result { + let env = get_environment(); + let path = format!("static/config/{env}.toml"); + let string = std::fs::read_to_string(path)?; + let config = toml::from_str(&string)?; + Ok(config) +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Environment { + Local, + Dev, + Staging, + Production, +} + +impl Display for Environment { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Local => f.write_str("local"), + Self::Dev => f.write_str("dev"), + Self::Staging => f.write_str("staging"), + Self::Production => f.write_str("prod"), + } + } +} + +#[derive(Debug, Clone, Error)] +#[error("Expected one of the following environments: local, dev, staging, prod. Found {string}")] +pub struct ParseEnvironmentError { + string: Box, +} + +impl FromStr for Environment { + type Err = ParseEnvironmentError; + + fn from_str(s: &str) -> Result { + match s { + "local" => Ok(Self::Local), + "dev" => Ok(Self::Dev), + "staging" => Ok(Self::Staging), + "prod" => Ok(Self::Production), + _ => Err(ParseEnvironmentError { string: s.into() }), + } + } +} + +pub fn set_environment(env: Environment) { + let mut env_ptr = ENVIRONMENT.write(); + *env_ptr = env; +} + +fn get_environment() -> Environment { + ENVIRONMENT.read().clone() +} diff --git a/src/services/jwt.rs b/src/services/jwt.rs index 488e0ac..86252c4 100644 --- a/src/services/jwt.rs +++ b/src/services/jwt.rs @@ -19,7 +19,7 @@ pub enum TokenType { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Claims { - iss: Url, + iss: Box, aud: Option>, #[serde(with = "ts_milliseconds")] exp: DateTime, @@ -45,7 +45,7 @@ pub enum RevokedRefreshTokenReason { impl Claims { pub async fn auth_code<'c>( db: &MySqlPool, - self_id: Url, + self_id: &str, client_id: Uuid, scopes: &str, redirect_uri: &Url, @@ -59,7 +59,7 @@ impl Claims { db::create_auth_code(db, id, exp).await?; Ok(Self { - iss: self_id, + iss: Box::from(self_id), aud: None, exp, nbf: None, @@ -76,7 +76,7 @@ impl Claims { pub async fn access_token<'c>( db: &MySqlPool, auth_code_id: Option, - self_id: Url, + self_id: &str, client_id: Uuid, duration: Duration, scopes: &str, @@ -90,7 +90,7 @@ impl Claims { .unexpect()?; Ok(Self { - iss: self_id, + iss: Box::from(self_id), aud: None, exp, nbf: None, @@ -186,7 +186,7 @@ pub enum VerifyJwtError { fn verify_jwt( token: &str, - self_id: Url, + self_id: &str, client_id: Option, ) -> Result> { let key = secrets::signing_key()?; @@ -194,7 +194,7 @@ fn verify_jwt( .verify_with_key(&key) .map_err(|e| VerifyJwtError::from(e))?; - if claims.iss != self_id { + if claims.iss != self_id.into() { yeet!(VerifyJwtError::IncorrectIssuer.into()) } @@ -228,7 +228,7 @@ fn verify_jwt( pub async fn verify_auth_code<'c>( db: &MySqlPool, token: &str, - self_id: Url, + self_id: &str, client_id: Uuid, redirect_uri: Url, ) -> Result> { @@ -252,7 +252,7 @@ pub async fn verify_auth_code<'c>( pub async fn verify_access_token<'c>( db: impl Executor<'c, Database = MySql>, token: &str, - self_id: Url, + self_id: &str, client_id: Uuid, ) -> Result> { let claims = verify_jwt(token, self_id, Some(client_id))?; @@ -267,7 +267,7 @@ pub async fn verify_access_token<'c>( pub async fn verify_refresh_token<'c>( db: impl Executor<'c, Database = MySql>, token: &str, - self_id: Url, + self_id: &str, client_id: Option, ) -> Result> { let claims = verify_jwt(token, self_id, client_id)?; diff --git a/src/services/mod.rs b/src/services/mod.rs index 5339594..de08b58 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -1,4 +1,5 @@ pub mod authorization; +pub mod config; pub mod crypto; pub mod db; pub mod id; -- cgit v1.2.3