From ff8b0aea71b9d01926ba9a18d8e239d48284d297 Mon Sep 17 00:00:00 2001 From: edelangh Date: Fri, 12 Oct 2018 16:25:31 +0200 Subject: [PATCH 1/3] Update reqwest to 0.9.5 Have remove impl Into> for Bearer because Authorization does not exist any more --- Cargo.toml | 2 +- src/client/mod.rs | 27 ++++++++++----------------- src/token/bearer.rs | 17 ++++++++--------- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fd1e9de..6dd8035 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,4 @@ serde = "1.0.8" serde_derive = "1.0.5" serde_json = "1.0.2" url = "1.1.0" -reqwest = "0.8.0" +reqwest = "0.9.2" diff --git a/src/client/mod.rs b/src/client/mod.rs index d0f1a71..814a2d2 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -5,15 +5,16 @@ mod error; pub mod response; pub use self::error::ClientError; -use reqwest::{self, header, mime}; +use reqwest; +use reqwest::header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE}; use serde_json::{self, Value}; -use url::Url; use url::form_urlencoded::Serializer; +use url::Url; use client::response::FromResponse; use error::OAuth2Error; use provider::Provider; -use token::{Token, Lifetime, Refresh}; +use token::{Lifetime, Refresh, Token}; /// OAuth 2.0 client. #[derive(Debug, Clone, PartialEq, Eq)] @@ -110,28 +111,20 @@ impl Client

{ fn post_token( &self, http_client: &reqwest::Client, - mut body: Serializer + mut body: Serializer, ) -> Result { if self.provider.credentials_in_body() { body.append_pair("client_id", &self.client_id); body.append_pair("client_secret", &self.client_secret); } - let auth_header = header::Authorization( - header::Basic { - username: self.client_id.clone(), - password: Some(self.client_secret.clone()), - } - ); - let accept_header = header::Accept(vec![ - header::qitem(mime::APPLICATION_JSON), - ]); let body = body.finish(); - let mut response = http_client.post(self.provider.token_uri().clone()) - .header(auth_header) - .header(accept_header) - .header(header::ContentType::form_url_encoded()) + let mut response = http_client + .post(self.provider.token_uri().clone()) + .basic_auth(&self.client_id, Some(&self.client_secret)) + .header(ACCEPT, "application/json") + .header(CONTENT_TYPE, "application/x-www-form-urlencoded") .body(body) .send()?; diff --git a/src/token/bearer.rs b/src/token/bearer.rs index 3f7f1f0..9478b94 100644 --- a/src/token/bearer.rs +++ b/src/token/bearer.rs @@ -1,4 +1,3 @@ -use reqwest::header; use serde_json::Value; use client::response::{FromResponse, ParseError}; @@ -15,14 +14,14 @@ pub struct Bearer { } impl Token for Bearer { - fn access_token(&self) -> &str { &self.access_token } - fn scope(&self) -> Option<&str> { self.scope.as_ref().map(|s| &s[..]) } - fn lifetime(&self) -> &L { &self.lifetime } -} - -impl<'a, L: Lifetime> Into> for &'a Bearer { - fn into(self) -> header::Authorization { - header::Authorization(header::Bearer { token: self.access_token.clone() }) + fn access_token(&self) -> &str { + &self.access_token + } + fn scope(&self) -> Option<&str> { + self.scope.as_ref().map(|s| &s[..]) + } + fn lifetime(&self) -> &L { + &self.lifetime } } From 613c8e5d5993f19da3c551f084417328f2dade3d Mon Sep 17 00:00:00 2001 From: edelangh Date: Mon, 15 Oct 2018 23:21:57 +0200 Subject: [PATCH 2/3] fix cargo test --- src/client/mod.rs | 2 +- src/lib.rs | 21 --------------------- tests/auth_uri.rs | 2 +- 3 files changed, 2 insertions(+), 23 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index 814a2d2..9038f65 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -6,7 +6,7 @@ pub mod response; pub use self::error::ClientError; use reqwest; -use reqwest::header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE}; +use reqwest::header::{ACCEPT, CONTENT_TYPE}; use serde_json::{self, Value}; use url::form_urlencoded::Serializer; use url::Url; diff --git a/src/lib.rs b/src/lib.rs index b464615..09f20e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,27 +103,6 @@ //! # } //! ``` //! -//! ### Using bearer access tokens -//! -//! Bearer tokens can be converted to Hyper headers. -//! -//! ```no_run -//! # extern crate inth_oauth2; -//! # extern crate reqwest; -//! # use inth_oauth2::Client; -//! # use inth_oauth2::provider::google::Installed; -//! use reqwest::header::Authorization; -//! -//! # fn main() { -//! # let oauth_client = Client::new(Installed, String::new(), String::new(), None); -//! # let http = reqwest::Client::new(); -//! # let token = oauth_client.request_token(&http, "").unwrap(); -//! let request = http.get("https://example.com/resource") -//! .header(Into::>::into(&token)) -//! .build(); -//! # } -//! ``` -//! //! ### Persisting tokens //! //! All token types implement `Serialize` and `Deserialize` from `serde`. diff --git a/tests/auth_uri.rs b/tests/auth_uri.rs index d97806f..2ae7b5c 100644 --- a/tests/auth_uri.rs +++ b/tests/auth_uri.rs @@ -8,7 +8,7 @@ use url::Url; fn assert_get_uri_ok(uri: Url) { let response = reqwest::get(uri).unwrap(); - assert_eq!(reqwest::StatusCode::Ok, response.status()); + assert_eq!(reqwest::StatusCode::OK, response.status()); } #[test] From 5627530f9caf94d9f8f32bdc75d0ca5dc2d1247d Mon Sep 17 00:00:00 2001 From: edelangh Date: Mon, 15 Oct 2018 23:54:36 +0200 Subject: [PATCH 3/3] update bearer documentation --- src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 09f20e0..3c16ac8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,6 +103,27 @@ //! # } //! ``` //! +//! ### Using bearer access tokens +//! +//! Bearer tokens can be converted to Hyper headers. +//! +//! ```no_run +//! # extern crate inth_oauth2; +//! # extern crate reqwest; +//! # use inth_oauth2::Client; +//! # use inth_oauth2::provider::google::Installed; +//! use inth_oauth2::Token; +//! +//! # fn main() { +//! # let oauth_client = Client::new(Installed, String::new(), String::new(), None); +//! # let http = reqwest::Client::new(); +//! # let token = oauth_client.request_token(&http, "").unwrap(); +//! let request = http.get("https://example.com/resource") +//! .bearer_auth(token.access_token()) +//! .build(); +//! # } +//! ``` +//! //! ### Persisting tokens //! //! All token types implement `Serialize` and `Deserialize` from `serde`.