accept http client as a reference to *_token() methods
This commit is contained in:
parent
f388ad8f9a
commit
d08e71760b
|
@ -7,7 +7,6 @@ use inth_oauth2::provider::GitHub;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let client = Client::<GitHub>::new(
|
let client = Client::<GitHub>::new(
|
||||||
Default::default(),
|
|
||||||
"01774654cd9a6051e478",
|
"01774654cd9a6051e478",
|
||||||
"9f14d16d95d605e715ec1a9aecec220d2565fd5c",
|
"9f14d16d95d605e715ec1a9aecec220d2565fd5c",
|
||||||
Some("https://cmcenroe.me/oauth2-paste/")
|
Some("https://cmcenroe.me/oauth2-paste/")
|
||||||
|
@ -19,6 +18,6 @@ fn main() {
|
||||||
let mut code = String::new();
|
let mut code = String::new();
|
||||||
io::stdin().read_line(&mut code).unwrap();
|
io::stdin().read_line(&mut code).unwrap();
|
||||||
|
|
||||||
let token = client.request_token(code.trim()).unwrap();
|
let token = client.request_token(&Default::default(), code.trim()).unwrap();
|
||||||
println!("{:?}", token);
|
println!("{:?}", token);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ use inth_oauth2::provider::Google;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let client = Client::<Google>::new(
|
let client = Client::<Google>::new(
|
||||||
Default::default(),
|
|
||||||
"143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com",
|
"143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com",
|
||||||
"3kZ5WomzHFlN2f_XbhkyPd3o",
|
"3kZ5WomzHFlN2f_XbhkyPd3o",
|
||||||
Some("urn:ietf:wg:oauth:2.0:oob")
|
Some("urn:ietf:wg:oauth:2.0:oob")
|
||||||
|
@ -20,9 +19,11 @@ fn main() {
|
||||||
let mut code = String::new();
|
let mut code = String::new();
|
||||||
io::stdin().read_line(&mut code).unwrap();
|
io::stdin().read_line(&mut code).unwrap();
|
||||||
|
|
||||||
let token = client.request_token(code.trim()).unwrap();
|
let http_client = Default::default();
|
||||||
|
|
||||||
|
let token = client.request_token(&http_client, code.trim()).unwrap();
|
||||||
println!("{:?}", token);
|
println!("{:?}", token);
|
||||||
|
|
||||||
let token = client.refresh_token(token, None).unwrap();
|
let token = client.refresh_token(&http_client, token, None).unwrap();
|
||||||
println!("{:?}", token);
|
println!("{:?}", token);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ use inth_oauth2::provider::Imgur;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let client = Client::<Imgur>::new(
|
let client = Client::<Imgur>::new(
|
||||||
Default::default(),
|
|
||||||
"505c8ca804230e0",
|
"505c8ca804230e0",
|
||||||
"c898d8cf28404102752b2119a3a1c6aab49899c8",
|
"c898d8cf28404102752b2119a3a1c6aab49899c8",
|
||||||
Some("https://cmcenroe.me/oauth2-paste/")
|
Some("https://cmcenroe.me/oauth2-paste/")
|
||||||
|
@ -19,9 +18,11 @@ fn main() {
|
||||||
let mut code = String::new();
|
let mut code = String::new();
|
||||||
io::stdin().read_line(&mut code).unwrap();
|
io::stdin().read_line(&mut code).unwrap();
|
||||||
|
|
||||||
let token = client.request_token(code.trim()).unwrap();
|
let http_client = Default::default();
|
||||||
|
|
||||||
|
let token = client.request_token(&http_client, code.trim()).unwrap();
|
||||||
println!("{:?}", token);
|
println!("{:?}", token);
|
||||||
|
|
||||||
let token = client.refresh_token(token, None).unwrap();
|
let token = client.refresh_token(&http_client, token, None).unwrap();
|
||||||
println!("{:?}", token);
|
println!("{:?}", token);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,6 @@ mod error;
|
||||||
|
|
||||||
/// OAuth 2.0 client.
|
/// OAuth 2.0 client.
|
||||||
pub struct Client<P: Provider> {
|
pub struct Client<P: Provider> {
|
||||||
http_client: hyper::Client,
|
|
||||||
client_id: String,
|
client_id: String,
|
||||||
client_secret: String,
|
client_secret: String,
|
||||||
redirect_uri: Option<String>,
|
redirect_uri: Option<String>,
|
||||||
|
@ -46,20 +45,17 @@ impl<P: Provider> Client<P> {
|
||||||
/// use inth_oauth2::provider::Google;
|
/// use inth_oauth2::provider::Google;
|
||||||
///
|
///
|
||||||
/// let client = Client::<Google>::new(
|
/// let client = Client::<Google>::new(
|
||||||
/// Default::default(),
|
|
||||||
/// "CLIENT_ID",
|
/// "CLIENT_ID",
|
||||||
/// "CLIENT_SECRET",
|
/// "CLIENT_SECRET",
|
||||||
/// Some("urn:ietf:wg:oauth:2.0:oob")
|
/// Some("urn:ietf:wg:oauth:2.0:oob")
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new<S>(
|
pub fn new<S>(
|
||||||
http_client: hyper::Client,
|
|
||||||
client_id: S,
|
client_id: S,
|
||||||
client_secret: S,
|
client_secret: S,
|
||||||
redirect_uri: Option<S>
|
redirect_uri: Option<S>
|
||||||
) -> Self where S: Into<String> {
|
) -> Self where S: Into<String> {
|
||||||
Client {
|
Client {
|
||||||
http_client: http_client,
|
|
||||||
client_id: client_id.into(),
|
client_id: client_id.into(),
|
||||||
client_secret: client_secret.into(),
|
client_secret: client_secret.into(),
|
||||||
redirect_uri: redirect_uri.map(Into::into),
|
redirect_uri: redirect_uri.map(Into::into),
|
||||||
|
@ -78,7 +74,6 @@ impl<P: Provider> Client<P> {
|
||||||
/// use inth_oauth2::provider::Google;
|
/// use inth_oauth2::provider::Google;
|
||||||
///
|
///
|
||||||
/// let client = Client::<Google>::new(
|
/// let client = Client::<Google>::new(
|
||||||
/// Default::default(),
|
|
||||||
/// "CLIENT_ID",
|
/// "CLIENT_ID",
|
||||||
/// "CLIENT_SECRET",
|
/// "CLIENT_SECRET",
|
||||||
/// Some("urn:ietf:wg:oauth:2.0:oob")
|
/// Some("urn:ietf:wg:oauth:2.0:oob")
|
||||||
|
@ -112,7 +107,11 @@ impl<P: Provider> Client<P> {
|
||||||
Ok(uri.serialize())
|
Ok(uri.serialize())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn post_token<'a>(&'a self, mut body_pairs: Vec<(&str, &'a str)>) -> Result<Json, ClientError> {
|
fn post_token<'a>(
|
||||||
|
&'a self,
|
||||||
|
http_client: &hyper::Client,
|
||||||
|
mut body_pairs: Vec<(&str, &'a str)>
|
||||||
|
) -> Result<Json, ClientError> {
|
||||||
if P::credentials_in_body() {
|
if P::credentials_in_body() {
|
||||||
body_pairs.push(("client_id", &self.client_id));
|
body_pairs.push(("client_id", &self.client_id));
|
||||||
body_pairs.push(("client_secret", &self.client_secret));
|
body_pairs.push(("client_secret", &self.client_secret));
|
||||||
|
@ -129,7 +128,7 @@ impl<P: Provider> Client<P> {
|
||||||
header::qitem(mime::Mime(mime::TopLevel::Application, mime::SubLevel::Json, vec![])),
|
header::qitem(mime::Mime(mime::TopLevel::Application, mime::SubLevel::Json, vec![])),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let request = self.http_client.post(P::token_uri())
|
let request = http_client.post(P::token_uri())
|
||||||
.header(auth_header)
|
.header(auth_header)
|
||||||
.header(accept_header)
|
.header(accept_header)
|
||||||
.header(header::ContentType::form_url_encoded())
|
.header(header::ContentType::form_url_encoded())
|
||||||
|
@ -150,7 +149,7 @@ impl<P: Provider> Client<P> {
|
||||||
/// Requests an access token using an authorization code.
|
/// Requests an access token using an authorization code.
|
||||||
///
|
///
|
||||||
/// See [RFC 6749, section 4.1.3](http://tools.ietf.org/html/rfc6749#section-4.1.3).
|
/// See [RFC 6749, section 4.1.3](http://tools.ietf.org/html/rfc6749#section-4.1.3).
|
||||||
pub fn request_token(&self, code: &str) -> Result<P::Token, ClientError> {
|
pub fn request_token(&self, http_client: &hyper::Client, code: &str) -> Result<P::Token, ClientError> {
|
||||||
let mut body_pairs = vec![
|
let mut body_pairs = vec![
|
||||||
("grant_type", "authorization_code"),
|
("grant_type", "authorization_code"),
|
||||||
("code", code),
|
("code", code),
|
||||||
|
@ -159,7 +158,8 @@ impl<P: Provider> Client<P> {
|
||||||
body_pairs.push(("redirect_uri", redirect_uri));
|
body_pairs.push(("redirect_uri", redirect_uri));
|
||||||
}
|
}
|
||||||
|
|
||||||
let json = try!(self.post_token(body_pairs));
|
|
||||||
|
let json = try!(self.post_token(http_client, body_pairs));
|
||||||
let token = try!(P::Token::from_response(&json));
|
let token = try!(P::Token::from_response(&json));
|
||||||
Ok(token)
|
Ok(token)
|
||||||
}
|
}
|
||||||
|
@ -171,6 +171,7 @@ impl<P: Provider> Client<P> where P::Token: Token<Expiring> {
|
||||||
/// See [RFC 6749, section 6](http://tools.ietf.org/html/rfc6749#section-6).
|
/// See [RFC 6749, section 6](http://tools.ietf.org/html/rfc6749#section-6).
|
||||||
pub fn refresh_token(
|
pub fn refresh_token(
|
||||||
&self,
|
&self,
|
||||||
|
http_client: &hyper::Client,
|
||||||
token: P::Token,
|
token: P::Token,
|
||||||
scope: Option<&str>
|
scope: Option<&str>
|
||||||
) -> Result<P::Token, ClientError> {
|
) -> Result<P::Token, ClientError> {
|
||||||
|
@ -182,15 +183,15 @@ impl<P: Provider> Client<P> where P::Token: Token<Expiring> {
|
||||||
body_pairs.push(("scope", scope));
|
body_pairs.push(("scope", scope));
|
||||||
}
|
}
|
||||||
|
|
||||||
let json = try!(self.post_token(body_pairs));
|
let json = try!(self.post_token(http_client, body_pairs));
|
||||||
let token = try!(P::Token::from_response_inherit(&json, &token));
|
let token = try!(P::Token::from_response_inherit(&json, &token));
|
||||||
Ok(token)
|
Ok(token)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ensures an access token is valid by refreshing it if necessary.
|
/// Ensures an access token is valid by refreshing it if necessary.
|
||||||
pub fn ensure_token(&self, token: P::Token) -> Result<P::Token, ClientError> {
|
pub fn ensure_token(&self, http_client: &hyper::Client, token: P::Token) -> Result<P::Token, ClientError> {
|
||||||
if token.lifetime().expired() {
|
if token.lifetime().expired() {
|
||||||
self.refresh_token(token, None)
|
self.refresh_token(http_client, token, None)
|
||||||
} else {
|
} else {
|
||||||
Ok(token)
|
Ok(token)
|
||||||
}
|
}
|
||||||
|
@ -213,7 +214,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn auth_uri() {
|
fn auth_uri() {
|
||||||
let client = Client::<Test>::new(Default::default(), "foo", "bar", None);
|
let client = Client::<Test>::new("foo", "bar", None);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"http://example.com/oauth2/auth?response_type=code&client_id=foo",
|
"http://example.com/oauth2/auth?response_type=code&client_id=foo",
|
||||||
client.auth_uri(None, None).unwrap()
|
client.auth_uri(None, None).unwrap()
|
||||||
|
@ -223,7 +224,6 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn auth_uri_with_redirect_uri() {
|
fn auth_uri_with_redirect_uri() {
|
||||||
let client = Client::<Test>::new(
|
let client = Client::<Test>::new(
|
||||||
Default::default(),
|
|
||||||
"foo",
|
"foo",
|
||||||
"bar",
|
"bar",
|
||||||
Some("http://example.com/oauth2/callback")
|
Some("http://example.com/oauth2/callback")
|
||||||
|
@ -236,7 +236,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn auth_uri_with_scope() {
|
fn auth_uri_with_scope() {
|
||||||
let client = Client::<Test>::new(Default::default(), "foo", "bar", None);
|
let client = Client::<Test>::new("foo", "bar", None);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"http://example.com/oauth2/auth?response_type=code&client_id=foo&scope=baz",
|
"http://example.com/oauth2/auth?response_type=code&client_id=foo&scope=baz",
|
||||||
client.auth_uri(Some("baz"), None).unwrap()
|
client.auth_uri(Some("baz"), None).unwrap()
|
||||||
|
@ -245,7 +245,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn auth_uri_with_state() {
|
fn auth_uri_with_state() {
|
||||||
let client = Client::<Test>::new(Default::default(), "foo", "bar", None);
|
let client = Client::<Test>::new("foo", "bar", None);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"http://example.com/oauth2/auth?response_type=code&client_id=foo&state=baz",
|
"http://example.com/oauth2/auth?response_type=code&client_id=foo&state=baz",
|
||||||
client.auth_uri(None, Some("baz")).unwrap()
|
client.auth_uri(None, Some("baz")).unwrap()
|
||||||
|
|
36
src/lib.rs
36
src/lib.rs
|
@ -33,7 +33,6 @@
|
||||||
//! use inth_oauth2::provider::Google;
|
//! use inth_oauth2::provider::Google;
|
||||||
//!
|
//!
|
||||||
//! let client = Client::<Google>::new(
|
//! let client = Client::<Google>::new(
|
||||||
//! Default::default(),
|
|
||||||
//! "client_id",
|
//! "client_id",
|
||||||
//! "client_secret",
|
//! "client_secret",
|
||||||
//! Some("redirect_uri")
|
//! Some("redirect_uri")
|
||||||
|
@ -45,7 +44,7 @@
|
||||||
//! ```
|
//! ```
|
||||||
//! # use inth_oauth2::Client;
|
//! # use inth_oauth2::Client;
|
||||||
//! # use inth_oauth2::provider::Google;
|
//! # use inth_oauth2::provider::Google;
|
||||||
//! # let client = Client::<Google>::new(Default::default(), "", "", None);
|
//! # let client = Client::<Google>::new("", "", None);
|
||||||
//! let auth_uri = client.auth_uri(Some("scope"), Some("state")).unwrap();
|
//! let auth_uri = client.auth_uri(Some("scope"), Some("state")).unwrap();
|
||||||
//! println!("Authorize the application by clicking on the link: {}", auth_uri);
|
//! println!("Authorize the application by clicking on the link: {}", auth_uri);
|
||||||
//! ```
|
//! ```
|
||||||
|
@ -56,12 +55,13 @@
|
||||||
//! use std::io;
|
//! use std::io;
|
||||||
//! use inth_oauth2::{Client, Token};
|
//! use inth_oauth2::{Client, Token};
|
||||||
//! # use inth_oauth2::provider::Google;
|
//! # use inth_oauth2::provider::Google;
|
||||||
//! # let client = Client::<Google>::new(Default::default(), "", "", None);
|
//! # let client = Client::<Google>::new("", "", None);
|
||||||
//!
|
//!
|
||||||
//! let mut code = String::new();
|
//! let mut code = String::new();
|
||||||
//! io::stdin().read_line(&mut code).unwrap();
|
//! io::stdin().read_line(&mut code).unwrap();
|
||||||
//!
|
//!
|
||||||
//! let token = client.request_token(code.trim()).unwrap();
|
//! let http_client = Default::default();
|
||||||
|
//! let token = client.request_token(&http_client, code.trim()).unwrap();
|
||||||
//! println!("{}", token.access_token());
|
//! println!("{}", token.access_token());
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
@ -70,9 +70,10 @@
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! # use inth_oauth2::Client;
|
//! # use inth_oauth2::Client;
|
||||||
//! # use inth_oauth2::provider::Google;
|
//! # use inth_oauth2::provider::Google;
|
||||||
//! # let client = Client::<Google>::new(Default::default(), "", "", None);
|
//! # let client = Client::<Google>::new("", "", None);
|
||||||
//! # let token = client.request_token("").unwrap();
|
//! # let http_client = Default::default();
|
||||||
//! let token = client.refresh_token(token, None).unwrap();
|
//! # let token = client.request_token(&http_client, "").unwrap();
|
||||||
|
//! let token = client.refresh_token(&http_client, token, None).unwrap();
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ### Ensuring an access token is still valid
|
//! ### Ensuring an access token is still valid
|
||||||
|
@ -80,10 +81,11 @@
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! # use inth_oauth2::Client;
|
//! # use inth_oauth2::Client;
|
||||||
//! # use inth_oauth2::provider::Google;
|
//! # use inth_oauth2::provider::Google;
|
||||||
//! # let client = Client::<Google>::new(Default::default(), "", "", None);
|
//! # let client = Client::<Google>::new("", "", None);
|
||||||
//! # let mut token = client.request_token("").unwrap();
|
//! # let http_client = Default::default();
|
||||||
|
//! # let mut token = client.request_token(&http_client, "").unwrap();
|
||||||
//! // Refresh token only if it has expired.
|
//! // Refresh token only if it has expired.
|
||||||
//! token = client.ensure_token(token).unwrap();
|
//! token = client.ensure_token(&http_client, token).unwrap();
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ### Using bearer access tokens
|
//! ### Using bearer access tokens
|
||||||
|
@ -98,9 +100,9 @@
|
||||||
//! use hyper::header::Authorization;
|
//! use hyper::header::Authorization;
|
||||||
//!
|
//!
|
||||||
//! # fn main() {
|
//! # fn main() {
|
||||||
//! # let client = Client::<Google>::new(Default::default(), "", "", None);
|
|
||||||
//! # let token = client.request_token("").unwrap();
|
|
||||||
//! let client = hyper::Client::new();
|
//! let client = hyper::Client::new();
|
||||||
|
//! # let oauth_client = Client::<Google>::new("", "", None);
|
||||||
|
//! # let token = oauth_client.request_token(&client, "").unwrap();
|
||||||
//! let request = client.get("https://example.com/resource")
|
//! let request = client.get("https://example.com/resource")
|
||||||
//! .header(Into::<Authorization<_>>::into(&token));
|
//! .header(Into::<Authorization<_>>::into(&token));
|
||||||
//! # }
|
//! # }
|
||||||
|
@ -118,8 +120,9 @@
|
||||||
//! # use inth_oauth2::provider::Google;
|
//! # use inth_oauth2::provider::Google;
|
||||||
//! use rustc_serialize::json;
|
//! use rustc_serialize::json;
|
||||||
//! # fn main() {
|
//! # fn main() {
|
||||||
//! # let client = Client::<Google>::new(Default::default(), "", "", None);
|
//! # let http_client = Default::default();
|
||||||
//! # let token = client.request_token("").unwrap();
|
//! # let client = Client::<Google>::new("", "", None);
|
||||||
|
//! # let token = client.request_token(&http_client, "").unwrap();
|
||||||
//! let json = json::encode(&token).unwrap();
|
//! let json = json::encode(&token).unwrap();
|
||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
|
@ -130,8 +133,9 @@
|
||||||
//! # use inth_oauth2::Client;
|
//! # use inth_oauth2::Client;
|
||||||
//! # use inth_oauth2::provider::Google;
|
//! # use inth_oauth2::provider::Google;
|
||||||
//! # fn main() {
|
//! # fn main() {
|
||||||
//! # let client = Client::<Google>::new(Default::default(), "", "", None);
|
//! # let http_client = Default::default();
|
||||||
//! # let token = client.request_token("").unwrap();
|
//! # let client = Client::<Google>::new("", "", None);
|
||||||
|
//! # let token = client.request_token(&http_client, "").unwrap();
|
||||||
//! let json = serde_json::to_string(&token).unwrap();
|
//! let json = serde_json::to_string(&token).unwrap();
|
||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
|
@ -13,7 +13,6 @@ fn assert_get_uri_ok(uri: &str) {
|
||||||
#[test]
|
#[test]
|
||||||
fn google_auth_uri_ok() {
|
fn google_auth_uri_ok() {
|
||||||
let client = Client::<Google>::new(
|
let client = Client::<Google>::new(
|
||||||
Default::default(),
|
|
||||||
"143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com",
|
"143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com",
|
||||||
"",
|
"",
|
||||||
Some("urn:ietf:wg:oauth:2.0:oob")
|
Some("urn:ietf:wg:oauth:2.0:oob")
|
||||||
|
@ -28,7 +27,6 @@ fn google_auth_uri_ok() {
|
||||||
#[test]
|
#[test]
|
||||||
fn github_auth_uri_ok() {
|
fn github_auth_uri_ok() {
|
||||||
let client = Client::<GitHub>::new(
|
let client = Client::<GitHub>::new(
|
||||||
Default::default(),
|
|
||||||
"01774654cd9a6051e478",
|
"01774654cd9a6051e478",
|
||||||
"",
|
"",
|
||||||
Some("https://cmcenroe.me/oauth2-paste/")
|
Some("https://cmcenroe.me/oauth2-paste/")
|
||||||
|
@ -40,7 +38,6 @@ fn github_auth_uri_ok() {
|
||||||
#[test]
|
#[test]
|
||||||
fn imgur_auth_uri_ok() {
|
fn imgur_auth_uri_ok() {
|
||||||
let client = Client::<Imgur>::new(
|
let client = Client::<Imgur>::new(
|
||||||
Default::default(),
|
|
||||||
"505c8ca804230e0",
|
"505c8ca804230e0",
|
||||||
"",
|
"",
|
||||||
Some("https://cmcenroe.me/oauth2-paste/")
|
Some("https://cmcenroe.me/oauth2-paste/")
|
||||||
|
|
|
@ -58,28 +58,28 @@ mod connector {
|
||||||
|
|
||||||
macro_rules! mock_client {
|
macro_rules! mock_client {
|
||||||
($p:ty, $c:ty) => {
|
($p:ty, $c:ty) => {
|
||||||
Client::<$p>::new(
|
(Client::<$p>::new(
|
||||||
hyper::Client::with_connector(<$c>::default()),
|
|
||||||
"client_id",
|
"client_id",
|
||||||
"client_secret",
|
"client_secret",
|
||||||
None
|
None
|
||||||
)
|
),
|
||||||
|
hyper::Client::with_connector(<$c>::default()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn request_token_bearer_static_success() {
|
fn request_token_bearer_static_success() {
|
||||||
let client = mock_client!(provider::BearerStatic, connector::BearerStatic);
|
let (client, http_client) = mock_client!(provider::BearerStatic, connector::BearerStatic);
|
||||||
let token = client.request_token("code").unwrap();
|
let token = client.request_token(&http_client, "code").unwrap();
|
||||||
assert_eq!("aaaaaaaa", token.access_token());
|
assert_eq!("aaaaaaaa", token.access_token());
|
||||||
assert_eq!(Some("example"), token.scope());
|
assert_eq!(Some("example"), token.scope());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn request_token_bearer_expiring_success() {
|
fn request_token_bearer_expiring_success() {
|
||||||
let client = mock_client!(provider::BearerExpiring, connector::BearerExpiring);
|
let (client, http_client) = mock_client!(provider::BearerExpiring, connector::BearerExpiring);
|
||||||
let token = client.request_token("code").unwrap();
|
let token = client.request_token(&http_client, "code").unwrap();
|
||||||
assert_eq!("aaaaaaaa", token.access_token());
|
assert_eq!("aaaaaaaa", token.access_token());
|
||||||
assert_eq!(Some("example"), token.scope());
|
assert_eq!(Some("example"), token.scope());
|
||||||
assert_eq!("bbbbbbbb", token.lifetime().refresh_token());
|
assert_eq!("bbbbbbbb", token.lifetime().refresh_token());
|
||||||
|
@ -90,9 +90,9 @@ fn request_token_bearer_expiring_success() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn refresh_token_bearer_full() {
|
fn refresh_token_bearer_full() {
|
||||||
let client = mock_client!(provider::BearerExpiring, connector::BearerExpiring);
|
let (client, http_client) = mock_client!(provider::BearerExpiring, connector::BearerExpiring);
|
||||||
let token = client.request_token("code").unwrap();
|
let token = client.request_token(&http_client, "code").unwrap();
|
||||||
let token = client.refresh_token(token, None).unwrap();
|
let token = client.refresh_token(&http_client, token, None).unwrap();
|
||||||
assert_eq!("cccccccc", token.access_token());
|
assert_eq!("cccccccc", token.access_token());
|
||||||
assert_eq!(Some("example"), token.scope());
|
assert_eq!(Some("example"), token.scope());
|
||||||
assert_eq!("dddddddd", token.lifetime().refresh_token());
|
assert_eq!("dddddddd", token.lifetime().refresh_token());
|
||||||
|
@ -103,9 +103,9 @@ fn refresh_token_bearer_full() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn refresh_token_bearer_partial() {
|
fn refresh_token_bearer_partial() {
|
||||||
let client = mock_client!(provider::BearerExpiring, connector::BearerExpiringPartial);
|
let (client, http_client) = mock_client!(provider::BearerExpiring, connector::BearerExpiringPartial);
|
||||||
let token = client.request_token("code").unwrap();
|
let token = client.request_token(&http_client, "code").unwrap();
|
||||||
let token = client.refresh_token(token, None).unwrap();
|
let token = client.refresh_token(&http_client, token, None).unwrap();
|
||||||
assert_eq!("cccccccc", token.access_token());
|
assert_eq!("cccccccc", token.access_token());
|
||||||
assert_eq!(Some("example"), token.scope());
|
assert_eq!(Some("example"), token.scope());
|
||||||
assert_eq!("bbbbbbbb", token.lifetime().refresh_token());
|
assert_eq!("bbbbbbbb", token.lifetime().refresh_token());
|
||||||
|
@ -116,22 +116,22 @@ fn refresh_token_bearer_partial() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn request_token_bearer_static_wrong_lifetime() {
|
fn request_token_bearer_static_wrong_lifetime() {
|
||||||
let client = mock_client!(provider::BearerStatic, connector::BearerExpiring);
|
let (client, http_client) = mock_client!(provider::BearerStatic, connector::BearerExpiring);
|
||||||
let err = client.request_token("code").unwrap_err();
|
let err = client.request_token(&http_client, "code").unwrap_err();
|
||||||
assert!(match err { ClientError::Parse(..) => true, _ => false });
|
assert!(match err { ClientError::Parse(..) => true, _ => false });
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn request_token_bearer_expiring_wrong_lifetime() {
|
fn request_token_bearer_expiring_wrong_lifetime() {
|
||||||
let client = mock_client!(provider::BearerExpiring, connector::BearerStatic);
|
let (client, http_client) = mock_client!(provider::BearerExpiring, connector::BearerStatic);
|
||||||
let err = client.request_token("code").unwrap_err();
|
let err = client.request_token(&http_client, "code").unwrap_err();
|
||||||
assert!(match err { ClientError::Parse(..) => true, _ => false });
|
assert!(match err { ClientError::Parse(..) => true, _ => false });
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn request_token_invalid_request() {
|
fn request_token_invalid_request() {
|
||||||
let client = mock_client!(provider::BearerStatic, connector::InvalidRequest);
|
let (client, http_client) = mock_client!(provider::BearerStatic, connector::InvalidRequest);
|
||||||
let err = client.request_token("code").unwrap_err();
|
let err = client.request_token(&http_client, "code").unwrap_err();
|
||||||
assert!(match err {
|
assert!(match err {
|
||||||
ClientError::OAuth2(err) => {
|
ClientError::OAuth2(err) => {
|
||||||
assert_eq!(OAuth2ErrorCode::InvalidRequest, err.code);
|
assert_eq!(OAuth2ErrorCode::InvalidRequest, err.code);
|
||||||
|
@ -145,9 +145,9 @@ fn request_token_invalid_request() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn refresh_token_invalid_request() {
|
fn refresh_token_invalid_request() {
|
||||||
let client = mock_client!(provider::BearerExpiring, connector::RefreshInvalidRequest);
|
let (client, http_client) = mock_client!(provider::BearerExpiring, connector::RefreshInvalidRequest);
|
||||||
let token = client.request_token("code").unwrap();
|
let token = client.request_token(&http_client, "code").unwrap();
|
||||||
let err = client.refresh_token(token, None).unwrap_err();
|
let err = client.refresh_token(&http_client, token, None).unwrap_err();
|
||||||
assert!(match err {
|
assert!(match err {
|
||||||
ClientError::OAuth2(err) => {
|
ClientError::OAuth2(err) => {
|
||||||
assert_eq!(OAuth2ErrorCode::InvalidRequest, err.code);
|
assert_eq!(OAuth2ErrorCode::InvalidRequest, err.code);
|
||||||
|
|
Loading…
Reference in New Issue