aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock41
-rw-r--r--Cargo.toml1
-rw-r--r--src/db.rs14
-rw-r--r--src/handlers.rs9
-rw-r--r--src/student.rs7
5 files changed, 67 insertions, 5 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 8228d5f..5a8a201 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -194,6 +194,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
194checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" 194checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae"
195 195
196[[package]] 196[[package]]
197name = "bstr"
198version = "0.2.15"
199source = "registry+https://github.com/rust-lang/crates.io-index"
200checksum = "a40b47ad93e1a5404e6c18dec46b628214fee441c70f4ab5d6942142cc268a3d"
201dependencies = [
202 "lazy_static",
203 "memchr",
204 "regex-automata",
205 "serde",
206]
207
208[[package]]
197name = "buf_redux" 209name = "buf_redux"
198version = "0.8.4" 210version = "0.8.4"
199source = "registry+https://github.com/rust-lang/crates.io-index" 211source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -294,6 +306,28 @@ dependencies = [
294] 306]
295 307
296[[package]] 308[[package]]
309name = "csv"
310version = "1.1.6"
311source = "registry+https://github.com/rust-lang/crates.io-index"
312checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1"
313dependencies = [
314 "bstr",
315 "csv-core",
316 "itoa",
317 "ryu",
318 "serde",
319]
320
321[[package]]
322name = "csv-core"
323version = "0.1.10"
324source = "registry+https://github.com/rust-lang/crates.io-index"
325checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
326dependencies = [
327 "memchr",
328]
329
330[[package]]
297name = "ctor" 331name = "ctor"
298version = "0.1.20" 332version = "0.1.20"
299source = "registry+https://github.com/rust-lang/crates.io-index" 333source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -477,6 +511,7 @@ dependencies = [
477 "blake2", 511 "blake2",
478 "block-modes", 512 "block-modes",
479 "chrono", 513 "chrono",
514 "csv",
480 "hex-literal", 515 "hex-literal",
481 "jsonwebtoken", 516 "jsonwebtoken",
482 "lazy_static", 517 "lazy_static",
@@ -1301,6 +1336,12 @@ dependencies = [
1301] 1336]
1302 1337
1303[[package]] 1338[[package]]
1339name = "regex-automata"
1340version = "0.1.10"
1341source = "registry+https://github.com/rust-lang/crates.io-index"
1342checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
1343
1344[[package]]
1304name = "regex-syntax" 1345name = "regex-syntax"
1305version = "0.6.23" 1346version = "0.6.23"
1306source = "registry+https://github.com/rust-lang/crates.io-index" 1347source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 77efd25..065cd84 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -27,6 +27,7 @@ sha2 = "0.9.3"
27block-modes = "0.7.0" 27block-modes = "0.7.0"
28aes = "0.6.0" 28aes = "0.6.0"
29askama = "0.10.5" 29askama = "0.10.5"
30csv = "1.1.6"
30 31
31[dev-dependencies] 32[dev-dependencies]
32serde_test = "1.0.117" 33serde_test = "1.0.117"
diff --git a/src/db.rs b/src/db.rs
index bf094ab..fd5c1be 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -19,6 +19,7 @@ pub struct Db {
19 pub blockchain: Arc<RwLock<Block>>, 19 pub blockchain: Arc<RwLock<Block>>,
20 pub pending_transactions: Arc<RwLock<HashMap<Id, Transaction>>>, 20 pub pending_transactions: Arc<RwLock<HashMap<Id, Transaction>>>,
21 pub users: Arc<RwLock<HashMap<Fingerprint, User>>>, 21 pub users: Arc<RwLock<HashMap<Fingerprint, User>>>,
22 approved_users: Vec<MetuId>,
22 // TODO: metu_ids or approved_users or something, metu_id struct <11-04-22, yigit> // 23 // TODO: metu_ids or approved_users or something, metu_id struct <11-04-22, yigit> //
23} 24}
24 25
@@ -36,11 +37,13 @@ impl Db {
36 } 37 }
37 38
38 let users: HashMap<Fingerprint, User> = get_friendly_users(); 39 let users: HashMap<Fingerprint, User> = get_friendly_users();
40 let approved_users = read_approved_users();
39 41
40 Db { 42 Db {
41 blockchain: Arc::new(RwLock::new(Block::default())), 43 blockchain: Arc::new(RwLock::new(Block::default())),
42 pending_transactions: Arc::new(RwLock::new(HashMap::new())), 44 pending_transactions: Arc::new(RwLock::new(HashMap::new())),
43 users: Arc::new(RwLock::new(users)), 45 users: Arc::new(RwLock::new(users)),
46 approved_users,
44 } 47 }
45 } 48 }
46 49
@@ -157,3 +160,14 @@ fn get_friendly_users() -> HashMap<Fingerprint, User> {
157 ); 160 );
158 users 161 users
159} 162}
163
164fn read_approved_users() -> Vec<MetuId> {
165 let mut approved_students: Vec<MetuId> = Vec::new();
166 let contents = fs::read_to_string("students.csv").unwrap();
167 let mut reader = csv::Reader::from_reader(contents.as_bytes());
168 for student in reader.records() {
169 let student = student.unwrap();
170 approved_students.push(MetuId::_new(student[0].to_owned(), student[1].to_owned()));
171 }
172 approved_students
173}
diff --git a/src/handlers.rs b/src/handlers.rs
index a64c012..96001ce 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -27,13 +27,13 @@ use warp::{http::StatusCode, reply};
27use crate::PRIVATE_KEY; 27use crate::PRIVATE_KEY;
28 28
29// Valid blocks should have this many transactions 29// Valid blocks should have this many transactions
30const BLOCK_TRANSACTION_COUNT: u8 = 8; 30const BLOCK_TRANSACTION_COUNT: u8 = 4;
31// Inital registration bonus 31// Inital registration bonus
32const REGISTER_BONUS: u16 = 40; 32const REGISTER_BONUS: u16 = 20;
33// Coinbase reward 33// Coinbase reward
34const BLOCK_REWARD: u16 = 3; 34const BLOCK_REWARD: u16 = 2;
35// Transaction amount limit 35// Transaction amount limit
36const TX_UPPER_LIMIT: u16 = 10; 36const TX_UPPER_LIMIT: u16 = 4;
37const TX_LOWER_LIMIT: u16 = 1; 37const TX_LOWER_LIMIT: u16 = 1;
38// Transaction traffic reward 38// Transaction traffic reward
39const TX_TRAFFIC_REWARD: u16 = 1; 39const TX_TRAFFIC_REWARD: u16 = 1;
@@ -278,6 +278,7 @@ pub async fn authenticate_user(
278 }; 278 };
279 279
280 // is the student in AuthRequest privileged? 280 // is the student in AuthRequest privileged?
281 // TODO: this is the only check for 'if metuid is approved' <15-04-22, yigit> //
281 let privileged_student_id = 282 let privileged_student_id =
282 if let Some(id) = MetuId::new(request.student_id.clone(), request.passwd.clone()) { 283 if let Some(id) = MetuId::new(request.student_id.clone(), request.passwd.clone()) {
283 id 284 id
diff --git a/src/student.rs b/src/student.rs
index 4b7acf1..711eeeb 100644
--- a/src/student.rs
+++ b/src/student.rs
@@ -26,6 +26,7 @@ pub struct User {
26} 26}
27 27
28/// The values are hard coded in [`static@OUR_STUDENTS`] so `MetuId::new`() can accept/reject values based on that 28/// The values are hard coded in [`static@OUR_STUDENTS`] so `MetuId::new`() can accept/reject values based on that
29/// TODO update the statement above
29#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] 30#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
30pub struct MetuId { 31pub struct MetuId {
31 id: String, 32 id: String,
@@ -46,10 +47,14 @@ impl MetuId {
46 None 47 None
47 } 48 }
48 } 49 }
50
51 // TODO: replace the function above with this <15-04-22, yigit> //
52 pub fn _new(id: String, passwd: String) -> Self {
53 MetuId { id, passwd }
54 }
49} 55}
50 56
51// TODO: remove this, read from a yaml or something, then MetuId::new gets a self <11-04-22, yigit> // 57// TODO: remove this, read from a yaml or something, then MetuId::new gets a self <11-04-22, yigit> //
52
53// Students who are authorized to have Gradecoin accounts 58// Students who are authorized to have Gradecoin accounts
54lazy_static! { 59lazy_static! {
55 static ref OUR_STUDENTS: HashSet<(&'static str, &'static str)> = { 60 static ref OUR_STUDENTS: HashSet<(&'static str, &'static str)> = {