mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-27 09:12:57 +01:00
docs(httpauth): rework example
This commit is contained in:
parent
20234ec555
commit
515a727ca3
@ -43,7 +43,7 @@ redis = { version = "0.25", default-features = false, features = ["tokio-comp",
|
|||||||
actix-session = { path = ".", features = ["cookie-session", "redis-session"] }
|
actix-session = { path = ".", features = ["cookie-session", "redis-session"] }
|
||||||
actix-test = "0.1.0-beta.10"
|
actix-test = "0.1.0-beta.10"
|
||||||
actix-web = { version = "4", default-features = false, features = ["cookies", "secure-cookies", "macros"] }
|
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"
|
tracing = "0.1.30"
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
|
@ -32,3 +32,5 @@ pin-project-lite = "0.2.7"
|
|||||||
actix-cors = "0.7"
|
actix-cors = "0.7"
|
||||||
actix-service = "2"
|
actix-service = "2"
|
||||||
actix-web = { version = "4.1", default-features = false, features = ["macros"] }
|
actix-web = { version = "4.1", default-features = false, features = ["macros"] }
|
||||||
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
|
tracing = "0.1.30"
|
||||||
|
@ -1,24 +1,57 @@
|
|||||||
use actix_web::{dev::ServiceRequest, middleware, web, App, Error, HttpServer};
|
use actix_web::{
|
||||||
use actix_web_httpauth::{extractors::basic::BasicAuth, middleware::HttpAuthentication};
|
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(
|
async fn validator(
|
||||||
req: ServiceRequest,
|
req: ServiceRequest,
|
||||||
_credentials: BasicAuth,
|
credentials: Option<BearerAuth>,
|
||||||
) -> Result<ServiceRequest, (Error, ServiceRequest)> {
|
) -> Result<ServiceRequest, (Error, ServiceRequest)> {
|
||||||
|
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)
|
Ok(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
|
async fn index(auth: BearerAuth) -> impl Responder {
|
||||||
|
format!("authenticated for token: {}", auth.token().to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
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(|| {
|
HttpServer::new(|| {
|
||||||
let auth = HttpAuthentication::basic(validator);
|
let auth = HttpAuthentication::with_fn(validator);
|
||||||
|
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(middleware::Logger::default())
|
.service(index)
|
||||||
.wrap(auth)
|
.wrap(auth)
|
||||||
.service(web::resource("/").to(|| async { "Test\r\n" }))
|
.wrap(Logger::default().log_target("@"))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.workers(1)
|
.workers(2)
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user