diff --git a/examples/github.rs b/examples/github.rs index 98e01e5..734d2bf 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. @@ -59,8 +57,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"); @@ -85,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 43b7731..389e3b8 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. @@ -60,8 +58,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"); @@ -83,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 d3dba53..b769c53 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"), ); @@ -68,12 +66,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(); @@ -99,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/google_devicecode.rs b/examples/google_devicecode.rs index 6948bb1..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. // @@ -66,7 +65,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/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/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..1179a89 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() @@ -76,9 +73,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(); @@ -104,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 fe960f2..15da7a8 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. @@ -185,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 fb82c68..b6f27b4 100644 --- a/src/client.rs +++ b/src/client.rs @@ -80,11 +80,11 @@ impl private::EndpointStateSealed for EndpointMaybeSet {} /// # use http::Response; /// # use oauth2::{*, basic::*}; /// # -/// # let client = BasicClient::new(ClientId::new("aaa".to_string())) -/// # .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()); +/// # let client = BasicClient::new(ClientId::new("aaa")) +/// # .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()); /// # /// # #[derive(Debug, Error)] /// # enum FakeError { @@ -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 401f49b..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(); @@ -278,14 +276,12 @@ mod tests { #[test] fn test_authorize_url_with_param() { - let client = BasicClient::new(ClientId::new("aaa".to_string())) - .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()); + let client = BasicClient::new(ClientId::new("aaa")) + .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( @@ -298,12 +294,9 @@ 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())) + .authorize_url(|| CsrfToken::new("csrf_token")) .add_scopes(scopes) .url(); @@ -323,8 +316,8 @@ mod tests { #[test] 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())) + .authorize_url(|| CsrfToken::new("csrf_token")) + .add_scope(Scope::new("read")) .url(); assert_eq!( @@ -345,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,12 +355,10 @@ 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())) - .url(); + let (url, _) = client.authorize_url(|| CsrfToken::new("csrf_token")).url(); assert_eq!( Url::parse( @@ -383,13 +374,13 @@ 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())) + .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/devicecode.rs b/src/devicecode.rs index 53e9140..e7cb37b 100644 --- a/src/devicecode.rs +++ b/src/devicecode.rs @@ -698,14 +698,13 @@ 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 .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 +789,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 +858,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 +936,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..3a3eef0 100644 --- a/src/introspection.rs +++ b/src/introspection.rs @@ -462,13 +462,11 @@ 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())) + .introspect(&AccessToken::new("access_token_123")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -512,13 +510,11 @@ 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())) + .introspect(&AccessToken::new("access_token_123")) .set_token_type_hint("access_token") .request(&mock_http_client( vec![ @@ -558,16 +554,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..f406616 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,12 +140,12 @@ //! # 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())) -//! .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())?) +//! let client = BasicClient::new(ClientId::new("client_id")) +//! .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. -//! .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(); @@ -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(); @@ -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)?; @@ -213,12 +213,12 @@ //! # 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())) -//! .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())?) +//! let client = BasicClient::new(ClientId::new("client_id")) +//! .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. -//! .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(); @@ -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(); @@ -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) @@ -281,9 +281,9 @@ //! use url::Url; //! //! # fn err_wrapper() -> Result<(), anyhow::Error> { -//! let client = BasicClient::new(ClientId::new("client_id".to_string())) -//! .set_client_secret(ClientSecret::new("client_secret".to_string())) -//! .set_auth_uri(AuthUrl::new("http://authorize".to_string())?); +//! let client = BasicClient::new(ClientId::new("client_id")) +//! .set_client_secret(ClientSecret::new("client_secret")) +//! .set_auth_uri(AuthUrl::new("http://authorize")?); //! //! // Generate the full authorization URL. //! let (auth_url, csrf_token) = client @@ -328,10 +328,10 @@ //! //! # #[cfg(feature = "reqwest-blocking")] //! # fn err_wrapper() -> Result<(), anyhow::Error> { -//! let client = BasicClient::new(ClientId::new("client_id".to_string())) -//! .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())?); +//! let client = BasicClient::new(ClientId::new("client_id")) +//! .set_client_secret(ClientSecret::new("client_secret")) +//! .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. @@ -342,10 +342,10 @@ //! let token_result = //! client //! .exchange_password( -//! &ResourceOwnerUsername::new("user".to_string()), -//! &ResourceOwnerPassword::new("pass".to_string()) +//! &ResourceOwnerUsername::new("user"), +//! &ResourceOwnerPassword::new("pass") //! ) -//! .add_scope(Scope::new("read".to_string())) +//! .add_scope(Scope::new("read")) //! .request(&http_client)?; //! # Ok(()) //! # } @@ -374,10 +374,10 @@ //! //! # #[cfg(feature = "reqwest-blocking")] //! # fn err_wrapper() -> Result<(), anyhow::Error> { -//! let client = BasicClient::new(ClientId::new("client_id".to_string())) -//! .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())?); +//! let client = BasicClient::new(ClientId::new("client_id")) +//! .set_client_secret(ClientSecret::new("client_secret")) +//! .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. @@ -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(()) //! # } @@ -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 client = BasicClient::new(ClientId::new("client_id".to_string())) -//! .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())?) +//! let device_auth_url = DeviceAuthorizationUrl::new("http://deviceauth")?; +//! let client = BasicClient::new(ClientId::new("client_id")) +//! .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); //! //! let http_client = reqwest::blocking::ClientBuilder::new() @@ -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..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 {}, /// # ); @@ -131,11 +131,11 @@ pub trait RevocableToken { /// # .unwrap()) /// # }; /// # -/// let client = BasicClient::new(ClientId::new("aaa".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()) +/// let client = BasicClient::new(ClientId::new("aaa")) +/// .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()); /// /// // ... /// @@ -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"); @@ -397,8 +397,8 @@ mod tests { let client = new_client(); let result = client - .set_revocation_url(RevocationUrl::new("http://revocation/url".to_string()).unwrap()) - .revoke_token(AccessToken::new("access_token_123".to_string()).into()) + .set_revocation_url(RevocationUrl::new("http://revocation/url").unwrap()) + .revoke_token(AccessToken::new("access_token_123").into()) .unwrap_err(); assert_eq!( @@ -409,11 +409,11 @@ 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() + .revoke_token(AccessToken::new("access_token_123").into()).unwrap() .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -452,11 +452,11 @@ 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()) + .revoke_token(AccessToken::new("access_token_123").into()) .unwrap() .request(&mock_http_client( vec![ @@ -480,11 +480,11 @@ 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()) + .revoke_token(AccessToken::new("access_token_123").into()) .unwrap() .request(&mock_http_client( vec![ @@ -504,11 +504,11 @@ 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()) + .revoke_token(AccessToken::new("access_token_123").into()) .unwrap() .request(&mock_http_client( vec![ @@ -532,11 +532,11 @@ 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()) + .revoke_token(RefreshToken::new("refresh_token_123").into()) .unwrap() .request(&mock_http_client( vec![ @@ -560,11 +560,11 @@ mod tests { #[test] fn test_extension_token_revocation_successful() { - let client = ColorfulClient::new(ClientId::new("aaa".to_string())) - .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()); + let client = ColorfulClient::new(ClientId::new("aaa")) + .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()); client .revoke_token(ColorfulRevocableToken::Red( diff --git a/src/tests.rs b/src/tests.rs index 6046e40..afc3909 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -21,9 +21,9 @@ use url::Url; pub(crate) fn new_client( ) -> BasicClient { - BasicClient::new(ClientId::new("aaa".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()) + BasicClient::new(ClientId::new("aaa")) + .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 aa23b1e..849235c 100644 --- a/src/token/tests.rs +++ b/src/token/tests.rs @@ -39,13 +39,13 @@ where #[test] fn test_exchange_code_successful_with_minimal_json_response() { - let client = BasicClient::new(ClientId::new("aaa".to_string())) - .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()); + let client = BasicClient::new(ClientId::new("aaa")) + .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"), @@ -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,10 +138,10 @@ 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())) - .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()) + let client = BasicClient::new(ClientId::new("aaa/;&")) + .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); let token = client @@ -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,9 +181,9 @@ 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())) - .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()) + let client = BasicClient::new(ClientId::new("aaa/;&")) + .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 @@ -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()); @@ -279,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"), @@ -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()); @@ -319,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"), @@ -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()), - &ResourceOwnerPassword::new("pass".to_string()), + &ResourceOwnerUsername::new("user"), + &ResourceOwnerPassword::new("pass"), ) - .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()); @@ -410,10 +389,10 @@ 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())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -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()); @@ -458,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())) + .exchange_code(AuthorizationCode::new("ccc")) .set_redirect_uri(Cow::Owned( - RedirectUrl::new("https://redirect/alternative".to_string()).unwrap(), + RedirectUrl::new("https://redirect/alternative").unwrap(), )) .request(&mock_http_client( vec![ @@ -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()); @@ -509,10 +482,10 @@ 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())) + .exchange_code(AuthorizationCode::new("ccc")) .request(&mock_http_client( vec![ (ACCEPT, "application/json"), @@ -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()); @@ -557,10 +527,10 @@ 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())) + .exchange_code(AuthorizationCode::new("ccc")) .set_pkce_verifier(PkceCodeVerifier::new( "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk".to_string(), )) @@ -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()); @@ -613,10 +580,10 @@ 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())) + .exchange_refresh_token(&RefreshToken::new("ccc")) .add_extra_param("foo", "bar") .request(&mock_http_client( vec![ @@ -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()); @@ -662,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"), @@ -752,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"), @@ -791,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"), @@ -822,12 +786,12 @@ 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())) - .set_auth_uri(AuthUrl::new("https://example.com/auth".to_string()).unwrap()) - .set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap()); + let client = BasicClient::new(ClientId::new("aaa")) + .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"), @@ -869,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"), @@ -913,13 +877,13 @@ 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())) - .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()); + let client = BasicClient::new(ClientId::new("aaa")) + .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()); @@ -932,13 +896,13 @@ 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())) - .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()); + let client = ColorfulClient::new(ClientId::new("aaa")) + .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"), @@ -983,14 +947,14 @@ 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())) - .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()) + let client = ColorfulClient::new(ClientId::new("aaa")) + .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"), @@ -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,13 +1012,13 @@ 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())) - .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()); + let client = ColorfulClient::new(ClientId::new("aaa")) + .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"), @@ -1175,13 +1136,13 @@ mod custom_errors { #[test] fn test_extension_with_custom_json_error() { - let client = CustomErrorClient::new(ClientId::new("aaa".to_string())) - .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()); + let client = CustomErrorClient::new(ClientId::new("aaa")) + .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"), @@ -1217,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()), @@ -1225,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 47bc8de..f627647 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 { @@ -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] /// @@ -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] @@ -485,7 +486,7 @@ impl PkceCodeChallenge { Self { code_challenge, - code_challenge_method: PkceCodeChallengeMethod::new("S256".to_string()), + code_challenge_method: PkceCodeChallengeMethod::new("S256"), } } @@ -522,7 +523,7 @@ impl PkceCodeChallenge { Self { code_challenge, - code_challenge_method: PkceCodeChallengeMethod::new("plain".to_string()), + code_challenge_method: PkceCodeChallengeMethod::new("plain"), } } @@ -610,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:?}")); } @@ -641,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",