Skip to content

Commit

Permalink
Add is_jwt_expired method on Error (#12)
Browse files Browse the repository at this point in the history
* Add is_jwt_expired method on Error

* deps

* deps fix

* update rust base image

* clippy & add a dockerignore

* tokio feature

* revert gitignore
  • Loading branch information
Guara92 authored Nov 11, 2022
1 parent a05dda9 commit db06e46
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/target/*
**/.git
**/.cargo
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.59.0
FROM rust:1.65

WORKDIR /code

Expand Down
6 changes: 3 additions & 3 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ impl Cache {
F: Future<Output = Result<JsonWebKeySet, Error>> + Send + 'static,
{
let read: RwLockReadGuard<Entry> = self.inner.read().await;
let is_entry_expired: bool = (*read).is_expired();
let get_key_result: Result<JsonWebKey, Error> = (*read).set.get_key(key).cloned();
let is_entry_expired: bool = read.is_expired();
let get_key_result: Result<JsonWebKey, Error> = read.set.get_key(key).cloned();
// Drop RwLock read guard prematurely to be able to write in the lock
drop(read);

Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 21 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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}")]
Expand Down Expand Up @@ -29,3 +31,22 @@ impl From<jsonwebtoken::errors::Error> 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,
}
}
}

0 comments on commit db06e46

Please sign in to comment.