From 515a727ca3459d915df9a6438df68c5f8719d1c6 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 11 Jun 2024 03:52:45 +0100 Subject: [PATCH] docs(httpauth): rework example --- actix-session/Cargo.toml | 2 +- actix-web-httpauth/Cargo.toml | 2 + actix-web-httpauth/examples/middleware.rs | 47 +++++++++++++++++++---- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/actix-session/Cargo.toml b/actix-session/Cargo.toml index baf3ff3d6..e2fac3869 100644 --- a/actix-session/Cargo.toml +++ b/actix-session/Cargo.toml @@ -43,7 +43,7 @@ redis = { version = "0.25", default-features = false, features = ["tokio-comp", actix-session = { path = ".", features = ["cookie-session", "redis-session"] } actix-test = "0.1.0-beta.10" actix-web = { version = "4", default-features = false, features = ["cookies", "secure-cookies", "macros"] } -tracing-subscriber = {version = "0.3", features = ["env-filter"]} +tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing = "0.1.30" [[example]] diff --git a/actix-web-httpauth/Cargo.toml b/actix-web-httpauth/Cargo.toml index 4ed3255aa..8270db561 100644 --- a/actix-web-httpauth/Cargo.toml +++ b/actix-web-httpauth/Cargo.toml @@ -32,3 +32,5 @@ pin-project-lite = "0.2.7" actix-cors = "0.7" actix-service = "2" actix-web = { version = "4.1", default-features = false, features = ["macros"] } +tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing = "0.1.30" diff --git a/actix-web-httpauth/examples/middleware.rs b/actix-web-httpauth/examples/middleware.rs index d3671c1a7..21053475a 100644 --- a/actix-web-httpauth/examples/middleware.rs +++ b/actix-web-httpauth/examples/middleware.rs @@ -1,24 +1,57 @@ -use actix_web::{dev::ServiceRequest, middleware, web, App, Error, HttpServer}; -use actix_web_httpauth::{extractors::basic::BasicAuth, middleware::HttpAuthentication}; +use actix_web::{ + dev::ServiceRequest, error, get, middleware::Logger, App, Error, HttpServer, Responder, +}; +use actix_web_httpauth::{extractors::bearer::BearerAuth, middleware::HttpAuthentication}; +use tracing::level_filters::LevelFilter; +use tracing_subscriber::EnvFilter; +/// Validator that: +/// - accepts Bearer auth; +/// - returns a custom response for requests without a valid Bearer Authorization header; +/// - rejects tokens containing an "x" (for quick testing using command line HTTP clients). async fn validator( req: ServiceRequest, - _credentials: BasicAuth, + credentials: Option, ) -> Result { + let Some(credentials) = credentials else { + return Err((error::ErrorBadRequest("no bearer header"), req)); + }; + + eprintln!("{credentials:?}"); + + if credentials.token().contains('x') { + return Err((error::ErrorBadRequest("token contains x"), req)); + } + Ok(req) } +#[get("/")] +async fn index(auth: BearerAuth) -> impl Responder { + format!("authenticated for token: {}", auth.token().to_owned()) +} + #[actix_web::main] async fn main() -> std::io::Result<()> { + tracing_subscriber::fmt() + .with_env_filter( + EnvFilter::builder() + .with_default_directive(LevelFilter::INFO.into()) + .from_env_lossy(), + ) + .without_time() + .init(); + HttpServer::new(|| { - let auth = HttpAuthentication::basic(validator); + let auth = HttpAuthentication::with_fn(validator); + App::new() - .wrap(middleware::Logger::default()) + .service(index) .wrap(auth) - .service(web::resource("/").to(|| async { "Test\r\n" })) + .wrap(Logger::default().log_target("@")) }) .bind("127.0.0.1:8080")? - .workers(1) + .workers(2) .run() .await }