2018-05-20 21:10:48 +02:00
|
|
|
extern crate actix_web;
|
|
|
|
extern crate actix_web_httpauth;
|
|
|
|
|
|
|
|
use actix_web::{server, App, HttpRequest, FromRequest, Result};
|
|
|
|
use actix_web::middleware::{Middleware, Started};
|
2018-05-23 14:31:14 +02:00
|
|
|
use actix_web_httpauth::basic::{BasicAuth, Config};
|
2018-05-20 21:10:48 +02:00
|
|
|
|
|
|
|
struct AuthMiddleware;
|
|
|
|
|
|
|
|
impl<S> Middleware<S> for AuthMiddleware {
|
|
|
|
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
2018-05-23 14:31:14 +02:00
|
|
|
let mut config = Config::default();
|
|
|
|
config.realm("Restricted area".to_string());
|
|
|
|
let auth = BasicAuth::from_request(&req, &config)?;
|
2018-05-20 21:10:48 +02:00
|
|
|
|
|
|
|
// Please note that this is only an example,
|
|
|
|
// do not ever hardcode your credentials!
|
|
|
|
if auth.username == "root" && auth.password == "pass" {
|
|
|
|
Ok(Started::Done)
|
|
|
|
} else {
|
2018-05-23 14:31:14 +02:00
|
|
|
let response = BasicAuth::error_response(&config);
|
2018-05-20 21:10:48 +02:00
|
|
|
Ok(Started::Response(response))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index(auth: BasicAuth) -> String {
|
|
|
|
format!("Hello, {}", auth.username)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
server::new(|| App::new()
|
2018-05-23 14:31:14 +02:00
|
|
|
// Comment the `.middleware()` line and let `BasicAuth` extractor
|
|
|
|
// in the `index` handler do the authentication routine
|
2018-05-20 21:10:48 +02:00
|
|
|
.middleware(AuthMiddleware)
|
|
|
|
.resource("/", |r| r.with(index)))
|
|
|
|
.bind("127.0.0.1:8088").unwrap()
|
|
|
|
.run();
|
|
|
|
}
|