use full http lib + remove all http crate dependency

This commit is contained in:
landrigun 2023-02-15 16:42:02 +00:00
parent 75e7d63795
commit bfd539731d
10 changed files with 46 additions and 303 deletions

4
Cargo.lock generated
View File

@ -635,8 +635,8 @@ dependencies = [
[[package]]
name = "http"
version = "0.1.1"
source = "git+https://gitea.thegux.fr/rmanach/http#57dcb801e8919fd9acd19efa92c521d7551bc5b7"
version = "0.1.2"
source = "git+https://gitea.thegux.fr/rmanach/http#fb164ba137c3f1492f7452ac29d5d08afd30e87d"
dependencies = [
"json",
"lazy_static",

View File

@ -16,7 +16,7 @@ simple_logger = "4.0.0"
log = "0.4.17"
base64 = "0.13.1"
http = { git = "https://gitea.thegux.fr/rmanach/http", version = "0.1.1" }
http = { git = "https://gitea.thegux.fr/rmanach/http", version = "0.1.2" }
# useful for tests (embedded files should be delete in release ?)
#rust-embed="6.4.1"

View File

@ -1,93 +0,0 @@
use json;
use std::collections::HashMap;
const JSON_DELIMITER: &'static str = ",";
/// `HashMap` wrapper, represents the JSON response body
pub struct HTTPMessage {
message: HashMap<String, String>,
}
impl Default for HTTPMessage {
fn default() -> Self {
HTTPMessage {
message: HashMap::new(),
}
}
}
/// try to convert `HTTPMessage` in `json::JsonValue`
impl TryInto<json::JsonValue> for HTTPMessage {
type Error = String;
fn try_into(self) -> Result<json::JsonValue, Self::Error> {
let message = format!(r#"{{{}}}"#, self.build_json());
match json::parse(&message) {
Ok(r) => Ok(r),
Err(e) => Err(format!(
"unable to parse the HTTPMessage correctly: {}, err={}",
message, e
)),
}
}
}
impl HTTPMessage {
pub fn put(&mut self, key: &str, value: &str) {
self.message.insert(key.to_string(), value.to_string());
}
/// associated function to build an HTTPMessage error
pub fn error(message: &str) -> Option<json::JsonValue> {
let mut http_message = HTTPMessage::default();
http_message.put("error", message);
match message.try_into() {
Ok(m) => Some(m),
Err(e) => {
eprintln!(
"unable to parse the message: {} into JSON, err={}",
message, e
);
return None;
}
}
}
/// loops over all the HashMap keys, builds a JSON key value for each one and join them with `JSON_DELIMITER`
fn build_json(self) -> String {
let unstruct: Vec<String> = self
.message
.keys()
.map(|k| format!(r#""{}":{:?}"#, k, self.message.get(k).unwrap()))
.collect();
let joined = unstruct.join(JSON_DELIMITER);
joined
}
}
#[test]
fn test_message() {
let mut http_message = HTTPMessage::default();
http_message.put("email", "toto@toto.fr");
http_message.put("password", "tata");
let mut json_result: Result<json::JsonValue, String> = http_message.try_into();
assert!(json_result.is_ok());
let mut json = json_result.unwrap();
assert!(json.has_key("email"));
assert!(json.has_key("password"));
let empty_http_message = HTTPMessage::default();
json_result = empty_http_message.try_into();
assert!(json_result.is_ok());
json = json_result.unwrap();
assert_eq!("{}", json.dump().to_string());
let mut bad_http_message = HTTPMessage::default();
bad_http_message.put("\"", "");
json_result = bad_http_message.try_into();
assert!(json_result.is_err());
}

View File

@ -1,9 +0,0 @@
//! http module includes tools to parse an HTTP request and build and HTTP response
pub mod message;
pub mod response;
pub mod router;
pub use message::HTTPMessage;
pub use response::{HTTPResponse, HTTPStatusCode};
pub use router::ROUTER;

View File

@ -1,173 +0,0 @@
//! response handles the incoming request parsed `HTTPRequest`
//! it will build an HTTPResponse corresponding to the HTTP message specs. see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages
//! NOTE: only few parts of the specification has been implemented
use super::HTTPMessage;
use http::HTTPVersion;
use json;
#[derive(Debug, PartialEq, Clone)]
pub enum HTTPStatusCode {
Http200,
Http400,
Http403,
Http404,
Http500,
}
impl Into<String> for HTTPStatusCode {
fn into(self) -> String {
match self {
Self::Http200 => "200".to_string(),
Self::Http400 => "400".to_string(),
Self::Http404 => "404".to_string(),
Self::Http403 => "403".to_string(),
Self::Http500 => "500".to_string(),
}
}
}
pub struct HTTPStatusLine {
version: HTTPVersion,
status_code: HTTPStatusCode,
}
impl Default for HTTPStatusLine {
fn default() -> HTTPStatusLine {
HTTPStatusLine {
version: HTTPVersion::Http1_1,
status_code: HTTPStatusCode::Http400,
}
}
}
impl Into<String> for HTTPStatusLine {
fn into(self) -> String {
let version: String = self.version.into();
let status_code: String = self.status_code.into();
format! {"{} {}", version, status_code}
}
}
impl HTTPStatusLine {
pub fn set_status_code(&mut self, code: HTTPStatusCode) {
self.status_code = code;
}
#[allow(dead_code)]
pub fn get_status_code(&self) -> HTTPStatusCode {
self.status_code.clone()
}
}
/// represents an HTTP response (headers are not parsed)
/// NOTE: for simplicity, only JSON body are accepted
pub struct HTTPResponse {
pub status_line: HTTPStatusLine,
body: json::JsonValue,
}
impl Default for HTTPResponse {
fn default() -> Self {
HTTPResponse {
status_line: HTTPStatusLine::default(),
body: json::parse(r#"{"error": "the incoming request is not valid"}"#).unwrap(),
}
}
}
impl Into<String> for HTTPResponse {
fn into(self) -> String {
// move `self.body` into a new var
let b = self.body;
let body: String = json::stringify(b);
let status_line: String = self.status_line.into();
format!(
"{}\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
status_line,
body.len(),
body
)
}
}
impl HTTPResponse {
pub fn as_500(message: Option<json::JsonValue>) -> Self {
let mut response = Self::default();
response
.status_line
.set_status_code(HTTPStatusCode::Http500);
response.body = {
match message {
Some(m) => m,
None => json::parse(r#"{"error": "unexpected error occurred"}"#).unwrap(),
}
};
response
}
pub fn as_404() -> Self {
let mut response = Self::default();
response
.status_line
.set_status_code(HTTPStatusCode::Http404);
response.body = json::parse(r#"{"error": "the url requested does not exist"}"#).unwrap();
response
}
pub fn as_403() -> Self {
let mut response = HTTPResponse {
status_line: HTTPStatusLine::default(),
body: json::parse(r#"{"error": "invalid credentials"}"#).unwrap(),
};
response
.status_line
.set_status_code(HTTPStatusCode::Http403);
response
}
/// wrap the `Self::default()` associated func (not really clear)
pub fn as_400() -> Self {
Self::default()
}
pub fn as_200(message: Option<json::JsonValue>) -> Self {
let mut response = Self::default();
response
.status_line
.set_status_code(HTTPStatusCode::Http200);
response.body = {
match message {
Some(m) => m,
None => json::parse(r#"{"status": "ok"}"#).unwrap(),
}
};
response
}
/// builds an HTTP 200 response with the generated JWT
pub fn send_token(token: &str) -> Self {
let mut http_message = HTTPMessage::default();
http_message.put("token", token);
let message = {
match http_message.try_into() {
Ok(m) => m,
Err(_e) => json::parse(r#"{"token": "error.generation.token"}"#).unwrap(),
}
};
HTTPResponse::as_200(Some(message))
}
}

View File

@ -1,6 +1,6 @@
mod config;
mod http;
mod jwt;
mod router;
mod stores;
mod utils;
@ -12,7 +12,7 @@ use tokio::{
time::{timeout, Duration},
};
use crate::http::ROUTER;
use crate::router::ROUTER;
use config::Config;
#[derive(Parser)]

4
src/router/mod.rs Normal file
View File

@ -0,0 +1,4 @@
//! router module includes all the handlers to get and validate JWT
mod router;
pub use router::ROUTER;

View File

@ -2,10 +2,9 @@
//! it implements all the logic to build an `HTTPResponse`
use base64;
use http;
use json;
use http::{HTTPRequest, HTTPResponse, JSONMessage};
use json::JsonValue;
use super::{HTTPMessage, HTTPResponse};
use crate::config::Config;
use crate::jwt::JWTSigner;
use crate::stores::{FileStore, Store};
@ -15,7 +14,7 @@ const GET_ROUTE: &'static str = "/get/";
const VALIDATE_ROUTE: &'static str = "/validate/";
const PUBKEY_ROUTE: &'static str = "/pubkey/";
async fn handle_get(request: http::HTTPRequest<'_>, config: Config, method: &str) -> HTTPResponse {
async fn handle_get(request: HTTPRequest<'_>, config: Config, method: &str) -> HTTPResponse {
if method.trim().to_lowercase() != "post" {
return HTTPResponse::as_400();
}
@ -33,16 +32,16 @@ async fn handle_get(request: http::HTTPRequest<'_>, config: Config, method: &str
match JWTSigner::new(config).await {
Ok(s) => s,
Err(e) => {
let message = HTTPMessage::error(&e);
let message = JSONMessage::error(&e);
return HTTPResponse::as_500(message);
}
}
};
match jwt_signer.sign(credentials.unwrap().email) {
Ok(t) => HTTPResponse::send_token(&t),
Ok(t) => send_token(&t),
Err(e) => {
let message = HTTPMessage::error(&e);
let message = JSONMessage::error(&e);
return HTTPResponse::as_500(message);
}
}
@ -55,7 +54,7 @@ async fn handle_get(request: http::HTTPRequest<'_>, config: Config, method: &str
/// * expiration time
/// * signature
async fn handle_validate(
request: http::HTTPRequest<'_>,
request: HTTPRequest<'_>,
config: Config,
method: &str,
) -> HTTPResponse {
@ -67,7 +66,7 @@ async fn handle_validate(
match request.get_body_value("token") {
Some(t) => t,
None => {
let mut message = HTTPMessage::default();
let mut message = JSONMessage::default();
message.put("valid", "false");
message.put("reason", "no token provided in the request body");
let json = message.try_into().unwrap();
@ -81,14 +80,14 @@ async fn handle_validate(
match JWTSigner::new(config).await {
Ok(s) => s,
Err(e) => {
let message = HTTPMessage::error(&e);
let message = JSONMessage::error(&e);
let json = message.try_into().unwrap();
return HTTPResponse::as_500(Some(json));
}
}
};
let mut message = HTTPMessage::default();
let mut message = JSONMessage::default();
match jwt_signer.validate(&token) {
Ok(()) => {
message.put("valid", "true");
@ -99,13 +98,13 @@ async fn handle_validate(
}
}
let json: json::JsonValue = message.try_into().unwrap();
let json: JsonValue = message.try_into().unwrap();
HTTPResponse::as_200(Some(json))
}
/// returns the JWT public key in base64 encoded
async fn handle_public_key(
request: http::HTTPRequest<'_>,
request: HTTPRequest<'_>,
config: Config,
method: &str,
) -> HTTPResponse {
@ -117,7 +116,7 @@ async fn handle_public_key(
match JWTSigner::new(config).await {
Ok(s) => s,
Err(e) => {
let message = HTTPMessage::error(&e);
let message = JSONMessage::error(&e);
let json = message.try_into().unwrap();
return HTTPResponse::as_500(Some(json));
}
@ -126,7 +125,7 @@ async fn handle_public_key(
let public_key = jwt_signer.get_public_key();
let mut message = HTTPMessage::default();
let mut message = JSONMessage::default();
message.put("pubkey", &base64::encode(public_key));
let json = message.try_into().unwrap();
@ -138,7 +137,7 @@ pub struct Router;
impl Router {
/// routes the request to the corresponding handling method
pub async fn route(&self, request_str: &str, config: Config) -> HTTPResponse {
let request = http::HTTPRequest::from(request_str);
let request = HTTPRequest::from(request_str);
match request.get_target() {
GET_ROUTE => handle_get(request, config, "post").await,
VALIDATE_ROUTE => handle_validate(request, config, "post").await,
@ -148,12 +147,27 @@ impl Router {
}
}
/// send_token generates an HTTPResponse with the new token
pub fn send_token(token: &str) -> HTTPResponse {
let mut message = JSONMessage::default();
message.put("token", token);
let json = {
match message.try_into() {
Ok(m) => m,
Err(_e) => json::parse(r#"{"token": "error.generation.token"}"#).unwrap(),
}
};
HTTPResponse::as_200(Some(json))
}
// this MUST be used like a Singleton
pub const ROUTER: Router = Router {};
#[tokio::test]
async fn test_route() {
use super::HTTPStatusCode;
use http::HTTPStatusCode;
let router: &Router = &ROUTER;
let config: Config = Config::default();

View File

@ -22,7 +22,7 @@ do
exit 1
fi
if [ "$(cat response.txt | jq -r '.error')" != "invalid credentials" ]
if [ "$(cat response.txt | jq -r '.error')" != "url forbidden" ]
then
echo "bad data returned, expect : invalid credentials"
exit 1

View File

@ -78,7 +78,7 @@ class TestResponse(TestCase):
self.assertIsNotNone(resp.json(), "response data can't be empty")
self.assertEqual(
resp.json()["error"],
"the url requested does not exist",
"url not found",
"bad status returned",
)
@ -88,7 +88,7 @@ class TestResponse(TestCase):
self.assertIsNotNone(resp.json(), "response data must not be empty")
self.assertEqual(
resp.json()["error"],
"the incoming request is not valid",
"bad request",
"invalid error message returned",
)
@ -98,7 +98,7 @@ class TestResponse(TestCase):
self.assertIsNotNone(resp.json(), "response data must not be empty")
self.assertEqual(
resp.json()["error"],
"invalid credentials",
"url forbidden",
"invalid error message returned",
)
@ -110,7 +110,7 @@ class TestResponse(TestCase):
self.assertIsNotNone(resp.json(), "response data must not be empty")
self.assertEqual(
resp.json()["error"],
"the url requested does not exist",
"url not found",
"invalid error message returned",
)
@ -133,6 +133,6 @@ class TestResponse(TestCase):
self.assertIsNotNone(resp.json(), "response data must not be empty")
self.assertEqual(
resp.json()["error"],
"the incoming request is not valid",
"bad request",
"invalid error message returned",
)