From 475c7b8f1671ba74001bbe50050c1a69931122cb Mon Sep 17 00:00:00 2001 From: Stefan Melmuk Date: Sun, 9 Oct 2022 13:28:41 +0200 Subject: [PATCH] return more descriptive JWT validation messages --- src/auth.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index f99fbd3990..c0d2f3e2ff 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,18 +1,14 @@ -// // JWT Handling // use chrono::{Duration, Utc}; use num_traits::FromPrimitive; use once_cell::sync::Lazy; -use jsonwebtoken::{self, Algorithm, DecodingKey, EncodingKey, Header}; +use jsonwebtoken::{self, errors::ErrorKind, Algorithm, DecodingKey, EncodingKey, Header}; use serde::de::DeserializeOwned; use serde::ser::Serialize; -use crate::{ - error::{Error, MapResult}, - CONFIG, -}; +use crate::{error::Error, CONFIG}; const JWT_ALGORITHM: Algorithm = Algorithm::RS256; @@ -61,7 +57,15 @@ fn decode_jwt(token: &str, issuer: String) -> Result Ok(d.claims), + Err(err) => match *err.kind() { + ErrorKind::InvalidToken => err!("Token is invalid"), + ErrorKind::InvalidIssuer => err!("Issuer is invalid"), + ErrorKind::ExpiredSignature => err!("Token has expired"), + _ => err!("Error decoding JWT"), + }, + } } pub fn decode_login(token: &str) -> Result {