Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use impl Into<String> in new type constructors #275

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions examples/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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");
Expand All @@ -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 :)";
Expand Down
16 changes: 7 additions & 9 deletions examples/github_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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");
Expand All @@ -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 :)";
Expand Down
22 changes: 8 additions & 14 deletions examples/google.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"),
);

Expand All @@ -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();

Expand All @@ -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 :)";
Expand Down
11 changes: 5 additions & 6 deletions examples/google_devicecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand All @@ -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");

Expand Down
4 changes: 2 additions & 2 deletions examples/letterboxd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
10 changes: 5 additions & 5 deletions examples/microsoft_devicecode_common_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
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()
Expand All @@ -26,7 +26,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

let details: StandardDeviceAuthorizationResponse = client
.exchange_device_code()
.add_scope(Scope::new("read".to_string()))
.add_scope(Scope::new("read"))
.request_async(&http_client)
.await?;

Expand Down
4 changes: 2 additions & 2 deletions examples/microsoft_devicecode_tenant_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const TENANT_ID: &str = "{tenant}";

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
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
Expand All @@ -33,7 +33,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

let details: StandardDeviceAuthorizationResponse = client
.exchange_device_code()
.add_scope(Scope::new("read".to_string()))
.add_scope(Scope::new("read"))
.request_async(&http_client)
.await?;

Expand Down
21 changes: 8 additions & 13 deletions examples/msgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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();

Expand All @@ -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 :)";
Expand Down
12 changes: 5 additions & 7 deletions examples/wunderlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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 :)";
Expand Down
12 changes: 6 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
///
Expand Down
Loading
Loading