diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 57 |
1 files changed, 17 insertions, 40 deletions
diff --git a/src/main.rs b/src/main.rs index 81f09e2..95476dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,7 +78,7 @@ async fn verify_login(cfg: &Config, data: &FormData) -> Result<bool> { // Processing the `authenticate-basic` called by cgit. fn cmd_authenticate_basic( matches: &ArgMatches, - cfg: Option<Config>, + cfg: Config, ) -> Result<()> { unimplemented!() } @@ -86,7 +86,7 @@ fn cmd_authenticate_basic( // Processing the `authenticate-cookie` called by cgit. async fn cmd_authenticate_cookie( matches: &ArgMatches<'_>, - cfg: Option<Config>, + cfg: Config, ) -> Result<bool> { let cookies = matches.value_of("http-cookie").unwrap_or(""); @@ -94,6 +94,7 @@ async fn cmd_authenticate_cookie( return Ok(false) } + // TODO: use async connection to redis let mut conn = redis::Client::open("redis://127.0.0.1/")?; for cookie in cookies.split(';').map(|x| x.trim()) { @@ -110,11 +111,9 @@ async fn cmd_authenticate_cookie( } -async fn cmd_init(cfg: Option<Config>) -> Result<()> { +async fn cmd_init(cfg: Config) -> Result<()> { - - // TODO: read database location from configure file - let mut conn = sqlx::SqliteConnection::connect("sqlite::memory:").await?; + let mut conn = sqlx::SqliteConnection::connect(cfg.get_database_location()).await?; let rows = sqlx::query(r#"SELECT name FROM sqlite_master WHERE type='table' AND name=?"#) .bind("auth_meta") @@ -133,41 +132,21 @@ async fn cmd_init(cfg: Option<Config>) -> Result<()> { // Processing the `authenticate-post` called by cgit. async fn cmd_authenticate_post( matches: &ArgMatches<'_>, - cfg: Option<Config>, + cfg: Config, ) -> Result<()> { - // Load configurations. - let cfg = cfg.unwrap_or_default(); // Read stdin from upstream. let mut buffer = String::new(); stdin().read_to_string(&mut buffer)?; - let mut data = datastructures::FormData::new(); + let data = datastructures::FormData::from(buffer); // Parsing user posted form. - let fields = form_urlencoded::parse(buffer.as_bytes()); - for f in fields { - match f.0 { - Cow::Borrowed("username") => { - data.set_user(f.1.to_string()) - } - Cow::Borrowed("password") => { - data.set_password(f.1.to_string()) - } - _ => {} - } - } // Authenticated via gogs. if verify_login(&cfg, &data).await.is_ok() { - //let hash = data.hash(); - //let cgitauth = format!("{}:{}", hash, data.nonce); - //let cgitauth_b64 = base64::encode_block(cgitauth.as_bytes()); - //let path = Path::new(&cfg.cache_dir).join(&hash); - //data.to_file(path, true); let cookie = rand_str(COOKIE_LENGTH); - //let redis = redis_async::client::connect(&SocketAddr::from_str("127.0.0.1:5432").unwrap()).await?; - //redis. + // TODO: same here let mut conn = redis::Client::open("redis://127.0.0.1/")?; - conn.set_ex::<_, &str, i32>(format!("cgit_auth_{}", cookie), "1", 600)?; + conn.set_ex::<_, &str, i32>(format!("cgit_auth_{}", cookie), "1", cfg.cookie_ttl)?; let is_secure = matches .value_of("https") @@ -185,9 +164,7 @@ async fn cmd_authenticate_post( println!("Location: {}", location); println!( "Set-Cookie: cgit_auth={}; Domain={}; Max-Age={}; HttpOnly{}", - // TODO: use cookie ttl instead - //cookie, domain, cfg.cookie_ttl, cookie_suffix - cookie, domain, 600, cookie_suffix + cookie, domain, cfg.cookie_ttl, cookie_suffix ); } else { println!("Status: 403 Forbidden"); @@ -201,7 +178,7 @@ async fn cmd_authenticate_post( // Processing the `body` called by cgit. -async fn cmd_body(matches: &ArgMatches<'_>, _cfg: Option<Config>) { +async fn cmd_body(matches: &ArgMatches<'_>, _cfg: Config) { let source = include_str!("authentication_page.html"); let handlebars = Handlebars::new(); let meta = Meta { @@ -214,7 +191,7 @@ async fn cmd_body(matches: &ArgMatches<'_>, _cfg: Option<Config>) { } // Processing the `body` called by cron. -fn cmd_expire(_matches: &ArgMatches, cfg: Option<Config>) { +fn cmd_expire(_matches: &ArgMatches, cfg: Config) { unimplemented!(); } @@ -222,21 +199,21 @@ fn cmd_expire(_matches: &ArgMatches, cfg: Option<Config>) { async fn async_main(arg_matches: ArgMatches<'_>, cfg: Config) -> Result<i32>{ match arg_matches.subcommand() { ("authenticate-cookie", Some(matches)) => { - if cmd_authenticate_cookie(matches, Some(cfg)).await.is_ok() { + if cmd_authenticate_cookie(matches, cfg).await.is_ok() { return Ok(1) } } ("authenticate-post", Some(matches)) => { - cmd_authenticate_post(matches, Some(cfg)).await.unwrap(); + cmd_authenticate_post(matches, cfg).await.unwrap(); } ("body", Some(matches)) => { - cmd_body(matches, Some(cfg)).await; + cmd_body(matches, cfg).await; } ("expire", Some(matches)) => { - cmd_expire(matches, Some(cfg)); + cmd_expire(matches, cfg); } ("init", Some(matches)) => { - cmd_init(Some(cfg)).await?; + cmd_init(cfg).await?; } _ => {} } |
