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 rustc_serialize::json;
use url::{Url, form_urlencoded}; use url::{Url, form_urlencoded};
use super::{TokenPair, AccessToken, RefreshToken}; use super::{TokenPair, AccessTokenType, AccessToken, RefreshToken};
use super::error::{Error, Result, OAuth2Error, OAuth2ErrorCode}; use super::error::{Error, Result, OAuth2Error, OAuth2ErrorCode};
/// OAuth 2.0 client. /// OAuth 2.0 client.
@ -38,7 +38,10 @@ impl Into<TokenPair> for TokenResponse {
TokenPair { TokenPair {
access: AccessToken { access: AccessToken {
token: self.access_token, 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)), expires: self.expires_in.map(|s| UTC::now() + Duration::seconds(s)),
scope: self.scope, scope: self.scope,
}, },

View File

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

View File

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