diff options
| author | Yigit Sever | 2021-04-15 13:48:22 +0300 |
|---|---|---|
| committer | Yigit Sever | 2021-04-15 13:48:22 +0300 |
| commit | 343bf9ec1942de4d4453846681d178f2ca7f0790 (patch) | |
| tree | ca1eabdc9e67603f93af5cbecb089ce52792c2ad | |
| parent | 6ddfaebe3ed45edb01d8c188fc5449b366ddcf55 (diff) | |
| download | gradecoin-343bf9ec1942de4d4453846681d178f2ca7f0790.tar.gz gradecoin-343bf9ec1942de4d4453846681d178f2ca7f0790.tar.bz2 gradecoin-343bf9ec1942de4d4453846681d178f2ca7f0790.zip | |
Listen to clippy
| -rw-r--r-- | src/handlers.rs | 25 | ||||
| -rw-r--r-- | src/lib.rs | 4 | ||||
| -rw-r--r-- | src/routes.rs | 2 | ||||
| -rw-r--r-- | src/schema.rs | 15 |
4 files changed, 20 insertions, 26 deletions
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 @@ | |||
| 1 | use aes::Aes128; | 1 | use aes::Aes128; |
| 2 | /// API handlers, the ends of each filter chain | 2 | /// API handlers, the ends of each filter chain |
| 3 | use askama::Template; | 3 | use askama::Template; |
| 4 | use base64; | ||
| 5 | use blake2::{Blake2s, Digest}; | 4 | use blake2::{Blake2s, Digest}; |
| 6 | use block_modes::block_padding::Pkcs7; | 5 | use block_modes::block_padding::Pkcs7; |
| 7 | use block_modes::{BlockMode, Cbc}; | 6 | use block_modes::{BlockMode, Cbc}; |
| @@ -12,7 +11,6 @@ use md5::Md5; | |||
| 12 | use parking_lot::RwLockUpgradableReadGuard; | 11 | use parking_lot::RwLockUpgradableReadGuard; |
| 13 | use rsa::{PaddingScheme, RSAPrivateKey}; | 12 | use rsa::{PaddingScheme, RSAPrivateKey}; |
| 14 | use serde::Serialize; | 13 | use serde::Serialize; |
| 15 | use serde_json; | ||
| 16 | use sha2::Sha256; | 14 | use sha2::Sha256; |
| 17 | use std::collections::HashMap; | 15 | use std::collections::HashMap; |
| 18 | use std::convert::Infallible; | 16 | use std::convert::Infallible; |
| @@ -93,7 +91,7 @@ pub async fn authenticate_user( | |||
| 93 | // Load our RSA Private Key as DER | 91 | // Load our RSA Private Key as DER |
| 94 | let der_encoded = PRIVATE_KEY | 92 | let der_encoded = PRIVATE_KEY |
| 95 | .lines() | 93 | .lines() |
| 96 | .filter(|line| !line.starts_with("-")) | 94 | .filter(|line| !line.starts_with('-')) |
| 97 | .fold(String::new(), |mut data, line| { | 95 | .fold(String::new(), |mut data, line| { |
| 98 | data.push_str(&line); | 96 | data.push_str(&line); |
| 99 | data | 97 | data |
| @@ -147,18 +145,7 @@ pub async fn authenticate_user( | |||
| 147 | 145 | ||
| 148 | // We're using this as the validator | 146 | // We're using this as the validator |
| 149 | // I hate myself | 147 | // I hate myself |
| 150 | if let Err(_) = DecodingKey::from_rsa_pem(request.public_key.as_bytes()) { | 148 | if DecodingKey::from_rsa_pem(request.public_key.as_bytes()).is_err() { |
| 151 | let res_json = warp::reply::json(&GradeCoinResponse { | ||
| 152 | res: ResponseType::Error, | ||
| 153 | message: "The supplied RSA public key is not in valid PEM format".to_owned(), | ||
| 154 | }); | ||
| 155 | |||
| 156 | return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); | ||
| 157 | } | ||
| 158 | |||
| 159 | // We're using this as the validator | ||
| 160 | // I hate myself | ||
| 161 | if let Err(_) = DecodingKey::from_rsa_pem(request.public_key.as_bytes()) { | ||
| 162 | let res_json = warp::reply::json(&GradeCoinResponse { | 149 | let res_json = warp::reply::json(&GradeCoinResponse { |
| 163 | res: ResponseType::Error, | 150 | res: ResponseType::Error, |
| 164 | message: "The supplied RSA public key is not in valid PEM format".to_owned(), | 151 | message: "The supplied RSA public key is not in valid PEM format".to_owned(), |
| @@ -231,7 +218,7 @@ pub async fn authorized_propose_block( | |||
| 231 | 218 | ||
| 232 | println!("{:?}", &new_block); | 219 | println!("{:?}", &new_block); |
| 233 | 220 | ||
| 234 | if new_block.transaction_list.len() < 1 { | 221 | if new_block.transaction_list.is_empty() { |
| 235 | let res_json = warp::reply::json(&GradeCoinResponse { | 222 | let res_json = warp::reply::json(&GradeCoinResponse { |
| 236 | res: ResponseType::Error, | 223 | res: ResponseType::Error, |
| 237 | message: format!( | 224 | message: format!( |
| @@ -312,8 +299,8 @@ pub async fn authorized_propose_block( | |||
| 312 | 299 | ||
| 313 | let naked_block = NakedBlock { | 300 | let naked_block = NakedBlock { |
| 314 | transaction_list: new_block.transaction_list.clone(), | 301 | transaction_list: new_block.transaction_list.clone(), |
| 315 | nonce: new_block.nonce.clone(), | 302 | nonce: new_block.nonce, |
| 316 | timestamp: new_block.timestamp.clone(), | 303 | timestamp: new_block.timestamp, |
| 317 | }; | 304 | }; |
| 318 | 305 | ||
| 319 | let naked_block_flat = serde_json::to_vec(&naked_block).unwrap(); | 306 | let naked_block_flat = serde_json::to_vec(&naked_block).unwrap(); |
| @@ -546,7 +533,7 @@ pub async fn list_blocks(db: Db) -> Result<impl warp::Reply, Infallible> { | |||
| 546 | /// *[`jwt_token`]: The raw JWT token, "Bearer aaa.bbb.ccc" | 533 | /// *[`jwt_token`]: The raw JWT token, "Bearer aaa.bbb.ccc" |
| 547 | /// *[`user_pem`]: User Public Key, "BEGIN RSA" | 534 | /// *[`user_pem`]: User Public Key, "BEGIN RSA" |
| 548 | /// NOT async, might look into it if this becomes a bottleneck | 535 | /// NOT async, might look into it if this becomes a bottleneck |
| 549 | fn authorize_proposer(jwt_token: String, user_pem: &String) -> Result<TokenData<Claims>, String> { | 536 | fn authorize_proposer(jwt_token: String, user_pem: &str) -> Result<TokenData<Claims>, String> { |
| 550 | // Throw away the "Bearer " part | 537 | // Throw away the "Bearer " part |
| 551 | let raw_jwt = jwt_token.trim_start_matches(BEARER).to_owned(); | 538 | let raw_jwt = jwt_token.trim_start_matches(BEARER).to_owned(); |
| 552 | debug!("raw_jwt: {:?}", raw_jwt); | 539 | debug!("raw_jwt: {:?}", raw_jwt); |
| @@ -26,7 +26,7 @@ pub mod handlers; | |||
| 26 | pub mod routes; | 26 | pub mod routes; |
| 27 | pub mod schema; | 27 | pub mod schema; |
| 28 | 28 | ||
| 29 | pub const PRIVATE_KEY: &'static str = "-----BEGIN RSA PRIVATE KEY----- | 29 | pub const PRIVATE_KEY: &str = "-----BEGIN RSA PRIVATE KEY----- |
| 30 | MIIEogIBAAKCAQEAyGuqiCPGcguy+Y9TH7Bl7XlEsalyqb9bYlzpbV0dnqZ3lPkE | 30 | MIIEogIBAAKCAQEAyGuqiCPGcguy+Y9TH7Bl7XlEsalyqb9bYlzpbV0dnqZ3lPkE |
| 31 | PkuOhkN+GcuiV6iXtSwyh7nB+xTRXKJFRUBO/jbN8jfcxVwBu0JxjF3v1YRBxbOH | 31 | PkuOhkN+GcuiV6iXtSwyh7nB+xTRXKJFRUBO/jbN8jfcxVwBu0JxjF3v1YRBxbOH |
| 32 | hz2A295mbKD9xHQCKxkfYBNkUXxj8gd+GaDvQiSW5NdrX/lEkvqfGtdEX1m2+Hdc | 32 | hz2A295mbKD9xHQCKxkfYBNkUXxj8gd+GaDvQiSW5NdrX/lEkvqfGtdEX1m2+Hdc |
| @@ -54,7 +54,7 @@ PDYHM9dfQ8xn51U0fTeaXjy/8Km8fyX2Jtxntlm6puyhSTJ8AX+FEgJkC4ajNEvA | |||
| 54 | mJ1Gsy2fXKUyyZdI2b74MLqOpzr9cvS60tmTIScuiHFzg/SJgiA= | 54 | mJ1Gsy2fXKUyyZdI2b74MLqOpzr9cvS60tmTIScuiHFzg/SJgiA= |
| 55 | -----END RSA PRIVATE KEY-----"; | 55 | -----END RSA PRIVATE KEY-----"; |
| 56 | 56 | ||
| 57 | pub const PUB_KEY: &'static str = "-----BEGIN PUBLIC KEY----- | 57 | pub const PUB_KEY: &str = "-----BEGIN PUBLIC KEY----- |
| 58 | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyGuqiCPGcguy+Y9TH7Bl | 58 | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyGuqiCPGcguy+Y9TH7Bl |
| 59 | 7XlEsalyqb9bYlzpbV0dnqZ3lPkEPkuOhkN+GcuiV6iXtSwyh7nB+xTRXKJFRUBO | 59 | 7XlEsalyqb9bYlzpbV0dnqZ3lPkEPkuOhkN+GcuiV6iXtSwyh7nB+xTRXKJFRUBO |
| 60 | /jbN8jfcxVwBu0JxjF3v1YRBxbOHhz2A295mbKD9xHQCKxkfYBNkUXxj8gd+GaDv | 60 | /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<Extract = impl Reply, Error = Rej | |||
| 18 | .or(register_user(db.clone())) | 18 | .or(register_user(db.clone())) |
| 19 | .or(auth_transaction_propose(db.clone())) | 19 | .or(auth_transaction_propose(db.clone())) |
| 20 | .or(auth_block_propose(db.clone())) | 20 | .or(auth_block_propose(db.clone())) |
| 21 | .or(block_list(db.clone())) | 21 | .or(block_list(db)) |
| 22 | .or(static_route) | 22 | .or(static_route) |
| 23 | } | 23 | } |
| 24 | 24 | ||
diff --git a/src/schema.rs b/src/schema.rs index 33dc301..6f2f1f3 100644 --- a/src/schema.rs +++ b/src/schema.rs | |||
| @@ -50,7 +50,8 @@ fn create_db_with_last_block(path: String) -> Db { | |||
| 50 | let block: Block = serde_json::from_str(json).unwrap(); | 50 | let block: Block = serde_json::from_str(json).unwrap(); |
| 51 | let db = Db::new(); | 51 | let db = Db::new(); |
| 52 | *db.blockchain.write() = block; | 52 | *db.blockchain.write() = block; |
| 53 | return db; | 53 | |
| 54 | db | ||
| 54 | } | 55 | } |
| 55 | 56 | ||
| 56 | /// Creates a new database, uses the previous last block if one exists | 57 | /// Creates a new database, uses the previous last block if one exists |
| @@ -59,9 +60,9 @@ pub fn create_database() -> Db { | |||
| 59 | fs::create_dir_all("users").unwrap(); | 60 | fs::create_dir_all("users").unwrap(); |
| 60 | let (res, path) = last_block_exists(); | 61 | let (res, path) = last_block_exists(); |
| 61 | if res { | 62 | if res { |
| 62 | return create_db_with_last_block(path); | 63 | create_db_with_last_block(path) |
| 63 | } else { | 64 | } else { |
| 64 | return Db::new(); | 65 | Db::new() |
| 65 | } | 66 | } |
| 66 | } | 67 | } |
| 67 | 68 | ||
| @@ -168,6 +169,12 @@ impl Block { | |||
| 168 | } | 169 | } |
| 169 | } | 170 | } |
| 170 | 171 | ||
| 172 | impl Default for Block { | ||
| 173 | fn default() -> Self { | ||
| 174 | Self::new() | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 171 | /// Simply a Student | 178 | /// Simply a Student |
| 172 | #[derive(Serialize, Deserialize, Debug, PartialEq)] | 179 | #[derive(Serialize, Deserialize, Debug, PartialEq)] |
| 173 | pub struct User { | 180 | pub struct User { |
| @@ -247,7 +254,7 @@ impl MetuId { | |||
| 247 | pub fn new(id: String, pwd: String) -> Option<Self> { | 254 | pub fn new(id: String, pwd: String) -> Option<Self> { |
| 248 | if OUR_STUDENTS.contains(&(&*id, &*pwd)) { | 255 | if OUR_STUDENTS.contains(&(&*id, &*pwd)) { |
| 249 | Some(MetuId { | 256 | Some(MetuId { |
| 250 | id: id, | 257 | id, |
| 251 | passwd: pwd, | 258 | passwd: pwd, |
| 252 | }) | 259 | }) |
| 253 | } else { | 260 | } else { |
