From 454f8a1f62feec781442a8afbb6e1b2ade783aa0 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sat, 28 Nov 2015 04:23:26 -0500 Subject: [PATCH] Move everything back into lib.rs --- src/client.rs | 72 --------------------------------------------------- src/lib.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 74 deletions(-) delete mode 100644 src/client.rs diff --git a/src/client.rs b/src/client.rs deleted file mode 100644 index f2f4f7b..0000000 --- a/src/client.rs +++ /dev/null @@ -1,72 +0,0 @@ -//! OAuth 2.0 client. - -use url::{Url, ParseResult}; - -/// OAuth 2.0 client. -pub struct Client { - auth_uri: String, - token_uri: String, - - client_id: String, - client_secret: String, - redirect_uri: Option, -} - -impl Client { - /// Creates an OAuth 2.0 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), - } - } - - /// Creates a Google OAuth 2.0 client. - 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), - } - } - - /// Constructs an authorization request URI. - 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 243d896..c573fd0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,72 @@ extern crate url; -pub use client::Client; -mod client; +use url::{Url, ParseResult}; + +/// OAuth 2.0 client. +pub struct Client { + auth_uri: String, + token_uri: String, + + client_id: String, + client_secret: String, + redirect_uri: Option, +} + +impl Client { + /// Creates an OAuth 2.0 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), + } + } + + /// Creates a Google OAuth 2.0 client. + 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), + } + } + + /// Constructs an authorization request URI. + 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()) + } +}