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

Implement require_auth with validate_request #290

Merged
merged 5 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion tower-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Removed

- None.
- Removed `RequireAuthorization` in favor of `ValidateRequest`

## Fixed

Expand Down
2 changes: 1 addition & 1 deletion tower-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ full = [
]

add-extension = []
auth = ["base64"]
auth = ["base64", "validate-request"]
catch-panic = ["tracing", "futures-util/std"]
cors = []
follow-redirect = ["iri-string", "tower/util"]
Expand Down
12 changes: 7 additions & 5 deletions tower-http/src/auth/add_authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! # Example
//!
//! ```
//! use tower_http::validate_request::{ValidateRequestHeader, ValidateRequestHeaderLayer};
//! use tower_http::auth::AddAuthorizationLayer;
//! use hyper::{Request, Response, Body, Error};
//! use http::{StatusCode, header::AUTHORIZATION};
Expand All @@ -15,7 +16,7 @@
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let service_that_requires_auth = tower_http::auth::RequireAuthorization::basic(
//! # let service_that_requires_auth = ValidateRequestHeader::basic(
//! # tower::service_fn(handle),
//! # "username",
//! # "password",
Expand Down Expand Up @@ -183,9 +184,10 @@ where

#[cfg(test)]
mod tests {
use crate::validate_request::ValidateRequestHeaderLayer;

#[allow(unused_imports)]
use super::*;
use crate::auth::RequireAuthorizationLayer;
use http::{Response, StatusCode};
use hyper::Body;
use tower::{BoxError, Service, ServiceBuilder, ServiceExt};
Expand All @@ -194,7 +196,7 @@ mod tests {
async fn basic() {
// service that requires auth for all requests
let svc = ServiceBuilder::new()
.layer(RequireAuthorizationLayer::basic("foo", "bar"))
.layer(ValidateRequestHeaderLayer::basic("foo", "bar"))
.service_fn(echo);

// make a client that adds auth
Expand All @@ -215,7 +217,7 @@ mod tests {
async fn token() {
// service that requires auth for all requests
let svc = ServiceBuilder::new()
.layer(RequireAuthorizationLayer::bearer("foo"))
.layer(ValidateRequestHeaderLayer::bearer("foo"))
.service_fn(echo);

// make a client that adds auth
Expand All @@ -235,7 +237,7 @@ mod tests {
#[tokio::test]
async fn making_header_sensitive() {
let svc = ServiceBuilder::new()
.layer(RequireAuthorizationLayer::bearer("foo"))
.layer(ValidateRequestHeaderLayer::bearer("foo"))
.service_fn(|request: Request<Body>| async move {
let auth = request.headers().get(http::header::AUTHORIZATION).unwrap();
assert!(auth.is_sensitive());
Expand Down
1 change: 0 additions & 1 deletion tower-http/src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ pub use self::{
async_require_authorization::{
AsyncAuthorizeRequest, AsyncRequireAuthorization, AsyncRequireAuthorizationLayer,
},
require_authorization::{AuthorizeRequest, RequireAuthorization, RequireAuthorizationLayer},
};
Loading