From 1c77b3b3826019efd8b7997b8c7383662840fbc7 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Sat, 11 May 2024 08:48:53 +0200 Subject: [PATCH 1/6] Use `impl Into<$type>` in newtype constructor parameters --- src/types.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types.rs b/src/types.rs index 47bc8de..376fb4f 100644 --- a/src/types.rs +++ b/src/types.rs @@ -86,8 +86,8 @@ macro_rules! new_type { $($item)* #[doc = $new_doc] - pub const fn new(s: $type) -> Self { - $name(s) + pub fn new(s: impl Into<$type>) -> Self { + $name(s.into()) } } impl Deref for $name { From caac2ca9b845864b0532a3a742939f9c7d46b837 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Sat, 11 May 2024 12:04:08 +0200 Subject: [PATCH 2/6] Leverage `Into` parameter in new type constructor usage --- examples/github.rs | 4 +- examples/github_async.rs | 4 +- examples/google.rs | 8 +- examples/google_devicecode.rs | 2 +- examples/microsoft_devicecode_tenant_user.rs | 4 +- examples/msgraph.rs | 4 +- src/client.rs | 2 +- src/code.rs | 9 +- src/devicecode.rs | 17 +--- src/introspection.rs | 10 +-- src/lib.rs | 28 +++--- src/revocation.rs | 2 +- src/tests.rs | 2 +- src/token/tests.rs | 93 ++++++-------------- 14 files changed, 63 insertions(+), 126 deletions(-) diff --git a/examples/github.rs b/examples/github.rs index 98e01e5..7361f94 100644 --- a/examples/github.rs +++ b/examples/github.rs @@ -59,8 +59,8 @@ fn main() { let (authorize_url, csrf_state) = client .authorize_url(CsrfToken::new_random) // This example is requesting access to the user's public repos and email. - .add_scope(Scope::new("public_repo".to_string())) - .add_scope(Scope::new("user:email".to_string())) + .add_scope(Scope::new("public_repo")) + .add_scope(Scope::new("user:email")) .url(); println!("Open this URL in your browser:\n{authorize_url}\n"); diff --git a/examples/github_async.rs b/examples/github_async.rs index 43b7731..e7700b6 100644 --- a/examples/github_async.rs +++ b/examples/github_async.rs @@ -60,8 +60,8 @@ async fn main() { let (authorize_url, csrf_state) = client .authorize_url(CsrfToken::new_random) // This example is requesting access to the user's public repos and email. - .add_scope(Scope::new("public_repo".to_string())) - .add_scope(Scope::new("user:email".to_string())) + .add_scope(Scope::new("public_repo")) + .add_scope(Scope::new("user:email")) .url(); println!("Open this URL in your browser:\n{authorize_url}\n"); diff --git a/examples/google.rs b/examples/google.rs index d3dba53..0915375 100644 --- a/examples/google.rs +++ b/examples/google.rs @@ -68,12 +68,8 @@ fn main() { let (authorize_url, csrf_state) = client .authorize_url(CsrfToken::new_random) // This example is requesting access to the "calendar" features and the user's profile. - .add_scope(Scope::new( - "https://www.googleapis.com/auth/calendar".to_string(), - )) - .add_scope(Scope::new( - "https://www.googleapis.com/auth/plus.me".to_string(), - )) + .add_scope(Scope::new("https://www.googleapis.com/auth/calendar")) + .add_scope(Scope::new("https://www.googleapis.com/auth/plus.me")) .set_pkce_challenge(pkce_code_challenge) .url(); diff --git a/examples/google_devicecode.rs b/examples/google_devicecode.rs index 6948bb1..e7402f7 100644 --- a/examples/google_devicecode.rs +++ b/examples/google_devicecode.rs @@ -66,7 +66,7 @@ fn main() { // Request the set of codes from the Device Authorization endpoint. let details: StoringDeviceAuthorizationResponse = device_client .exchange_device_code() - .add_scope(Scope::new("profile".to_string())) + .add_scope(Scope::new("profile")) .request(&http_client) .expect("Failed to request codes from device auth endpoint"); diff --git a/examples/microsoft_devicecode_tenant_user.rs b/examples/microsoft_devicecode_tenant_user.rs index c5bd8e8..44e022c 100644 --- a/examples/microsoft_devicecode_tenant_user.rs +++ b/examples/microsoft_devicecode_tenant_user.rs @@ -11,7 +11,7 @@ const TENANT_ID: &str = "{tenant}"; #[tokio::main] async fn main() -> Result<(), Box> { - let client = BasicClient::new(ClientId::new("client_id".to_string())) + let client = BasicClient::new(ClientId::new("client_id")) .set_auth_uri(AuthUrl::new(format!( "https://login.microsoftonline.com/{}/oauth2/v2.0/authorize", TENANT_ID @@ -33,7 +33,7 @@ async fn main() -> Result<(), Box> { let details: StandardDeviceAuthorizationResponse = client .exchange_device_code() - .add_scope(Scope::new("read".to_string())) + .add_scope(Scope::new("read")) .request_async(&http_client) .await?; diff --git a/examples/msgraph.rs b/examples/msgraph.rs index 2e56258..50742c2 100644 --- a/examples/msgraph.rs +++ b/examples/msgraph.rs @@ -76,9 +76,7 @@ fn main() { let (authorize_url, csrf_state) = client .authorize_url(CsrfToken::new_random) // This example requests read access to OneDrive. - .add_scope(Scope::new( - "https://graph.microsoft.com/Files.Read".to_string(), - )) + .add_scope(Scope::new("https://graph.microsoft.com/Files.Read")) .set_pkce_challenge(pkce_code_challenge) .url(); diff --git a/src/client.rs b/src/client.rs index fb82c68..8a081c5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -80,7 +80,7 @@ impl private::EndpointStateSealed for EndpointMaybeSet {} /// # use http::Response; /// # use oauth2::{*, basic::*}; /// # -/// # let client = BasicClient::new(ClientId::new("aaa".to_string())) +/// # let client = BasicClient::new(ClientId::new("aaa")) /// # .set_client_secret(ClientSecret::new("bbb".to_string())) /// # .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) /// # .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) diff --git a/src/code.rs b/src/code.rs index 401f49b..1b6e408 100644 --- a/src/code.rs +++ b/src/code.rs @@ -278,7 +278,7 @@ mod tests { #[test] fn test_authorize_url_with_param() { - let client = BasicClient::new(ClientId::new("aaa".to_string())) + let client = BasicClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) .set_auth_uri(AuthUrl::new("https://example.com/auth?foo=bar".to_string()).unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); @@ -298,10 +298,7 @@ mod tests { #[test] fn test_authorize_url_with_scopes() { - let scopes = vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]; + let scopes = vec![Scope::new("read"), Scope::new("write")]; let (url, _) = new_client() .authorize_url(|| CsrfToken::new("csrf_token".to_string())) .add_scopes(scopes) @@ -324,7 +321,7 @@ mod tests { fn test_authorize_url_with_one_scope() { let (url, _) = new_client() .authorize_url(|| CsrfToken::new("csrf_token".to_string())) - .add_scope(Scope::new("read".to_string())) + .add_scope(Scope::new("read")) .url(); assert_eq!( diff --git a/src/devicecode.rs b/src/devicecode.rs index 53e9140..85243d7 100644 --- a/src/devicecode.rs +++ b/src/devicecode.rs @@ -705,7 +705,7 @@ mod tests { client .exchange_device_code() .add_extra_param("foo", "bar") - .add_scope(Scope::new("openid".to_string())) + .add_scope(Scope::new("openid")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -790,10 +790,7 @@ mod tests { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); - assert_eq!( - Some(&vec![Scope::new("openid".to_string()),]), - token.scopes() - ); + assert_eq!(Some(&vec![Scope::new("openid"),]), token.scopes()); assert_eq!(None, token.expires_in()); assert!(token.refresh_token().is_none()); } @@ -862,10 +859,7 @@ mod tests { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); - assert_eq!( - Some(&vec![Scope::new("openid".to_string()),]), - token.scopes() - ); + assert_eq!(Some(&vec![Scope::new("openid"),]), token.scopes()); assert_eq!(None, token.expires_in()); assert!(token.refresh_token().is_none()); } @@ -943,10 +937,7 @@ mod tests { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); - assert_eq!( - Some(&vec![Scope::new("openid".to_string()),]), - token.scopes() - ); + assert_eq!(Some(&vec![Scope::new("openid"),]), token.scopes()); assert_eq!(None, token.expires_in()); assert!(token.refresh_token().is_none()); } diff --git a/src/introspection.rs b/src/introspection.rs index a40166f..20743f2 100644 --- a/src/introspection.rs +++ b/src/introspection.rs @@ -558,16 +558,10 @@ mod tests { assert!(introspection_response.active); assert_eq!( - Some(vec![ - Scope::new("email".to_string()), - Scope::new("profile".to_string()) - ]), + Some(vec![Scope::new("email"), Scope::new("profile")]), introspection_response.scopes ); - assert_eq!( - Some(ClientId::new("aaa".to_string())), - introspection_response.client_id - ); + assert_eq!(Some(ClientId::new("aaa")), introspection_response.client_id); assert_eq!(Some("demo".to_string()), introspection_response.username); assert_eq!( Some(BasicTokenType::Bearer), diff --git a/src/lib.rs b/src/lib.rs index c25d502..bfa3261 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,7 +140,7 @@ //! # fn err_wrapper() -> Result<(), anyhow::Error> { //! // Create an OAuth2 client by specifying the client ID, client secret, authorization URL and //! // token URL. -//! let client = BasicClient::new(ClientId::new("client_id".to_string())) +//! let client = BasicClient::new(ClientId::new("client_id")) //! .set_client_secret(ClientSecret::new("client_secret".to_string())) //! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?) //! .set_token_uri(TokenUrl::new("http://token".to_string())?) @@ -154,8 +154,8 @@ //! let (auth_url, csrf_token) = client //! .authorize_url(CsrfToken::new_random) //! // Set the desired scopes. -//! .add_scope(Scope::new("read".to_string())) -//! .add_scope(Scope::new("write".to_string())) +//! .add_scope(Scope::new("read")) +//! .add_scope(Scope::new("write")) //! // Set the PKCE code challenge. //! .set_pkce_challenge(pkce_challenge) //! .url(); @@ -213,7 +213,7 @@ //! # async fn err_wrapper() -> Result<(), anyhow::Error> { //! // Create an OAuth2 client by specifying the client ID, client secret, authorization URL and //! // token URL. -//! let client = BasicClient::new(ClientId::new("client_id".to_string())) +//! let client = BasicClient::new(ClientId::new("client_id")) //! .set_client_secret(ClientSecret::new("client_secret".to_string())) //! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?) //! .set_token_uri(TokenUrl::new("http://token".to_string())?) @@ -227,8 +227,8 @@ //! let (auth_url, csrf_token) = client //! .authorize_url(CsrfToken::new_random) //! // Set the desired scopes. -//! .add_scope(Scope::new("read".to_string())) -//! .add_scope(Scope::new("write".to_string())) +//! .add_scope(Scope::new("read")) +//! .add_scope(Scope::new("write")) //! // Set the PKCE code challenge. //! .set_pkce_challenge(pkce_challenge) //! .url(); @@ -281,7 +281,7 @@ //! use url::Url; //! //! # fn err_wrapper() -> Result<(), anyhow::Error> { -//! let client = BasicClient::new(ClientId::new("client_id".to_string())) +//! let client = BasicClient::new(ClientId::new("client_id")) //! .set_client_secret(ClientSecret::new("client_secret".to_string())) //! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?); //! @@ -328,7 +328,7 @@ //! //! # #[cfg(feature = "reqwest-blocking")] //! # fn err_wrapper() -> Result<(), anyhow::Error> { -//! let client = BasicClient::new(ClientId::new("client_id".to_string())) +//! let client = BasicClient::new(ClientId::new("client_id")) //! .set_client_secret(ClientSecret::new("client_secret".to_string())) //! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?) //! .set_token_uri(TokenUrl::new("http://token".to_string())?); @@ -342,10 +342,10 @@ //! let token_result = //! client //! .exchange_password( -//! &ResourceOwnerUsername::new("user".to_string()), +//! &ResourceOwnerUsername::new("user"), //! &ResourceOwnerPassword::new("pass".to_string()) //! ) -//! .add_scope(Scope::new("read".to_string())) +//! .add_scope(Scope::new("read")) //! .request(&http_client)?; //! # Ok(()) //! # } @@ -374,7 +374,7 @@ //! //! # #[cfg(feature = "reqwest-blocking")] //! # fn err_wrapper() -> Result<(), anyhow::Error> { -//! let client = BasicClient::new(ClientId::new("client_id".to_string())) +//! let client = BasicClient::new(ClientId::new("client_id")) //! .set_client_secret(ClientSecret::new("client_secret".to_string())) //! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?) //! .set_token_uri(TokenUrl::new("http://token".to_string())?); @@ -387,7 +387,7 @@ //! //! let token_result = client //! .exchange_client_credentials() -//! .add_scope(Scope::new("read".to_string())) +//! .add_scope(Scope::new("read")) //! .request(&http_client)?; //! # Ok(()) //! # } @@ -422,7 +422,7 @@ //! # #[cfg(feature = "reqwest-blocking")] //! # fn err_wrapper() -> Result<(), anyhow::Error> { //! let device_auth_url = DeviceAuthorizationUrl::new("http://deviceauth".to_string())?; -//! let client = BasicClient::new(ClientId::new("client_id".to_string())) +//! let client = BasicClient::new(ClientId::new("client_id")) //! .set_client_secret(ClientSecret::new("client_secret".to_string())) //! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?) //! .set_token_uri(TokenUrl::new("http://token".to_string())?) @@ -436,7 +436,7 @@ //! //! let details: StandardDeviceAuthorizationResponse = client //! .exchange_device_code() -//! .add_scope(Scope::new("read".to_string())) +//! .add_scope(Scope::new("read")) //! .request(&http_client)?; //! //! println!( diff --git a/src/revocation.rs b/src/revocation.rs index fcee130..1dd0f70 100644 --- a/src/revocation.rs +++ b/src/revocation.rs @@ -131,7 +131,7 @@ pub trait RevocableToken { /// # .unwrap()) /// # }; /// # -/// let client = BasicClient::new(ClientId::new("aaa".to_string())) +/// let client = BasicClient::new(ClientId::new("aaa")) /// .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) /// .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) /// // Be sure to set a revocation URL. diff --git a/src/tests.rs b/src/tests.rs index 6046e40..96a00f3 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -21,7 +21,7 @@ use url::Url; pub(crate) fn new_client( ) -> BasicClient { - BasicClient::new(ClientId::new("aaa".to_string())) + BasicClient::new(ClientId::new("aaa")) .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) .set_client_secret(ClientSecret::new("bbb".to_string())) diff --git a/src/token/tests.rs b/src/token/tests.rs index aa23b1e..0f62ec0 100644 --- a/src/token/tests.rs +++ b/src/token/tests.rs @@ -39,7 +39,7 @@ where #[test] fn test_exchange_code_successful_with_minimal_json_response() { - let client = BasicClient::new(ClientId::new("aaa".to_string())) + let client = BasicClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); @@ -117,10 +117,7 @@ fn test_exchange_code_successful_with_complete_json_response() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(3600, token.expires_in().unwrap().as_secs()); @@ -141,7 +138,7 @@ fn test_exchange_code_successful_with_complete_json_response() { #[test] fn test_exchange_client_credentials_with_basic_auth() { - let client = BasicClient::new(ClientId::new("aaa/;&".to_string())) + let client = BasicClient::new(ClientId::new("aaa/;&")) .set_client_secret(ClientSecret::new("bbb/;&".to_string())) .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) @@ -175,10 +172,7 @@ fn test_exchange_client_credentials_with_basic_auth() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(None, token.expires_in()); @@ -187,7 +181,7 @@ fn test_exchange_client_credentials_with_basic_auth() { #[test] fn test_exchange_client_credentials_with_basic_auth_but_no_client_secret() { - let client = BasicClient::new(ClientId::new("aaa/;&".to_string())) + let client = BasicClient::new(ClientId::new("aaa/;&")) .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) .set_auth_type(AuthType::BasicAuth); @@ -219,10 +213,7 @@ fn test_exchange_client_credentials_with_basic_auth_but_no_client_secret() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(None, token.expires_in()); @@ -234,8 +225,8 @@ fn test_exchange_client_credentials_with_body_auth_and_scope() { let client = new_client().set_auth_type(AuthType::RequestBody); let token = client .exchange_client_credentials() - .add_scope(Scope::new("read".to_string())) - .add_scope(Scope::new("write".to_string())) + .add_scope(Scope::new("read")) + .add_scope(Scope::new("write")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -265,10 +256,7 @@ fn test_exchange_client_credentials_with_body_auth_and_scope() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(None, token.expires_in()); @@ -305,10 +293,7 @@ fn test_exchange_refresh_token_with_basic_auth() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(None, token.expires_in()); @@ -346,10 +331,7 @@ fn test_exchange_refresh_token_with_json_response() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(None, token.expires_in()); @@ -361,11 +343,11 @@ fn test_exchange_password_with_json_response() { let client = new_client(); let token = client .exchange_password( - &ResourceOwnerUsername::new("user".to_string()), + &ResourceOwnerUsername::new("user"), &ResourceOwnerPassword::new("pass".to_string()), ) - .add_scope(Scope::new("read".to_string())) - .add_scope(Scope::new("write".to_string())) + .add_scope(Scope::new("read")) + .add_scope(Scope::new("write")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -396,10 +378,7 @@ fn test_exchange_password_with_json_response() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(None, token.expires_in()); @@ -444,10 +423,7 @@ fn test_exchange_code_successful_with_redirect_url() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(None, token.expires_in()); @@ -495,10 +471,7 @@ fn test_exchange_code_successful_with_redirect_url_override() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(None, token.expires_in()); @@ -543,10 +516,7 @@ fn test_exchange_code_successful_with_basic_auth() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(None, token.expires_in()); @@ -599,10 +569,7 @@ fn test_exchange_code_successful_with_pkce_and_extension() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(None, token.expires_in()); @@ -648,10 +615,7 @@ fn test_exchange_refresh_token_successful_with_extension() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(BasicTokenType::Bearer, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(None, token.expires_in()); @@ -822,7 +786,7 @@ fn test_exchange_code_with_unexpected_content_type() { #[test] fn test_exchange_code_with_invalid_token_type() { - let client = BasicClient::new(ClientId::new("aaa".to_string())) + let client = BasicClient::new(ClientId::new("aaa")) .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); @@ -913,7 +877,7 @@ fn test_exchange_code_with_400_status_code() { #[test] fn test_exchange_code_fails_gracefully_on_transport_error() { - let client = BasicClient::new(ClientId::new("aaa".to_string())) + let client = BasicClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) .set_auth_uri(AuthUrl::new("https://auth".to_string()).unwrap()) .set_token_uri(TokenUrl::new("https://token".to_string()).unwrap()); @@ -932,7 +896,7 @@ fn test_exchange_code_fails_gracefully_on_transport_error() { #[test] fn test_extension_successful_with_minimal_json_response() { - let client = ColorfulClient::new(ClientId::new("aaa".to_string())) + let client = ColorfulClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); @@ -983,7 +947,7 @@ fn test_extension_successful_with_minimal_json_response() { #[test] fn test_extension_successful_with_complete_json_response() { - let client = ColorfulClient::new(ClientId::new("aaa".to_string())) + let client = ColorfulClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) @@ -1024,10 +988,7 @@ fn test_extension_successful_with_complete_json_response() { assert_eq!("12/34", token.access_token().secret()); assert_eq!(ColorfulTokenType::Red, *token.token_type()); assert_eq!( - Some(&vec![ - Scope::new("read".to_string()), - Scope::new("write".to_string()), - ]), + Some(&vec![Scope::new("read"), Scope::new("write"),]), token.scopes() ); assert_eq!(3600, token.expires_in().unwrap().as_secs()); @@ -1051,7 +1012,7 @@ fn test_extension_successful_with_complete_json_response() { #[test] fn test_extension_with_simple_json_error() { - let client = ColorfulClient::new(ClientId::new("aaa".to_string())) + let client = ColorfulClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); @@ -1175,7 +1136,7 @@ mod custom_errors { #[test] fn test_extension_with_custom_json_error() { - let client = CustomErrorClient::new(ClientId::new("aaa".to_string())) + let client = CustomErrorClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); From 09eebe0647b4924b17df9d338317daf2b97fed36 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Sat, 11 May 2024 12:08:10 +0200 Subject: [PATCH 3/6] Use `impl Into` in url_type `new()` constructor --- src/types.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/types.rs b/src/types.rs index 376fb4f..3f80a0f 100644 --- a/src/types.rs +++ b/src/types.rs @@ -249,7 +249,8 @@ macro_rules! new_url_type { pub struct $name(Url, String); impl $name { #[doc = $new_doc] - pub fn new(url: String) -> Result { + pub fn new(url: impl Into) -> Result { + let url = url.into(); Ok($name(Url::parse(&url)?, url)) } #[doc = $from_url_doc] From 58c3e6412bd87a824a8b1714795e7d3312630662 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Sat, 11 May 2024 12:18:35 +0200 Subject: [PATCH 4/6] Leverage `Into` parameter in url type constructor usage --- examples/github.rs | 8 ++-- examples/github_async.rs | 8 ++-- examples/google.rs | 10 ++-- examples/google_devicecode.rs | 9 ++-- examples/letterboxd.rs | 4 +- examples/microsoft_devicecode_common_user.rs | 10 ++-- examples/msgraph.rs | 13 ++---- examples/wunderlist.rs | 8 ++-- src/client.rs | 6 +-- src/code.rs | 12 ++--- src/devicecode.rs | 3 +- src/introspection.rs | 12 ++--- src/lib.rs | 28 ++++++------ src/revocation.rs | 36 +++++++-------- src/tests.rs | 4 +- src/token/tests.rs | 48 ++++++++++---------- 16 files changed, 101 insertions(+), 118 deletions(-) diff --git a/examples/github.rs b/examples/github.rs index 7361f94..e216f60 100644 --- a/examples/github.rs +++ b/examples/github.rs @@ -33,9 +33,9 @@ fn main() { env::var("GITHUB_CLIENT_SECRET") .expect("Missing the GITHUB_CLIENT_SECRET environment variable."), ); - let auth_url = AuthUrl::new("https://github.com/login/oauth/authorize".to_string()) + let auth_url = AuthUrl::new("https://github.com/login/oauth/authorize") .expect("Invalid authorization endpoint URL"); - let token_url = TokenUrl::new("https://github.com/login/oauth/access_token".to_string()) + let token_url = TokenUrl::new("https://github.com/login/oauth/access_token") .expect("Invalid token endpoint URL"); // Set up the config for the Github OAuth2 process. @@ -45,9 +45,7 @@ fn main() { .set_token_uri(token_url) // This example will be running its own server at localhost:8080. // See below for the server implementation. - .set_redirect_uri( - RedirectUrl::new("http://localhost:8080".to_string()).expect("Invalid redirect URL"), - ); + .set_redirect_uri(RedirectUrl::new("http://localhost:8080").expect("Invalid redirect URL")); let http_client = reqwest::blocking::ClientBuilder::new() // Following redirects opens the client up to SSRF vulnerabilities. diff --git a/examples/github_async.rs b/examples/github_async.rs index e7700b6..6abfacf 100644 --- a/examples/github_async.rs +++ b/examples/github_async.rs @@ -34,9 +34,9 @@ async fn main() { env::var("GITHUB_CLIENT_SECRET") .expect("Missing the GITHUB_CLIENT_SECRET environment variable."), ); - let auth_url = AuthUrl::new("https://github.com/login/oauth/authorize".to_string()) + let auth_url = AuthUrl::new("https://github.com/login/oauth/authorize") .expect("Invalid authorization endpoint URL"); - let token_url = TokenUrl::new("https://github.com/login/oauth/access_token".to_string()) + let token_url = TokenUrl::new("https://github.com/login/oauth/access_token") .expect("Invalid token endpoint URL"); // Set up the config for the Github OAuth2 process. @@ -46,9 +46,7 @@ async fn main() { .set_token_uri(token_url) // This example will be running its own server at localhost:8080. // See below for the server implementation. - .set_redirect_uri( - RedirectUrl::new("http://localhost:8080".to_string()).expect("Invalid redirect URL"), - ); + .set_redirect_uri(RedirectUrl::new("http://localhost:8080").expect("Invalid redirect URL")); let http_client = reqwest::ClientBuilder::new() // Following redirects opens the client up to SSRF vulnerabilities. diff --git a/examples/google.rs b/examples/google.rs index 0915375..36a61c6 100644 --- a/examples/google.rs +++ b/examples/google.rs @@ -33,9 +33,9 @@ fn main() { env::var("GOOGLE_CLIENT_SECRET") .expect("Missing the GOOGLE_CLIENT_SECRET environment variable."), ); - let auth_url = AuthUrl::new("https://accounts.google.com/o/oauth2/v2/auth".to_string()) + let auth_url = AuthUrl::new("https://accounts.google.com/o/oauth2/v2/auth") .expect("Invalid authorization endpoint URL"); - let token_url = TokenUrl::new("https://www.googleapis.com/oauth2/v3/token".to_string()) + let token_url = TokenUrl::new("https://www.googleapis.com/oauth2/v3/token") .expect("Invalid token endpoint URL"); // Set up the config for the Google OAuth2 process. @@ -45,12 +45,10 @@ fn main() { .set_token_uri(token_url) // This example will be running its own server at localhost:8080. // See below for the server implementation. - .set_redirect_uri( - RedirectUrl::new("http://localhost:8080".to_string()).expect("Invalid redirect URL"), - ) + .set_redirect_uri(RedirectUrl::new("http://localhost:8080").expect("Invalid redirect URL")) // Google supports OAuth 2.0 Token Revocation (RFC-7009) .set_revocation_url( - RevocationUrl::new("https://oauth2.googleapis.com/revoke".to_string()) + RevocationUrl::new("https://oauth2.googleapis.com/revoke") .expect("Invalid revocation endpoint URL"), ); diff --git a/examples/google_devicecode.rs b/examples/google_devicecode.rs index e7402f7..edd0652 100644 --- a/examples/google_devicecode.rs +++ b/examples/google_devicecode.rs @@ -38,13 +38,12 @@ fn main() { env::var("GOOGLE_CLIENT_SECRET") .expect("Missing the GOOGLE_CLIENT_SECRET environment variable."), ); - let auth_url = AuthUrl::new("https://accounts.google.com/o/oauth2/v2/auth".to_string()) + let auth_url = AuthUrl::new("https://accounts.google.com/o/oauth2/v2/auth") .expect("Invalid authorization endpoint URL"); - let token_url = TokenUrl::new("https://www.googleapis.com/oauth2/v3/token".to_string()) + let token_url = TokenUrl::new("https://www.googleapis.com/oauth2/v3/token") .expect("Invalid token endpoint URL"); - let device_auth_url = - DeviceAuthorizationUrl::new("https://oauth2.googleapis.com/device/code".to_string()) - .expect("Invalid device authorization endpoint URL"); + let device_auth_url = DeviceAuthorizationUrl::new("https://oauth2.googleapis.com/device/code") + .expect("Invalid device authorization endpoint URL"); // Set up the config for the Google OAuth2 process. // diff --git a/examples/letterboxd.rs b/examples/letterboxd.rs index 7ec702e..0c1e054 100644 --- a/examples/letterboxd.rs +++ b/examples/letterboxd.rs @@ -37,8 +37,8 @@ fn main() -> Result<(), anyhow::Error> { .expect("Missing the LETTERBOXD_CLIENT_SECRET environment variable."), ); // Letterboxd uses the Resource Owner flow and does not have an auth url - let auth_url = AuthUrl::new("https://api.letterboxd.com/api/v0/auth/404".to_string())?; - let token_url = TokenUrl::new("https://api.letterboxd.com/api/v0/auth/token".to_string())?; + let auth_url = AuthUrl::new("https://api.letterboxd.com/api/v0/auth/404")?; + let token_url = TokenUrl::new("https://api.letterboxd.com/api/v0/auth/token")?; // Set up the config for the Letterboxd OAuth2 process. let client = BasicClient::new(letterboxd_client_id.clone()) diff --git a/examples/microsoft_devicecode_common_user.rs b/examples/microsoft_devicecode_common_user.rs index e148942..794525f 100644 --- a/examples/microsoft_devicecode_common_user.rs +++ b/examples/microsoft_devicecode_common_user.rs @@ -7,15 +7,15 @@ use std::error::Error; #[tokio::main] async fn main() -> Result<(), Box> { - let client = BasicClient::new(ClientId::new("client_id".to_string())) + let client = BasicClient::new(ClientId::new("client_id")) .set_auth_uri(AuthUrl::new( - "https://login.microsoftonline.com/common/oauth2/v2.0/authorize".to_string(), + "https://login.microsoftonline.com/common/oauth2/v2.0/authorize", )?) .set_token_uri(TokenUrl::new( - "https://login.microsoftonline.com/common/oauth2/v2.0/token".to_string(), + "https://login.microsoftonline.com/common/oauth2/v2.0/token", )?) .set_device_authorization_url(DeviceAuthorizationUrl::new( - "https://login.microsoftonline.com/common/oauth2/v2.0/devicecode".to_string(), + "https://login.microsoftonline.com/common/oauth2/v2.0/devicecode", )?); let http_client = reqwest::ClientBuilder::new() @@ -26,7 +26,7 @@ async fn main() -> Result<(), Box> { let details: StandardDeviceAuthorizationResponse = client .exchange_device_code() - .add_scope(Scope::new("read".to_string())) + .add_scope(Scope::new("read")) .request_async(&http_client) .await?; diff --git a/examples/msgraph.rs b/examples/msgraph.rs index 50742c2..8126435 100644 --- a/examples/msgraph.rs +++ b/examples/msgraph.rs @@ -40,12 +40,10 @@ fn main() { env::var("MSGRAPH_CLIENT_SECRET") .expect("Missing the MSGRAPH_CLIENT_SECRET environment variable."), ); - let auth_url = - AuthUrl::new("https://login.microsoftonline.com/common/oauth2/v2.0/authorize".to_string()) - .expect("Invalid authorization endpoint URL"); - let token_url = - TokenUrl::new("https://login.microsoftonline.com/common/oauth2/v2.0/token".to_string()) - .expect("Invalid token endpoint URL"); + let auth_url = AuthUrl::new("https://login.microsoftonline.com/common/oauth2/v2.0/authorize") + .expect("Invalid authorization endpoint URL"); + let token_url = TokenUrl::new("https://login.microsoftonline.com/common/oauth2/v2.0/token") + .expect("Invalid token endpoint URL"); // Set up the config for the Microsoft Graph OAuth2 process. let client = BasicClient::new(graph_client_id) @@ -58,8 +56,7 @@ fn main() { // This example will be running its own server at localhost:3003. // See below for the server implementation. .set_redirect_uri( - RedirectUrl::new("http://localhost:3003/redirect".to_string()) - .expect("Invalid redirect URL"), + RedirectUrl::new("http://localhost:3003/redirect").expect("Invalid redirect URL"), ); let http_client = reqwest::blocking::ClientBuilder::new() diff --git a/examples/wunderlist.rs b/examples/wunderlist.rs index fe960f2..3ed2ea1 100644 --- a/examples/wunderlist.rs +++ b/examples/wunderlist.rs @@ -138,9 +138,9 @@ fn main() { let wunder_client_id = ClientId::new(client_id_str.clone()); let wunderlist_client_secret = ClientSecret::new(client_secret_str.clone()); - let auth_url = AuthUrl::new("https://www.wunderlist.com/oauth/authorize".to_string()) + let auth_url = AuthUrl::new("https://www.wunderlist.com/oauth/authorize") .expect("Invalid authorization endpoint URL"); - let token_url = TokenUrl::new("https://www.wunderlist.com/oauth/access_token".to_string()) + let token_url = TokenUrl::new("https://www.wunderlist.com/oauth/access_token") .expect("Invalid token endpoint URL"); // Set up the config for the Wunderlist OAuth2 process. @@ -150,9 +150,7 @@ fn main() { .set_token_uri(token_url) // This example will be running its own server at localhost:8080. // See below for the server implementation. - .set_redirect_uri( - RedirectUrl::new("http://localhost:8080".to_string()).expect("Invalid redirect URL"), - ); + .set_redirect_uri(RedirectUrl::new("http://localhost:8080").expect("Invalid redirect URL")); let http_client = reqwest::blocking::ClientBuilder::new() // Following redirects opens the client up to SSRF vulnerabilities. diff --git a/src/client.rs b/src/client.rs index 8a081c5..abf345b 100644 --- a/src/client.rs +++ b/src/client.rs @@ -82,9 +82,9 @@ impl private::EndpointStateSealed for EndpointMaybeSet {} /// # /// # let client = BasicClient::new(ClientId::new("aaa")) /// # .set_client_secret(ClientSecret::new("bbb".to_string())) -/// # .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) -/// # .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) -/// # .set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap()); +/// # .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) +/// # .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()) +/// # .set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); /// # /// # #[derive(Debug, Error)] /// # enum FakeError { diff --git a/src/code.rs b/src/code.rs index 1b6e408..8e471cf 100644 --- a/src/code.rs +++ b/src/code.rs @@ -280,8 +280,8 @@ mod tests { fn test_authorize_url_with_param() { let client = BasicClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) - .set_auth_uri(AuthUrl::new("https://example.com/auth?foo=bar".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); + .set_auth_uri(AuthUrl::new("https://example.com/auth?foo=bar").unwrap()) + .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()); let (url, _) = client .authorize_url(|| CsrfToken::new("csrf_token".to_string())) @@ -359,8 +359,8 @@ mod tests { #[test] fn test_authorize_url_with_redirect_url() { - let client = new_client() - .set_redirect_uri(RedirectUrl::new("https://localhost/redirect".to_string()).unwrap()); + let client = + new_client().set_redirect_uri(RedirectUrl::new("https://localhost/redirect").unwrap()); let (url, _) = client .authorize_url(|| CsrfToken::new("csrf_token".to_string())) @@ -380,8 +380,8 @@ mod tests { #[test] fn test_authorize_url_with_redirect_url_override() { - let client = new_client() - .set_redirect_uri(RedirectUrl::new("https://localhost/redirect".to_string()).unwrap()); + let client = + new_client().set_redirect_uri(RedirectUrl::new("https://localhost/redirect").unwrap()); let (url, _) = client .authorize_url(|| CsrfToken::new("csrf_token".to_string())) diff --git a/src/devicecode.rs b/src/devicecode.rs index 85243d7..e7cb37b 100644 --- a/src/devicecode.rs +++ b/src/devicecode.rs @@ -698,8 +698,7 @@ mod tests { }}" ); - let device_auth_url = - DeviceAuthorizationUrl::new("https://deviceauth/here".to_string()).unwrap(); + let device_auth_url = DeviceAuthorizationUrl::new("https://deviceauth/here").unwrap(); let client = new_client().set_device_authorization_url(device_auth_url.clone()); client diff --git a/src/introspection.rs b/src/introspection.rs index 20743f2..8a321c0 100644 --- a/src/introspection.rs +++ b/src/introspection.rs @@ -462,10 +462,8 @@ mod tests { fn test_token_introspection_successful_with_basic_auth_minimal_response() { let client = new_client() .set_auth_type(AuthType::BasicAuth) - .set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap()) - .set_introspection_url( - IntrospectionUrl::new("https://introspection/url".to_string()).unwrap(), - ); + .set_redirect_uri(RedirectUrl::new("https://redirect/here").unwrap()) + .set_introspection_url(IntrospectionUrl::new("https://introspection/url").unwrap()); let introspection_response = client .introspect(&AccessToken::new("access_token_123".to_string())) @@ -512,10 +510,8 @@ mod tests { fn test_token_introspection_successful_with_basic_auth_full_response() { let client = new_client() .set_auth_type(AuthType::BasicAuth) - .set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap()) - .set_introspection_url( - IntrospectionUrl::new("https://introspection/url".to_string()).unwrap(), - ); + .set_redirect_uri(RedirectUrl::new("https://redirect/here").unwrap()) + .set_introspection_url(IntrospectionUrl::new("https://introspection/url").unwrap()); let introspection_response = client .introspect(&AccessToken::new("access_token_123".to_string())) diff --git a/src/lib.rs b/src/lib.rs index bfa3261..660f00a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -142,10 +142,10 @@ //! // token URL. //! let client = BasicClient::new(ClientId::new("client_id")) //! .set_client_secret(ClientSecret::new("client_secret".to_string())) -//! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?) -//! .set_token_uri(TokenUrl::new("http://token".to_string())?) +//! .set_auth_uri(AuthUrl::new("http://authorize")?) +//! .set_token_uri(TokenUrl::new("http://token")?) //! // Set the URL the user will be redirected to after the authorization process. -//! .set_redirect_uri(RedirectUrl::new("http://redirect".to_string())?); +//! .set_redirect_uri(RedirectUrl::new("http://redirect")?); //! //! // Generate a PKCE challenge. //! let (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256(); @@ -215,10 +215,10 @@ //! // token URL. //! let client = BasicClient::new(ClientId::new("client_id")) //! .set_client_secret(ClientSecret::new("client_secret".to_string())) -//! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?) -//! .set_token_uri(TokenUrl::new("http://token".to_string())?) +//! .set_auth_uri(AuthUrl::new("http://authorize")?) +//! .set_token_uri(TokenUrl::new("http://token")?) //! // Set the URL the user will be redirected to after the authorization process. -//! .set_redirect_uri(RedirectUrl::new("http://redirect".to_string())?); +//! .set_redirect_uri(RedirectUrl::new("http://redirect")?); //! //! // Generate a PKCE challenge. //! let (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256(); @@ -283,7 +283,7 @@ //! # fn err_wrapper() -> Result<(), anyhow::Error> { //! let client = BasicClient::new(ClientId::new("client_id")) //! .set_client_secret(ClientSecret::new("client_secret".to_string())) -//! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?); +//! .set_auth_uri(AuthUrl::new("http://authorize")?); //! //! // Generate the full authorization URL. //! let (auth_url, csrf_token) = client @@ -330,8 +330,8 @@ //! # fn err_wrapper() -> Result<(), anyhow::Error> { //! let client = BasicClient::new(ClientId::new("client_id")) //! .set_client_secret(ClientSecret::new("client_secret".to_string())) -//! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?) -//! .set_token_uri(TokenUrl::new("http://token".to_string())?); +//! .set_auth_uri(AuthUrl::new("http://authorize")?) +//! .set_token_uri(TokenUrl::new("http://token")?); //! //! let http_client = reqwest::blocking::ClientBuilder::new() //! // Following redirects opens the client up to SSRF vulnerabilities. @@ -376,8 +376,8 @@ //! # fn err_wrapper() -> Result<(), anyhow::Error> { //! let client = BasicClient::new(ClientId::new("client_id")) //! .set_client_secret(ClientSecret::new("client_secret".to_string())) -//! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?) -//! .set_token_uri(TokenUrl::new("http://token".to_string())?); +//! .set_auth_uri(AuthUrl::new("http://authorize")?) +//! .set_token_uri(TokenUrl::new("http://token")?); //! //! let http_client = reqwest::blocking::ClientBuilder::new() //! // Following redirects opens the client up to SSRF vulnerabilities. @@ -421,11 +421,11 @@ //! //! # #[cfg(feature = "reqwest-blocking")] //! # fn err_wrapper() -> Result<(), anyhow::Error> { -//! let device_auth_url = DeviceAuthorizationUrl::new("http://deviceauth".to_string())?; +//! let device_auth_url = DeviceAuthorizationUrl::new("http://deviceauth")?; //! let client = BasicClient::new(ClientId::new("client_id")) //! .set_client_secret(ClientSecret::new("client_secret".to_string())) -//! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?) -//! .set_token_uri(TokenUrl::new("http://token".to_string())?) +//! .set_auth_uri(AuthUrl::new("http://authorize")?) +//! .set_token_uri(TokenUrl::new("http://token")?) //! .set_device_authorization_url(device_auth_url); //! //! let http_client = reqwest::blocking::ClientBuilder::new() diff --git a/src/revocation.rs b/src/revocation.rs index 1dd0f70..0bf4f5d 100644 --- a/src/revocation.rs +++ b/src/revocation.rs @@ -132,10 +132,10 @@ pub trait RevocableToken { /// # }; /// # /// let client = BasicClient::new(ClientId::new("aaa")) -/// .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) -/// .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) +/// .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) +/// .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()) /// // Be sure to set a revocation URL. -/// .set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap()); +/// .set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); /// /// // ... /// @@ -397,7 +397,7 @@ mod tests { let client = new_client(); let result = client - .set_revocation_url(RevocationUrl::new("http://revocation/url".to_string()).unwrap()) + .set_revocation_url(RevocationUrl::new("http://revocation/url").unwrap()) .revoke_token(AccessToken::new("access_token_123".to_string()).into()) .unwrap_err(); @@ -409,8 +409,8 @@ mod tests { #[test] fn test_token_revocation_with_unsupported_token_type() { - let client = new_client() - .set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap()); + let client = + new_client().set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); let revocation_response = client .revoke_token(AccessToken::new("access_token_123".to_string()).into()).unwrap() @@ -452,8 +452,8 @@ mod tests { #[test] fn test_token_revocation_with_access_token_and_empty_json_response() { - let client = new_client() - .set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap()); + let client = + new_client().set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); client .revoke_token(AccessToken::new("access_token_123".to_string()).into()) @@ -480,8 +480,8 @@ mod tests { #[test] fn test_token_revocation_with_access_token_and_empty_response() { - let client = new_client() - .set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap()); + let client = + new_client().set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); client .revoke_token(AccessToken::new("access_token_123".to_string()).into()) @@ -504,8 +504,8 @@ mod tests { #[test] fn test_token_revocation_with_access_token_and_non_json_response() { - let client = new_client() - .set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap()); + let client = + new_client().set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); client .revoke_token(AccessToken::new("access_token_123".to_string()).into()) @@ -532,8 +532,8 @@ mod tests { #[test] fn test_token_revocation_with_refresh_token() { - let client = new_client() - .set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap()); + let client = + new_client().set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); client .revoke_token(RefreshToken::new("refresh_token_123".to_string()).into()) @@ -560,11 +560,11 @@ mod tests { #[test] fn test_extension_token_revocation_successful() { - let client = ColorfulClient::new(ClientId::new("aaa".to_string())) + let client = ColorfulClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) - .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) - .set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap()); + .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) + .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()) + .set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); client .revoke_token(ColorfulRevocableToken::Red( diff --git a/src/tests.rs b/src/tests.rs index 96a00f3..afc3909 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -22,8 +22,8 @@ use url::Url; pub(crate) fn new_client( ) -> BasicClient { BasicClient::new(ClientId::new("aaa")) - .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) + .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) + .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()) .set_client_secret(ClientSecret::new("bbb".to_string())) } diff --git a/src/token/tests.rs b/src/token/tests.rs index 0f62ec0..83d4717 100644 --- a/src/token/tests.rs +++ b/src/token/tests.rs @@ -41,8 +41,8 @@ where fn test_exchange_code_successful_with_minimal_json_response() { let client = BasicClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) - .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); + .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) + .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()); let token = client .exchange_code(AuthorizationCode::new("ccc".to_string())) @@ -140,8 +140,8 @@ fn test_exchange_code_successful_with_complete_json_response() { fn test_exchange_client_credentials_with_basic_auth() { let client = BasicClient::new(ClientId::new("aaa/;&")) .set_client_secret(ClientSecret::new("bbb/;&".to_string())) - .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) + .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) + .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()) .set_auth_type(AuthType::BasicAuth); let token = client @@ -182,8 +182,8 @@ fn test_exchange_client_credentials_with_basic_auth() { #[test] fn test_exchange_client_credentials_with_basic_auth_but_no_client_secret() { let client = BasicClient::new(ClientId::new("aaa/;&")) - .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) + .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) + .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()) .set_auth_type(AuthType::BasicAuth); let token = client @@ -389,7 +389,7 @@ fn test_exchange_password_with_json_response() { fn test_exchange_code_successful_with_redirect_url() { let client = new_client() .set_auth_type(AuthType::RequestBody) - .set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap()); + .set_redirect_uri(RedirectUrl::new("https://redirect/here").unwrap()); let token = client .exchange_code(AuthorizationCode::new("ccc".to_string())) @@ -434,12 +434,12 @@ fn test_exchange_code_successful_with_redirect_url() { fn test_exchange_code_successful_with_redirect_url_override() { let client = new_client() .set_auth_type(AuthType::RequestBody) - .set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap()); + .set_redirect_uri(RedirectUrl::new("https://redirect/here").unwrap()); let token = client .exchange_code(AuthorizationCode::new("ccc".to_string())) .set_redirect_uri(Cow::Owned( - RedirectUrl::new("https://redirect/alternative".to_string()).unwrap(), + RedirectUrl::new("https://redirect/alternative").unwrap(), )) .request(&mock_http_client( vec![ @@ -482,7 +482,7 @@ fn test_exchange_code_successful_with_redirect_url_override() { fn test_exchange_code_successful_with_basic_auth() { let client = new_client() .set_auth_type(AuthType::BasicAuth) - .set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap()); + .set_redirect_uri(RedirectUrl::new("https://redirect/here").unwrap()); let token = client .exchange_code(AuthorizationCode::new("ccc".to_string())) @@ -527,7 +527,7 @@ fn test_exchange_code_successful_with_basic_auth() { fn test_exchange_code_successful_with_pkce_and_extension() { let client = new_client() .set_auth_type(AuthType::BasicAuth) - .set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap()); + .set_redirect_uri(RedirectUrl::new("https://redirect/here").unwrap()); let token = client .exchange_code(AuthorizationCode::new("ccc".to_string())) @@ -580,7 +580,7 @@ fn test_exchange_code_successful_with_pkce_and_extension() { fn test_exchange_refresh_token_successful_with_extension() { let client = new_client() .set_auth_type(AuthType::BasicAuth) - .set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap()); + .set_redirect_uri(RedirectUrl::new("https://redirect/here").unwrap()); let token = client .exchange_refresh_token(&RefreshToken::new("ccc".to_string())) @@ -787,8 +787,8 @@ fn test_exchange_code_with_unexpected_content_type() { #[test] fn test_exchange_code_with_invalid_token_type() { let client = BasicClient::new(ClientId::new("aaa")) - .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); + .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) + .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()); let token = client .exchange_code(AuthorizationCode::new("ccc".to_string())) @@ -879,8 +879,8 @@ fn test_exchange_code_with_400_status_code() { fn test_exchange_code_fails_gracefully_on_transport_error() { let client = BasicClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) - .set_auth_uri(AuthUrl::new("https://auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://token".to_string()).unwrap()); + .set_auth_uri(AuthUrl::new("https://auth").unwrap()) + .set_token_uri(TokenUrl::new("https://token").unwrap()); let token = client .exchange_code(AuthorizationCode::new("ccc".to_string())) @@ -898,8 +898,8 @@ fn test_exchange_code_fails_gracefully_on_transport_error() { fn test_extension_successful_with_minimal_json_response() { let client = ColorfulClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) - .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); + .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) + .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()); let token = client .exchange_code(AuthorizationCode::new("ccc".to_string())) @@ -949,8 +949,8 @@ fn test_extension_successful_with_minimal_json_response() { fn test_extension_successful_with_complete_json_response() { let client = ColorfulClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) - .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) + .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) + .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()) .set_auth_type(AuthType::RequestBody); let token = client @@ -1014,8 +1014,8 @@ fn test_extension_successful_with_complete_json_response() { fn test_extension_with_simple_json_error() { let client = ColorfulClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) - .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); + .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) + .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()); let token = client .exchange_code(AuthorizationCode::new("ccc".to_string())) @@ -1138,8 +1138,8 @@ mod custom_errors { fn test_extension_with_custom_json_error() { let client = CustomErrorClient::new(ClientId::new("aaa")) .set_client_secret(ClientSecret::new("bbb".to_string())) - .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); + .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) + .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()); let token = client .exchange_code(AuthorizationCode::new("ccc".to_string())) From 92f8c38b64f5d82c2ae1b0efc3b47aa2c4e35ded Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Sat, 11 May 2024 12:19:38 +0200 Subject: [PATCH 5/6] Use `impl Into<$type>` in new_secret_type constructor parameter --- src/types.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types.rs b/src/types.rs index 3f80a0f..8b62880 100644 --- a/src/types.rs +++ b/src/types.rs @@ -156,8 +156,8 @@ macro_rules! new_secret_type { $($item)* #[doc = $new_doc] - pub fn new(s: $type) -> Self { - $name(s) + pub fn new(s: impl Into<$type>) -> Self { + $name(s.into()) } #[doc = $secret_doc] /// From 8ddeef1b79868fcf1f7bc3b955402ca64df32351 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Sat, 11 May 2024 12:26:24 +0200 Subject: [PATCH 6/6] Leverage `Into` parameter in secret type constructor usage --- examples/github.rs | 4 +-- examples/github_async.rs | 4 +-- examples/google.rs | 4 +-- examples/msgraph.rs | 4 +-- examples/wunderlist.rs | 4 +-- src/client.rs | 4 +-- src/code.rs | 32 +++++++++------------- src/introspection.rs | 4 +-- src/lib.rs | 18 ++++++------- src/revocation.rs | 18 ++++++------- src/token/tests.rs | 58 ++++++++++++++++++++-------------------- src/types.rs | 9 +++---- 12 files changed, 78 insertions(+), 85 deletions(-) diff --git a/examples/github.rs b/examples/github.rs index e216f60..734d2bf 100644 --- a/examples/github.rs +++ b/examples/github.rs @@ -83,13 +83,13 @@ fn main() { let code = url .query_pairs() .find(|(key, _)| key == "code") - .map(|(_, code)| AuthorizationCode::new(code.into_owned())) + .map(|(_, code)| AuthorizationCode::new(code)) .unwrap(); let state = url .query_pairs() .find(|(key, _)| key == "state") - .map(|(_, state)| CsrfToken::new(state.into_owned())) + .map(|(_, state)| CsrfToken::new(state)) .unwrap(); let message = "Go back to your terminal :)"; diff --git a/examples/github_async.rs b/examples/github_async.rs index 6abfacf..389e3b8 100644 --- a/examples/github_async.rs +++ b/examples/github_async.rs @@ -81,13 +81,13 @@ async fn main() { let code = url .query_pairs() .find(|(key, _)| key == "code") - .map(|(_, code)| AuthorizationCode::new(code.into_owned())) + .map(|(_, code)| AuthorizationCode::new(code)) .unwrap(); let state = url .query_pairs() .find(|(key, _)| key == "state") - .map(|(_, state)| CsrfToken::new(state.into_owned())) + .map(|(_, state)| CsrfToken::new(state)) .unwrap(); let message = "Go back to your terminal :)"; diff --git a/examples/google.rs b/examples/google.rs index 36a61c6..b769c53 100644 --- a/examples/google.rs +++ b/examples/google.rs @@ -93,13 +93,13 @@ fn main() { let code = url .query_pairs() .find(|(key, _)| key == "code") - .map(|(_, code)| AuthorizationCode::new(code.into_owned())) + .map(|(_, code)| AuthorizationCode::new(code)) .unwrap(); let state = url .query_pairs() .find(|(key, _)| key == "state") - .map(|(_, state)| CsrfToken::new(state.into_owned())) + .map(|(_, state)| CsrfToken::new(state)) .unwrap(); let message = "Go back to your terminal :)"; diff --git a/examples/msgraph.rs b/examples/msgraph.rs index 8126435..1179a89 100644 --- a/examples/msgraph.rs +++ b/examples/msgraph.rs @@ -99,13 +99,13 @@ fn main() { let code = url .query_pairs() .find(|(key, _)| key == "code") - .map(|(_, code)| AuthorizationCode::new(code.into_owned())) + .map(|(_, code)| AuthorizationCode::new(code)) .unwrap(); let state = url .query_pairs() .find(|(key, _)| key == "state") - .map(|(_, state)| CsrfToken::new(state.into_owned())) + .map(|(_, state)| CsrfToken::new(state)) .unwrap(); let message = "Go back to your terminal :)"; diff --git a/examples/wunderlist.rs b/examples/wunderlist.rs index 3ed2ea1..15da7a8 100644 --- a/examples/wunderlist.rs +++ b/examples/wunderlist.rs @@ -183,13 +183,13 @@ fn main() { let code = url .query_pairs() .find(|(key, _)| key == "code") - .map(|(_, code)| AuthorizationCode::new(code.into_owned())) + .map(|(_, code)| AuthorizationCode::new(code)) .unwrap(); let state = url .query_pairs() .find(|(key, _)| key == "state") - .map(|(_, state)| CsrfToken::new(state.into_owned())) + .map(|(_, state)| CsrfToken::new(state)) .unwrap(); let message = "Go back to your terminal :)"; diff --git a/src/client.rs b/src/client.rs index abf345b..b6f27b4 100644 --- a/src/client.rs +++ b/src/client.rs @@ -81,7 +81,7 @@ impl private::EndpointStateSealed for EndpointMaybeSet {} /// # use oauth2::{*, basic::*}; /// # /// # let client = BasicClient::new(ClientId::new("aaa")) -/// # .set_client_secret(ClientSecret::new("bbb".to_string())) +/// # .set_client_secret(ClientSecret::new("bbb")) /// # .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) /// # .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()) /// # .set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); @@ -107,7 +107,7 @@ impl private::EndpointStateSealed for EndpointMaybeSet {} /// # }; /// # /// let res = client -/// .revoke_token(AccessToken::new("some token".to_string()).into()) +/// .revoke_token(AccessToken::new("some token").into()) /// .unwrap() /// .request(&http_client); /// diff --git a/src/code.rs b/src/code.rs index 8e471cf..c74f815 100644 --- a/src/code.rs +++ b/src/code.rs @@ -204,9 +204,7 @@ mod tests { #[test] fn test_authorize_url() { let client = new_client(); - let (url, _) = client - .authorize_url(|| CsrfToken::new("csrf_token".to_string())) - .url(); + let (url, _) = client.authorize_url(|| CsrfToken::new("csrf_token")).url(); assert_eq!( Url::parse( @@ -240,9 +238,9 @@ mod tests { let client = new_client(); let (url, _) = client - .authorize_url(|| CsrfToken::new("csrf_token".to_string())) + .authorize_url(|| CsrfToken::new("csrf_token")) .set_pkce_challenge(PkceCodeChallenge::from_code_verifier_sha256( - &PkceCodeVerifier::new("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk".to_string()), + &PkceCodeVerifier::new("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"), )) .url(); assert_eq!( @@ -263,7 +261,7 @@ mod tests { let client = new_client(); let (url, _) = client - .authorize_url(|| CsrfToken::new("csrf_token".to_string())) + .authorize_url(|| CsrfToken::new("csrf_token")) .use_implicit_flow() .url(); @@ -279,13 +277,11 @@ mod tests { #[test] fn test_authorize_url_with_param() { let client = BasicClient::new(ClientId::new("aaa")) - .set_client_secret(ClientSecret::new("bbb".to_string())) + .set_client_secret(ClientSecret::new("bbb")) .set_auth_uri(AuthUrl::new("https://example.com/auth?foo=bar").unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()); - let (url, _) = client - .authorize_url(|| CsrfToken::new("csrf_token".to_string())) - .url(); + let (url, _) = client.authorize_url(|| CsrfToken::new("csrf_token")).url(); assert_eq!( Url::parse( @@ -300,7 +296,7 @@ mod tests { fn test_authorize_url_with_scopes() { let scopes = vec![Scope::new("read"), Scope::new("write")]; let (url, _) = new_client() - .authorize_url(|| CsrfToken::new("csrf_token".to_string())) + .authorize_url(|| CsrfToken::new("csrf_token")) .add_scopes(scopes) .url(); @@ -320,7 +316,7 @@ mod tests { #[test] fn test_authorize_url_with_one_scope() { let (url, _) = new_client() - .authorize_url(|| CsrfToken::new("csrf_token".to_string())) + .authorize_url(|| CsrfToken::new("csrf_token")) .add_scope(Scope::new("read")) .url(); @@ -342,8 +338,8 @@ mod tests { let client = new_client(); let (url, _) = client - .authorize_url(|| CsrfToken::new("csrf_token".to_string())) - .set_response_type(&ResponseType::new("code token".to_string())) + .authorize_url(|| CsrfToken::new("csrf_token")) + .set_response_type(&ResponseType::new("code token")) .add_extra_param("foo", "bar") .url(); @@ -362,9 +358,7 @@ mod tests { let client = new_client().set_redirect_uri(RedirectUrl::new("https://localhost/redirect").unwrap()); - let (url, _) = client - .authorize_url(|| CsrfToken::new("csrf_token".to_string())) - .url(); + let (url, _) = client.authorize_url(|| CsrfToken::new("csrf_token")).url(); assert_eq!( Url::parse( @@ -384,9 +378,9 @@ mod tests { new_client().set_redirect_uri(RedirectUrl::new("https://localhost/redirect").unwrap()); let (url, _) = client - .authorize_url(|| CsrfToken::new("csrf_token".to_string())) + .authorize_url(|| CsrfToken::new("csrf_token")) .set_redirect_uri(Cow::Owned( - RedirectUrl::new("https://localhost/alternative".to_string()).unwrap(), + RedirectUrl::new("https://localhost/alternative").unwrap(), )) .url(); diff --git a/src/introspection.rs b/src/introspection.rs index 8a321c0..3a3eef0 100644 --- a/src/introspection.rs +++ b/src/introspection.rs @@ -466,7 +466,7 @@ mod tests { .set_introspection_url(IntrospectionUrl::new("https://introspection/url").unwrap()); let introspection_response = client - .introspect(&AccessToken::new("access_token_123".to_string())) + .introspect(&AccessToken::new("access_token_123")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -514,7 +514,7 @@ mod tests { .set_introspection_url(IntrospectionUrl::new("https://introspection/url").unwrap()); let introspection_response = client - .introspect(&AccessToken::new("access_token_123".to_string())) + .introspect(&AccessToken::new("access_token_123")) .set_token_type_hint("access_token") .request(&mock_http_client( vec![ diff --git a/src/lib.rs b/src/lib.rs index 660f00a..f406616 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -141,7 +141,7 @@ //! // Create an OAuth2 client by specifying the client ID, client secret, authorization URL and //! // token URL. //! let client = BasicClient::new(ClientId::new("client_id")) -//! .set_client_secret(ClientSecret::new("client_secret".to_string())) +//! .set_client_secret(ClientSecret::new("client_secret")) //! .set_auth_uri(AuthUrl::new("http://authorize")?) //! .set_token_uri(TokenUrl::new("http://token")?) //! // Set the URL the user will be redirected to after the authorization process. @@ -177,7 +177,7 @@ //! // Now you can trade it for an access token. //! let token_result = //! client -//! .exchange_code(AuthorizationCode::new("some authorization code".to_string())) +//! .exchange_code(AuthorizationCode::new("some authorization code")) //! // Set the PKCE code verifier. //! .set_pkce_verifier(pkce_verifier) //! .request(&http_client)?; @@ -214,7 +214,7 @@ //! // Create an OAuth2 client by specifying the client ID, client secret, authorization URL and //! // token URL. //! let client = BasicClient::new(ClientId::new("client_id")) -//! .set_client_secret(ClientSecret::new("client_secret".to_string())) +//! .set_client_secret(ClientSecret::new("client_secret")) //! .set_auth_uri(AuthUrl::new("http://authorize")?) //! .set_token_uri(TokenUrl::new("http://token")?) //! // Set the URL the user will be redirected to after the authorization process. @@ -249,7 +249,7 @@ //! //! // Now you can trade it for an access token. //! let token_result = client -//! .exchange_code(AuthorizationCode::new("some authorization code".to_string())) +//! .exchange_code(AuthorizationCode::new("some authorization code")) //! // Set the PKCE code verifier. //! .set_pkce_verifier(pkce_verifier) //! .request_async(&http_client) @@ -282,7 +282,7 @@ //! //! # fn err_wrapper() -> Result<(), anyhow::Error> { //! let client = BasicClient::new(ClientId::new("client_id")) -//! .set_client_secret(ClientSecret::new("client_secret".to_string())) +//! .set_client_secret(ClientSecret::new("client_secret")) //! .set_auth_uri(AuthUrl::new("http://authorize")?); //! //! // Generate the full authorization URL. @@ -329,7 +329,7 @@ //! # #[cfg(feature = "reqwest-blocking")] //! # fn err_wrapper() -> Result<(), anyhow::Error> { //! let client = BasicClient::new(ClientId::new("client_id")) -//! .set_client_secret(ClientSecret::new("client_secret".to_string())) +//! .set_client_secret(ClientSecret::new("client_secret")) //! .set_auth_uri(AuthUrl::new("http://authorize")?) //! .set_token_uri(TokenUrl::new("http://token")?); //! @@ -343,7 +343,7 @@ //! client //! .exchange_password( //! &ResourceOwnerUsername::new("user"), -//! &ResourceOwnerPassword::new("pass".to_string()) +//! &ResourceOwnerPassword::new("pass") //! ) //! .add_scope(Scope::new("read")) //! .request(&http_client)?; @@ -375,7 +375,7 @@ //! # #[cfg(feature = "reqwest-blocking")] //! # fn err_wrapper() -> Result<(), anyhow::Error> { //! let client = BasicClient::new(ClientId::new("client_id")) -//! .set_client_secret(ClientSecret::new("client_secret".to_string())) +//! .set_client_secret(ClientSecret::new("client_secret")) //! .set_auth_uri(AuthUrl::new("http://authorize")?) //! .set_token_uri(TokenUrl::new("http://token")?); //! @@ -423,7 +423,7 @@ //! # fn err_wrapper() -> Result<(), anyhow::Error> { //! let device_auth_url = DeviceAuthorizationUrl::new("http://deviceauth")?; //! let client = BasicClient::new(ClientId::new("client_id")) -//! .set_client_secret(ClientSecret::new("client_secret".to_string())) +//! .set_client_secret(ClientSecret::new("client_secret")) //! .set_auth_uri(AuthUrl::new("http://authorize")?) //! .set_token_uri(TokenUrl::new("http://token")?) //! .set_device_authorization_url(device_auth_url); diff --git a/src/revocation.rs b/src/revocation.rs index 0bf4f5d..04cc5a7 100644 --- a/src/revocation.rs +++ b/src/revocation.rs @@ -116,7 +116,7 @@ pub trait RevocableToken { /// # fn err_wrapper() -> Result<(), anyhow::Error> { /// # /// # let token_response = BasicTokenResponse::new( -/// # AccessToken::new("access".to_string()), +/// # AccessToken::new("access"), /// # BasicTokenType::Bearer, /// # EmptyExtraTokenFields {}, /// # ); @@ -386,7 +386,7 @@ mod tests { let client = new_client().set_revocation_url_option(None); let result = client - .revoke_token(AccessToken::new("access_token_123".to_string()).into()) + .revoke_token(AccessToken::new("access_token_123").into()) .unwrap_err(); assert_eq!(result.to_string(), "No revocation endpoint URL specified"); @@ -398,7 +398,7 @@ mod tests { let result = client .set_revocation_url(RevocationUrl::new("http://revocation/url").unwrap()) - .revoke_token(AccessToken::new("access_token_123".to_string()).into()) + .revoke_token(AccessToken::new("access_token_123").into()) .unwrap_err(); assert_eq!( @@ -413,7 +413,7 @@ mod tests { new_client().set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); let revocation_response = client - .revoke_token(AccessToken::new("access_token_123".to_string()).into()).unwrap() + .revoke_token(AccessToken::new("access_token_123").into()).unwrap() .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -456,7 +456,7 @@ mod tests { new_client().set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); client - .revoke_token(AccessToken::new("access_token_123".to_string()).into()) + .revoke_token(AccessToken::new("access_token_123").into()) .unwrap() .request(&mock_http_client( vec![ @@ -484,7 +484,7 @@ mod tests { new_client().set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); client - .revoke_token(AccessToken::new("access_token_123".to_string()).into()) + .revoke_token(AccessToken::new("access_token_123").into()) .unwrap() .request(&mock_http_client( vec![ @@ -508,7 +508,7 @@ mod tests { new_client().set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); client - .revoke_token(AccessToken::new("access_token_123".to_string()).into()) + .revoke_token(AccessToken::new("access_token_123").into()) .unwrap() .request(&mock_http_client( vec![ @@ -536,7 +536,7 @@ mod tests { new_client().set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); client - .revoke_token(RefreshToken::new("refresh_token_123".to_string()).into()) + .revoke_token(RefreshToken::new("refresh_token_123").into()) .unwrap() .request(&mock_http_client( vec![ @@ -561,7 +561,7 @@ mod tests { #[test] fn test_extension_token_revocation_successful() { let client = ColorfulClient::new(ClientId::new("aaa")) - .set_client_secret(ClientSecret::new("bbb".to_string())) + .set_client_secret(ClientSecret::new("bbb")) .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()) .set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap()); diff --git a/src/token/tests.rs b/src/token/tests.rs index 83d4717..849235c 100644 --- a/src/token/tests.rs +++ b/src/token/tests.rs @@ -40,12 +40,12 @@ where #[test] fn test_exchange_code_successful_with_minimal_json_response() { let client = BasicClient::new(ClientId::new("aaa")) - .set_client_secret(ClientSecret::new("bbb".to_string())) + .set_client_secret(ClientSecret::new("bbb")) .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -85,7 +85,7 @@ fn test_exchange_code_successful_with_minimal_json_response() { fn test_exchange_code_successful_with_complete_json_response() { let client = new_client().set_auth_type(AuthType::RequestBody); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -139,7 +139,7 @@ fn test_exchange_code_successful_with_complete_json_response() { #[test] fn test_exchange_client_credentials_with_basic_auth() { let client = BasicClient::new(ClientId::new("aaa/;&")) - .set_client_secret(ClientSecret::new("bbb/;&".to_string())) + .set_client_secret(ClientSecret::new("bbb/;&")) .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()) .set_auth_type(AuthType::BasicAuth); @@ -267,7 +267,7 @@ fn test_exchange_client_credentials_with_body_auth_and_scope() { fn test_exchange_refresh_token_with_basic_auth() { let client = new_client().set_auth_type(AuthType::BasicAuth); let token = client - .exchange_refresh_token(&RefreshToken::new("ccc".to_string())) + .exchange_refresh_token(&RefreshToken::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -304,7 +304,7 @@ fn test_exchange_refresh_token_with_basic_auth() { fn test_exchange_refresh_token_with_json_response() { let client = new_client(); let token = client - .exchange_refresh_token(&RefreshToken::new("ccc".to_string())) + .exchange_refresh_token(&RefreshToken::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -344,7 +344,7 @@ fn test_exchange_password_with_json_response() { let token = client .exchange_password( &ResourceOwnerUsername::new("user"), - &ResourceOwnerPassword::new("pass".to_string()), + &ResourceOwnerPassword::new("pass"), ) .add_scope(Scope::new("read")) .add_scope(Scope::new("write")) @@ -392,7 +392,7 @@ fn test_exchange_code_successful_with_redirect_url() { .set_redirect_uri(RedirectUrl::new("https://redirect/here").unwrap()); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -437,7 +437,7 @@ fn test_exchange_code_successful_with_redirect_url_override() { .set_redirect_uri(RedirectUrl::new("https://redirect/here").unwrap()); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .set_redirect_uri(Cow::Owned( RedirectUrl::new("https://redirect/alternative").unwrap(), )) @@ -485,7 +485,7 @@ fn test_exchange_code_successful_with_basic_auth() { .set_redirect_uri(RedirectUrl::new("https://redirect/here").unwrap()); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -530,7 +530,7 @@ fn test_exchange_code_successful_with_pkce_and_extension() { .set_redirect_uri(RedirectUrl::new("https://redirect/here").unwrap()); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .set_pkce_verifier(PkceCodeVerifier::new( "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk".to_string(), )) @@ -583,7 +583,7 @@ fn test_exchange_refresh_token_successful_with_extension() { .set_redirect_uri(RedirectUrl::new("https://redirect/here").unwrap()); let token = client - .exchange_refresh_token(&RefreshToken::new("ccc".to_string())) + .exchange_refresh_token(&RefreshToken::new("ccc")) .add_extra_param("foo", "bar") .request(&mock_http_client( vec![ @@ -626,7 +626,7 @@ fn test_exchange_refresh_token_successful_with_extension() { fn test_exchange_code_with_simple_json_error() { let client = new_client(); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -716,7 +716,7 @@ fn test_exchange_code_with_simple_json_error() { fn test_exchange_code_with_json_parse_error() { let client = new_client(); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -755,7 +755,7 @@ fn test_exchange_code_with_json_parse_error() { fn test_exchange_code_with_unexpected_content_type() { let client = new_client(); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -791,7 +791,7 @@ fn test_exchange_code_with_invalid_token_type() { .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -833,7 +833,7 @@ fn test_exchange_code_with_400_status_code() { let body = r#"{"error":"invalid_request","error_description":"Expired code."}"#; let client = new_client(); let token_err = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -878,12 +878,12 @@ fn test_exchange_code_with_400_status_code() { #[test] fn test_exchange_code_fails_gracefully_on_transport_error() { let client = BasicClient::new(ClientId::new("aaa")) - .set_client_secret(ClientSecret::new("bbb".to_string())) + .set_client_secret(ClientSecret::new("bbb")) .set_auth_uri(AuthUrl::new("https://auth").unwrap()) .set_token_uri(TokenUrl::new("https://token").unwrap()); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&|_| Err(FakeError::Err)); assert!(token.is_err()); @@ -897,12 +897,12 @@ fn test_exchange_code_fails_gracefully_on_transport_error() { #[test] fn test_extension_successful_with_minimal_json_response() { let client = ColorfulClient::new(ClientId::new("aaa")) - .set_client_secret(ClientSecret::new("bbb".to_string())) + .set_client_secret(ClientSecret::new("bbb")) .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -948,13 +948,13 @@ fn test_extension_successful_with_minimal_json_response() { #[test] fn test_extension_successful_with_complete_json_response() { let client = ColorfulClient::new(ClientId::new("aaa")) - .set_client_secret(ClientSecret::new("bbb".to_string())) + .set_client_secret(ClientSecret::new("bbb")) .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()) .set_auth_type(AuthType::RequestBody); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -1013,12 +1013,12 @@ fn test_extension_successful_with_complete_json_response() { #[test] fn test_extension_with_simple_json_error() { let client = ColorfulClient::new(ClientId::new("aaa")) - .set_client_secret(ClientSecret::new("bbb".to_string())) + .set_client_secret(ClientSecret::new("bbb")) .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -1137,12 +1137,12 @@ mod custom_errors { #[test] fn test_extension_with_custom_json_error() { let client = CustomErrorClient::new(ClientId::new("aaa")) - .set_client_secret(ClientSecret::new("bbb".to_string())) + .set_client_secret(ClientSecret::new("bbb")) .set_auth_uri(AuthUrl::new("https://example.com/auth").unwrap()) .set_token_uri(TokenUrl::new("https://example.com/token").unwrap()); let token = client - .exchange_code(AuthorizationCode::new("ccc".to_string())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -1178,7 +1178,7 @@ fn test_extension_with_custom_json_error() { #[test] fn test_extension_serializer() { let mut token_response = ColorfulTokenResponse::new( - AccessToken::new("mysecret".to_string()), + AccessToken::new("mysecret"), ColorfulTokenType::Red, ColorfulFields { shape: Some("circle".to_string()), @@ -1186,7 +1186,7 @@ fn test_extension_serializer() { }, ); token_response.set_expires_in(Some(&Duration::from_secs(3600))); - token_response.set_refresh_token(Some(RefreshToken::new("myothersecret".to_string()))); + token_response.set_refresh_token(Some(RefreshToken::new("myothersecret"))); let serialized = serde_json::to_string(&token_response).unwrap(); assert_eq!( "{\ diff --git a/src/types.rs b/src/types.rs index 8b62880..f627647 100644 --- a/src/types.rs +++ b/src/types.rs @@ -486,7 +486,7 @@ impl PkceCodeChallenge { Self { code_challenge, - code_challenge_method: PkceCodeChallengeMethod::new("S256".to_string()), + code_challenge_method: PkceCodeChallengeMethod::new("S256"), } } @@ -523,7 +523,7 @@ impl PkceCodeChallenge { Self { code_challenge, - code_challenge_method: PkceCodeChallengeMethod::new("plain".to_string()), + code_challenge_method: PkceCodeChallengeMethod::new("plain"), } } @@ -611,7 +611,7 @@ mod tests { #[test] fn test_secret_redaction() { - let secret = ClientSecret::new("top_secret".to_string()); + let secret = ClientSecret::new("top_secret"); assert_eq!("ClientSecret([redacted])", format!("{secret:?}")); } @@ -642,8 +642,7 @@ mod tests { #[test] fn test_code_verifier_challenge() { // Example from https://tools.ietf.org/html/rfc7636#appendix-B - let code_verifier = - PkceCodeVerifier::new("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk".to_string()); + let code_verifier = PkceCodeVerifier::new("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"); assert_eq!( PkceCodeChallenge::from_code_verifier_sha256(&code_verifier).as_str(), "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",