ask for credentials, add postinst script

This commit is contained in:
jb-alvarado 2022-06-27 14:10:29 +02:00
parent a1cd6486c5
commit 643edd0178
8 changed files with 94 additions and 19 deletions

15
Cargo.lock generated
View File

@ -1027,7 +1027,7 @@ dependencies = [
[[package]]
name = "ffplayout-api"
version = "0.3.1"
version = "0.3.2"
dependencies = [
"actix-multipart",
"actix-web",
@ -1045,6 +1045,7 @@ dependencies = [
"rand 0.8.5",
"relative-path",
"reqwest",
"rpassword",
"sanitize-filename",
"serde",
"serde_json",
@ -2454,6 +2455,18 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "rpassword"
version = "6.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bf099a1888612545b683d2661a1940089f6c2e5a8e38979b2159da876bfd956"
dependencies = [
"libc",
"serde",
"serde_json",
"winapi 0.3.9",
]
[[package]]
name = "rustc_version"
version = "0.4.0"

0
debian/.gitkeep vendored
View File

10
debian/postinst vendored Normal file
View File

@ -0,0 +1,10 @@
#DEBHELPER#
if [ ! -d "/usr/share/ffplayout/db" ]; then
mkdir "/usr/share/ffplayout/db"
chmod 777 "/usr/share/ffplayout/db"
/usr/bin/ffpapi -i
chown www-data. "/usr/share/ffplayout/db/ffplayout.db"
fi

View File

@ -4,7 +4,7 @@ description = "Rest API for ffplayout"
license = "GPL-3.0"
authors = ["Jonathan Baecker jonbae77@gmail.com"]
readme = "README.md"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
[dependencies]
@ -24,6 +24,7 @@ once_cell = "1.10"
rand = "0.8"
relative-path = "1.6"
reqwest = { version = "0.11", features = ["blocking", "json"] }
rpassword = "6.0"
sanitize-filename = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View File

@ -21,4 +21,6 @@ Then run the API thru the systemd service, or like:
ffpapi -l 127.0.0.1:8080
```
If you plan to run ffpapi with systemd set permission from **/usr/share/ffplayout** and content to user **www-data:www-data**.
**For possible endpoints read: [api endpoints](/docs/api.md)**

View File

@ -5,6 +5,9 @@ use clap::Parser;
about = "REST API for ffplayout",
long_about = None)]
pub struct Args {
#[clap(short, long, help = "ask for user credentials")]
pub ask: bool,
#[clap(short, long, help = "Listen on IP:PORT, like: 127.0.0.1:8080")]
pub listen: Option<String>,

View File

@ -1,7 +1,13 @@
use std::{error::Error, fs::File, path::Path};
use std::{
error::Error,
fs::File,
io::{stdin, stdout, Write},
path::Path,
};
use faccess::PathExt;
use once_cell::sync::OnceCell;
use rpassword::read_password;
use simplelog::*;
pub mod args_parse;
@ -69,20 +75,24 @@ pub async fn init_config() {
}
pub fn db_path() -> Result<String, Box<dyn std::error::Error>> {
let sys_path = Path::new("/usr/share/ffplayout");
let mut db_path = String::from("./ffplayout.db");
let sys_path = Path::new("/usr/share/ffplayout/db");
let mut db_path = "./ffplayout.db".to_string();
if sys_path.is_dir() && !sys_path.writable() {
error!("Path {} is not writable!", sys_path.display());
}
if sys_path.is_dir() && sys_path.writable() {
db_path = String::from("/usr/share/ffplayout/ffplayout.db");
db_path = "/usr/share/ffplayout/db/ffplayout.db".to_string();
} else if Path::new("./assets").is_dir() {
db_path = String::from("./assets/ffplayout.db");
db_path = "./assets/ffplayout.db".to_string();
}
Ok(db_path)
}
pub async fn run_args(args: Args) -> Result<(), i32> {
if !args.init && args.listen.is_none() && args.username.is_none() {
pub async fn run_args(mut args: Args) -> Result<(), i32> {
if !args.init && args.listen.is_none() && !args.ask && args.username.is_none() {
error!("Wrong number of arguments! Run ffpapi --help for more information.");
return Err(0);
@ -96,6 +106,46 @@ pub async fn run_args(args: Args) -> Result<(), i32> {
return Err(0);
}
if args.ask {
let mut user = String::new();
print!("Username: ");
stdout().flush().unwrap();
stdin()
.read_line(&mut user)
.expect("Did not enter a correct name?");
if let Some('\n') = user.chars().next_back() {
user.pop();
}
if let Some('\r') = user.chars().next_back() {
user.pop();
}
args.username = Some(user);
print!("Password: ");
stdout().flush().unwrap();
let password = read_password();
args.password = password.ok();
let mut email = String::new();
print!("EMail: ");
stdout().flush().unwrap();
stdin()
.read_line(&mut email)
.expect("Did not enter a correct name?");
if let Some('\n') = email.chars().next_back() {
email.pop();
}
if let Some('\r') = email.chars().next_back() {
email.pop();
}
args.email = Some(email);
}
if let Some(username) = args.username {
if args.email.is_none() || args.password.is_none() {
error!("Email/password missing!");

View File

@ -41,20 +41,16 @@ suggests = "ffmpeg"
copyright = "Copyright (c) 2022, Jonathan Baecker. All rights reserved."
conf-files = ["/etc/ffplayout/ffplayout.yml"]
assets = [
[
"../target/x86_64-unknown-linux-musl/release/ffpapi",
"/usr/bin/ffpapi",
"755"
],
["../target/x86_64-unknown-linux-musl/release/ffpapi", "/usr/bin/", "755"],
[
"../target/x86_64-unknown-linux-musl/release/ffplayout",
"/usr/bin/ffplayout",
"/usr/bin/",
"755"
],
["../assets/ffpapi.service", "/lib/systemd/system/ffpapi.service", "644"],
["../assets/11-ffplayout", "/etc/sudoers.d/11-ffplayout", "644"],
["../assets/ffplayout.yml", "/etc/ffplayout/ffplayout.yml", "644"],
["../assets/logo.png", "/usr/share/ffplayout/logo.png", "644"],
["../assets/ffpapi.service", "/lib/systemd/system/", "644"],
["../assets/11-ffplayout", "/etc/sudoers.d/", "644"],
["../assets/ffplayout.yml", "/etc/ffplayout/", "644"],
["../assets/logo.png", "/usr/share/ffplayout/", "644"],
["../README.md", "/usr/share/doc/ffplayout/README", "644"],
]
maintainer-scripts = "../debian/"