Skip to content

Commit

Permalink
feat(headers): add bearer token support
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Joseph Deray committed Jun 17, 2015
1 parent 41823ca commit edf6ac2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
56 changes: 55 additions & 1 deletion src/header/common/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use header::{Header, HeaderFormat};
///
/// # Example values
/// * `Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==`
/// * `Bearer fpKL54jvWmEGVoRdCNjG`
///
/// # Examples
/// ```
Expand All @@ -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<S: Scheme>(pub S);

Expand Down Expand Up @@ -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<Bearer> {
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]
Expand Down Expand Up @@ -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<Bearer> = Header::parse_header(
&[b"Bearer fpKL54jvWmEGVoRdCNjG".to_vec()]).unwrap();
assert_eq!(auth.0.token, "fpKL54jvWmEGVoRdCNjG");
}
}

bench_header!(raw, Authorization<String>, { vec![b"foo bar baz".to_vec()] });
bench_header!(basic, Authorization<Basic>, { vec![b"Basic QWxhZGRpbjpuIHNlc2FtZQ==".to_vec()] });
bench_header!(bearer, Authorization<Bearer>, { vec![b"Bearer fpKL54jvWmEGVoRdCNjG".to_vec()] });
2 changes: 1 addition & 1 deletion src/header/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit edf6ac2

Please sign in to comment.