Rename Expiring to Refresh

This commit is contained in:
Curtis McEnroe 2016-02-22 23:08:07 -05:00
parent 93ac962b32
commit 9ae0feccc8
7 changed files with 80 additions and 80 deletions

View File

@ -8,7 +8,7 @@ use url::{form_urlencoded, Url};
use error::OAuth2Error;
use provider::Provider;
use token::{Token, Lifetime, Expiring};
use token::{Token, Lifetime, Refresh};
use self::response::FromResponse;
pub mod response;
@ -157,7 +157,7 @@ impl<P: Provider> Client<P> {
}
}
impl<P: Provider> Client<P> where P::Token: Token<Expiring> {
impl<P: Provider> Client<P> where P::Token: Token<Refresh> {
/// Refreshes an access token.
///
/// See [RFC 6749, section 6](http://tools.ietf.org/html/rfc6749#section-6).

View File

@ -1,6 +1,6 @@
//! Providers.
use token::{Token, Lifetime, Bearer, Static, Expiring};
use token::{Token, Lifetime, Bearer, Static, Refresh};
/// OAuth 2.0 providers.
pub trait Provider {
@ -40,7 +40,7 @@ pub trait Provider {
/// See [Using OAuth 2.0 to Access Google
/// APIs](https://developers.google.com/identity/protocols/OAuth2).
pub mod google {
use token::{Bearer, Expiring};
use token::{Bearer, Refresh};
use super::Provider;
/// Google OAuth 2.0 provider for installed applications.
@ -50,8 +50,8 @@ pub mod google {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Installed;
impl Provider for Installed {
type Lifetime = Expiring;
type Token = Bearer<Expiring>;
type Lifetime = Refresh;
type Token = Bearer<Refresh>;
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" }
}
@ -75,8 +75,8 @@ impl Provider for GitHub {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Imgur;
impl Provider for Imgur {
type Lifetime = Expiring;
type Token = Bearer<Expiring>;
type Lifetime = Refresh;
type Token = Bearer<Refresh>;
fn auth_uri() -> &'static str { "https://api.imgur.com/oauth2/authorize" }
fn token_uri() -> &'static str { "https://api.imgur.com/oauth2/token" }
}

View File

@ -160,7 +160,7 @@ mod tests {
use serde_json;
use client::response::{FromResponse, ParseError};
use token::{Static, Expiring};
use token::{Static, Refresh};
use super::Bearer;
#[test]
@ -214,7 +214,7 @@ mod tests {
}
#[test]
fn from_response_expiring() {
fn from_response_refresh() {
let json = Json::from_str(r#"
{
"token_type":"Bearer",
@ -223,17 +223,17 @@ mod tests {
"refresh_token":"bbbbbbbb"
}
"#).unwrap();
let bearer = Bearer::<Expiring>::from_response(&json).unwrap();
let bearer = Bearer::<Refresh>::from_response(&json).unwrap();
assert_eq!("aaaaaaaa", bearer.access_token);
assert_eq!(None, bearer.scope);
let expiring = bearer.lifetime;
assert_eq!("bbbbbbbb", expiring.refresh_token());
assert!(expiring.expires() > &UTC::now());
assert!(expiring.expires() <= &(UTC::now() + Duration::seconds(3600)));
let refresh = bearer.lifetime;
assert_eq!("bbbbbbbb", refresh.refresh_token());
assert!(refresh.expires() > &UTC::now());
assert!(refresh.expires() <= &(UTC::now() + Duration::seconds(3600)));
}
#[test]
fn from_response_inherit_expiring() {
fn from_response_inherit_refresh() {
let json = Json::from_str(r#"
{
"token_type":"Bearer",
@ -242,7 +242,7 @@ mod tests {
"refresh_token":"bbbbbbbb"
}
"#).unwrap();
let prev = Bearer::<Expiring>::from_response(&json).unwrap();
let prev = Bearer::<Refresh>::from_response(&json).unwrap();
let json = Json::from_str(r#"
{
@ -251,13 +251,13 @@ mod tests {
"expires_in":3600
}
"#).unwrap();
let bearer = Bearer::<Expiring>::from_response_inherit(&json, &prev).unwrap();
let bearer = Bearer::<Refresh>::from_response_inherit(&json, &prev).unwrap();
assert_eq!("cccccccc", bearer.access_token);
assert_eq!(None, bearer.scope);
let expiring = bearer.lifetime;
assert_eq!("bbbbbbbb", expiring.refresh_token());
assert!(expiring.expires() > &UTC::now());
assert!(expiring.expires() <= &(UTC::now() + Duration::seconds(3600)));
let refresh = bearer.lifetime;
assert_eq!("bbbbbbbb", refresh.refresh_token());
assert!(refresh.expires() > &UTC::now());
assert!(refresh.expires() <= &(UTC::now() + Duration::seconds(3600)));
}
#[test]

View File

@ -35,5 +35,5 @@ mod bearer;
pub use self::statik::Static;
mod statik;
pub use self::expiring::Expiring;
mod expiring;
pub use self::refresh::Refresh;
mod refresh;

View File

@ -7,14 +7,14 @@ use serde::{ser, de};
use super::Lifetime;
use client::response::{FromResponse, ParseError, JsonHelper};
/// An expiring token.
/// An expiring token which can be refreshed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Expiring {
pub struct Refresh {
refresh_token: String,
expires: DateTime<UTC>,
}
impl Expiring {
impl Refresh {
/// Returns the refresh token.
///
/// See [RFC 6749, section 1.5](http://tools.ietf.org/html/rfc6749#section-1.5).
@ -24,18 +24,18 @@ impl Expiring {
pub fn expires(&self) -> &DateTime<UTC> { &self.expires }
}
impl Lifetime for Expiring {
impl Lifetime for Refresh {
fn expired(&self) -> bool { self.expires < UTC::now() }
}
impl FromResponse for Expiring {
impl FromResponse for Refresh {
fn from_response(json: &Json) -> Result<Self, ParseError> {
let obj = try!(JsonHelper(json).as_object());
let refresh_token = try!(obj.get_string("refresh_token"));
let expires_in = try!(obj.get_i64("expires_in"));
Ok(Expiring {
Ok(Refresh {
refresh_token: refresh_token.into(),
expires: UTC::now() + Duration::seconds(expires_in),
})
@ -50,7 +50,7 @@ impl FromResponse for Expiring {
};
let expires_in = try!(obj.get_i64("expires_in"));
Ok(Expiring {
Ok(Refresh {
refresh_token: refresh_token.into(),
expires: UTC::now() + Duration::seconds(expires_in),
})
@ -63,43 +63,43 @@ struct Serializable {
expires: i64,
}
impl<'a> From<&'a Expiring> for Serializable {
fn from(expiring: &Expiring) -> Self {
impl<'a> From<&'a Refresh> for Serializable {
fn from(refresh: &Refresh) -> Self {
Serializable {
refresh_token: expiring.refresh_token.clone(),
expires: expiring.expires.timestamp(),
refresh_token: refresh.refresh_token.clone(),
expires: refresh.expires.timestamp(),
}
}
}
impl Into<Expiring> for Serializable {
fn into(self) -> Expiring {
Expiring {
impl Into<Refresh> for Serializable {
fn into(self) -> Refresh {
Refresh {
refresh_token: self.refresh_token,
expires: UTC.timestamp(self.expires, 0),
}
}
}
impl Encodable for Expiring {
impl Encodable for Refresh {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
Serializable::from(self).encode(s)
}
}
impl Decodable for Expiring {
impl Decodable for Refresh {
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
Serializable::decode(d).map(Into::into)
}
}
impl Serialize for Expiring {
impl Serialize for Refresh {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
serializer.serialize_struct("Expiring", SerVisitor(self, 0))
serializer.serialize_struct("Refresh", SerVisitor(self, 0))
}
}
struct SerVisitor<'a>(&'a Expiring, u8);
struct SerVisitor<'a>(&'a Refresh, u8);
impl<'a> ser::MapVisitor for SerVisitor<'a> {
fn visit<S: Serializer>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error> {
self.1 += 1;
@ -113,18 +113,18 @@ impl<'a> ser::MapVisitor for SerVisitor<'a> {
fn len(&self) -> Option<usize> { Some(2) }
}
impl Deserialize for Expiring {
impl Deserialize for Refresh {
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
static FIELDS: &'static [&'static str] = &["refresh_token", "expires"];
deserializer.deserialize_struct("Expiring", FIELDS, DeVisitor)
deserializer.deserialize_struct("Refresh", FIELDS, DeVisitor)
}
}
struct DeVisitor;
impl de::Visitor for DeVisitor {
type Value = Expiring;
type Value = Refresh;
fn visit_map<V: de::MapVisitor>(&mut self, mut visitor: V) -> Result<Expiring, V::Error> {
fn visit_map<V: de::MapVisitor>(&mut self, mut visitor: V) -> Result<Refresh, V::Error> {
let mut refresh_token = None;
let mut expires = None;
@ -147,7 +147,7 @@ impl de::Visitor for DeVisitor {
try!(visitor.end());
Ok(Expiring {
Ok(Refresh {
refresh_token: refresh_token,
expires: expires,
})
@ -185,44 +185,44 @@ mod tests {
use serde_json;
use client::response::FromResponse;
use super::Expiring;
use super::Refresh;
#[test]
fn from_response() {
let json = Json::from_str(r#"{"refresh_token":"aaaaaaaa","expires_in":3600}"#).unwrap();
let expiring = Expiring::from_response(&json).unwrap();
assert_eq!("aaaaaaaa", expiring.refresh_token);
assert!(expiring.expires > UTC::now());
assert!(expiring.expires <= UTC::now() + Duration::seconds(3600));
let refresh = Refresh::from_response(&json).unwrap();
assert_eq!("aaaaaaaa", refresh.refresh_token);
assert!(refresh.expires > UTC::now());
assert!(refresh.expires <= UTC::now() + Duration::seconds(3600));
}
#[test]
fn from_response_inherit() {
let json = Json::from_str(r#"{"expires_in":3600}"#).unwrap();
let prev = Expiring {
let prev = Refresh {
refresh_token: String::from("aaaaaaaa"),
expires: UTC::now(),
};
let expiring = Expiring::from_response_inherit(&json, &prev).unwrap();
assert_eq!("aaaaaaaa", expiring.refresh_token);
assert!(expiring.expires > UTC::now());
assert!(expiring.expires <= UTC::now() + Duration::seconds(3600));
let refresh = Refresh::from_response_inherit(&json, &prev).unwrap();
assert_eq!("aaaaaaaa", refresh.refresh_token);
assert!(refresh.expires > UTC::now());
assert!(refresh.expires <= UTC::now() + Duration::seconds(3600));
}
#[test]
fn encode_decode() {
let expiring = Expiring {
let refresh = Refresh {
refresh_token: String::from("foo"),
expires: UTC::now().with_nanosecond(0).unwrap(),
};
let json = json::encode(&expiring).unwrap();
let json = json::encode(&refresh).unwrap();
let decoded = json::decode(&json).unwrap();
assert_eq!(expiring, decoded);
assert_eq!(refresh, decoded);
}
#[test]
fn serialize_deserialize() {
let original = Expiring {
let original = Refresh {
refresh_token: String::from("foo"),
expires: UTC::now().with_nanosecond(0).unwrap(),
};

View File

@ -9,7 +9,7 @@ use inth_oauth2::{Client, ClientError, Token, Lifetime};
use inth_oauth2::error::OAuth2ErrorCode;
mod provider {
use inth_oauth2::token::{Bearer, Static, Expiring};
use inth_oauth2::token::{Bearer, Static, Refresh};
use inth_oauth2::provider::Provider;
pub struct BearerStatic;
@ -20,10 +20,10 @@ mod provider {
fn token_uri() -> &'static str { "https://example.com/oauth/token" }
}
pub struct BearerExpiring;
impl Provider for BearerExpiring {
type Lifetime = Expiring;
type Token = Bearer<Expiring>;
pub struct BearerRefresh;
impl Provider for BearerRefresh {
type Lifetime = Refresh;
type Token = Bearer<Refresh>;
fn auth_uri() -> &'static str { "https://example.com/oauth/auth" }
fn token_uri() -> &'static str { "https://example.com/oauth/token" }
}
@ -36,13 +36,13 @@ mod connector {
include_str!("response/request_token_bearer_static.http")
});
mock_connector_in_order!(BearerExpiring {
include_str!("response/request_token_bearer_expiring.http")
mock_connector_in_order!(BearerRefresh {
include_str!("response/request_token_bearer_refresh.http")
include_str!("response/refresh_token_bearer_full.http")
});
mock_connector_in_order!(BearerExpiringPartial {
include_str!("response/request_token_bearer_expiring.http")
mock_connector_in_order!(BearerRefreshPartial {
include_str!("response/request_token_bearer_refresh.http")
include_str!("response/refresh_token_bearer_partial.http")
});
@ -51,7 +51,7 @@ mod connector {
});
mock_connector_in_order!(RefreshInvalidRequest {
include_str!("response/request_token_bearer_expiring.http")
include_str!("response/request_token_bearer_refresh.http")
include_str!("response/invalid_request.http")
});
}
@ -77,8 +77,8 @@ fn request_token_bearer_static_success() {
}
#[test]
fn request_token_bearer_expiring_success() {
let (client, http_client) = mock_client!(provider::BearerExpiring, connector::BearerExpiring);
fn request_token_bearer_refresh_success() {
let (client, http_client) = mock_client!(provider::BearerRefresh, connector::BearerRefresh);
let token = client.request_token(&http_client, "code").unwrap();
assert_eq!("aaaaaaaa", token.access_token());
assert_eq!(Some("example"), token.scope());
@ -90,7 +90,7 @@ fn request_token_bearer_expiring_success() {
#[test]
fn refresh_token_bearer_full() {
let (client, http_client) = mock_client!(provider::BearerExpiring, connector::BearerExpiring);
let (client, http_client) = mock_client!(provider::BearerRefresh, connector::BearerRefresh);
let token = client.request_token(&http_client, "code").unwrap();
let token = client.refresh_token(&http_client, token, None).unwrap();
assert_eq!("cccccccc", token.access_token());
@ -103,7 +103,7 @@ fn refresh_token_bearer_full() {
#[test]
fn refresh_token_bearer_partial() {
let (client, http_client) = mock_client!(provider::BearerExpiring, connector::BearerExpiringPartial);
let (client, http_client) = mock_client!(provider::BearerRefresh, connector::BearerRefreshPartial);
let token = client.request_token(&http_client, "code").unwrap();
let token = client.refresh_token(&http_client, token, None).unwrap();
assert_eq!("cccccccc", token.access_token());
@ -116,14 +116,14 @@ fn refresh_token_bearer_partial() {
#[test]
fn request_token_bearer_static_wrong_lifetime() {
let (client, http_client) = mock_client!(provider::BearerStatic, connector::BearerExpiring);
let (client, http_client) = mock_client!(provider::BearerStatic, connector::BearerRefresh);
let err = client.request_token(&http_client, "code").unwrap_err();
assert!(match err { ClientError::Parse(..) => true, _ => false });
}
#[test]
fn request_token_bearer_expiring_wrong_lifetime() {
let (client, http_client) = mock_client!(provider::BearerExpiring, connector::BearerStatic);
fn request_token_bearer_refresh_wrong_lifetime() {
let (client, http_client) = mock_client!(provider::BearerRefresh, connector::BearerStatic);
let err = client.request_token(&http_client, "code").unwrap_err();
assert!(match err { ClientError::Parse(..) => true, _ => false });
}
@ -145,7 +145,7 @@ fn request_token_invalid_request() {
#[test]
fn refresh_token_invalid_request() {
let (client, http_client) = mock_client!(provider::BearerExpiring, connector::RefreshInvalidRequest);
let (client, http_client) = mock_client!(provider::BearerRefresh, connector::RefreshInvalidRequest);
let token = client.request_token(&http_client, "code").unwrap();
let err = client.refresh_token(&http_client, token, None).unwrap_err();
assert!(match err {