diff --git a/src/client.rs b/src/client.rs index 8f9dc61..5cabcc9 100644 --- a/src/client.rs +++ b/src/client.rs @@ -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 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, }, diff --git a/src/lib.rs b/src/lib.rs index da4eecd..5098fab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}; diff --git a/src/token.rs b/src/token.rs index 58a8dcf..e9296f6 100644 --- a/src/token.rs +++ b/src/token.rs @@ -12,6 +12,20 @@ pub struct TokenPair { pub refresh: Option, } +/// 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>, @@ -59,7 +71,7 @@ impl Deref for TokenPair { #[derive(RustcEncodable, RustcDecodable)] struct SerializableAccessToken { token: String, - token_type: String, + token_type: AccessTokenType, expires: Option, scope: Option, }