Make token_type an enum

This commit is contained in:
Curtis McEnroe 2015-11-30 23:18:19 -05:00
parent 714089c683
commit 9b309f5697
3 changed files with 24 additions and 9 deletions

View File

@ -5,7 +5,7 @@ use hyper::{self, header, mime};
use rustc_serialize::json;
use url::{Url, form_urlencoded};
use super::{TokenPair, AccessToken, RefreshToken};
use super::{TokenPair, AccessTokenType, AccessToken, RefreshToken};
use super::error::{Error, Result, OAuth2Error, OAuth2ErrorCode};
/// OAuth 2.0 client.
@ -38,7 +38,10 @@ impl Into<TokenPair> for TokenResponse {
TokenPair {
access: AccessToken {
token: self.access_token,
token_type: self.token_type,
token_type: match &self.token_type[..] {
"Bearer" | "bearer" => AccessTokenType::Bearer,
_ => AccessTokenType::Unrecognized(self.token_type),
},
expires: self.expires_in.map(|s| UTC::now() + Duration::seconds(s)),
scope: self.scope,
},

View File

@ -98,14 +98,14 @@
//! # extern crate inth_oauth2;
//! # extern crate rustc_serialize;
//! # extern crate chrono;
//! use inth_oauth2::{TokenPair, AccessToken, RefreshToken};
//! use inth_oauth2::{TokenPair, AccessTokenType, AccessToken, RefreshToken};
//! use rustc_serialize::json;
//! # use chrono::{UTC, Timelike};
//! # fn main() {
//! # let token_pair = TokenPair {
//! # access: AccessToken {
//! # token: String::from("AAAAAAAA"),
//! # token_type: String::from("bearer"),
//! # token_type: AccessTokenType::Bearer,
//! # expires: Some(UTC::now().with_nanosecond(0).unwrap()),
//! # scope: None,
//! # },
@ -126,7 +126,7 @@ extern crate url;
pub use client::Client;
pub mod client;
pub use token::{TokenPair, AccessToken, RefreshToken};
pub use token::{TokenPair, AccessTokenType, AccessToken, RefreshToken};
pub mod token;
pub use error::{Error, Result};

View File

@ -12,6 +12,20 @@ pub struct TokenPair {
pub refresh: Option<RefreshToken>,
}
/// 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),
}
/// OAuth 2.0 access token.
///
/// See [RFC6749 section 5](http://tools.ietf.org/html/rfc6749#section-5).
@ -21,9 +35,7 @@ pub struct AccessToken {
pub token: String,
/// The type of the token issued.
///
/// See [RFC6749 section 7.1](http://tools.ietf.org/html/rfc6749#section-7.1).
pub token_type: String,
pub token_type: AccessTokenType,
/// The expiry time of the access token.
pub expires: Option<DateTime<UTC>>,
@ -59,7 +71,7 @@ impl Deref for TokenPair {
#[derive(RustcEncodable, RustcDecodable)]
struct SerializableAccessToken {
token: String,
token_type: String,
token_type: AccessTokenType,
expires: Option<i64>,
scope: Option<String>,
}