inth-oauth2/src/token.rs

111 lines
3.0 KiB
Rust
Raw Normal View History

use std::ops::Deref;
use chrono::{DateTime, UTC, TimeZone};
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
2015-11-28 18:09:43 +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-12-01 04:18:19 +00:00
/// OAuth 2.0 access token type.
///
/// See [RFC6749 section 7.1](http://tools.ietf.org/html/rfc6749#section-7.1).
#[derive(Debug, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable)]
pub enum AccessTokenType {
/// The bearer token type.
///
/// See [RFC6750](http://tools.ietf.org/html/rfc6750).
Bearer,
/// An unrecognized token type.
Unrecognized(String),
}
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)]
pub struct AccessToken {
2015-11-29 04:33:58 +00:00
/// The access token issued by the authorization server.
pub token: String,
2015-11-29 04:33:58 +00:00
/// The type of the token issued.
2015-12-01 04:18:19 +00:00
pub token_type: AccessTokenType,
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>,
}
/// 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())
}
}
impl Deref for TokenPair {
type Target = AccessToken;
2015-12-01 03:35:09 +00:00
fn deref(&self) -> &AccessToken {
&self.access
}
}
#[derive(RustcEncodable, RustcDecodable)]
struct SerializableAccessToken {
token: String,
2015-12-01 04:18:19 +00:00
token_type: AccessTokenType,
expires: Option<i64>,
scope: Option<String>,
}
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(),
}
}
fn into_access_token(self) -> AccessToken {
AccessToken {
token: self.token,
token_type: self.token_type,
expires: self.expires.map(|t| UTC.timestamp(t, 0)),
scope: self.scope,
}
}
}
impl Encodable for AccessToken {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
SerializableAccessToken::from_access_token(self).encode(s)
}
}
impl Decodable for AccessToken {
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
SerializableAccessToken::decode(d)
.map(SerializableAccessToken::into_access_token)
}
}