From 5d1b95bec6521014b288d7b49cf31948893382f9 Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 11 Aug 2020 19:43:05 -0700 Subject: [PATCH] making routes::is_open behavior a lot more clear moved /join to be handled by the dynamic path handler --- server/src/auth.rs | 6 +----- server/src/main.rs | 11 +++++++---- server/src/routes.rs | 11 ++++++++--- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/server/src/auth.rs b/server/src/auth.rs index 4a5fd4d..85fb60a 100644 --- a/server/src/auth.rs +++ b/server/src/auth.rs @@ -15,10 +15,6 @@ pub enum AuthReason { } -fn open_route(path: &str) -> bool { - return path == routes::INVITE_JOIN -} - fn valid_user(secret: &str, row: &Option<(VarChar, VarChar, BigInt, Integer, UBigInt)>) -> bool { match row { Some(row) => { @@ -33,7 +29,7 @@ fn valid_user(secret: &str, row: &Option<(VarChar, VarChar, BigInt, Integer, UBi pub async fn wall_entry(path: &str, pool: &Pool, params: &mut serde_json::Value) -> Result { // Start by Checking if the api key is in our keystore - if open_route(path) { + if routes::is_open(path) { Ok(AuthReason::OpenAuth) } else { diff --git a/server/src/main.rs b/server/src/main.rs index 568262a..3cb548a 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -46,7 +46,6 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response, meth: &Method, use routes::resolve_dynamic_route; match (meth, path) { /* INVITES */ - (&Method::GET, routes::INVITE_JOIN) => invites::join(pool, resp, params).await, (&Method::GET, routes::INVITE_CREATE) => invites::create(pool, resp, params).await, /* CHANNELS */ (&Method::GET, routes::CHANNELS_LIST) => channels::list_channels(pool, resp).await, @@ -63,9 +62,13 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response, meth: &Method, // Computatinoal bounds are really of no concern with this api since // we're not doing any heavy calculations at any point if let Some(route) = resolve_dynamic_route(path) { - *resp.status_mut() = StatusCode::OK; - println!("\tStatic part: {}", route.base); - println!("\tDynamic part: {}", route.dynamic); + match (meth, route.base.as_str()) { + (&Method::GET, routes::DYN_JOIN) => invites::join(pool, resp, params).await, + _ => { + println!("\tNOT FOUND: {}: {}", meth, path); + *resp.status_mut() = StatusCode::NOT_FOUND + } + } } else { println!("\tNOT FOUND: {}: {}", meth, path); diff --git a/server/src/routes.rs b/server/src/routes.rs index b27b106..391bffb 100644 --- a/server/src/routes.rs +++ b/server/src/routes.rs @@ -9,11 +9,12 @@ pub const MESSAGE_SEND: &'static str = "/message/send"; // requires @content pub const SERVER_META: &'static str = "/meta"; // open // potentially adding more bases later -const DYNAMIC_ROUTE_BASES: [(&'static str, bool);3] = [ +pub const DYNAMIC_ROUTE_BASES: [(&'static str, bool);3] = [ ("/join", true), // open ("/public", true), // open : valid sections := neighbors|rules|description ("/user", false), // TODO: valid sections := /meta/|/dm/ ]; +pub const DYN_JOIN: &'static str = DYNAMIC_ROUTE_BASES[0].0; pub struct DynRoute { pub base: String, @@ -43,8 +44,11 @@ pub fn resolve_dynamic_route(uri: &str) -> Option { } pub fn is_open(path: &str) -> bool { - let mut ret = false; - ret = path == SERVER_META; + /* + * Simple interface for determining if a route/base is open + * i.e. requires authentication or not + */ + let mut ret = path == SERVER_META; for route in DYNAMIC_ROUTE_BASES.iter() { if route.1 == true || ret == true{ ret = true; @@ -53,3 +57,4 @@ pub fn is_open(path: &str) -> bool { } return ret; } +