Add bearer token type

This commit is contained in:
Curtis McEnroe 2015-12-21 22:07:52 -05:00
parent 40e428f5e9
commit 527ab2deed
2 changed files with 27 additions and 0 deletions

24
src/token/bearer.rs Normal file
View File

@ -0,0 +1,24 @@
use hyper::header;
use super::{Token, Lifetime};
/// The bearer token type.
///
/// See [RFC6750](http://tools.ietf.org/html/rfc6750).
pub struct Bearer<L: Lifetime> {
access_token: String,
scope: Option<String>,
lifetime: L,
}
impl<L: Lifetime> Token<L> for Bearer<L> {
fn access_token(&self) -> &str { &self.access_token }
fn scope(&self) -> Option<&str> { self.scope.as_ref().map(|s| &s[..]) }
fn lifetime(&self) -> &L { &self.lifetime }
}
impl<'a, L: Lifetime> Into<header::Authorization<header::Bearer>> for &'a Bearer<L> {
fn into(self) -> header::Authorization<header::Bearer> {
header::Authorization(header::Bearer { token: self.access_token.clone() })
}
}

View File

@ -26,3 +26,6 @@ pub trait Lifetime {
/// Returns true if the token is no longer valid.
fn expired(&self) -> bool;
}
pub use self::bearer::Bearer;
mod bearer;