From edf6ac2074d11694ded275807a66df3a8a8e33a6 Mon Sep 17 00:00:00 2001 From: Joseph Deray Date: Wed, 17 Jun 2015 00:34:26 -0400 Subject: [PATCH] feat(headers): add bearer token support this allows servers/clients using bearer tokens to work out of the box without having to implement their own bearer scheme. while this would be pretty easy seems like a more general thing that is useful for a lib like this --- src/header/common/authorization.rs | 56 +++++++++++++++++++++++++++++- src/header/common/mod.rs | 2 +- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/header/common/authorization.rs b/src/header/common/authorization.rs index 495ff1e182..3775791694 100644 --- a/src/header/common/authorization.rs +++ b/src/header/common/authorization.rs @@ -20,6 +20,7 @@ use header::{Header, HeaderFormat}; /// /// # Example values /// * `Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==` +/// * `Bearer fpKL54jvWmEGVoRdCNjG` /// /// # Examples /// ``` @@ -41,6 +42,18 @@ use header::{Header, HeaderFormat}; /// ) /// ); /// ``` +/// ``` +/// use hyper::header::{Headers, Authorization, Bearer}; +/// +/// let mut headers = Headers::new(); +/// headers.set( +/// Authorization( +/// Bearer { +/// token: "QWxhZGRpbjpvcGVuIHNlc2FtZQ".to_owned() +/// } +/// ) +/// ); +/// ``` #[derive(Clone, PartialEq, Debug)] pub struct Authorization(pub S); @@ -181,9 +194,33 @@ impl FromStr for Basic { } } +#[derive(Clone, PartialEq, Debug)] +///Token holder for Bearer Authentication, most often seen with oauth +pub struct Bearer { + ///Actual bearer token as a string + pub token: String +} + +impl Scheme for Bearer { + fn scheme() -> Option<&'static str> { + Some("Bearer") + } + + fn fmt_scheme(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.token) + } +} + +impl FromStr for Bearer { + type Err = ::Error; + fn from_str(s: &str) -> ::Result { + Ok(Bearer { token: s.to_owned()}) + } +} + #[cfg(test)] mod tests { - use super::{Authorization, Basic}; + use super::{Authorization, Basic, Bearer}; use super::super::super::{Headers, Header}; #[test] @@ -233,7 +270,24 @@ mod tests { assert_eq!(auth.0.password, Some("".to_owned())); } + #[test] + fn test_bearer_auth() { + let mut headers = Headers::new(); + headers.set(Authorization( + Bearer { token: "fpKL54jvWmEGVoRdCNjG".to_owned() })); + assert_eq!( + headers.to_string(), + "Authorization: Bearer fpKL54jvWmEGVoRdCNjG\r\n".to_owned()); + } + + #[test] + fn test_bearer_auth_parse() { + let auth: Authorization = Header::parse_header( + &[b"Bearer fpKL54jvWmEGVoRdCNjG".to_vec()]).unwrap(); + assert_eq!(auth.0.token, "fpKL54jvWmEGVoRdCNjG"); + } } bench_header!(raw, Authorization, { vec![b"foo bar baz".to_vec()] }); bench_header!(basic, Authorization, { vec![b"Basic QWxhZGRpbjpuIHNlc2FtZQ==".to_vec()] }); +bench_header!(bearer, Authorization, { vec![b"Bearer fpKL54jvWmEGVoRdCNjG".to_vec()] }); diff --git a/src/header/common/mod.rs b/src/header/common/mod.rs index feb27e80bc..c3aa389e4e 100644 --- a/src/header/common/mod.rs +++ b/src/header/common/mod.rs @@ -18,7 +18,7 @@ pub use self::accept_encoding::AcceptEncoding; pub use self::accept_language::AcceptLanguage; pub use self::accept_ranges::{AcceptRanges, RangeUnit}; pub use self::allow::Allow; -pub use self::authorization::{Authorization, Scheme, Basic}; +pub use self::authorization::{Authorization, Scheme, Basic, Bearer}; pub use self::cache_control::{CacheControl, CacheDirective}; pub use self::connection::{Connection, ConnectionOption}; pub use self::content_length::ContentLength;