diff --git a/CHANGELOG.md b/CHANGELOG.md index cad3fd26f..57fdcd9cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] + ### Changed +- *BREAKING* - Remove dependency and re-export of `hyper-old-types` which is no longer maintained. + - This changes the inner types of the `AuthData` enum and thus the various methods on it to avoid re-exports. + - The `hyper_old_types` are no longer re-exported, and the enums just wrap `String`s. + - `AuthData::bearer()` now returns `Option`, returning `None` if the provided token is not valid base64. + - The `auth::make_headers` function now returns an `Option`. + ### Added ### Fixed diff --git a/Cargo.toml b/Cargo.toml index 4cc9e8622..58fe64149 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,8 +34,8 @@ serde_json = { version = "1.0", optional = true } hyper = "0.14" slog = { version = "2", features = [ "max_level_trace", "release_max_level_debug"] } uuid = { version = "0.8", features = ["serde", "v4"] } -hyper-old-types = "0.11.0" futures = "0.3" +headers = "0.3" # Conversion frunk = { version = "0.3.0", optional = true } diff --git a/src/auth.rs b/src/auth.rs index d85ba397a..ef7d4f2e0 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -2,13 +2,11 @@ use crate::context::Push; use futures::future::FutureExt; +use headers::authorization::{Basic, Bearer, Credentials}; +use headers::Authorization as Header; use hyper::header::AUTHORIZATION; use hyper::service::Service; use hyper::{HeaderMap, Request}; -pub use hyper_old_types::header::Authorization as Header; -use hyper_old_types::header::Header as HeaderTrait; -pub use hyper_old_types::header::{Basic, Bearer}; -use hyper_old_types::header::{Raw, Scheme}; use std::collections::BTreeSet; use std::marker::PhantomData; use std::string::ToString; @@ -53,10 +51,10 @@ pub struct Authorization { /// request authentication, and for authenticating outgoing client requests. #[derive(Clone, Debug, PartialEq)] pub enum AuthData { - /// HTTP Basic auth. - Basic(Basic), - /// HTTP Bearer auth, used for OAuth2. - Bearer(Bearer), + /// HTTP Basic auth - username and password. + Basic(String, String), + /// HTTP Bearer auth, used for OAuth2 - token. + Bearer(String), /// Header-based or query parameter-based API key auth. ApiKey(String), } @@ -64,17 +62,14 @@ pub enum AuthData { impl AuthData { /// Set Basic authentication pub fn basic(username: &str, password: &str) -> Self { - AuthData::Basic(Basic { - username: username.to_owned(), - password: Some(password.to_owned()), - }) + AuthData::Basic(username.to_owned(), password.to_owned()) } - /// Set Bearer token authentication - pub fn bearer(token: &str) -> Self { - AuthData::Bearer(Bearer { - token: token.to_owned(), - }) + /// Set Bearer token authentication. Returns None if the token was invalid. + pub fn bearer(token: &str) -> Option { + Some(AuthData::Bearer( + Header::bearer(token).ok()?.token().to_owned(), + )) } /// Set ApiKey authentication @@ -211,16 +206,19 @@ where } /// Retrieve an authorization scheme data from a set of headers -pub fn from_headers(headers: &HeaderMap) -> Option -where - S: std::str::FromStr + 'static, - S::Err: 'static, -{ +pub fn from_headers(headers: &HeaderMap) -> Option { headers .get(AUTHORIZATION) - .and_then(|v| v.to_str().ok()) - .and_then(|s| Header::::parse_header(&Raw::from(s)).ok()) - .map(|a| a.0) + .and_then(|s| match Basic::decode(s) { + Some(basic) => Some(AuthData::Basic( + basic.username().to_string(), + basic.password().to_string(), + )), + None => match Bearer::decode(s) { + Some(bearer) => Some(AuthData::Bearer(bearer.token().to_string())), + None => None, + }, + }) } /// Retrieve an API key from a header diff --git a/src/header.rs b/src/header.rs index 91e83970c..89a6ceb28 100644 --- a/src/header.rs +++ b/src/header.rs @@ -15,8 +15,7 @@ impl XSpanIdString { let x_span_id = req.headers().get(X_SPAN_ID); x_span_id - .map(|x| x.to_str().ok()) - .flatten() + .and_then(|x| x.to_str().ok()) .map(|x| XSpanIdString(x.to_string())) .unwrap_or_default() }