making routes::is_open behavior a lot more clear

moved /join to be handled by the dynamic path handler
This commit is contained in:
shockrah 2020-08-11 19:43:05 -07:00
parent 32ee49ed08
commit 5d1b95bec6
3 changed files with 16 additions and 12 deletions

View File

@ -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<AuthReason, mysql_async::error::Error> {
// Start by Checking if the api key is in our keystore
if open_route(path) {
if routes::is_open(path) {
Ok(AuthReason::OpenAuth)
}
else {

View File

@ -46,7 +46,6 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response<Body>, 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<Body>, 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);

View File

@ -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/<name>|/dm/<name>
];
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<DynRoute> {
}
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;
}