2015-12-01 02:55:07 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
2015-11-29 20:36:12 +00:00
|
|
|
use chrono::{DateTime, UTC, TimeZone};
|
|
|
|
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
|
2015-11-28 18:09:43 +00:00
|
|
|
|
2015-12-01 02:55:07 +00:00
|
|
|
/// OAuth 2.0 access token and refresh token pair.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
|
|
|
pub struct TokenPair {
|
|
|
|
/// The access token.
|
|
|
|
pub access: AccessToken,
|
|
|
|
/// The refresh token.
|
|
|
|
pub refresh: Option<RefreshToken>,
|
|
|
|
}
|
|
|
|
|
2015-11-28 18:09:43 +00:00
|
|
|
/// OAuth 2.0 access token.
|
2015-11-29 04:33:58 +00:00
|
|
|
///
|
|
|
|
/// See [RFC6749 section 5](http://tools.ietf.org/html/rfc6749#section-5).
|
2015-11-29 20:54:21 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2015-12-01 02:55:07 +00:00
|
|
|
pub struct AccessToken {
|
2015-11-29 04:33:58 +00:00
|
|
|
/// The access token issued by the authorization server.
|
2015-12-01 02:55:07 +00:00
|
|
|
pub token: String,
|
2015-11-29 04:33:58 +00:00
|
|
|
|
|
|
|
/// The type of the token issued.
|
|
|
|
///
|
|
|
|
/// See [RFC6749 section 7.1](http://tools.ietf.org/html/rfc6749#section-7.1).
|
2015-11-28 18:09:43 +00:00
|
|
|
pub token_type: String,
|
2015-11-29 04:33:58 +00:00
|
|
|
|
|
|
|
/// The expiry time of the access token.
|
2015-11-28 18:09:43 +00:00
|
|
|
pub expires: Option<DateTime<UTC>>,
|
2015-11-29 04:33:58 +00:00
|
|
|
|
|
|
|
/// The scope of the access token.
|
2015-11-28 18:09:43 +00:00
|
|
|
pub scope: Option<String>,
|
|
|
|
}
|
|
|
|
|
2015-12-01 02:55:07 +00:00
|
|
|
/// OAuth 2.0 refresh token.
|
|
|
|
///
|
|
|
|
/// See [RFC6749 section 1.5](http://tools.ietf.org/html/rfc6749#section-1.5).
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
|
|
|
pub struct RefreshToken {
|
|
|
|
/// The refresh token issued by the authorization server.
|
|
|
|
pub token: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AccessToken {
|
2015-11-28 18:09:43 +00:00
|
|
|
/// Returns true if token is expired.
|
|
|
|
pub fn expired(&self) -> bool {
|
|
|
|
self.expires.map_or(false, |dt| dt < UTC::now())
|
|
|
|
}
|
|
|
|
}
|
2015-11-29 20:36:12 +00:00
|
|
|
|
2015-12-01 02:55:07 +00:00
|
|
|
impl Deref for TokenPair {
|
|
|
|
type Target = AccessToken;
|
|
|
|
|
2015-12-01 03:35:09 +00:00
|
|
|
fn deref(&self) -> &AccessToken {
|
2015-12-01 02:55:07 +00:00
|
|
|
&self.access
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-29 20:36:12 +00:00
|
|
|
#[derive(RustcEncodable, RustcDecodable)]
|
2015-12-01 02:55:07 +00:00
|
|
|
struct SerializableAccessToken {
|
|
|
|
token: String,
|
2015-11-29 20:36:12 +00:00
|
|
|
token_type: String,
|
|
|
|
expires: Option<i64>,
|
|
|
|
scope: Option<String>,
|
|
|
|
}
|
|
|
|
|
2015-12-01 02:55:07 +00:00
|
|
|
impl SerializableAccessToken {
|
|
|
|
fn from_access_token(access: &AccessToken) -> Self {
|
|
|
|
SerializableAccessToken {
|
|
|
|
token: access.token.clone(),
|
|
|
|
token_type: access.token_type.clone(),
|
|
|
|
expires: access.expires.as_ref().map(DateTime::timestamp),
|
|
|
|
scope: access.scope.clone(),
|
2015-11-29 20:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 02:55:07 +00:00
|
|
|
fn into_access_token(self) -> AccessToken {
|
|
|
|
AccessToken {
|
|
|
|
token: self.token,
|
2015-11-29 20:36:12 +00:00
|
|
|
token_type: self.token_type,
|
|
|
|
expires: self.expires.map(|t| UTC.timestamp(t, 0)),
|
|
|
|
scope: self.scope,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 02:55:07 +00:00
|
|
|
impl Encodable for AccessToken {
|
2015-11-29 20:36:12 +00:00
|
|
|
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
2015-12-01 02:55:07 +00:00
|
|
|
SerializableAccessToken::from_access_token(self).encode(s)
|
2015-11-29 20:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 02:55:07 +00:00
|
|
|
impl Decodable for AccessToken {
|
2015-11-29 20:36:12 +00:00
|
|
|
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
|
2015-12-01 02:55:07 +00:00
|
|
|
SerializableAccessToken::decode(d)
|
|
|
|
.map(SerializableAccessToken::into_access_token)
|
2015-11-29 20:36:12 +00:00
|
|
|
}
|
|
|
|
}
|