auth: Enable backends based on the configuration or --backends
This commit is contained in:
parent
eebb4ca762
commit
7a294ee146
4 changed files with 39 additions and 18 deletions
|
@ -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,
|
||||||
|
|
|
@ -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) => {
|
||||||
#[cfg(feature = "csv")]
|
$d.add_source(config::File::from_str(
|
||||||
cli::Backend::CSV => config.get::<cli::CSV>("csv").unwrap().to_config().new(),
|
&format!("[{}]\n{}", $section, toml::to_string($i).unwrap()),
|
||||||
};
|
config::FileFormat::Toml,
|
||||||
match &i.command {
|
))
|
||||||
cli::Command::Open(open) => acl.auth_user(
|
.build()
|
||||||
open.method.clone(),
|
.unwrap()
|
||||||
open.identifier.clone(),
|
|
||||||
open.resource.clone(),
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")]
|
||||||
|
cli::Backend::CSV => config.get::<cli::CSV>("csv").unwrap().to_config().new(),
|
||||||
|
};
|
||||||
|
match &cli.command {
|
||||||
|
cli::Command::Open(cliargs) => {
|
||||||
|
let found = acl.auth_user(
|
||||||
|
cliargs.method.clone(),
|
||||||
|
cliargs.identifier.clone(),
|
||||||
|
cliargs.resource.clone(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -11,3 +11,6 @@ device = ""
|
||||||
pin = 1
|
pin = 1
|
||||||
active_low = false
|
active_low = false
|
||||||
active_time = 2000
|
active_time = 2000
|
||||||
|
|
||||||
|
[auth]
|
||||||
|
backends = ["csv"]
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue