diff --git a/src/lib.rs b/src/lib.rs index 9ca4d0b..b5c2630 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ use serde; -use worker::*; +use worker::{kv::KvStore, *}; #[event(fetch)] pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result { @@ -16,8 +16,23 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result bool { + // Check if the token provided by the endpoint is valid. + match kv.get("authorized_tokens").cache_ttl(300).text().await.unwrap() { + Some(t) => t.split(",") + .any(|authorized_token| authorized_token == token), + None => false, +} + } + router - .get_async("/asterisk/:hostname", |_req, ctx| async move { + .get_async("/asterisk/:hostname/:token", |_req, ctx| async move { + if let Some(token) = ctx.param("token") { + if !check_endpoint_auth(token, ctx.kv("GONK_NODES")?).await { + return Response::error("Unauthorized", 401); + } + } + if let Some(hostname) = ctx.param("hostname") { let kv = ctx.kv("GONK_NODES")?; @@ -31,7 +46,12 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result