Create new open
module for calling auth
and gpio
appropriately
This commit is contained in:
parent
e65e086b90
commit
30b7e5bfe3
7 changed files with 114 additions and 0 deletions
|
@ -6,5 +6,6 @@ members = [
|
||||||
"labadoor-csv",
|
"labadoor-csv",
|
||||||
"labadoor",
|
"labadoor",
|
||||||
"labadoor-auth",
|
"labadoor-auth",
|
||||||
|
"labadoor-open",
|
||||||
"labadoor-acl",
|
"labadoor-acl",
|
||||||
]
|
]
|
||||||
|
|
6
labadoor-open/Cargo.toml
Normal file
6
labadoor-open/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "labadoor-open"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
66
labadoor-open/src/lib.rs
Normal file
66
labadoor-open/src/lib.rs
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
pub type Binary = Vec<String>;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct OpenArgs {
|
||||||
|
pub auth: BTreeMap<String, Binary>,
|
||||||
|
pub hardware: BTreeMap<String, Binary>,
|
||||||
|
pub method: String,
|
||||||
|
pub identifier: String,
|
||||||
|
pub resource_shortcut: i8,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct AuthResult {
|
||||||
|
pub username: String,
|
||||||
|
pub resource: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_bin(bin: Binary) -> Result<String, ()> {
|
||||||
|
let mut ret = Err(());
|
||||||
|
let mut iter = bin.iter();
|
||||||
|
let mut cmd = std::process::Command::new(iter.next().unwrap());
|
||||||
|
cmd.args(iter);
|
||||||
|
|
||||||
|
if let Ok(_) = cmd.status() {
|
||||||
|
let stdout = cmd.output().unwrap().stdout;
|
||||||
|
let str = std::str::from_utf8(stdout.as_slice()).unwrap().trim();
|
||||||
|
ret = Ok(String::from(str));
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_auth(args: &OpenArgs, bin: Binary) -> Result<AuthResult, ()> {
|
||||||
|
let mut ret = Err(());
|
||||||
|
|
||||||
|
let mut auth_bin = bin.clone();
|
||||||
|
auth_bin.push(args.method.clone());
|
||||||
|
auth_bin.push(args.identifier.clone());
|
||||||
|
auth_bin.push(args.resource_shortcut.to_string());
|
||||||
|
let output = run_bin(auth_bin);
|
||||||
|
|
||||||
|
if let Ok(out) = output {
|
||||||
|
let s: Vec<String> = out.split(',').map(|s| String::from(s)).collect();
|
||||||
|
ret = Ok(AuthResult {
|
||||||
|
username: s[0].clone(),
|
||||||
|
resource: s[1].clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn open(args: OpenArgs) -> Result<(), String> {
|
||||||
|
let mut ret = Err("Not authorized!".to_string());
|
||||||
|
|
||||||
|
for (method, binary) in args.auth.iter() {
|
||||||
|
let output = run_auth(&args, binary.to_vec());
|
||||||
|
if let Ok(user) = output {
|
||||||
|
let resource_bin = args.hardware.get(&user.resource).unwrap();
|
||||||
|
if let Ok(_) = run_bin(resource_bin.to_vec()) {
|
||||||
|
ret = Err("Hardware failure!".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
|
@ -14,3 +14,10 @@ active_time = 2000
|
||||||
|
|
||||||
[auth]
|
[auth]
|
||||||
backends = ["csv"]
|
backends = ["csv"]
|
||||||
|
|
||||||
|
[open.auth]
|
||||||
|
labadoor = [ "/usr/bin/labadoor", "auth" ]
|
||||||
|
|
||||||
|
[open.hardware]
|
||||||
|
tolabaki = [ "/usr/bin/labadoor", "gpio" ]
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,8 @@ pub enum Command {
|
||||||
GPIO(GPIO),
|
GPIO(GPIO),
|
||||||
#[cfg(feature = "auth")]
|
#[cfg(feature = "auth")]
|
||||||
Auth(labadoor_auth::cli::Cli),
|
Auth(labadoor_auth::cli::Cli),
|
||||||
|
#[cfg(feature = "open")]
|
||||||
|
Open(Open),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "telegram")]
|
#[cfg(feature = "telegram")]
|
||||||
|
@ -52,6 +54,19 @@ pub struct GPIO {
|
||||||
pub active_time: Option<u32>,
|
pub active_time: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
#[cfg(feature = "open")]
|
||||||
|
#[derive(Serialize, Deserialize, Parser, Clone, Debug)]
|
||||||
|
pub struct Open {
|
||||||
|
#[clap(skip)]
|
||||||
|
pub auth: BTreeMap<String, Vec<String>>,
|
||||||
|
#[clap(skip)]
|
||||||
|
pub hardware: BTreeMap<String, Vec<String>>,
|
||||||
|
pub method: String,
|
||||||
|
pub identifier: String,
|
||||||
|
pub resource_shortcut: i8,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse() -> Cli {
|
pub fn parse() -> Cli {
|
||||||
Cli::parse()
|
Cli::parse()
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,12 @@ fn main() -> Result<(), ()> {
|
||||||
let gpio = config.get::<cli::GPIO>("gpio").unwrap().to_config();
|
let gpio = config.get::<cli::GPIO>("gpio").unwrap().to_config();
|
||||||
labadoor_gpio::gpio(gpio);
|
labadoor_gpio::gpio(gpio);
|
||||||
}
|
}
|
||||||
|
#[cfg(feature = "open")]
|
||||||
|
cli::Command::Open(cliargs) => {
|
||||||
|
let config = add_cliargs!(config, "open", cliargs);
|
||||||
|
let open = config.get::<cli::Open>("open").unwrap().to_config();
|
||||||
|
labadoor_open::open(open);
|
||||||
|
}
|
||||||
#[cfg(feature = "auth")]
|
#[cfg(feature = "auth")]
|
||||||
cli::Command::Auth(cli) => ret = labadoor_auth::auth(&cli, config),
|
cli::Command::Auth(cli) => ret = labadoor_auth::auth(&cli, config),
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,3 +35,16 @@ impl ToConfig<labadoor_gpio::GPIOArgs> for GPIO {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "open")]
|
||||||
|
impl ToConfig<labadoor_open::OpenArgs> for Open {
|
||||||
|
fn to_config(&self) -> labadoor_open::OpenArgs {
|
||||||
|
labadoor_open::OpenArgs {
|
||||||
|
auth: self.auth.clone(),
|
||||||
|
hardware: self.hardware.clone(),
|
||||||
|
method: self.method.clone(),
|
||||||
|
identifier: self.identifier.clone(),
|
||||||
|
resource_shortcut: self.resource_shortcut,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue