Compare commits
3 Commits
234e2ccaa3
...
9af4fa56e1
Author | SHA1 | Date |
---|---|---|
Hubert | 9af4fa56e1 | |
Hubert | 90bc6d6d4f | |
Hubert | 381347e66a |
12
src/error.rs
12
src/error.rs
|
@ -12,18 +12,20 @@ impl Error {
|
|||
|
||||
impl From<git2::Error> for Error {
|
||||
fn from(giterr: git2::Error) -> Self {
|
||||
// panic!()
|
||||
Error::BadGateway(format!("{}", giterr))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(ioerr: std::io::Error) -> Self {
|
||||
Error::BadGateway(format!("{}", ioerr))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for actix_web::error::Error {
|
||||
fn from(e: Error) -> Self {
|
||||
match e {
|
||||
Error::BadGateway(msg) => {HttpResponseBuilder::new(StatusCode::BAD_GATEWAY).body(msg).into()}
|
||||
Error::Unauthorized(msg) => {
|
||||
let config : basic::Config = Default::default();
|
||||
AuthenticationError::from(config).into()}
|
||||
}
|
||||
Error::Unauthorized(realm) => {AuthenticationError::from(basic::Config::default().realm(realm)).into()}}
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
pub struct Gitust {
|
||||
pub repo_root_path : String,
|
||||
pub session_key : String,
|
||||
}
|
|
@ -14,6 +14,7 @@ use tokio::process::{Child, Command};
|
|||
|
||||
use crate::gitust::Gitust;
|
||||
use crate::reader::ToStream;
|
||||
use crate::error;
|
||||
|
||||
//#[get("/git/{owner}/{repo}.git/{path:.*}")]
|
||||
pub async fn git_proto(
|
||||
|
@ -22,7 +23,7 @@ pub async fn git_proto(
|
|||
mut req: HttpRequest,
|
||||
gitust : web::Data<Gitust>,
|
||||
auth : BasicAuth,
|
||||
) -> io::Result<HttpResponse>{
|
||||
) -> Result<HttpResponse, error::Error>{
|
||||
//println!("enter git_proto");
|
||||
let mut cmd = Command::new("git");
|
||||
cmd.arg("http-backend");
|
||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -155,13 +155,16 @@ async fn main() -> std::io::Result<()> {
|
|||
env_logger::from_env(Env::default().default_filter_or("info")).init();
|
||||
HttpServer::new(|| {
|
||||
let auth = HttpAuthentication::basic(basic_auth_validator);
|
||||
let gitust = Gitust {
|
||||
repo_root_path: "/home/hubert/gitust".to_string(),
|
||||
session_key: "oWe0ait9bi2Ohyiod2eeXirao1Oochie".to_string(),
|
||||
};
|
||||
let session_key = (&gitust.session_key).as_bytes();
|
||||
App::new()
|
||||
.data(Gitust {
|
||||
repo_root_path: "/home/hubert/gitust".to_string(),
|
||||
})
|
||||
.wrap(Logger::default())
|
||||
// .wrap(Logger::new("%a %{User-Agent}i"))
|
||||
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
||||
.wrap(CookieSession::signed(session_key).secure(false))
|
||||
.data(gitust)
|
||||
.service(hello)
|
||||
.service(echo)
|
||||
.service(hello_test)
|
||||
|
@ -183,7 +186,6 @@ async fn main() -> std::io::Result<()> {
|
|||
)
|
||||
.service(
|
||||
webx::resource("/git/{user}/{repo}.git/{path:.*}")
|
||||
// .wrap(auth)
|
||||
.route(webx::route().to(gitproto::git_proto))
|
||||
)
|
||||
.service(
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::path::Path;
|
||||
|
||||
use actix_web::web;
|
||||
use actix_web_httpauth::extractors::basic::BasicAuth;
|
||||
use actix_web_httpauth::extractors::basic;
|
||||
|
||||
use actix_web::get;
|
||||
use askama_actix::Template;
|
||||
|
@ -60,9 +60,10 @@ pub async fn git_main(
|
|||
web::Path((ownername, reponame)): web::Path<(String, String)>,
|
||||
web::Query(GitWebQ{commit : commitnameopt, path : pathopt, branch : branchopt}) : web::Query<GitWebQ>,
|
||||
gitust : web::Data<Gitust>,
|
||||
auth : Option<BasicAuth>,
|
||||
auth : Option<basic::BasicAuth>,
|
||||
//auth : BasicAuth,
|
||||
) -> Result<GitMainTemplate<Vec<Entry>, Vec<(String, String)>>, error::Error> {
|
||||
// let authtorization = auth.ok_or(error::Error::Unauthorized("safe repo".to_string()))?;
|
||||
let rootname = match pathopt {
|
||||
None => {"".to_string()}
|
||||
Some(s) => {
|
||||
|
@ -82,7 +83,8 @@ pub async fn git_main(
|
|||
};
|
||||
let owner = Owner { name : ownername};
|
||||
let repo = Repository {name : reponame, owner};
|
||||
let user = User { name : "Hubert".to_string()};
|
||||
// let user = User { name : "Hubert".to_string()};
|
||||
let user = auth.map(|auth| User{name : auth.user_id().to_string()});
|
||||
// il faut ajouter le commit/branch dans la query
|
||||
let path = rootname.split("/").map_accum("/git/".to_string() + &repo.owner.name + "/" + &repo.name, |str_ref, b| {
|
||||
let href = b + "/" + str_ref;
|
||||
|
@ -104,5 +106,5 @@ pub async fn git_main(
|
|||
}
|
||||
}
|
||||
}
|
||||
Ok(GitMainTemplate { repo, browse : entries, root : path, user_opt : Some(user)})
|
||||
Ok(GitMainTemplate { repo, browse : entries, root : path, user_opt : user})
|
||||
}
|
||||
|
|
|
@ -1,9 +1,21 @@
|
|||
use actix_web_httpauth::extractors::basic::BasicAuth;
|
||||
use std::borrow::Cow;
|
||||
|
||||
pub fn check_user(auth : BasicAuth) -> bool {
|
||||
match auth.password() {
|
||||
None => {false}
|
||||
Some(pwd) => {pwd.to_string().eq(&(auth.user_id().to_string() + "pwd"))}
|
||||
}
|
||||
pub trait AuthValidator {
|
||||
fn check_user(&self, name : &String, pwd : &String) -> bool;
|
||||
|
||||
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())}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TestValidator;
|
||||
|
||||
impl AuthValidator for TestValidator {
|
||||
fn check_user(&self, name: &String, pwd: &String) -> bool {
|
||||
pwd.eq(&(name.clone() + "pwd")) //stub!
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue