feat: improve error logging when JSON does not parse
This commit is contained in:
1 parent
f8502ba781
commit
152c01124b
1 file changed
+15
-16
+15
-16
@@ -1,19 +1,16 @@
|
||||
#[cfg(feature = "ssr")]
|
||||
use jsonwebtoken::{jwk::JwkSet, DecodingKey};
|
||||
use crate::common::UserClaims;
|
||||
#[cfg(feature = "ssr")]
|
||||
use jsonwebtoken::{DecodingKey, jwk::JwkSet};
|
||||
use leptos::prelude::*;
|
||||
use crate::common::UserClaims;
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn fetch_jwks(user_pool_id: String, region: String) -> Result<JwkSet, ServerFnError> {
|
||||
let request_url = format!(
|
||||
"https://cognito-idp.{}.amazonaws.com/{}/.well-known/jwks.json",
|
||||
region,
|
||||
user_pool_id
|
||||
region, user_pool_id
|
||||
);
|
||||
|
||||
let jwks = reqwest::get(&request_url).await?
|
||||
.json::<JwkSet>()
|
||||
.await?;
|
||||
let jwks = reqwest::get(&request_url).await?.json::<JwkSet>().await?;
|
||||
|
||||
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.
|
||||
#[cfg(feature = "ssr")]
|
||||
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?;
|
||||
|
||||
@@ -31,16 +28,18 @@ pub async fn validate_and_get_token_info(token: String) -> Result<UserClaims, Se
|
||||
let header = jsonwebtoken::decode_header(&token)
|
||||
.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"))?;
|
||||
|
||||
let token_data = decode::<UserClaims>(
|
||||
&token,
|
||||
&DecodingKey::from_jwk(&jwk)?,
|
||||
&validation
|
||||
).map_err(|_| ServerFnError::new("Invalid token found".to_string()))?;
|
||||
leptos::logging::debug_log!("Parsing token: {:?}", token);
|
||||
|
||||
let token_data = decode::<UserClaims>(&token, &DecodingKey::from_jwk(&jwk)?, &validation)
|
||||
.map_err(|err| ServerFnError::new(format!("Invalid token found: {err}")))?;
|
||||
|
||||
if token_data.claims.token_use != "access" {
|
||||
return Err(ServerFnError::new("Token type mismatch"));
|
||||
|
||||
Reference in new issue
Block a user