From 7f91412ddf3895214a8b56ac520ea7a60c0d24a1 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Thu, 15 Apr 2021 00:32:28 +0300 Subject: [WIP] Add template support --- src/routes.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/routes.rs') diff --git a/src/routes.rs b/src/routes.rs index 280de35..f8d9605 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -7,7 +7,10 @@ use crate::schema::Db; /// Every route combined pub fn consensus_routes(db: Db) -> impl Filter + Clone { - transaction_list(db.clone()) + let welcome_route = warp::path::end().and_then(handlers::welcome_handler); + + welcome_route + .or(transaction_list(db.clone())) .or(register_user(db.clone())) .or(auth_transaction_propose(db.clone())) .or(auth_block_propose(db.clone())) @@ -60,4 +63,3 @@ pub fn auth_block_propose(db: Db) -> impl Filter impl Filter + Clone { - let welcome_route = warp::path::end().and_then(handlers::welcome_handler); + // Remember when we wanted to implement templating + // Why would we? Just put a staic webpage under /public (next to Cargo.toml) and place it and + // the end of the filter chain - welcome_route - .or(transaction_list(db.clone())) + // Fully fledged website support, phew! + let static_route = warp::any().and(warp::fs::dir("public")); + + transaction_list(db.clone()) .or(register_user(db.clone())) .or(auth_transaction_propose(db.clone())) .or(auth_block_propose(db.clone())) .or(block_list(db.clone())) + .or(static_route) } /// POST /register warp route -- cgit v1.2.3-70-g09d2 From bff5cd1eb6a67456426f02d6730e0bf945fc67e3 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Thu, 15 Apr 2021 13:48:22 +0300 Subject: Listen to clippy --- src/handlers.rs | 25 ++++++------------------- src/lib.rs | 4 ++-- src/routes.rs | 2 +- src/schema.rs | 15 +++++++++++---- 4 files changed, 20 insertions(+), 26 deletions(-) (limited to 'src/routes.rs') diff --git a/src/handlers.rs b/src/handlers.rs index 5110bd5..848cb75 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,7 +1,6 @@ use aes::Aes128; /// API handlers, the ends of each filter chain use askama::Template; -use base64; use blake2::{Blake2s, Digest}; use block_modes::block_padding::Pkcs7; use block_modes::{BlockMode, Cbc}; @@ -12,7 +11,6 @@ use md5::Md5; use parking_lot::RwLockUpgradableReadGuard; use rsa::{PaddingScheme, RSAPrivateKey}; use serde::Serialize; -use serde_json; use sha2::Sha256; use std::collections::HashMap; use std::convert::Infallible; @@ -93,7 +91,7 @@ pub async fn authenticate_user( // Load our RSA Private Key as DER let der_encoded = PRIVATE_KEY .lines() - .filter(|line| !line.starts_with("-")) + .filter(|line| !line.starts_with('-')) .fold(String::new(), |mut data, line| { data.push_str(&line); data @@ -147,18 +145,7 @@ pub async fn authenticate_user( // We're using this as the validator // I hate myself - if let Err(_) = DecodingKey::from_rsa_pem(request.public_key.as_bytes()) { - let res_json = warp::reply::json(&GradeCoinResponse { - res: ResponseType::Error, - message: "The supplied RSA public key is not in valid PEM format".to_owned(), - }); - - return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); - } - - // We're using this as the validator - // I hate myself - if let Err(_) = DecodingKey::from_rsa_pem(request.public_key.as_bytes()) { + if DecodingKey::from_rsa_pem(request.public_key.as_bytes()).is_err() { let res_json = warp::reply::json(&GradeCoinResponse { res: ResponseType::Error, message: "The supplied RSA public key is not in valid PEM format".to_owned(), @@ -231,7 +218,7 @@ pub async fn authorized_propose_block( println!("{:?}", &new_block); - if new_block.transaction_list.len() < 1 { + if new_block.transaction_list.is_empty() { let res_json = warp::reply::json(&GradeCoinResponse { res: ResponseType::Error, message: format!( @@ -312,8 +299,8 @@ pub async fn authorized_propose_block( let naked_block = NakedBlock { transaction_list: new_block.transaction_list.clone(), - nonce: new_block.nonce.clone(), - timestamp: new_block.timestamp.clone(), + nonce: new_block.nonce, + timestamp: new_block.timestamp, }; let naked_block_flat = serde_json::to_vec(&naked_block).unwrap(); @@ -546,7 +533,7 @@ pub async fn list_blocks(db: Db) -> Result { /// *[`jwt_token`]: The raw JWT token, "Bearer aaa.bbb.ccc" /// *[`user_pem`]: User Public Key, "BEGIN RSA" /// NOT async, might look into it if this becomes a bottleneck -fn authorize_proposer(jwt_token: String, user_pem: &String) -> Result, String> { +fn authorize_proposer(jwt_token: String, user_pem: &str) -> Result, String> { // Throw away the "Bearer " part let raw_jwt = jwt_token.trim_start_matches(BEARER).to_owned(); debug!("raw_jwt: {:?}", raw_jwt); diff --git a/src/lib.rs b/src/lib.rs index 82fb51f..5442c6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ pub mod handlers; pub mod routes; pub mod schema; -pub const PRIVATE_KEY: &'static str = "-----BEGIN RSA PRIVATE KEY----- +pub const PRIVATE_KEY: &str = "-----BEGIN RSA PRIVATE KEY----- MIIEogIBAAKCAQEAyGuqiCPGcguy+Y9TH7Bl7XlEsalyqb9bYlzpbV0dnqZ3lPkE PkuOhkN+GcuiV6iXtSwyh7nB+xTRXKJFRUBO/jbN8jfcxVwBu0JxjF3v1YRBxbOH hz2A295mbKD9xHQCKxkfYBNkUXxj8gd+GaDvQiSW5NdrX/lEkvqfGtdEX1m2+Hdc @@ -54,7 +54,7 @@ PDYHM9dfQ8xn51U0fTeaXjy/8Km8fyX2Jtxntlm6puyhSTJ8AX+FEgJkC4ajNEvA mJ1Gsy2fXKUyyZdI2b74MLqOpzr9cvS60tmTIScuiHFzg/SJgiA= -----END RSA PRIVATE KEY-----"; -pub const PUB_KEY: &'static str = "-----BEGIN PUBLIC KEY----- +pub const PUB_KEY: &str = "-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyGuqiCPGcguy+Y9TH7Bl 7XlEsalyqb9bYlzpbV0dnqZ3lPkEPkuOhkN+GcuiV6iXtSwyh7nB+xTRXKJFRUBO /jbN8jfcxVwBu0JxjF3v1YRBxbOHhz2A295mbKD9xHQCKxkfYBNkUXxj8gd+GaDv diff --git a/src/routes.rs b/src/routes.rs index 59342bb..52d357a 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -18,7 +18,7 @@ pub fn consensus_routes(db: Db) -> impl Filter Db { let block: Block = serde_json::from_str(json).unwrap(); let db = Db::new(); *db.blockchain.write() = block; - return db; + + db } /// Creates a new database, uses the previous last block if one exists @@ -59,9 +60,9 @@ pub fn create_database() -> Db { fs::create_dir_all("users").unwrap(); let (res, path) = last_block_exists(); if res { - return create_db_with_last_block(path); + create_db_with_last_block(path) } else { - return Db::new(); + Db::new() } } @@ -168,6 +169,12 @@ impl Block { } } +impl Default for Block { + fn default() -> Self { + Self::new() + } +} + /// Simply a Student #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct User { @@ -247,7 +254,7 @@ impl MetuId { pub fn new(id: String, pwd: String) -> Option { if OUR_STUDENTS.contains(&(&*id, &*pwd)) { Some(MetuId { - id: id, + id, passwd: pwd, }) } else { -- cgit v1.2.3-70-g09d2