Exhaustive response to various authentication failures

response route_dispatcher now guaranteed to fire on proper authentication only
This commit is contained in:
shockrah 2020-06-02 00:47:28 -07:00
parent e2abc49087
commit 9033c80369

View File

@ -5,7 +5,7 @@ extern crate base64;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::convert::Infallible; // our main dispatcher basically never fails hence why we use this use std::convert::Infallible; // our main dispatcher basically never fails hence why we use this
use std::env::var; use std::env;
use std::collections::HashMap; use std::collections::HashMap;
use tokio; use tokio;
@ -13,12 +13,14 @@ use hyper::{
self, self,
Server, Server,
Response, Request, Body, Response, Request, Body,
Method, Method, StatusCode,
service::{make_service_fn, service_fn} service::{make_service_fn, service_fn}
}; };
use mysql_async::Conn;
use dotenv::dotenv; use dotenv::dotenv;
mod auth; mod auth;
use auth::AuthReason;
fn map_qs(query_string_raw: Option<&str>) -> HashMap<&str, &str> { fn map_qs(query_string_raw: Option<&str>) -> HashMap<&str, &str> {
/* /*
@ -38,7 +40,7 @@ fn map_qs(query_string_raw: Option<&str>) -> HashMap<&str, &str> {
map map
} }
async fn route_dispatcher(resp: &Response<Body>, meth: &Method, path: &str, params: &HashMap<&str, Option<&str>>) { async fn route_dispatcher(resp: &Response<Body>, meth: &Method, path: &str, params: &HashMap<&str, &str>) {
// we do have to handle some general hyper errors // we do have to handle some general hyper errors
unimplemented!() unimplemented!()
} }
@ -49,12 +51,17 @@ async fn main_responder(request: Request<Body>) -> Result<Response<Body>, hyper:
let path = request.uri().path(); let path = request.uri().path();
let params = map_qs(request.uri().query()); let params = map_qs(request.uri().query());
// go through our auth wall first if let Ok(conn) = Conn::from_url(env::var("DATABASE_URL").unwrap()).await {
if auth::wall_entry(&params).await { // some more information in the response would be great right about here
route_dispatcher(&response, &method, path, &params); match auth::wall_entry(conn, &params).await {
AuthReason::Good => route_dispatcher(&response, &method, path, &params).await,
AuthReason::LimitPassed => *response.status_mut() = StatusCode::UNAUTHORIZED,
AuthReason::NoKey => *response.status_mut() = StatusCode::UNAUTHORIZED,
AuthReason::InternalFailure => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
}
} }
else { else {
auth::wall_failure(&response); *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
} }
Ok(response) Ok(response)