diff options
Diffstat (limited to 'src/handlers.rs')
-rw-r--r-- | src/handlers.rs | 25 |
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 @@ | |||
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); |