feat: improve error logging when JSON does not parse

This commit is contained in:
alphafetus committed 2026-04-01 13:50:31 -04:00
1 parent f8502ba781
commit 152c01124b
1 file changed
+15 -16
+15 -16
View File
@@ -1,19 +1,16 @@
#[cfg(feature = "ssr")] use crate::common::UserClaims;
use jsonwebtoken::{jwk::JwkSet, DecodingKey}; #[cfg(feature = "ssr")]
use jsonwebtoken::{DecodingKey, jwk::JwkSet};
use leptos::prelude::*; use leptos::prelude::*;
use crate::common::UserClaims;
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
pub async fn fetch_jwks(user_pool_id: String, region: String) -> Result<JwkSet, ServerFnError> { pub async fn fetch_jwks(user_pool_id: String, region: String) -> Result<JwkSet, ServerFnError> {
let request_url = format!( let request_url = format!(
"https://cognito-idp.{}.amazonaws.com/{}/.well-known/jwks.json", "https://cognito-idp.{}.amazonaws.com/{}/.well-known/jwks.json",
region, region, user_pool_id
user_pool_id
); );
let jwks = reqwest::get(&request_url).await? let jwks = reqwest::get(&request_url).await?.json::<JwkSet>().await?;
.json::<JwkSet>()
.await?;
Ok(jwks) Ok(jwks)
} }
@@ -21,7 +18,7 @@ pub async fn fetch_jwks(user_pool_id: String, region: String) -> Result<JwkSet,
/// Validates a JWT, and, if successful, decodes it into a series of user claims. /// Validates a JWT, and, if successful, decodes it into a series of user claims.
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
pub async fn validate_and_get_token_info(token: String) -> Result<UserClaims, ServerFnError> { pub async fn validate_and_get_token_info(token: String) -> Result<UserClaims, ServerFnError> {
use jsonwebtoken::{decode, Algorithm, Validation}; use jsonwebtoken::{Algorithm, Validation, decode};
let jwks = fetch_jwks("us-east-1_Lfjuy5zaM".into(), "us-east-1".into()).await?; let jwks = fetch_jwks("us-east-1_Lfjuy5zaM".into(), "us-east-1".into()).await?;
@@ -31,16 +28,18 @@ pub async fn validate_and_get_token_info(token: String) -> Result<UserClaims, Se
let header = jsonwebtoken::decode_header(&token) let header = jsonwebtoken::decode_header(&token)
.map_err(|e| ServerFnError::new(format!("Invalid token header: {e}")))?; .map_err(|e| ServerFnError::new(format!("Invalid token header: {e}")))?;
let kid = header.kid.ok_or_else(|| ServerFnError::new("Missing kid header"))?; let kid = header
.kid
.ok_or_else(|| ServerFnError::new("Missing kid header"))?;
let jwk = jwks.find(&kid) let jwk = jwks
.find(&kid)
.ok_or_else(|| ServerFnError::new("Couldn't find matching key in JWKS"))?; .ok_or_else(|| ServerFnError::new("Couldn't find matching key in JWKS"))?;
let token_data = decode::<UserClaims>( leptos::logging::debug_log!("Parsing token: {:?}", token);
&token,
&DecodingKey::from_jwk(&jwk)?, let token_data = decode::<UserClaims>(&token, &DecodingKey::from_jwk(&jwk)?, &validation)
&validation .map_err(|err| ServerFnError::new(format!("Invalid token found: {err}")))?;
).map_err(|_| ServerFnError::new("Invalid token found".to_string()))?;
if token_data.claims.token_use != "access" { if token_data.claims.token_use != "access" {
return Err(ServerFnError::new("Token type mismatch")); return Err(ServerFnError::new("Token type mismatch"));