auth: Enable backends based on the configuration or --backends

This commit is contained in:
George Kaklamanos 2023-11-25 18:08:56 +02:00
parent eebb4ca762
commit 7a294ee146
No known key found for this signature in database
GPG key ID: C0CAB8A6BDC9399D
4 changed files with 39 additions and 18 deletions

View file

@ -1,26 +1,27 @@
use clap::{Parser, ValueEnum}; use clap::{Parser, ValueEnum};
use serde::Deserialize; use serde::{Deserialize, Serialize};
#[derive(Parser, Debug)] #[derive(Serialize, Parser, Debug)]
pub struct Cli { pub struct Cli {
#[command(subcommand)] #[command(subcommand)]
pub command: Command, pub command: Command,
#[arg(short, long, value_enum)] #[arg(short, long, value_enum)]
pub backend: Backend, pub backends: Option<Vec<Backend>>,
} }
#[derive(Parser, ValueEnum, Clone, Debug)] #[derive(Serialize, Deserialize, Parser, ValueEnum, Clone, Debug)]
#[serde(rename_all = "lowercase")]
pub enum Backend { pub enum Backend {
#[cfg(feature = "csv")] #[cfg(feature = "csv")]
CSV, CSV,
} }
#[derive(Parser, Debug)] #[derive(Serialize, Parser, Debug)]
pub enum Command { pub enum Command {
Open(Open), Open(Open),
} }
#[derive(Parser, Debug)] #[derive(Serialize, Parser, Debug)]
pub struct Open { pub struct Open {
pub method: String, pub method: String,
pub identifier: String, pub identifier: String,

View file

@ -3,16 +3,34 @@ pub mod to_config;
use labadoor_acl::ACL; use labadoor_acl::ACL;
use to_config::ToConfig; use to_config::ToConfig;
pub fn auth(i: &cli::Cli, config: config::Config) { macro_rules! add_cliargs {
let acl = match &i.backend { ($d:ident,$section:expr,$i:ident) => {
$d.add_source(config::File::from_str(
&format!("[{}]\n{}", $section, toml::to_string($i).unwrap()),
config::FileFormat::Toml,
))
.build()
.unwrap()
};
}
use std::collections::{HashMap, HashSet};
pub fn auth(cli: &cli::Cli, config: config::ConfigBuilder<config::builder::DefaultState>) {
let config = add_cliargs!(config, "auth", cli);
let backends = config.get::<Vec<cli::Backend>>("auth.backends").unwrap();
for backend in backends {
let acl = match backend {
#[cfg(feature = "csv")] #[cfg(feature = "csv")]
cli::Backend::CSV => config.get::<cli::CSV>("csv").unwrap().to_config().new(), cli::Backend::CSV => config.get::<cli::CSV>("csv").unwrap().to_config().new(),
}; };
match &i.command { match &cli.command {
cli::Command::Open(open) => acl.auth_user( cli::Command::Open(cliargs) => {
open.method.clone(), let found = acl.auth_user(
open.identifier.clone(), cliargs.method.clone(),
open.resource.clone(), cliargs.identifier.clone(),
), cliargs.resource.clone(),
);
}
}; };
}
} }

View file

@ -11,3 +11,6 @@ device = ""
pin = 1 pin = 1
active_low = false active_low = false
active_time = 2000 active_time = 2000
[auth]
backends = ["csv"]

View file

@ -39,7 +39,6 @@ fn main() {
} }
#[cfg(feature = "auth")] #[cfg(feature = "auth")]
cli::Command::Auth(cli) => { cli::Command::Auth(cli) => {
let config = config.build().unwrap();
labadoor_auth::auth(&cli, config); labadoor_auth::auth(&cli, config);
} }
} }