aboutsummaryrefslogtreecommitdiffstats
path: root/src/handlers.rs
diff options
context:
space:
mode:
authorYigit Sever2021-04-15 13:48:22 +0300
committerYigit Sever2021-04-15 13:48:22 +0300
commitbff5cd1eb6a67456426f02d6730e0bf945fc67e3 (patch)
tree0d1ce4505db19b340373f84da75b6a7c5fd4b0b1 /src/handlers.rs
parent77b99f7d3a8747f562f2b8f1e8df551aafea1b28 (diff)
downloadgradecoin-bff5cd1eb6a67456426f02d6730e0bf945fc67e3.tar.gz
gradecoin-bff5cd1eb6a67456426f02d6730e0bf945fc67e3.tar.bz2
gradecoin-bff5cd1eb6a67456426f02d6730e0bf945fc67e3.zip
Listen to clippy
Diffstat (limited to 'src/handlers.rs')
-rw-r--r--src/handlers.rs25
1 files changed, 6 insertions, 19 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 @@
1use aes::Aes128; 1use aes::Aes128;
2/// API handlers, the ends of each filter chain 2/// API handlers, the ends of each filter chain
3use askama::Template; 3use askama::Template;
4use base64;
5use blake2::{Blake2s, Digest}; 4use blake2::{Blake2s, Digest};
6use block_modes::block_padding::Pkcs7; 5use block_modes::block_padding::Pkcs7;
7use block_modes::{BlockMode, Cbc}; 6use block_modes::{BlockMode, Cbc};
@@ -12,7 +11,6 @@ use md5::Md5;
12use parking_lot::RwLockUpgradableReadGuard; 11use parking_lot::RwLockUpgradableReadGuard;
13use rsa::{PaddingScheme, RSAPrivateKey}; 12use rsa::{PaddingScheme, RSAPrivateKey};
14use serde::Serialize; 13use serde::Serialize;
15use serde_json;
16use sha2::Sha256; 14use sha2::Sha256;
17use std::collections::HashMap; 15use std::collections::HashMap;
18use std::convert::Infallible; 16use 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
549fn authorize_proposer(jwt_token: String, user_pem: &String) -> Result<TokenData<Claims>, String> { 536fn 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);