diff options
author | Yigit Sever | 2021-04-14 03:27:27 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-14 19:11:49 +0300 |
commit | edfab6ae2f97a7288ff456265050c01ff397ea8c (patch) | |
tree | e98ce8b12c1ef4d61c70944f47d87d74297a8ed3 | |
parent | a5d5ab88d3f73d0b6f5fa847df6dace90810313d (diff) | |
download | gradecoin-edfab6ae2f97a7288ff456265050c01ff397ea8c.tar.gz gradecoin-edfab6ae2f97a7288ff456265050c01ff397ea8c.tar.bz2 gradecoin-edfab6ae2f97a7288ff456265050c01ff397ea8c.zip |
[WIP] Initial implementation of user auth
There is a dance involved and everything
Write down specs for RSA and AES, padding scheme, ugh.
-rw-r--r-- | Cargo.lock | 169 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | scripts/python_client.py | 57 | ||||
-rw-r--r-- | secrets/gradecoin.pem | 27 | ||||
-rw-r--r-- | secrets/gradecoin.pub | 9 | ||||
-rw-r--r-- | src/custom_filters.rs | 8 | ||||
-rw-r--r-- | src/handlers.rs | 34 | ||||
-rw-r--r-- | src/lib.rs | 40 | ||||
-rw-r--r-- | src/schema.rs | 9 |
9 files changed, 342 insertions, 14 deletions
@@ -22,6 +22,12 @@ dependencies = [ | |||
22 | 22 | ||
23 | [[package]] | 23 | [[package]] |
24 | name = "autocfg" | 24 | name = "autocfg" |
25 | version = "0.1.7" | ||
26 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
27 | checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" | ||
28 | |||
29 | [[package]] | ||
30 | name = "autocfg" | ||
25 | version = "1.0.1" | 31 | version = "1.0.1" |
26 | source = "registry+https://github.com/rust-lang/crates.io-index" | 32 | source = "registry+https://github.com/rust-lang/crates.io-index" |
27 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" | 33 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" |
@@ -313,6 +319,7 @@ dependencies = [ | |||
313 | name = "gradecoin" | 319 | name = "gradecoin" |
314 | version = "0.1.0" | 320 | version = "0.1.0" |
315 | dependencies = [ | 321 | dependencies = [ |
322 | "base64 0.13.0", | ||
316 | "blake2", | 323 | "blake2", |
317 | "chrono", | 324 | "chrono", |
318 | "hex-literal", | 325 | "hex-literal", |
@@ -322,9 +329,11 @@ dependencies = [ | |||
322 | "md-5", | 329 | "md-5", |
323 | "parking_lot", | 330 | "parking_lot", |
324 | "pretty_env_logger", | 331 | "pretty_env_logger", |
332 | "rsa", | ||
325 | "serde", | 333 | "serde", |
326 | "serde_json", | 334 | "serde_json", |
327 | "serde_test", | 335 | "serde_test", |
336 | "sha2", | ||
328 | "tokio", | 337 | "tokio", |
329 | "warp", | 338 | "warp", |
330 | ] | 339 | ] |
@@ -478,7 +487,7 @@ version = "1.6.2" | |||
478 | source = "registry+https://github.com/rust-lang/crates.io-index" | 487 | source = "registry+https://github.com/rust-lang/crates.io-index" |
479 | checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" | 488 | checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" |
480 | dependencies = [ | 489 | dependencies = [ |
481 | "autocfg", | 490 | "autocfg 1.0.1", |
482 | "hashbrown", | 491 | "hashbrown", |
483 | ] | 492 | ] |
484 | 493 | ||
@@ -526,7 +535,7 @@ dependencies = [ | |||
526 | "ring", | 535 | "ring", |
527 | "serde", | 536 | "serde", |
528 | "serde_json", | 537 | "serde_json", |
529 | "simple_asn1", | 538 | "simple_asn1 0.4.1", |
530 | ] | 539 | ] |
531 | 540 | ||
532 | [[package]] | 541 | [[package]] |
@@ -544,6 +553,9 @@ name = "lazy_static" | |||
544 | version = "1.4.0" | 553 | version = "1.4.0" |
545 | source = "registry+https://github.com/rust-lang/crates.io-index" | 554 | source = "registry+https://github.com/rust-lang/crates.io-index" |
546 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" | 555 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" |
556 | dependencies = [ | ||
557 | "spin", | ||
558 | ] | ||
547 | 559 | ||
548 | [[package]] | 560 | [[package]] |
549 | name = "libc" | 561 | name = "libc" |
@@ -552,6 +564,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
552 | checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41" | 564 | checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41" |
553 | 565 | ||
554 | [[package]] | 566 | [[package]] |
567 | name = "libm" | ||
568 | version = "0.2.1" | ||
569 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
570 | checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" | ||
571 | |||
572 | [[package]] | ||
555 | name = "lock_api" | 573 | name = "lock_api" |
556 | version = "0.3.4" | 574 | version = "0.3.4" |
557 | source = "registry+https://github.com/rust-lang/crates.io-index" | 575 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -674,18 +692,58 @@ version = "0.2.6" | |||
674 | source = "registry+https://github.com/rust-lang/crates.io-index" | 692 | source = "registry+https://github.com/rust-lang/crates.io-index" |
675 | checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304" | 693 | checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304" |
676 | dependencies = [ | 694 | dependencies = [ |
677 | "autocfg", | 695 | "autocfg 1.0.1", |
696 | "num-integer", | ||
697 | "num-traits", | ||
698 | ] | ||
699 | |||
700 | [[package]] | ||
701 | name = "num-bigint" | ||
702 | version = "0.3.2" | ||
703 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
704 | checksum = "7d0a3d5e207573f948a9e5376662aa743a2ea13f7c50a554d7af443a73fbfeba" | ||
705 | dependencies = [ | ||
706 | "autocfg 1.0.1", | ||
678 | "num-integer", | 707 | "num-integer", |
679 | "num-traits", | 708 | "num-traits", |
680 | ] | 709 | ] |
681 | 710 | ||
682 | [[package]] | 711 | [[package]] |
712 | name = "num-bigint-dig" | ||
713 | version = "0.7.0" | ||
714 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
715 | checksum = "4547ee5541c18742396ae2c895d0717d0f886d8823b8399cdaf7b07d63ad0480" | ||
716 | dependencies = [ | ||
717 | "autocfg 0.1.7", | ||
718 | "byteorder", | ||
719 | "lazy_static", | ||
720 | "libm", | ||
721 | "num-integer", | ||
722 | "num-iter", | ||
723 | "num-traits", | ||
724 | "rand 0.8.3", | ||
725 | "smallvec", | ||
726 | "zeroize", | ||
727 | ] | ||
728 | |||
729 | [[package]] | ||
683 | name = "num-integer" | 730 | name = "num-integer" |
684 | version = "0.1.44" | 731 | version = "0.1.44" |
685 | source = "registry+https://github.com/rust-lang/crates.io-index" | 732 | source = "registry+https://github.com/rust-lang/crates.io-index" |
686 | checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" | 733 | checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" |
687 | dependencies = [ | 734 | dependencies = [ |
688 | "autocfg", | 735 | "autocfg 1.0.1", |
736 | "num-traits", | ||
737 | ] | ||
738 | |||
739 | [[package]] | ||
740 | name = "num-iter" | ||
741 | version = "0.1.42" | ||
742 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
743 | checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59" | ||
744 | dependencies = [ | ||
745 | "autocfg 1.0.1", | ||
746 | "num-integer", | ||
689 | "num-traits", | 747 | "num-traits", |
690 | ] | 748 | ] |
691 | 749 | ||
@@ -695,7 +753,8 @@ version = "0.2.14" | |||
695 | source = "registry+https://github.com/rust-lang/crates.io-index" | 753 | source = "registry+https://github.com/rust-lang/crates.io-index" |
696 | checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" | 754 | checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" |
697 | dependencies = [ | 755 | dependencies = [ |
698 | "autocfg", | 756 | "autocfg 1.0.1", |
757 | "libm", | ||
699 | ] | 758 | ] |
700 | 759 | ||
701 | [[package]] | 760 | [[package]] |
@@ -988,6 +1047,26 @@ dependencies = [ | |||
988 | ] | 1047 | ] |
989 | 1048 | ||
990 | [[package]] | 1049 | [[package]] |
1050 | name = "rsa" | ||
1051 | version = "0.4.0" | ||
1052 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1053 | checksum = "68ef841a26fc5d040ced0417c6c6a64ee851f42489df11cdf0218e545b6f8d28" | ||
1054 | dependencies = [ | ||
1055 | "byteorder", | ||
1056 | "digest", | ||
1057 | "lazy_static", | ||
1058 | "num-bigint-dig", | ||
1059 | "num-integer", | ||
1060 | "num-iter", | ||
1061 | "num-traits", | ||
1062 | "pem", | ||
1063 | "rand 0.8.3", | ||
1064 | "simple_asn1 0.5.1", | ||
1065 | "subtle", | ||
1066 | "zeroize", | ||
1067 | ] | ||
1068 | |||
1069 | [[package]] | ||
991 | name = "ryu" | 1070 | name = "ryu" |
992 | version = "1.0.5" | 1071 | version = "1.0.5" |
993 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1072 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1077,14 +1156,39 @@ dependencies = [ | |||
1077 | ] | 1156 | ] |
1078 | 1157 | ||
1079 | [[package]] | 1158 | [[package]] |
1159 | name = "sha2" | ||
1160 | version = "0.9.3" | ||
1161 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1162 | checksum = "fa827a14b29ab7f44778d14a88d3cb76e949c45083f7dbfa507d0cb699dc12de" | ||
1163 | dependencies = [ | ||
1164 | "block-buffer", | ||
1165 | "cfg-if 1.0.0", | ||
1166 | "cpuid-bool", | ||
1167 | "digest", | ||
1168 | "opaque-debug", | ||
1169 | ] | ||
1170 | |||
1171 | [[package]] | ||
1080 | name = "simple_asn1" | 1172 | name = "simple_asn1" |
1081 | version = "0.4.1" | 1173 | version = "0.4.1" |
1082 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1174 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1083 | checksum = "692ca13de57ce0613a363c8c2f1de925adebc81b04c923ac60c5488bb44abe4b" | 1175 | checksum = "692ca13de57ce0613a363c8c2f1de925adebc81b04c923ac60c5488bb44abe4b" |
1084 | dependencies = [ | 1176 | dependencies = [ |
1085 | "chrono", | 1177 | "chrono", |
1086 | "num-bigint", | 1178 | "num-bigint 0.2.6", |
1179 | "num-traits", | ||
1180 | ] | ||
1181 | |||
1182 | [[package]] | ||
1183 | name = "simple_asn1" | ||
1184 | version = "0.5.1" | ||
1185 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1186 | checksum = "db8d597fce66eb0f19dd129b9956e4054cba21aeaf97d4116595027b670fac50" | ||
1187 | dependencies = [ | ||
1188 | "chrono", | ||
1189 | "num-bigint 0.3.2", | ||
1087 | "num-traits", | 1190 | "num-traits", |
1191 | "thiserror", | ||
1088 | ] | 1192 | ] |
1089 | 1193 | ||
1090 | [[package]] | 1194 | [[package]] |
@@ -1134,6 +1238,18 @@ dependencies = [ | |||
1134 | ] | 1238 | ] |
1135 | 1239 | ||
1136 | [[package]] | 1240 | [[package]] |
1241 | name = "synstructure" | ||
1242 | version = "0.12.4" | ||
1243 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1244 | checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" | ||
1245 | dependencies = [ | ||
1246 | "proc-macro2", | ||
1247 | "quote", | ||
1248 | "syn", | ||
1249 | "unicode-xid", | ||
1250 | ] | ||
1251 | |||
1252 | [[package]] | ||
1137 | name = "tempfile" | 1253 | name = "tempfile" |
1138 | version = "3.2.0" | 1254 | version = "3.2.0" |
1139 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1255 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1157,6 +1273,26 @@ dependencies = [ | |||
1157 | ] | 1273 | ] |
1158 | 1274 | ||
1159 | [[package]] | 1275 | [[package]] |
1276 | name = "thiserror" | ||
1277 | version = "1.0.24" | ||
1278 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1279 | checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e" | ||
1280 | dependencies = [ | ||
1281 | "thiserror-impl", | ||
1282 | ] | ||
1283 | |||
1284 | [[package]] | ||
1285 | name = "thiserror-impl" | ||
1286 | version = "1.0.24" | ||
1287 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1288 | checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" | ||
1289 | dependencies = [ | ||
1290 | "proc-macro2", | ||
1291 | "quote", | ||
1292 | "syn", | ||
1293 | ] | ||
1294 | |||
1295 | [[package]] | ||
1160 | name = "time" | 1296 | name = "time" |
1161 | version = "0.1.44" | 1297 | version = "0.1.44" |
1162 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1298 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1550,3 +1686,24 @@ dependencies = [ | |||
1550 | "winapi 0.2.8", | 1686 | "winapi 0.2.8", |
1551 | "winapi-build", | 1687 | "winapi-build", |
1552 | ] | 1688 | ] |
1689 | |||
1690 | [[package]] | ||
1691 | name = "zeroize" | ||
1692 | version = "1.2.0" | ||
1693 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1694 | checksum = "81a974bcdd357f0dca4d41677db03436324d45a4c9ed2d0b873a5a360ce41c36" | ||
1695 | dependencies = [ | ||
1696 | "zeroize_derive", | ||
1697 | ] | ||
1698 | |||
1699 | [[package]] | ||
1700 | name = "zeroize_derive" | ||
1701 | version = "1.0.1" | ||
1702 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1703 | checksum = "c3f369ddb18862aba61aa49bf31e74d29f0f162dec753063200e1dc084345d16" | ||
1704 | dependencies = [ | ||
1705 | "proc-macro2", | ||
1706 | "quote", | ||
1707 | "syn", | ||
1708 | "synstructure", | ||
1709 | ] | ||
@@ -20,6 +20,9 @@ blake2 = "0.9.1" | |||
20 | hex-literal = "0.3.1" | 20 | hex-literal = "0.3.1" |
21 | jsonwebtoken = "7.2.0" | 21 | jsonwebtoken = "7.2.0" |
22 | md-5 = "0.9.1" | 22 | md-5 = "0.9.1" |
23 | rsa = "0.4.0" | ||
24 | base64 = "0.13.0" | ||
25 | sha2 = "0.9.3" | ||
23 | 26 | ||
24 | [dev-dependencies] | 27 | [dev-dependencies] |
25 | serde_test = "1.0.117" | 28 | serde_test = "1.0.117" |
diff --git a/scripts/python_client.py b/scripts/python_client.py new file mode 100644 index 0000000..fe96cc2 --- /dev/null +++ b/scripts/python_client.py | |||
@@ -0,0 +1,57 @@ | |||
1 | from Crypto.PublicKey import RSA | ||
2 | import json | ||
3 | from Crypto.Cipher import PKCS1_OAEP | ||
4 | from Crypto.Signature import PKCS1_v1_5 | ||
5 | from Crypto.Hash import SHA512, SHA384, SHA256, SHA, MD5 | ||
6 | from Crypto import Random | ||
7 | from base64 import b64encode, b64decode | ||
8 | hash = "SHA-256" | ||
9 | |||
10 | # def newkeys(keysize): | ||
11 | # random_generator = Random.new().read | ||
12 | # key = RSA.generate(keysize, random_generator) | ||
13 | # private, public = key, key.publickey() | ||
14 | # return public, private | ||
15 | |||
16 | # def importKey(externKey): | ||
17 | # return RSA.importKey(externKey) | ||
18 | |||
19 | # def getpublickey(priv_key): | ||
20 | # return priv_key.publickey() | ||
21 | |||
22 | |||
23 | def encrypt(message, pub_key): | ||
24 | cipher = PKCS1_OAEP.new(pub_key) | ||
25 | return cipher.encrypt(message) | ||
26 | |||
27 | |||
28 | if __name__ == "__main__": | ||
29 | |||
30 | myself = { | ||
31 | "student_id": "e2482057", | ||
32 | "public_key": """-----BEGIN PUBLIC KEY----- | ||
33 | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3agASpH/TplIAX0YBqmh | ||
34 | 5q3Iq6+LcJtlUVWiI/v0T74XwYPZaJpAArHaiMUGXAWxmzfbvEo1wE9RzySYV/5k | ||
35 | QSpYDRekpOn0flIAQHORVbJ08s0udH6/c2AyAzqiwZbR1DRr7M90pSLvWvzHQT+c | ||
36 | kT6rXYcp9GlSAv3AXRw5ZYalbQf7ST/Mb4T8O1MRkAatzXg3T4x3XJ3uxHOletLL | ||
37 | SzsfY52kEn0uaFG6UI7UG50h8jcjqBxn+ETbn2YEZG5ecmPdYNakq2pqrdWXWMhE | ||
38 | AUd927qkxgg/nyyFqwxIbTxebxzpNX8IHMT8PgNdMxVMqnhBWxiw1nborY+pwGVL | ||
39 | MwIDAQAB | ||
40 | -----END PUBLIC KEY-----""" | ||
41 | } | ||
42 | with open("../secrets/gradecoin.pub", "r") as fs: | ||
43 | data = fs.read() | ||
44 | pubkeyobj = RSA.importKey(data) | ||
45 | |||
46 | cipher = PKCS1_OAEP.new(pubkeyobj) | ||
47 | |||
48 | ser = json.dumps(myself, separators=(',', ':')) | ||
49 | |||
50 | a = cipher.encrypt(ser) | ||
51 | |||
52 | print(f"{a}") | ||
53 | |||
54 | |||
55 | |||
56 | |||
57 | |||
diff --git a/secrets/gradecoin.pem b/secrets/gradecoin.pem new file mode 100644 index 0000000..d5ce1ef --- /dev/null +++ b/secrets/gradecoin.pem | |||
@@ -0,0 +1,27 @@ | |||
1 | -----BEGIN RSA PRIVATE KEY----- | ||
2 | MIIEogIBAAKCAQEAyGuqiCPGcguy+Y9TH7Bl7XlEsalyqb9bYlzpbV0dnqZ3lPkE | ||
3 | PkuOhkN+GcuiV6iXtSwyh7nB+xTRXKJFRUBO/jbN8jfcxVwBu0JxjF3v1YRBxbOH | ||
4 | hz2A295mbKD9xHQCKxkfYBNkUXxj8gd+GaDvQiSW5NdrX/lEkvqfGtdEX1m2+Hdc | ||
5 | G0+3YW24Xg0znhCwLr+sorLuJaDy9Xa0Uo+DPWGC5s001U/BxkCIWJ+eJQCb7Bv+ | ||
6 | 9vXb8BGRK/ecMb/fb6h5O+8fgB64RCHMgcc2v+Q/dPt8kHX1OJdMuYUrUJGACppM | ||
7 | QY3W6e1HdlRIBcZKL2LMZ2CrIB/2D5LiJhPThQIDAQABAoIBABbHrg1lS5QA4mnd | ||
8 | MYyDh0JTq0wqP18t4dwvRVTp5Yj30NW87A+MlPmLyFR0QdKG1h+Ak4m7wmGgfx9x | ||
9 | TkBNy+y3G/dxBAXmrEe1iKR0tOLm8nbfLgNgKTpUb/3e2pkuumRdqaRI7/kXE2Ea | ||
10 | Guoc0bUJ5aDDH3A8K+As3lK1rw7LNxwxZdmqmpO+EAldP6NaLnXNP5BegjLK50xP | ||
11 | NXTDNx6pw+I2ZHHwC/A6+QVksSA6zPipI1poANaO0frHffwKhcEZ/VucuXlJGGq/ | ||
12 | aqXT/cc7IkKUVq8EZUwUqHi4SrnyDDq/mtuikSD0MazxumbeC6fBKRP98Kavy2rT | ||
13 | JItHSYECgYEA8H/yC9GDrR1bwBesD0pKdKBy18UMFQF3BrB04OjqdGzugdVafF4e | ||
14 | 7azYQQTQ0ZddLDvgYl0QYvQaZfv26L7o4VrN5XEg8WjUWKuww8XUYOCfPn4gOFL1 | ||
15 | ar8nQ0w3P65gYf/rw0rFMo3eB78rJMROYnG8nZ/3OdgQjVaYPJxFKmECgYEA1VZy | ||
16 | EQz8dHK3+F0EfQIFeXOSlYGUegmPZ9iYmh+yvW/zWKLYdXBEHNhAIRlBmfe7Yhj6 | ||
17 | 1FNluNGjFqZYuRnP0RuiBxt2RCd+AL90Lqq+O6jem4XNgr3cOKoaV0FbaU49sI4s | ||
18 | /B6iiYBFdVuPBiknz+Wf1KEF9lQ+w2VYSLucY6UCgYAWPe73ste3sehjWo0aGOfL | ||
19 | 427bj6ivZKRKZRVaG5BbVhu0vDOTHu1DU+HoGXbqe1ItnhgBYNP8ItEyL1xFaCqH | ||
20 | dOtn1c+TI/vHe5FseaZLk1qG4AlAzENQLP+HlMvjQtA9H/sA47BbHY20L7TgwJrz | ||
21 | NcuY1Et7+QSG3cRUjqtC4QKBgGuP+VUVehfwW0dzBrdMlJwGpGqS+dyKA271awOS | ||
22 | ZdlTn5saCA82OnFcqwDFLilGGYk9VQJGxivoLtVVq7gwBnLE/u2ccAWu773KyfZZ | ||
23 | ii6kVxCM5vA7b9R2F2/U+RTgKQRiutWnUIYJUXv5XORbTcJpYSugwFPRaA+2gkux | ||
24 | pAktAoGABRyVs5LOhQ/oeXe2H2kvuaUq9c7f/dTtnyMNdNxK0uZcQn4jcB2eK9kB | ||
25 | PDYHM9dfQ8xn51U0fTeaXjy/8Km8fyX2Jtxntlm6puyhSTJ8AX+FEgJkC4ajNEvA | ||
26 | mJ1Gsy2fXKUyyZdI2b74MLqOpzr9cvS60tmTIScuiHFzg/SJgiA= | ||
27 | -----END RSA PRIVATE KEY----- | ||
diff --git a/secrets/gradecoin.pub b/secrets/gradecoin.pub new file mode 100644 index 0000000..ffe2f12 --- /dev/null +++ b/secrets/gradecoin.pub | |||
@@ -0,0 +1,9 @@ | |||
1 | -----BEGIN PUBLIC KEY----- | ||
2 | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyGuqiCPGcguy+Y9TH7Bl | ||
3 | 7XlEsalyqb9bYlzpbV0dnqZ3lPkEPkuOhkN+GcuiV6iXtSwyh7nB+xTRXKJFRUBO | ||
4 | /jbN8jfcxVwBu0JxjF3v1YRBxbOHhz2A295mbKD9xHQCKxkfYBNkUXxj8gd+GaDv | ||
5 | QiSW5NdrX/lEkvqfGtdEX1m2+HdcG0+3YW24Xg0znhCwLr+sorLuJaDy9Xa0Uo+D | ||
6 | PWGC5s001U/BxkCIWJ+eJQCb7Bv+9vXb8BGRK/ecMb/fb6h5O+8fgB64RCHMgcc2 | ||
7 | v+Q/dPt8kHX1OJdMuYUrUJGACppMQY3W6e1HdlRIBcZKL2LMZ2CrIB/2D5LiJhPT | ||
8 | hQIDAQAB | ||
9 | -----END PUBLIC KEY----- | ||
diff --git a/src/custom_filters.rs b/src/custom_filters.rs index ae8a56c..299cd8d 100644 --- a/src/custom_filters.rs +++ b/src/custom_filters.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | /// Functions that extracts Structs to be used in warp routines | 1 | /// Functions that extracts Structs to be used in warp routines |
2 | use crate::schema::{AuthRequest, Block, Db, Transaction}; | 2 | use crate::schema::{Block, Db, InitialAuthRequest, Transaction}; |
3 | use std::convert::Infallible; | 3 | use std::convert::Infallible; |
4 | use warp::{Filter, Rejection}; | 4 | use warp::{Filter, Rejection}; |
5 | 5 | ||
@@ -8,12 +8,12 @@ pub fn with_db(db: Db) -> impl Filter<Extract = (Db,), Error = Infallible> + Clo | |||
8 | warp::any().map(move || db.clone()) | 8 | warp::any().map(move || db.clone()) |
9 | } | 9 | } |
10 | 10 | ||
11 | /// Extracts an `AuthRequest` JSON body from the request | 11 | /// Extracts an `InitialAuthRequest` JSON body from the request |
12 | /// Accepts only JSON encoded `AuthRequest` body and rejects big payloads | 12 | /// Accepts only JSON encoded `AuthRequest` body and rejects big payloads |
13 | /// | 13 | /// |
14 | // TODO: find a good limit for this, (=e2482057; 8 char String + rsa pem) <11-04-21, yigit> // | 14 | // TODO: find a good limit for this, (=e2482057; 8 char String + rsa pem) <11-04-21, yigit> // |
15 | pub fn auth_request_json_body() -> impl Filter<Extract = (AuthRequest,), Error = Rejection> + Clone | 15 | pub fn auth_request_json_body( |
16 | { | 16 | ) -> impl Filter<Extract = (InitialAuthRequest,), Error = Rejection> + Clone { |
17 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) | 17 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) |
18 | } | 18 | } |
19 | 19 | ||
diff --git a/src/handlers.rs b/src/handlers.rs index b9df931..9d1bb10 100644 --- a/src/handlers.rs +++ b/src/handlers.rs | |||
@@ -1,3 +1,4 @@ | |||
1 | use base64; | ||
1 | /// API handlers, the ends of each filter chain | 2 | /// API handlers, the ends of each filter chain |
2 | use blake2::{Blake2s, Digest}; | 3 | use blake2::{Blake2s, Digest}; |
3 | use jsonwebtoken::errors::ErrorKind; | 4 | use jsonwebtoken::errors::ErrorKind; |
@@ -5,12 +6,16 @@ use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation}; | |||
5 | use log::{debug, warn}; | 6 | use log::{debug, warn}; |
6 | use md5::Md5; | 7 | use md5::Md5; |
7 | use parking_lot::RwLockUpgradableReadGuard; | 8 | use parking_lot::RwLockUpgradableReadGuard; |
9 | use rsa::{PaddingScheme, RSAPrivateKey}; | ||
8 | use serde::Serialize; | 10 | use serde::Serialize; |
9 | use serde_json; | 11 | use serde_json; |
12 | use sha2; | ||
10 | use std::convert::Infallible; | 13 | use std::convert::Infallible; |
11 | use std::fs; | 14 | use std::fs; |
12 | use warp::{http::StatusCode, reply}; | 15 | use warp::{http::StatusCode, reply}; |
13 | 16 | ||
17 | use crate::PRIVATE_KEY; | ||
18 | |||
14 | #[derive(Serialize, Debug)] | 19 | #[derive(Serialize, Debug)] |
15 | struct GradeCoinResponse { | 20 | struct GradeCoinResponse { |
16 | res: ResponseType, | 21 | res: ResponseType, |
@@ -23,7 +28,9 @@ enum ResponseType { | |||
23 | Error, | 28 | Error, |
24 | } | 29 | } |
25 | 30 | ||
26 | use crate::schema::{AuthRequest, Block, Claims, Db, MetuId, NakedBlock, Transaction, User}; | 31 | use crate::schema::{ |
32 | AuthRequest, Block, Claims, Db, InitialAuthRequest, MetuId, NakedBlock, Transaction, User, | ||
33 | }; | ||
27 | 34 | ||
28 | const BEARER: &str = "Bearer "; | 35 | const BEARER: &str = "Bearer "; |
29 | 36 | ||
@@ -32,11 +39,34 @@ const BEARER: &str = "Bearer "; | |||
32 | /// Lets a [`User`] (=student) to authenticate themselves to the system | 39 | /// Lets a [`User`] (=student) to authenticate themselves to the system |
33 | /// This `request` can be rejected if the payload is malformed (= not authenticated properly) or if | 40 | /// This `request` can be rejected if the payload is malformed (= not authenticated properly) or if |
34 | /// the [`AuthRequest.user_id`] of the `request` is not in the list of users that can hold a Gradecoin account | 41 | /// the [`AuthRequest.user_id`] of the `request` is not in the list of users that can hold a Gradecoin account |
42 | /// The request first comes in encrypted | ||
35 | pub async fn authenticate_user( | 43 | pub async fn authenticate_user( |
36 | request: AuthRequest, | 44 | request: InitialAuthRequest, |
37 | db: Db, | 45 | db: Db, |
38 | ) -> Result<impl warp::Reply, warp::Rejection> { | 46 | ) -> Result<impl warp::Reply, warp::Rejection> { |
39 | debug!("POST request to /register, authenticate_user"); | 47 | debug!("POST request to /register, authenticate_user"); |
48 | |||
49 | // TODO: lazyload or something <14-04-21, yigit> // | ||
50 | let der_encoded = PRIVATE_KEY | ||
51 | .lines() | ||
52 | .filter(|line| !line.starts_with("-")) | ||
53 | .fold(String::new(), |mut data, line| { | ||
54 | data.push_str(&line); | ||
55 | data | ||
56 | }); | ||
57 | let der_bytes = base64::decode(&der_encoded).expect("failed to decode base64 content"); | ||
58 | let private_key = RSAPrivateKey::from_pkcs1(&der_bytes).expect("failed to parse key"); | ||
59 | |||
60 | let padding = PaddingScheme::new_oaep::<sha2::Sha256>(); | ||
61 | let dec_key = private_key | ||
62 | .decrypt(padding, &request.key.as_bytes()) | ||
63 | .expect("failed to decrypt"); | ||
64 | |||
65 | // then decrypt c using key dec_key | ||
66 | |||
67 | // let request: AuthRequest = serde_json::from_str(&String::from_utf8(dec_data).unwrap()).unwrap(); | ||
68 | let request; | ||
69 | |||
40 | let provided_id = request.student_id.clone(); | 70 | let provided_id = request.student_id.clone(); |
41 | 71 | ||
42 | let priv_student_id = match MetuId::new(request.student_id, request.passwd) { | 72 | let priv_student_id = match MetuId::new(request.student_id, request.passwd) { |
@@ -22,7 +22,45 @@ | |||
22 | //! `Authorization`: The request header should have Bearer JWT.Token signed with Student Public Key | 22 | //! `Authorization`: The request header should have Bearer JWT.Token signed with Student Public Key |
23 | 23 | ||
24 | pub mod custom_filters; | 24 | pub mod custom_filters; |
25 | pub mod error; | ||
25 | pub mod handlers; | 26 | pub mod handlers; |
26 | pub mod routes; | 27 | pub mod routes; |
27 | pub mod schema; | 28 | pub mod schema; |
28 | pub mod error; | 29 | |
30 | pub const PRIVATE_KEY: &'static str = "-----BEGIN RSA PRIVATE KEY----- | ||
31 | MIIEogIBAAKCAQEAyGuqiCPGcguy+Y9TH7Bl7XlEsalyqb9bYlzpbV0dnqZ3lPkE | ||
32 | PkuOhkN+GcuiV6iXtSwyh7nB+xTRXKJFRUBO/jbN8jfcxVwBu0JxjF3v1YRBxbOH | ||
33 | hz2A295mbKD9xHQCKxkfYBNkUXxj8gd+GaDvQiSW5NdrX/lEkvqfGtdEX1m2+Hdc | ||
34 | G0+3YW24Xg0znhCwLr+sorLuJaDy9Xa0Uo+DPWGC5s001U/BxkCIWJ+eJQCb7Bv+ | ||
35 | 9vXb8BGRK/ecMb/fb6h5O+8fgB64RCHMgcc2v+Q/dPt8kHX1OJdMuYUrUJGACppM | ||
36 | QY3W6e1HdlRIBcZKL2LMZ2CrIB/2D5LiJhPThQIDAQABAoIBABbHrg1lS5QA4mnd | ||
37 | MYyDh0JTq0wqP18t4dwvRVTp5Yj30NW87A+MlPmLyFR0QdKG1h+Ak4m7wmGgfx9x | ||
38 | TkBNy+y3G/dxBAXmrEe1iKR0tOLm8nbfLgNgKTpUb/3e2pkuumRdqaRI7/kXE2Ea | ||
39 | Guoc0bUJ5aDDH3A8K+As3lK1rw7LNxwxZdmqmpO+EAldP6NaLnXNP5BegjLK50xP | ||
40 | NXTDNx6pw+I2ZHHwC/A6+QVksSA6zPipI1poANaO0frHffwKhcEZ/VucuXlJGGq/ | ||
41 | aqXT/cc7IkKUVq8EZUwUqHi4SrnyDDq/mtuikSD0MazxumbeC6fBKRP98Kavy2rT | ||
42 | JItHSYECgYEA8H/yC9GDrR1bwBesD0pKdKBy18UMFQF3BrB04OjqdGzugdVafF4e | ||
43 | 7azYQQTQ0ZddLDvgYl0QYvQaZfv26L7o4VrN5XEg8WjUWKuww8XUYOCfPn4gOFL1 | ||
44 | ar8nQ0w3P65gYf/rw0rFMo3eB78rJMROYnG8nZ/3OdgQjVaYPJxFKmECgYEA1VZy | ||
45 | EQz8dHK3+F0EfQIFeXOSlYGUegmPZ9iYmh+yvW/zWKLYdXBEHNhAIRlBmfe7Yhj6 | ||
46 | 1FNluNGjFqZYuRnP0RuiBxt2RCd+AL90Lqq+O6jem4XNgr3cOKoaV0FbaU49sI4s | ||
47 | /B6iiYBFdVuPBiknz+Wf1KEF9lQ+w2VYSLucY6UCgYAWPe73ste3sehjWo0aGOfL | ||
48 | 427bj6ivZKRKZRVaG5BbVhu0vDOTHu1DU+HoGXbqe1ItnhgBYNP8ItEyL1xFaCqH | ||
49 | dOtn1c+TI/vHe5FseaZLk1qG4AlAzENQLP+HlMvjQtA9H/sA47BbHY20L7TgwJrz | ||
50 | NcuY1Et7+QSG3cRUjqtC4QKBgGuP+VUVehfwW0dzBrdMlJwGpGqS+dyKA271awOS | ||
51 | ZdlTn5saCA82OnFcqwDFLilGGYk9VQJGxivoLtVVq7gwBnLE/u2ccAWu773KyfZZ | ||
52 | ii6kVxCM5vA7b9R2F2/U+RTgKQRiutWnUIYJUXv5XORbTcJpYSugwFPRaA+2gkux | ||
53 | pAktAoGABRyVs5LOhQ/oeXe2H2kvuaUq9c7f/dTtnyMNdNxK0uZcQn4jcB2eK9kB | ||
54 | PDYHM9dfQ8xn51U0fTeaXjy/8Km8fyX2Jtxntlm6puyhSTJ8AX+FEgJkC4ajNEvA | ||
55 | mJ1Gsy2fXKUyyZdI2b74MLqOpzr9cvS60tmTIScuiHFzg/SJgiA= | ||
56 | -----END RSA PRIVATE KEY-----"; | ||
57 | |||
58 | pub const PUB_KEY: &'static str = "-----BEGIN PUBLIC KEY----- | ||
59 | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyGuqiCPGcguy+Y9TH7Bl | ||
60 | 7XlEsalyqb9bYlzpbV0dnqZ3lPkEPkuOhkN+GcuiV6iXtSwyh7nB+xTRXKJFRUBO | ||
61 | /jbN8jfcxVwBu0JxjF3v1YRBxbOHhz2A295mbKD9xHQCKxkfYBNkUXxj8gd+GaDv | ||
62 | QiSW5NdrX/lEkvqfGtdEX1m2+HdcG0+3YW24Xg0znhCwLr+sorLuJaDy9Xa0Uo+D | ||
63 | PWGC5s001U/BxkCIWJ+eJQCb7Bv+9vXb8BGRK/ecMb/fb6h5O+8fgB64RCHMgcc2 | ||
64 | v+Q/dPt8kHX1OJdMuYUrUJGACppMQY3W6e1HdlRIBcZKL2LMZ2CrIB/2D5LiJhPT | ||
65 | hQIDAQAB | ||
66 | -----END PUBLIC KEY-----"; | ||
diff --git a/src/schema.rs b/src/schema.rs index 9e157c7..f159d83 100644 --- a/src/schema.rs +++ b/src/schema.rs | |||
@@ -178,7 +178,7 @@ pub struct MetuId { | |||
178 | passwd: String, | 178 | passwd: String, |
179 | } | 179 | } |
180 | 180 | ||
181 | // TODO: this will arrive encrypted <13-04-21, yigit> // | 181 | /// The plaintext of the initial user authentication request |
182 | #[derive(Serialize, Deserialize, Debug, PartialEq)] | 182 | #[derive(Serialize, Deserialize, Debug, PartialEq)] |
183 | pub struct AuthRequest { | 183 | pub struct AuthRequest { |
184 | pub student_id: String, | 184 | pub student_id: String, |
@@ -186,6 +186,13 @@ pub struct AuthRequest { | |||
186 | pub public_key: String, | 186 | pub public_key: String, |
187 | } | 187 | } |
188 | 188 | ||
189 | /// Ciphertext of the initial authentication request, or what we will receive | ||
190 | #[derive(Serialize, Deserialize, Debug)] | ||
191 | pub struct InitialAuthRequest { | ||
192 | pub c: String, | ||
193 | pub key: String, | ||
194 | } | ||
195 | |||
189 | lazy_static! { | 196 | lazy_static! { |
190 | static ref OUR_STUDENTS: HashSet<(&'static str, &'static str)> = { | 197 | static ref OUR_STUDENTS: HashSet<(&'static str, &'static str)> = { |
191 | [ | 198 | [ |