diff options
author | Yigit Sever | 2021-04-16 00:53:03 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-16 00:53:03 +0300 |
commit | a1af17aad7c1308fc714a60595bae07cc8bb8a9a (patch) | |
tree | 3734a317d02b6d0b365419ab15419e3afe88bc72 /src/handlers.rs | |
parent | 81bba2651c8b67c376a78f80664ca119f0272979 (diff) | |
download | gradecoin-a1af17aad7c1308fc714a60595bae07cc8bb8a9a.tar.gz gradecoin-a1af17aad7c1308fc714a60595bae07cc8bb8a9a.tar.bz2 gradecoin-a1af17aad7c1308fc714a60595bae07cc8bb8a9a.zip |
Load users from disk at start
Diffstat (limited to 'src/handlers.rs')
-rw-r--r-- | src/handlers.rs | 36 |
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; | |||
8 | use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation}; | 8 | use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation}; |
9 | use log::{debug, warn}; | 9 | use log::{debug, warn}; |
10 | use md5::Md5; | 10 | use md5::Md5; |
11 | use parking_lot::RwLockUpgradableReadGuard; | ||
12 | use rsa::{PaddingScheme, RSAPrivateKey}; | 11 | use rsa::{PaddingScheme, RSAPrivateKey}; |
13 | use serde::Serialize; | 12 | use serde::Serialize; |
14 | use sha2::Sha256; | 13 | use sha2::Sha256; |
@@ -38,6 +37,7 @@ enum ResponseType { | |||
38 | 37 | ||
39 | use crate::schema::{ | 38 | use 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 | ||
43 | const BEARER: &str = "Bearer "; | 43 | const 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 | ||