From 527ab2deed63d4ca7e6b152c10e9fc59dc6aa68b Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Mon, 21 Dec 2015 22:07:52 -0500 Subject: [PATCH] Add bearer token type --- src/token/bearer.rs | 24 ++++++++++++++++++++++++ src/token/mod.rs | 3 +++ 2 files changed, 27 insertions(+) create mode 100644 src/token/bearer.rs diff --git a/src/token/bearer.rs b/src/token/bearer.rs new file mode 100644 index 0000000..3394d99 --- /dev/null +++ b/src/token/bearer.rs @@ -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 { + access_token: String, + scope: Option, + lifetime: L, +} + +impl Token for Bearer { + 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> for &'a Bearer { + fn into(self) -> header::Authorization { + header::Authorization(header::Bearer { token: self.access_token.clone() }) + } +} diff --git a/src/token/mod.rs b/src/token/mod.rs index 39032ea..468ca70 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -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;