mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-26 10:27:42 +02:00
fix httpauth extraction error handling in middleware (#128)
This commit is contained in:
33
actix-web-httpauth/examples/cors.rs
Normal file
33
actix-web-httpauth/examples/cors.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use actix_cors::Cors;
|
||||
use actix_web::{dev::ServiceRequest, get, App, Error, HttpResponse, HttpServer};
|
||||
use actix_web_httpauth::{
|
||||
extractors::bearer::BearerAuth, middleware::HttpAuthentication,
|
||||
};
|
||||
|
||||
async fn ok_validator(
|
||||
req: ServiceRequest,
|
||||
credentials: BearerAuth,
|
||||
) -> Result<ServiceRequest, Error> {
|
||||
eprintln!("{:?}", credentials);
|
||||
Ok(req)
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(HttpAuthentication::bearer(ok_validator))
|
||||
// ensure the CORS middleware is wrapped around the httpauth middleware so it is able to
|
||||
// add headers to error responses
|
||||
.wrap(Cors::permissive())
|
||||
.service(index)
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
@ -2,7 +2,7 @@ use actix_web::{middleware, web, App, HttpServer};
|
||||
|
||||
use actix_web_httpauth::middleware::HttpAuthentication;
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
let auth = HttpAuthentication::basic(|req, _credentials| async { Ok(req) });
|
||||
|
@ -11,7 +11,7 @@ async fn validator(
|
||||
Ok(req)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
let auth = HttpAuthentication::basic(validator);
|
||||
|
Reference in New Issue
Block a user