From 1e7c773c19d0a8ddcf7fd035245b562eb7701325 Mon Sep 17 00:00:00 2001 From: Hubert Date: Fri, 23 Jul 2021 06:55:24 +0200 Subject: [PATCH] basic auth triggered by query --- src/main.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index fd8c409..17419ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, +} + #[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::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::)) ) .service(hello_session)