Skip to content

bearer_token

Extracts the token from an Authorization: Bearer <token> header value.

Follows RFC 7235 and RFC 6750: the scheme name is case-insensitive, it is followed by one or more spaces, and the token is a non-empty b64token (letters, digits and - . _ ~ + /, optionally ending with = padding). Leading and trailing spaces or tabs around the whole value are ignored. Anything else, including another scheme or an empty token, gives None.

Only the syntax is checked: whether the token is valid is up to the caller.

use helpers4::http::bearer_token;

Cargo feature http (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features http

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.5", default-features = false, features = ["http"] }
pub fn bearer_token(header: &str) -> Option<&str>
ParameterTypeDescription
header&strThe value of an Authorization header.

Option<&str>

use helpers4::http::bearer_token;

assert_eq!(bearer_token("Bearer abc.def-123"), Some("abc.def-123"));
assert_eq!(bearer_token("bearer   abc"), Some("abc"));
assert_eq!(bearer_token("Basic dXNlcjpwYXNz"), None);
assert_eq!(bearer_token("Bearer "), None);

src/http/bearer_token.rs