From e134da1061f227d113fc98217ec404a916e1e526 Mon Sep 17 00:00:00 2001 From: Guarabot Date: Fri, 11 Nov 2022 14:18:42 +0100 Subject: [PATCH] Closes #11 (#13) * Closes #11 * update rust base image * clippy & changelog * fix other tests * changelog * changelog breaking --- CHANGELOG.md | 1 + examples/get_jwks.rs | 4 ++-- src/client.rs | 20 ++++++++++---------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a546af..2247e9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/examples/get_jwks.rs b/examples/get_jwks.rs index 3a21171..f830fb3 100644 --- a/examples/get_jwks.rs +++ b/examples/get_jwks.rs @@ -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 = client.get("unknown".to_string()).await; + let result: Result = 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 = client.get(kid.clone()).await; + let result: Result = client.get(&kid).await; println!("Get with kid \"{}\": {:?}", kid, result.unwrap()); } diff --git a/src/client.rs b/src/client.rs index bcfbc96..7c9ebe3 100644 --- a/src/client.rs +++ b/src/client.rs @@ -43,12 +43,12 @@ impl JwksClient { /// 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 { + pub async fn get(&self, key_id: &str) -> Result { let source: Arc = 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) @@ -56,7 +56,7 @@ impl JwksClient { /// 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, JwksClientError> { + pub async fn get_opt(&self, key_id: &str) -> Result, JwksClientError> { match self.get(key_id).await { Ok(res) => Ok(Some(res)), Err(error) => Err(error), @@ -73,7 +73,7 @@ impl JwksClient { ) -> Result { 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() { @@ -134,7 +134,7 @@ mod test { let source: WebSource = WebSource::builder().build(url).unwrap(); let client: JwksClient = JwksClient::new(source, None); - assert!(client.get(kid.to_string()).await.is_ok()); + assert!(client.get(kid).await.is_ok()); mock.assert(); } @@ -157,7 +157,7 @@ mod test { let ttl_opt: Option = Some(Duration::from_millis(1)); let client: JwksClient = 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(); @@ -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(); @@ -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(); @@ -222,7 +222,7 @@ mod test { let source: WebSource = WebSource::builder().build(url).unwrap(); let client: JwksClient = 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 { @@ -254,7 +254,7 @@ mod test { let source: WebSource = WebSource::builder().build(url).unwrap(); let client: JwksClient = 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() {