Skip to content

Commit

Permalink
Closes #11 (#13)
Browse files Browse the repository at this point in the history
* Closes #11

* update rust base image

* clippy & changelog

* fix other tests

* changelog

* changelog breaking
  • Loading branch information
Guara92 authored Nov 11, 2022
1 parent db06e46 commit e134da1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- README code example updated with the latest changes.
- *BREAKING* [#11](https://github.com/primait/jwks_client/issues/11) - avoid require owned string as params in getters

## [0.3.0] - 2022-04-01

Expand Down
4 changes: 2 additions & 2 deletions examples/get_jwks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ async fn main() {
.build(source);

// The kid "unknown" cannot be a JWKS valid KID. This must not be found here
let result: Result<JsonWebKey, JwksClientError> = client.get("unknown".to_string()).await;
let result: Result<JsonWebKey, JwksClientError> = client.get("unknown").await;
println!(
"Get with kid \"unknown\": {}",
result.unwrap_err().to_string()
);

// The provided kid (assuming is the same you got from your tenant) is valid and could be found.
let result: Result<JsonWebKey, JwksClientError> = client.get(kid.clone()).await;
let result: Result<JsonWebKey, JwksClientError> = client.get(&kid).await;
println!("Get with kid \"{}\": {:?}", kid, result.unwrap());
}
20 changes: 10 additions & 10 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ impl<T: JwksSource + Send + Sync + 'static> JwksClient<T> {

/// Retrieves the key from the cache, if not found it fetches it from the provided `source`.
/// If the key is not found after fetching it, returns an error.
pub async fn get(&self, key_id: String) -> Result<JsonWebKey, JwksClientError> {
pub async fn get(&self, key_id: &str) -> Result<JsonWebKey, JwksClientError> {
let source: Arc<T> = self.source.clone();

let key: JsonWebKey = self
.cache
.get_or_refresh(&key_id, async move { source.fetch_keys().await })
.get_or_refresh(key_id, async move { source.fetch_keys().await })
.await?;

Ok(key)
}

/// Retrieves the key from the cache, if not found it fetches it from the provided `source`.
/// If the key is not found after fetching it, returns Ok(None).
pub async fn get_opt(&self, key_id: String) -> Result<Option<JsonWebKey>, JwksClientError> {
pub async fn get_opt(&self, key_id: &str) -> Result<Option<JsonWebKey>, JwksClientError> {
match self.get(key_id).await {
Ok(res) => Ok(Some(res)),
Err(error) => Err(error),
Expand All @@ -73,7 +73,7 @@ impl<T: JwksSource + Send + Sync + 'static> JwksClient<T> {
) -> Result<O, JwksClientError> {
let header: Header = jsonwebtoken::decode_header(token)?;

if let Some(kid) = header.kid {
if let Some(kid) = header.kid.as_ref() {
let key: JsonWebKey = self.get(kid).await?;

let mut validation = if let Some(alg) = key.alg() {
Expand Down Expand Up @@ -134,7 +134,7 @@ mod test {
let source: WebSource = WebSource::builder().build(url).unwrap();
let client: JwksClient<WebSource> = JwksClient::new(source, None);

assert!(client.get(kid.to_string()).await.is_ok());
assert!(client.get(kid).await.is_ok());
mock.assert();
}

Expand All @@ -157,7 +157,7 @@ mod test {
let ttl_opt: Option<Duration> = Some(Duration::from_millis(1));
let client: JwksClient<WebSource> = JwksClient::new(source, ttl_opt);

let result_key_1 = client.get(kid.to_string()).await;
let result_key_1 = client.get(kid).await;
assert!(result_key_1.is_ok());
let x5t_1: String = result_key_1.unwrap().x5t().unwrap();

Expand All @@ -177,7 +177,7 @@ mod test {
.json_body(jwks_endpoint_response(kid));
});

let result_key_2 = client.get(kid.to_string()).await;
let result_key_2 = client.get(kid).await;
assert!(result_key_2.is_ok());
let x5t_2: String = result_key_2.unwrap().x5t().unwrap();

Expand All @@ -197,7 +197,7 @@ mod test {
then.status(400).body("Error");
});

let result_key_3 = client.get(kid.to_string()).await;
let result_key_3 = client.get(kid).await;
assert!(result_key_3.is_ok());
let x5t_3: String = result_key_3.unwrap().x5t().unwrap();

Expand All @@ -222,7 +222,7 @@ mod test {
let source: WebSource = WebSource::builder().build(url).unwrap();
let client: JwksClient<WebSource> = JwksClient::new(source, None);

let result = client.get(kid.to_string()).await;
let result = client.get(kid).await;
assert!(result.is_err());
match result.err().unwrap() {
JwksClientError::Error(err) => match *err {
Expand Down Expand Up @@ -254,7 +254,7 @@ mod test {
let source: WebSource = WebSource::builder().build(url).unwrap();
let client: JwksClient<WebSource> = JwksClient::new(source, None);

let result = client.get(kid.to_string()).await;
let result = client.get(kid).await;
assert!(result.is_err());

match result.err().unwrap() {
Expand Down

0 comments on commit e134da1

Please sign in to comment.