Skip to content

Commit

Permalink
return more descriptive JWT validation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan0xC committed Oct 9, 2022
1 parent 6fa6eb1 commit 475c7b8
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/auth.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -61,7 +57,15 @@ fn decode_jwt<T: DeserializeOwned>(token: &str, issuer: String) -> Result<T, Err
validation.set_issuer(&[issuer]);

let token = token.replace(char::is_whitespace, "");
jsonwebtoken::decode(&token, &PUBLIC_RSA_KEY, &validation).map(|d| d.claims).map_res("Error decoding JWT")
match jsonwebtoken::decode(&token, &PUBLIC_RSA_KEY, &validation) {
Ok(d) => 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<LoginJwtClaims, Error> {
Expand Down

0 comments on commit 475c7b8

Please sign in to comment.