Skip to content

Commit

Permalink
chore(server): remove api key auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Threated committed Jul 22, 2024
1 parent 90055fb commit b46d524
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 20 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ license = "Apache-2.0"

[dependencies]
axum = { version = "0.7", optional = true}
axum-extra = { version = "0.9", optional = true, features = ["typed-header"]}
http-serde = "2.1"
reqwest = { version = "0.12", features = ["rustls-tls", "stream"], default-features = false }
clap = { version = "4.5", features = ["derive", "env"] }
Expand All @@ -23,7 +22,7 @@ anyhow = "1"
base64 = "0.22"

[features]
server = ["dep:axum", "dep:axum-extra"]
server = ["dep:axum"]

[profile.release]
lto = true
Expand Down
4 changes: 0 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ pub enum Mode {
/// Address the server should bind to
#[clap(env, long, default_value = "0.0.0.0:8080")]
bind_addr: std::net::SocketAddr,

/// Api key required for uploading files
#[clap(env, long)]
api_key: String,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async fn main() -> ExitCode {
.map(Ok)
.boxed(),
#[cfg(feature = "server")]
Mode::Server { bind_addr, api_key } => server::serve(bind_addr, api_key).boxed(),
Mode::Server { bind_addr } => server::serve(bind_addr).boxed(),
};
let result = tokio::select! {
res = work => res,
Expand Down
17 changes: 4 additions & 13 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
use std::{net::SocketAddr, sync::Arc};
use std::net::SocketAddr;

use axum::{
extract::{Path, State}, http::{HeaderMap, StatusCode}, routing::post, Router
extract::Path, http::{HeaderMap, StatusCode}, routing::post, Router
};
use axum_extra::{headers::{authorization, Authorization}, TypedHeader};
use beam_lib::{AppId, MsgId, RawString, TaskRequest};
use tokio::net::TcpListener;

use crate::{FileMeta, BEAM_CLIENT, CONFIG};

pub async fn serve(addr: &SocketAddr, api_key: &str) -> anyhow::Result<()> {
pub async fn serve(addr: &SocketAddr) -> anyhow::Result<()> {
let app = Router::new()
.route("/send/:to", post(send_file))
.with_state(Arc::from(api_key));
.route("/send/:to", post(send_file));
axum::serve(TcpListener::bind(&addr).await? ,app.into_make_service())
.with_graceful_shutdown(async { tokio::signal::ctrl_c().await.unwrap() })
.await?;
Ok(())
}

type AppState = Arc<str>;

async fn send_file(
Path(other_proxy_name): Path<String>,
auth: TypedHeader<Authorization<authorization::Basic>>,
headers: HeaderMap,
State(api_key): State<AppState>,
req: String,
) -> Result<(), StatusCode> {
if auth.password() != api_key.as_ref() {
return Err(StatusCode::UNAUTHORIZED);
}
let to = AppId::new_unchecked(format!(
"{other_proxy_name}.{}",
CONFIG.beam_id.as_ref().splitn(3, '.').nth(2).expect("Invalid app id")
Expand Down

0 comments on commit b46d524

Please sign in to comment.