use std::str::FromStr; use jsonwebtoken::DecodingKey; use sellershut_core::auth::{ValidationRequest, ValidationResponse, auth_server::Auth}; use tonic::{Request, Response, Status, async_trait}; use tower_sessions::{SessionStore, session::Id}; use tracing::warn; use crate::{auth::Claims, state::AppHandle}; #[async_trait] impl Auth for AppHandle { async fn validate_auth_token( &self, request: Request, ) -> Result, Status> { let token = request.into_inner().token; let token = jsonwebtoken::decode::( &token, &DecodingKey::from_secret(self.local_config.oauth.jwt_encoding_key.as_bytes()), &jsonwebtoken::Validation::default(), ); match token { Ok(value) => { let session_id = value.claims.sid; let store = &self.session_store; match Id::from_str(&session_id) { Ok(ref id) => { if let Ok(Some(_)) = store.load(id).await { return Ok(Response::new(ValidationResponse { valid: true })); } else { return Ok(Response::new(Default::default())); } } Err(e) => { warn!("{e}"); return Ok(Response::new(Default::default())); } } } Err(e) => { warn!("{e}"); Ok(Response::new(ValidationResponse::default())) } } } }