aboutsummaryrefslogtreecommitdiffstats
path: root/src/handlers.rs
diff options
context:
space:
mode:
authorYigit Sever2021-04-16 00:53:03 +0300
committerYigit Sever2021-04-16 00:53:03 +0300
commit72f8ae422eeb03ed87c7819af5d5e25758267b03 (patch)
tree94abedf3a7c06ca0f9537beedb328a2e5016b575 /src/handlers.rs
parent2f2d0deb0ab2dc5fb63eee7b209531c8686e702e (diff)
downloadgradecoin-72f8ae422eeb03ed87c7819af5d5e25758267b03.tar.gz
gradecoin-72f8ae422eeb03ed87c7819af5d5e25758267b03.tar.bz2
gradecoin-72f8ae422eeb03ed87c7819af5d5e25758267b03.zip
Load users from disk at start
Diffstat (limited to 'src/handlers.rs')
-rw-r--r--src/handlers.rs36
1 files changed, 23 insertions, 13 deletions
diff --git a/src/handlers.rs b/src/handlers.rs
index 515caa5..fe60ded 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -8,7 +8,6 @@ use jsonwebtoken::errors::ErrorKind;
8use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation}; 8use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation};
9use log::{debug, warn}; 9use log::{debug, warn};
10use md5::Md5; 10use md5::Md5;
11use parking_lot::RwLockUpgradableReadGuard;
12use rsa::{PaddingScheme, RSAPrivateKey}; 11use rsa::{PaddingScheme, RSAPrivateKey};
13use serde::Serialize; 12use serde::Serialize;
14use sha2::Sha256; 13use sha2::Sha256;
@@ -38,6 +37,7 @@ enum ResponseType {
38 37
39use crate::schema::{ 38use crate::schema::{
40 AuthRequest, Block, Claims, Db, InitialAuthRequest, MetuId, NakedBlock, Transaction, User, 39 AuthRequest, Block, Claims, Db, InitialAuthRequest, MetuId, NakedBlock, Transaction, User,
40 UserAtRest,
41}; 41};
42 42
43const BEARER: &str = "Bearer "; 43const BEARER: &str = "Bearer ";
@@ -238,17 +238,19 @@ pub async fn authenticate_user(
238 } 238 }
239 }; 239 };
240 240
241 let userlist = db.users.upgradable_read(); 241 {
242 let userlist = db.users.read();
242 243
243 if userlist.contains_key(&provided_id) { 244 if userlist.contains_key(&provided_id) {
244 let res_json = warp::reply::json(&GradeCoinResponse { 245 let res_json = warp::reply::json(&GradeCoinResponse {
245 res: ResponseType::Error, 246 res: ResponseType::Error,
246 message: 247 message:
247 "This user is already authenticated, do you think this is a mistake? Contact me" 248 "This user is already authenticated, do you think this is a mistake? Contact me"
248 .to_owned(), 249 .to_owned(),
249 }); 250 });
250 251
251 return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); 252 return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST));
253 }
252 } 254 }
253 255
254 // We're using this as the validator 256 // We're using this as the validator
@@ -270,11 +272,19 @@ pub async fn authenticate_user(
270 balance: 0, 272 balance: 0,
271 }; 273 };
272 274
273 let user_json = serde_json::to_string(&new_user).unwrap(); 275 let user_at_rest_json = serde_json::to_string(&UserAtRest {
276 user: User {
277 user_id: new_user.user_id.clone(),
278 public_key: new_user.public_key.clone(),
279 balance: 0,
280 },
281 fingerprint: fingerprint.clone(),
282 })
283 .unwrap();
274 284
275 fs::write(format!("users/{}.guy", new_user.user_id), user_json).unwrap(); 285 fs::write(format!("users/{}.guy", new_user.user_id), user_at_rest_json).unwrap();
276 286
277 let mut userlist = RwLockUpgradableReadGuard::upgrade(userlist); 287 let mut userlist = db.users.write();
278 288
279 userlist.insert(fingerprint.clone(), new_user); 289 userlist.insert(fingerprint.clone(), new_user);
280 290