Skip to content

Commit 410133c

Browse files
committed
web-passwd
1 parent d83e7a9 commit 410133c

File tree

5 files changed

+155
-96
lines changed

5 files changed

+155
-96
lines changed

Cargo.lock

Lines changed: 56 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hooyad/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ serde_json = "1.0"
3737
chrono = "0.4"
3838
jsonwebtoken = "9.0"
3939
bcrypt = "0.17"
40+
user_dirs = "0.2"
4041

hooyad/src/config.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::path::PathBuf;
21
use std::fs;
2+
use std::path::PathBuf;
33

44
pub const DEFAULT_HOOYAD_ENDPOINT: &str = "127.0.0.1:8531";
55

@@ -20,7 +20,9 @@ impl RuntimeConfig {
2020
Ok(())
2121
}
2222

23-
pub fn ensure_filestore_structure(&self) -> Result<(), Box<dyn std::error::Error>> {
23+
pub fn ensure_filestore_structure(
24+
&self,
25+
) -> Result<(), Box<dyn std::error::Error>> {
2426
self.ensure_data_dir()?;
2527
fs::create_dir_all(self.data_dir.join("store"))?;
2628
fs::create_dir_all(self.data_dir.join("forgotten"))?;
@@ -38,23 +40,30 @@ impl RuntimeConfig {
3840
}
3941

4042
pub fn web_password_hash_path(&self) -> PathBuf {
41-
self.data_dir.join("web-password-hash")
43+
self.data_dir.join("web-passwd")
4244
}
4345

44-
pub fn store_password_hash(&self, password: &str) -> Result<(), Box<dyn std::error::Error>> {
46+
pub fn store_password_hash(
47+
&self,
48+
password: &str,
49+
) -> Result<(), Box<dyn std::error::Error>> {
4550
use bcrypt::{hash, DEFAULT_COST};
4651
self.ensure_data_dir()?;
4752
let hashed = hash(password, DEFAULT_COST)?;
4853
fs::write(self.web_password_hash_path(), hashed)?;
4954
Ok(())
5055
}
5156

52-
pub fn load_password_hash(&self) -> Result<String, Box<dyn std::error::Error>> {
57+
pub fn load_password_hash(
58+
&self,
59+
) -> Result<String, Box<dyn std::error::Error>> {
5360
let hash_content = fs::read_to_string(self.web_password_hash_path())?;
5461
Ok(hash_content.trim().to_string())
5562
}
5663

57-
pub fn ensure_password_exists(&self) -> Result<Option<String>, Box<dyn std::error::Error>> {
64+
pub fn ensure_password_exists(
65+
&self,
66+
) -> Result<Option<String>, Box<dyn std::error::Error>> {
5867
let hash_file = self.web_password_hash_path();
5968

6069
if hash_file.exists() {
@@ -63,7 +72,8 @@ impl RuntimeConfig {
6372

6473
// Generate new password
6574
use rand::Rng;
66-
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
75+
const CHARSET: &[u8] =
76+
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
6777
let mut rng = rand::thread_rng();
6878
let password: String = (0..16)
6979
.map(|_| {

hooyad/src/server.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ use hooya::runtime::Runtime;
1515
use rand::distributions::DistString;
1616
use sqlx::migrate::MigrateDatabase;
1717
use sqlx::{Sqlite, SqlitePool};
18-
use std::{
19-
fs::File,
20-
io::Write,
21-
path::PathBuf,
22-
pin::Pin,
23-
};
18+
use std::{fs::File, io::Write, path::PathBuf, pin::Pin};
2419
use tokio_stream::StreamExt;
2520
use tonic::{transport::Server, Request, Response, Status};
2621

@@ -415,14 +410,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
415410
.arg(Arg::new("db-uri").long("db-uri").env("HOOYAD_DB_URI"))
416411
.get_matches();
417412

418-
// Apply XDG logic if using default path
419-
let mut filestore_path = matches.get_one::<PathBuf>("filestore").unwrap().clone();
420-
if filestore_path == PathBuf::from(".hooya") && !std::env::var("HOOYAD_FILESTORE").is_ok() {
421-
if let Ok(xdg_pictures_dir) = std::env::var("XDG_PICTURES_DIR") {
422-
let xdg_pictures_path = std::path::Path::new(&xdg_pictures_dir);
423-
if xdg_pictures_path.is_dir() {
424-
filestore_path = xdg_pictures_path.join("hooya");
425-
}
413+
// Apply cross-platform user directory logic if using default path
414+
let mut filestore_path =
415+
matches.get_one::<PathBuf>("filestore").unwrap().clone();
416+
if filestore_path == PathBuf::from(".hooya")
417+
&& !std::env::var("HOOYAD_FILESTORE").is_ok()
418+
{
419+
if let Ok(data_dir) = user_dirs::data_dir() {
420+
filestore_path = data_dir.join("hooya");
426421
}
427422
}
428423

@@ -452,10 +447,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
452447
Server::builder()
453448
.accept_http1(true)
454449
.add_service(ControlServer::new(IControl {
455-
runtime: Runtime {
456-
filestore_path,
457-
db,
458-
},
450+
runtime: Runtime { filestore_path, db },
459451
}))
460452
.serve(matches.get_one::<String>("endpoint").unwrap().parse()?)
461453
.await?;

0 commit comments

Comments
 (0)