diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8322515 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +**/target/* +**/.git +**/.cargo diff --git a/CHANGELOG.md b/CHANGELOG.md index d9dde63..9a546af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Is now possible to know if the error is caused by expired jwt (useful for refreshing purposes) +- Deps improvements + ### Fixed - README code example updated with the latest changes. diff --git a/Cargo.toml b/Cargo.toml index 919f84f..c5ce0e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,19 +11,19 @@ readme = "README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -tokio = { version = "1", features = ["sync", "macros"] } -anyhow = "1.0.44" -async-trait = "0.1.51" -jsonwebtoken = "8.0.0-beta.2" -chrono = "0.4" +async-trait = "0.1" +tokio = { version = "1", features = ["sync"] } +jsonwebtoken = "8.1" +chrono = { version = "0.4", default-features = false, features = ["clock"] } reqwest = { version = "0.11", features = ["json"] } serde = { version = "1.0", features = ["derive"]} serde_json = "1.0" -thiserror = "1.0.29" -url = "2.2" +thiserror = "1.0" +url = "2.3" [dev-dependencies] -mockall = "0.10" +tokio = { version = "1", features = ["macros"] } +mockall = "0.11" httpmock = "0.6" rand = "0.8" diff --git a/Dockerfile b/Dockerfile index 74a17af..16aafd0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM rust:1.59.0 +FROM rust:1.65 WORKDIR /code diff --git a/src/cache.rs b/src/cache.rs index e261992..8467ed4 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -36,8 +36,8 @@ impl Cache { F: Future> + Send + 'static, { let read: RwLockReadGuard = self.inner.read().await; - let is_entry_expired: bool = (*read).is_expired(); - let get_key_result: Result = (*read).set.get_key(key).cloned(); + let is_entry_expired: bool = read.is_expired(); + let get_key_result: Result = read.set.get_key(key).cloned(); // Drop RwLock read guard prematurely to be able to write in the lock drop(read); @@ -68,7 +68,7 @@ impl Cache { self.refreshed.store(true, Ordering::SeqCst); Ok(set) } else { - Ok((*guard).set.clone()) + Ok(guard.set.clone()) } // we drop the write guard here so "refresh=true" for the other threads/tasks } diff --git a/src/error.rs b/src/error.rs index a12c329..13ec69f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,7 @@ use std::sync::Arc; +use jsonwebtoken::errors::ErrorKind; + #[derive(thiserror::Error, Debug)] pub enum Error { #[error("Failed fetching the key: {0}")] @@ -29,3 +31,22 @@ impl From for JwksClientError { Self::Error(Arc::new(error.into())) } } + +impl JwksClientError { + pub fn is_jwt_expired(&self) -> bool { + match self { + JwksClientError::Error(e) => e.is_jwt_expired(), + } + } +} + +impl Error { + fn is_jwt_expired(&self) -> bool { + match self { + Error::JsonWebToken(err) => { + matches!(err.kind(), ErrorKind::ExpiredSignature) + } + _ => false, + } + } +}