Skip to content

Commit

Permalink
Allow to read the authentication password from a file
Browse files Browse the repository at this point in the history
ClementNerma committed Sep 19, 2024
1 parent d9fbb7d commit 3674410
Showing 4 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tapo-rest"
version = "0.1.1"
version = "0.1.2"
edition = "2021"

[dependencies]
12 changes: 11 additions & 1 deletion src/cmd.rs
Original file line number Diff line number Diff line change
@@ -16,6 +16,16 @@ pub struct ServerConfig {
#[clap(short, long, help = "Port to serve on")]
pub port: u16,

#[clap(flatten)]
pub password: PasswordArgGroup,
}

#[derive(Parser)]
#[group(required = true, multiple = false)]
pub struct PasswordArgGroup {
#[clap(short, long, help = "Login password")]
pub auth_password: String,
pub auth_password: Option<String>,

#[clap(short = 'f', long, help = "Read the login password from a file")]
pub password_from_file: Option<PathBuf>,
}
33 changes: 27 additions & 6 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{path::PathBuf, sync::Arc};
use std::{fs, path::PathBuf, sync::Arc};

use anyhow::Result;
use anyhow::{bail, Context, Result};
use axum::{
routing::{get, post},
Router,
@@ -9,7 +9,7 @@ use tokio::{net::TcpListener, sync::RwLock};
use tower_http::cors::{AllowHeaders, AllowMethods, AllowOrigin, CorsLayer};

use crate::{
cmd::ServerConfig,
cmd::{PasswordArgGroup, ServerConfig},
devices::TapoDevice,
server::{actions::make_router, state::StateInit},
};
@@ -34,10 +34,31 @@ pub async fn serve(
devices: Vec<TapoDevice>,
sessions_file: PathBuf,
) -> Result<()> {
let ServerConfig {
port,
let ServerConfig { port, password } = config;

let PasswordArgGroup {
auth_password,
} = config;
password_from_file,
} = password;

let auth_password = match (auth_password, password_from_file) {
(Some(auth_password), None) => auth_password,

(None, Some(file)) => {
if !file.is_file() {
bail!(
"Provided file password path does not exist: {}",
file.display()
);
}

fs::read_to_string(&file).with_context(|| {
format!("Failed to read file password at path: {}", file.display())
})?
}

(Some(_), Some(_)) | (None, None) => unreachable!(),
};

let cors = CorsLayer::new()
.allow_methods(AllowMethods::any())

0 comments on commit 3674410

Please sign in to comment.