From bb4a216c9d114f4e6c1eb07dfe4e92d8679c9598 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sat, 28 Nov 2015 04:14:12 -0500 Subject: [PATCH] Add google constructor --- examples/google.rs | 13 ++++----- src/client.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 36 ++---------------------- 3 files changed, 77 insertions(+), 41 deletions(-) create mode 100644 src/client.rs diff --git a/examples/google.rs b/examples/google.rs index 0e36642..88fcedb 100644 --- a/examples/google.rs +++ b/examples/google.rs @@ -3,14 +3,13 @@ extern crate inth_oauth2; use inth_oauth2::Client; fn main() { - let client = Client { - authorization_uri: String::from("https://accounts.google.com/o/oauth2/auth"), - client_id: String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"), - client_secret: String::from("3kZ5WomzHFlN2f_XbhkyPd3o"), - redirect_uri: Some(String::from("urn:ietf:wg:oauth:2.0:oob")), - }; + let client = Client::google( + "143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com", + "3kZ5WomzHFlN2f_XbhkyPd3o", + Some("urn:ietf:wg:oauth:2.0:oob") + ); - let auth_uri = client.authorization_uri( + let auth_uri = client.auth_uri( Some("https://www.googleapis.com/auth/userinfo.email"), None ).unwrap(); diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 0000000..d5f4a23 --- /dev/null +++ b/src/client.rs @@ -0,0 +1,69 @@ +//! OAuth2 client. + +use url::{Url, ParseResult}; + +/// OAuth2 client. +pub struct Client { + auth_uri: String, + token_uri: String, + + client_id: String, + client_secret: String, + redirect_uri: Option, +} + +impl Client { + pub fn new>( + auth_uri: S, + token_uri: S, + client_id: S, + client_secret: S, + redirect_uri: Option + ) -> Self { + Client { + auth_uri: auth_uri.into(), + token_uri: token_uri.into(), + + client_id: client_id.into(), + client_secret: client_secret.into(), + redirect_uri: redirect_uri.map(Into::::into), + } + } + + pub fn google>( + client_id: S, + client_secret: S, + redirect_uri: Option + ) -> Self { + Client { + auth_uri: String::from("https://accounts.google.com/o/oauth2/auth"), + token_uri: String::from("https://accounts.google.com/o/oauth2/token"), + + client_id: client_id.into(), + client_secret: client_secret.into(), + redirect_uri: redirect_uri.map(Into::::into), + } + } + + pub fn auth_uri(&self, scope: Option<&str>, state: Option<&str>) -> ParseResult { + let mut uri = try!(Url::parse(&self.auth_uri)); + + let mut query_pairs = vec![ + ("response_type", "code"), + ("client_id", &self.client_id), + ]; + if let Some(ref redirect_uri) = self.redirect_uri { + query_pairs.push(("redirect_uri", redirect_uri)); + } + if let Some(scope) = scope { + query_pairs.push(("scope", scope)); + } + if let Some(state) = state { + query_pairs.push(("state", state)); + } + + uri.set_query_from_pairs(query_pairs.iter()); + + Ok(uri.serialize()) + } +} diff --git a/src/lib.rs b/src/lib.rs index 501aabc..243d896 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,36 +1,4 @@ extern crate url; -use url::{Url, ParseResult}; - -/// OAuth2 client. -pub struct Client { - pub authorization_uri: String, - - pub client_id: String, - pub client_secret: String, - pub redirect_uri: Option, -} - -impl Client { - pub fn authorization_uri(&self, scope: Option<&str>, state: Option<&str>) -> ParseResult { - let mut uri = try!(Url::parse(&self.authorization_uri)); - - let mut query_pairs = vec![ - ("response_type", "code"), - ("client_id", &self.client_id), - ]; - if let Some(ref redirect_uri) = self.redirect_uri { - query_pairs.push(("redirect_uri", redirect_uri)); - } - if let Some(scope) = scope { - query_pairs.push(("scope", scope)); - } - if let Some(state) = state { - query_pairs.push(("state", state)); - } - - uri.set_query_from_pairs(query_pairs.iter()); - - Ok(uri.serialize()) - } -} +pub use client::Client; +mod client;