From d9b2eeada7acf17c4aea07382d36ecbcd09cad25 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 5 Jun 2019 01:38:00 +0200 Subject: [PATCH] write oauth2 POST-data to request body instead url --- src/oauth2.rs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/oauth2.rs b/src/oauth2.rs index e51b57d9c..a0da3aa85 100644 --- a/src/oauth2.rs +++ b/src/oauth2.rs @@ -39,7 +39,6 @@ struct Response { // Should always be there according to: https://www.oauth.com/oauth2-servers/access-tokens/access-token-response/ // but previous code handled its abscense. access_token: Option, - token_type: String, expires_in: Option, refresh_token: Option, scope: Option, @@ -86,6 +85,7 @@ pub fn dc_get_oauth2_access_token( } } + // generate new token: build & call auth url let refresh_token = get_config(context, "oauth2_refresh_token"); let refresh_token_for = get_config(context, "oauth2_refresh_token_for").unwrap_or_else(|| "unset".into()); @@ -113,6 +113,9 @@ pub fn dc_get_oauth2_access_token( false, ) }; + + // create url to query as `domain?param1=value1¶m2=value2` + // (this format allows easier printing, handling and is compatible with GET) let mut token_url = replace_in_uri(&token_url, "$CLIENT_ID", oauth2.client_id); token_url = replace_in_uri(&token_url, "$REDIRECT_URI", &redirect_uri); token_url = replace_in_uri(&token_url, "$CODE", code.as_ref()); @@ -120,22 +123,27 @@ pub fn dc_get_oauth2_access_token( token_url = replace_in_uri(&token_url, "$REFRESH_TOKEN", token); } - let response = reqwest::Client::new().post(&token_url).send(); + // split url into domain an parameters and POST + let parts: Vec<&str> = token_url.split('?').collect(); + let post_url = parts[0]; + let parts = parts[1].split('&'); + let mut post_param: Vec<(&str,&str)> = Vec::new(); + for part in parts { + let part: Vec<&str> = part.split('=').collect(); + post_param.push((part[0], part[1])); + } + println!("{} {:#?}", post_url, post_param); + + let response = reqwest::Client::new().post(post_url).form(&post_param).send(); if response.is_err() { - warn!( - context, - 0, "Error calling OAuth2 at {}: {:?}", token_url, response - ); + warn!(context, 0, "Error calling OAuth2 at {}: {:?}", token_url, response); return None; } + let mut response = response.unwrap(); if !response.status().is_success() { - warn!( - context, - 0, - "Error calling OAuth2 at {}: {:?}", - token_url, - response.status() + warn!(context, 0, "Error calling OAuth2 at {}: {:?}: {:?}", + token_url, response.status(), response.text() ); return None; }