basic auth triggered by query

This commit is contained in:
Hubert 2021-07-23 06:55:24 +02:00
parent 9832f30360
commit 1e7c773c19
1 changed files with 27 additions and 2 deletions

View File

@ -9,14 +9,14 @@ use actix_files::Files;
use actix_session::{CookieSession, Session};
use actix_web::{App, Error, get, HttpMessage, HttpRequest, HttpResponse, HttpServer, post, Responder};
use actix_web::client::PayloadError;
use actix_web::dev::ServiceRequest;
use actix_web::dev::{ServiceRequest, Service};
use actix_web::http::{header, StatusCode};
use actix_web::http::header::Header;
use actix_web::http::header::IntoHeaderValue;
use actix_web::middleware::Logger;
use actix_web::web as webx;
use actix_web::web::{Buf, Bytes};
use actix_web_httpauth::extractors::AuthenticationError;
use actix_web_httpauth::extractors::{AuthenticationError, AuthExtractor};
use actix_web_httpauth::extractors::basic::{BasicAuth, Config};
use actix_web_httpauth::headers::authorization::{Authorization, Basic};
use actix_web_httpauth::middleware::HttpAuthentication;
@ -41,6 +41,8 @@ use crate::gitust::Gitust;
use crate::ite::SuperIterator;
use crate::reader::ToStream;
use crate::writer::Writer;
use crate::web::repo::GitWebQ;
use crate::webutils::auth::{TestValidator, AuthValidator, User};
mod git;
mod ite;
@ -150,6 +152,11 @@ fn validate_credentials(user_id: &str, user_password: Option<&str>) -> Result<bo
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Authentication failed!"));
}
#[derive(Deserialize)]
struct LoginQ {
login: Option<bool>,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
@ -174,6 +181,24 @@ async fn main() -> std::io::Result<()> {
.service(askama)
.service(
webx::resource("/git/{owner}/{repo}.git")
.wrap_fn(|req, serv| {
let query : Result<webx::Query<LoginQ>, _> = webx::Query::from_query(req.query_string());
let authFut = BasicAuth::from_service_request(&req);
let resFut = serv.call(req);
async {
let webx::Query(LoginQ{login}) = query?;
match login {
Some(true) => {
let auth = authFut.await?;
match TestValidator.check_basic(&auth) {
None => {Err(AuthenticationError::from(Config::default().realm("auth")).into())}
Some(_) => {resFut.await}
}
}
_ => {resFut.await}
}
}
})
.route(webx::get().to(repo::git_main::<auth::TestValidator>))
)
.service(hello_session)