Compare commits
2 Commits
9af4fa56e1
...
6d87adb2e6
Author | SHA1 | Date |
---|---|---|
Hubert | 6d87adb2e6 | |
Hubert | aa131d009f |
|
@ -15,15 +15,18 @@ use tokio::process::{Child, Command};
|
|||
use crate::gitust::Gitust;
|
||||
use crate::reader::ToStream;
|
||||
use crate::error;
|
||||
use crate::webutils::auth;
|
||||
|
||||
//#[get("/git/{owner}/{repo}.git/{path:.*}")]
|
||||
pub async fn git_proto(
|
||||
pub async fn git_proto<T : auth::AuthValidator>(
|
||||
mut payload : web::Payload,
|
||||
web::Path((owner, reponame, path)): web::Path<(String, String, String)>,
|
||||
mut req: HttpRequest,
|
||||
gitust : web::Data<Gitust>,
|
||||
auth : BasicAuth,
|
||||
authenticator : web::Data<T>,
|
||||
auth : Option<BasicAuth>,
|
||||
) -> Result<HttpResponse, error::Error>{
|
||||
let user = auth.and_then(|a| authenticator.check_basic(&a)).ok_or(error::Error::Unauthorized("git_proto".to_string()))?;
|
||||
//println!("enter git_proto");
|
||||
let mut cmd = Command::new("git");
|
||||
cmd.arg("http-backend");
|
||||
|
@ -32,7 +35,7 @@ pub async fn git_proto(
|
|||
cmd.env("REQUEST_METHOD", req.method().as_str());
|
||||
cmd.env("GIT_PROJECT_ROOT", &gitust.repo_root_path);
|
||||
cmd.env("PATH_INFO", format!("/{}/{}.git/{}",owner, reponame, path));
|
||||
cmd.env("REMOTE_USER", auth.user_id().to_string());
|
||||
cmd.env("REMOTE_USER", user.get_name());
|
||||
//cmd.env("REMOTE_ADDR", req.remote_addr().to_string());
|
||||
cmd.env("QUERY_STRING", req.query_string());
|
||||
cmd.env("CONTENT_TYPE", header(&req, header::CONTENT_TYPE));
|
||||
|
|
|
@ -34,6 +34,7 @@ use gitutils::gitfile::GitFile;
|
|||
use gitutils::gitproto;
|
||||
use gitutils::gitrepo::GitRepo;
|
||||
use web::repo;
|
||||
use webutils::auth;
|
||||
|
||||
use crate::git::GitBrowseEntry;
|
||||
use crate::gitust::Gitust;
|
||||
|
@ -165,6 +166,7 @@ async fn main() -> std::io::Result<()> {
|
|||
// .wrap(Logger::new("%a %{User-Agent}i"))
|
||||
.wrap(CookieSession::signed(session_key).secure(false))
|
||||
.data(gitust)
|
||||
.data(auth::TestValidator)
|
||||
.service(hello)
|
||||
.service(echo)
|
||||
.service(hello_test)
|
||||
|
@ -186,7 +188,7 @@ async fn main() -> std::io::Result<()> {
|
|||
)
|
||||
.service(
|
||||
webx::resource("/git/{user}/{repo}.git/{path:.*}")
|
||||
.route(webx::route().to(gitproto::git_proto))
|
||||
.route(webx::route().to(gitproto::git_proto::<auth::TestValidator>))
|
||||
)
|
||||
.service(
|
||||
Files::new("/static", "static")
|
||||
|
|
|
@ -1,13 +1,19 @@
|
|||
use actix_session::Session;
|
||||
use actix_web::Error;
|
||||
use actix_web_httpauth::extractors::basic::BasicAuth;
|
||||
use std::borrow::Cow;
|
||||
|
||||
pub trait AuthValidator {
|
||||
fn check_user(&self, name : &String, pwd : &String) -> bool;
|
||||
fn check_user(&self, name : &String, pwd : &String) -> Option<User>;
|
||||
|
||||
fn check_basic(&self, basic : BasicAuth) -> bool {
|
||||
match basic.password() {
|
||||
None => {false}
|
||||
Some(pwd) => {self.check_user(&basic.user_id().to_string(), &pwd.to_string())}
|
||||
fn check_basic(&self, basic : &BasicAuth) -> Option<User> {
|
||||
basic.password().and_then(|pwd| self.check_user(&basic.user_id().to_string(), &pwd.to_string()))
|
||||
}
|
||||
|
||||
fn check_session(&self, session : &Session) -> Option<User> {
|
||||
let result = session.get::<String>("user");
|
||||
match result {
|
||||
Ok(username) => {username.map(|u| User(u))}
|
||||
Err(e) => {None}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +21,19 @@ pub trait AuthValidator {
|
|||
pub struct TestValidator;
|
||||
|
||||
impl AuthValidator for TestValidator {
|
||||
fn check_user(&self, name: &String, pwd: &String) -> bool {
|
||||
pwd.eq(&(name.clone() + "pwd")) //stub!
|
||||
fn check_user(&self, name: &String, pwd: &String) -> Option<User> {
|
||||
if pwd.eq(&(name.clone() + "pwd")) {
|
||||
Some(User(name.clone()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct User(String);
|
||||
|
||||
impl User {
|
||||
pub fn get_name(&self) -> String {
|
||||
self.0.clone()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
pub mod auth;
|
||||
pub mod auth;
|
||||
pub mod user;
|
Loading…
Reference in New Issue