Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make authorization schemes case and whitespace insensitive #153

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/common/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ impl<C: Credentials> ::Header for Authorization<C> {
.next()
.and_then(|val| {
let slice = val.as_bytes();
if slice.starts_with(C::SCHEME.as_bytes())
&& slice.len() > C::SCHEME.len()
if slice.len() > C::SCHEME.len()
&& slice[C::SCHEME.len()] == b' '
&& slice[..C::SCHEME.len()].eq_ignore_ascii_case(C::SCHEME.as_bytes())
{
C::decode(val).map(Authorization)
} else {
Expand Down Expand Up @@ -151,7 +151,7 @@ impl Credentials for Basic {

fn decode(value: &HeaderValue) -> Option<Self> {
debug_assert!(
value.as_bytes().starts_with(b"Basic "),
value.as_bytes()[..Self::SCHEME.len()].eq_ignore_ascii_case(Self::SCHEME.as_bytes()),
"HeaderValue to decode should start with \"Basic ..\", received = {:?}",
value,
);
Expand Down Expand Up @@ -186,7 +186,7 @@ pub struct Bearer(HeaderValueString);
impl Bearer {
/// View the token part as a `&str`.
pub fn token(&self) -> &str {
&self.0.as_str()["Bearer ".len()..]
self.0.as_str()["Bearer ".len()..].trim_start()
}
}

Expand All @@ -195,7 +195,7 @@ impl Credentials for Bearer {

fn decode(value: &HeaderValue) -> Option<Self> {
debug_assert!(
value.as_bytes().starts_with(b"Bearer "),
value.as_bytes()[..Self::SCHEME.len()].eq_ignore_ascii_case(Self::SCHEME.as_bytes()),
"HeaderValue to decode should start with \"Bearer ..\", received = {:?}",
value,
);
Expand Down Expand Up @@ -252,6 +252,22 @@ mod tests {
assert_eq!(auth.0.password(), "open sesame");
}

#[test]
fn basic_decode_case_insensitive() {
let auth: Authorization<Basic> =
test_decode(&["basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="]).unwrap();
assert_eq!(auth.0.username(), "Aladdin");
assert_eq!(auth.0.password(), "open sesame");
}

#[test]
fn basic_decode_extra_whitespaces() {
let auth: Authorization<Basic> =
test_decode(&["Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="]).unwrap();
assert_eq!(auth.0.username(), "Aladdin");
assert_eq!(auth.0.password(), "open sesame");
}

#[test]
fn basic_decode_no_password() {
let auth: Authorization<Basic> = test_decode(&["Basic QWxhZGRpbjo="]).unwrap();
Expand All @@ -273,6 +289,18 @@ mod tests {
let auth: Authorization<Bearer> = test_decode(&["Bearer fpKL54jvWmEGVoRdCNjG"]).unwrap();
assert_eq!(auth.0.token().as_bytes(), b"fpKL54jvWmEGVoRdCNjG");
}

#[test]
fn bearer_decode_case_insensitive() {
let auth: Authorization<Bearer> = test_decode(&["bearer fpKL54jvWmEGVoRdCNjG"]).unwrap();
assert_eq!(auth.0.token().as_bytes(), b"fpKL54jvWmEGVoRdCNjG");
}

#[test]
fn bearer_decode_extra_whitespaces() {
let auth: Authorization<Bearer> = test_decode(&["Bearer fpKL54jvWmEGVoRdCNjG"]).unwrap();
assert_eq!(auth.0.token().as_bytes(), b"fpKL54jvWmEGVoRdCNjG");
}
}

//bench_header!(raw, Authorization<String>, { vec![b"foo bar baz".to_vec()] });
Expand Down