2018-05-30 15:43:39 +02:00
|
|
|
extern crate actix_web;
|
|
|
|
extern crate actix_web_httpauth;
|
|
|
|
|
|
|
|
use actix_web::{server, App, Result, HttpRequest, FromRequest};
|
|
|
|
use actix_web::middleware::{Middleware, Started};
|
|
|
|
use actix_web_httpauth::extractors::basic::{BasicAuth, Config};
|
|
|
|
use actix_web_httpauth::extractors::AuthenticationError;
|
|
|
|
|
|
|
|
struct Auth;
|
|
|
|
|
|
|
|
impl<S> Middleware<S> for Auth {
|
2018-09-08 09:27:21 +02:00
|
|
|
fn start(&self, req: &HttpRequest<S>) -> Result<Started> {
|
2018-05-30 15:43:39 +02:00
|
|
|
let mut config = Config::default();
|
|
|
|
config.realm("WallyWorld");
|
|
|
|
let auth = BasicAuth::from_request(&req, &config)?;
|
|
|
|
|
|
|
|
if auth.username() == "Aladdin" && auth.password() == Some("open sesame") {
|
|
|
|
Ok(Started::Done)
|
|
|
|
} else {
|
|
|
|
Err(AuthenticationError::from(config).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index(_req: HttpRequest) -> String {
|
|
|
|
"Hello, authorized user!".to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
server::new(|| App::new()
|
|
|
|
.middleware(Auth)
|
|
|
|
.resource("/", |r| r.with(index))
|
|
|
|
)
|
|
|
|
.bind("127.0.0.1:8088").unwrap()
|
|
|
|
.run();
|
|
|
|
}
|