diff --git a/examples/google.rs b/examples/google-installed.rs similarity index 89% rename from examples/google.rs rename to examples/google-installed.rs index 3c91e30..f83f64e 100644 --- a/examples/google.rs +++ b/examples/google-installed.rs @@ -3,10 +3,10 @@ extern crate inth_oauth2; use std::io; use inth_oauth2::Client; -use inth_oauth2::provider::Google; +use inth_oauth2::provider::google::Installed; fn main() { - let client = Client::::new( + let client = Client::::new( String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"), String::from("3kZ5WomzHFlN2f_XbhkyPd3o"), Some(String::from("urn:ietf:wg:oauth:2.0:oob")) diff --git a/src/client/mod.rs b/src/client/mod.rs index 110225c..5ebed46 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -38,9 +38,9 @@ impl Client

{ /// /// ``` /// use inth_oauth2::Client; - /// use inth_oauth2::provider::Google; + /// use inth_oauth2::provider::google::Installed; /// - /// let client = Client::::new( + /// let client = Client::::new( /// String::from("CLIENT_ID"), /// String::from("CLIENT_SECRET"), /// Some(String::from("urn:ietf:wg:oauth:2.0:oob")) @@ -63,9 +63,9 @@ impl Client

{ /// /// ``` /// use inth_oauth2::Client; - /// use inth_oauth2::provider::Google; + /// use inth_oauth2::provider::google::Installed; /// - /// let client = Client::::new( + /// let client = Client::::new( /// String::from("CLIENT_ID"), /// String::from("CLIENT_SECRET"), /// Some(String::from("urn:ietf:wg:oauth:2.0:oob")) diff --git a/src/lib.rs b/src/lib.rs index ef5ff8e..e017aae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ //! Support for the following OAuth 2.0 providers is included: //! //! - Google +//! - Installed //! - GitHub //! - Imgur //! @@ -30,9 +31,9 @@ //! //! ``` //! use inth_oauth2::Client; -//! use inth_oauth2::provider::Google; +//! use inth_oauth2::provider::google::Installed; //! -//! let client = Client::::new( +//! let client = Client::::new( //! String::from("client_id"), //! String::from("client_secret"), //! Some(String::from("redirect_uri")) @@ -43,8 +44,8 @@ //! //! ``` //! # use inth_oauth2::Client; -//! # use inth_oauth2::provider::Google; -//! # let client = Client::::new(String::new(), String::new(), None); +//! # use inth_oauth2::provider::google::Installed; +//! # let client = Client::::new(String::new(), String::new(), None); //! let auth_uri = client.auth_uri(Some("scope"), Some("state")).unwrap(); //! println!("Authorize the application by clicking on the link: {}", auth_uri); //! ``` @@ -54,8 +55,8 @@ //! ```no_run //! use std::io; //! use inth_oauth2::{Client, Token}; -//! # use inth_oauth2::provider::Google; -//! # let client = Client::::new(String::new(), String::new(), None); +//! # use inth_oauth2::provider::google::Installed; +//! # let client = Client::::new(String::new(), String::new(), None); //! //! let mut code = String::new(); //! io::stdin().read_line(&mut code).unwrap(); @@ -69,8 +70,8 @@ //! //! ```no_run //! # use inth_oauth2::Client; -//! # use inth_oauth2::provider::Google; -//! # let client = Client::::new(String::new(), String::new(), None); +//! # use inth_oauth2::provider::google::Installed; +//! # let client = Client::::new(String::new(), String::new(), None); //! # let http_client = Default::default(); //! # let token = client.request_token(&http_client, "").unwrap(); //! let token = client.refresh_token(&http_client, token, None).unwrap(); @@ -80,8 +81,8 @@ //! //! ```no_run //! # use inth_oauth2::Client; -//! # use inth_oauth2::provider::Google; -//! # let client = Client::::new(String::new(), String::new(), None); +//! # use inth_oauth2::provider::google::Installed; +//! # let client = Client::::new(String::new(), String::new(), None); //! # let http_client = Default::default(); //! # let mut token = client.request_token(&http_client, "").unwrap(); //! // Refresh token only if it has expired. @@ -96,12 +97,12 @@ //! # extern crate hyper; //! # extern crate inth_oauth2; //! # use inth_oauth2::Client; -//! # use inth_oauth2::provider::Google; +//! # use inth_oauth2::provider::google::Installed; //! use hyper::header::Authorization; //! //! # fn main() { //! let client = hyper::Client::new(); -//! # let oauth_client = Client::::new(String::new(), String::new(), None); +//! # let oauth_client = Client::::new(String::new(), String::new(), None); //! # let token = oauth_client.request_token(&client, "").unwrap(); //! let request = client.get("https://example.com/resource") //! .header(Into::>::into(&token)); @@ -117,11 +118,11 @@ //! # extern crate inth_oauth2; //! # extern crate rustc_serialize; //! # use inth_oauth2::Client; -//! # use inth_oauth2::provider::Google; +//! # use inth_oauth2::provider::google::Installed; //! use rustc_serialize::json; //! # fn main() { //! # let http_client = Default::default(); -//! # let client = Client::::new(String::new(), String::new(), None); +//! # let client = Client::::new(String::new(), String::new(), None); //! # let token = client.request_token(&http_client, "").unwrap(); //! let json = json::encode(&token).unwrap(); //! # } @@ -131,10 +132,10 @@ //! # extern crate inth_oauth2; //! extern crate serde_json; //! # use inth_oauth2::Client; -//! # use inth_oauth2::provider::Google; +//! # use inth_oauth2::provider::google::Installed; //! # fn main() { //! # let http_client = Default::default(); -//! # let client = Client::::new(String::new(), String::new(), None); +//! # let client = Client::::new(String::new(), String::new(), None); //! # let token = client.request_token(&http_client, "").unwrap(); //! let json = serde_json::to_string(&token).unwrap(); //! # } diff --git a/src/provider.rs b/src/provider.rs index 74659f7..f9baf58 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -35,17 +35,26 @@ pub trait Provider { fn credentials_in_body() -> bool { false } } -/// Google OAuth 2.0 provider. +/// Google OAuth 2.0 providers. /// /// See [Using OAuth 2.0 to Access Google /// APIs](https://developers.google.com/identity/protocols/OAuth2). -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Google; -impl Provider for Google { - type Lifetime = Expiring; - type Token = Bearer; - fn auth_uri() -> &'static str { "https://accounts.google.com/o/oauth2/v2/auth" } - fn token_uri() -> &'static str { "https://www.googleapis.com/oauth2/v4/token" } +pub mod google { + use token::{Bearer, Expiring}; + use super::Provider; + + /// Google OAuth 2.0 provider for installed applications. + /// + /// See [Using OAuth 2.0 for Installed + /// Applications](https://developers.google.com/identity/protocols/OAuth2InstalledApp). + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + pub struct Installed; + impl Provider for Installed { + type Lifetime = Expiring; + type Token = Bearer; + fn auth_uri() -> &'static str { "https://accounts.google.com/o/oauth2/v2/auth" } + fn token_uri() -> &'static str { "https://www.googleapis.com/oauth2/v4/token" } + } } /// GitHub OAuth 2.0 provider. diff --git a/tests/auth_uri.rs b/tests/auth_uri.rs index 39a8790..c63f9a3 100644 --- a/tests/auth_uri.rs +++ b/tests/auth_uri.rs @@ -11,8 +11,8 @@ fn assert_get_uri_ok(uri: &str) { } #[test] -fn google_auth_uri_ok() { - let client = Client::::new( +fn google_installed_auth_uri_ok() { + let client = Client::::new( String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"), String::new(), Some(String::from("urn:ietf:wg:oauth:2.0:oob"))