Add google constructor

This commit is contained in:
Curtis McEnroe 2015-11-28 04:14:12 -05:00
parent c80fb5855d
commit bb4a216c9d
3 changed files with 77 additions and 41 deletions

View File

@ -3,14 +3,13 @@ extern crate inth_oauth2;
use inth_oauth2::Client; use inth_oauth2::Client;
fn main() { fn main() {
let client = Client { let client = Client::google(
authorization_uri: String::from("https://accounts.google.com/o/oauth2/auth"), "143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com",
client_id: String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"), "3kZ5WomzHFlN2f_XbhkyPd3o",
client_secret: String::from("3kZ5WomzHFlN2f_XbhkyPd3o"), Some("urn:ietf:wg:oauth:2.0:oob")
redirect_uri: Some(String::from("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"), Some("https://www.googleapis.com/auth/userinfo.email"),
None None
).unwrap(); ).unwrap();

69
src/client.rs Normal file
View File

@ -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<String>,
}
impl Client {
pub fn new<S: Into<String>>(
auth_uri: S,
token_uri: S,
client_id: S,
client_secret: S,
redirect_uri: Option<S>
) -> 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::<String>::into),
}
}
pub fn google<S: Into<String>>(
client_id: S,
client_secret: S,
redirect_uri: Option<S>
) -> 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::<String>::into),
}
}
pub fn auth_uri(&self, scope: Option<&str>, state: Option<&str>) -> ParseResult<String> {
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())
}
}

View File

@ -1,36 +1,4 @@
extern crate url; extern crate url;
use url::{Url, ParseResult}; pub use client::Client;
mod client;
/// OAuth2 client.
pub struct Client {
pub authorization_uri: String,
pub client_id: String,
pub client_secret: String,
pub redirect_uri: Option<String>,
}
impl Client {
pub fn authorization_uri(&self, scope: Option<&str>, state: Option<&str>) -> ParseResult<String> {
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())
}
}