diff options
Diffstat (limited to 'crates/api-auth/src/util.rs')
| -rw-r--r-- | crates/api-auth/src/util.rs | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/crates/api-auth/src/util.rs b/crates/api-auth/src/util.rs index 0893bd5..b15a5e2 100644 --- a/crates/api-auth/src/util.rs +++ b/crates/api-auth/src/util.rs @@ -2,7 +2,7 @@ use api_core::models::user::User; use async_session::{Session, serde_json}; use oauth2::{AuthorizationCode, CsrfToken, Scope, TokenResponse}; use redis::AsyncCommands; -use serde::{Deserialize, de::DeserializeOwned}; +use serde::de::DeserializeOwned; use sh_util::cache::{CacheKey, RedisManager}; use crate::{BasicClient, CSRF_TOKEN, SessionResponse, client::AuthHttpClient, error::AuthError}; @@ -44,7 +44,7 @@ pub async fn get_user<T>( c: &BasicClient, client: &AuthHttpClient, code: &str, - endpoint: &str, + _endpoint: &str, ) -> Result<User, AuthError> where User: TryFrom<T>, @@ -71,33 +71,37 @@ where User::try_from(user_data).map_err(|_e| AuthError::UserDeserialisation) } - pub async fn validate_session(cache: &RedisManager, cookie: &str, state: &str) -> Result<(), AuthError> { - let id = Session::id_from_cookie_value(cookie)?; - let cache_key = CacheKey::Session(&id); - let mut cache = cache.get().await.unwrap(); - let session = cache.get::<_, String>(&cache_key).await?; - let session: Session = - serde_json::from_str(&session).map_err(|_e| AuthError::InvalidSession)?; +pub async fn validate_session( + cache: &RedisManager, + cookie: &str, + state: &str, +) -> Result<(), AuthError> { + let id = Session::id_from_cookie_value(cookie)?; + let cache_key = CacheKey::Session(&id); + let mut cache = cache.get().await.unwrap(); + let session = cache.get::<_, String>(&cache_key).await?; + let session: Session = + serde_json::from_str(&session).map_err(|_e| AuthError::InvalidSession)?; - match session.validate() { - Some(session) => { - // Extract the CSRF token from the session - let stored_csrf_token = session.get::<CsrfToken>(CSRF_TOKEN); + match session.validate() { + Some(session) => { + // Extract the CSRF token from the session + let stored_csrf_token = session.get::<CsrfToken>(CSRF_TOKEN); - if let Some(stored) = stored_csrf_token { - // Cleanup the CSRF token session - cache.del::<_, ()>(cache_key).await?; + if let Some(stored) = stored_csrf_token { + // Cleanup the CSRF token session + cache.del::<_, ()>(cache_key).await?; - // Validate CSRF token is the same as the one in the auth request - if *stored.secret() != state { - Err(AuthError::TokenMismatch) - } else { - Ok(()) - } + // Validate CSRF token is the same as the one in the auth request + if *stored.secret() != state { + Err(AuthError::TokenMismatch) } else { - Err(AuthError::NoCSRFToken) + Ok(()) } + } else { + Err(AuthError::NoCSRFToken) } - None => Err(AuthError::MissingSession), } + None => Err(AuthError::MissingSession), } +} |
