diff options
author | Yigit Sever | 2021-04-10 16:56:00 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-10 16:56:00 +0300 |
commit | 001a9ac21c29af53d31e9710d12e37d565880207 (patch) | |
tree | 7e0bbbc0b828694950ffaff6f6b51f63a62f46c7 /src | |
parent | a3bc90d6755acbe3670dfcf23f35acdaf9b00ffc (diff) | |
download | gradecoin-001a9ac21c29af53d31e9710d12e37d565880207.tar.gz gradecoin-001a9ac21c29af53d31e9710d12e37d565880207.tar.bz2 gradecoin-001a9ac21c29af53d31e9710d12e37d565880207.zip |
giving up with style
Diffstat (limited to 'src')
-rw-r--r-- | src/auth.rs | 2 | ||||
-rw-r--r-- | src/custom_filters.rs | 2 | ||||
-rw-r--r-- | src/handlers.rs | 9 | ||||
-rw-r--r-- | src/routes.rs | 11 | ||||
-rw-r--r-- | src/schema.rs | 4 |
5 files changed, 14 insertions, 14 deletions
diff --git a/src/auth.rs b/src/auth.rs index 65d639b..51b2e6a 100644 --- a/src/auth.rs +++ b/src/auth.rs | |||
@@ -90,7 +90,7 @@ async fn authorize( | |||
90 | let decoded = decode::<Claims>( | 90 | let decoded = decode::<Claims>( |
91 | &jwt, | 91 | &jwt, |
92 | // TODO: what key are we using here? pass db/pw store here to get the claimant's public key <10-04-21, yigit> // | 92 | // TODO: what key are we using here? pass db/pw store here to get the claimant's public key <10-04-21, yigit> // |
93 | &DecodingKey::from_rsa_pem(PUBLIC_KEY_PEM.as_bytes()).unwrap(), | 93 | &DecodingKey::from_rsa_pem(db.users.read().get(&source).unwrap().pubkey.as_bytes()).unwrap(), |
94 | &Validation::new(Algorithm::HS512), | 94 | &Validation::new(Algorithm::HS512), |
95 | ) | 95 | ) |
96 | .map_err(|_| reject::custom(Error::JWTTokenError)) | 96 | .map_err(|_| reject::custom(Error::JWTTokenError)) |
diff --git a/src/custom_filters.rs b/src/custom_filters.rs index 8c36d02..1d65c69 100644 --- a/src/custom_filters.rs +++ b/src/custom_filters.rs | |||
@@ -12,7 +12,7 @@ pub fn with_db(db: Db) -> impl Filter<Extract = (Db,), Error = Infallible> + Clo | |||
12 | 12 | ||
13 | // Accept only json encoded Transaction body and reject big payloads | 13 | // Accept only json encoded Transaction body and reject big payloads |
14 | pub fn transaction_json_body() -> impl Filter<Extract = (Transaction,), Error = Rejection> + Clone { | 14 | pub fn transaction_json_body() -> impl Filter<Extract = (Transaction,), Error = Rejection> + Clone { |
15 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) | 15 | warp::body::content_length_limit(1024 * 32).and(warp::filters::body::json()) |
16 | } | 16 | } |
17 | 17 | ||
18 | // Accept only json encoded Transaction body and reject big payloads | 18 | // Accept only json encoded Transaction body and reject big payloads |
diff --git a/src/handlers.rs b/src/handlers.rs index 0bcbd49..a9c0315 100644 --- a/src/handlers.rs +++ b/src/handlers.rs | |||
@@ -1,10 +1,11 @@ | |||
1 | use crate::auth::Pubkey; | ||
1 | /// API handlers, the ends of each filter chain | 2 | /// API handlers, the ends of each filter chain |
2 | use log::debug; // this is more useful than debug! learn how to use this | 3 | use log::debug; // this is more useful than debug! learn how to use this |
3 | use parking_lot::RwLockUpgradableReadGuard; | 4 | use parking_lot::RwLockUpgradableReadGuard; |
4 | use std::convert::Infallible; | 5 | use std::convert::Infallible; |
5 | use warp::{http::StatusCode, reply}; | 6 | use warp::filters::BoxedFilter; |
6 | use warp::reject::Rejection; | 7 | use warp::reject::Rejection; |
7 | use crate::auth::Pubkey; | 8 | use warp::{http::StatusCode, reply}; |
8 | 9 | ||
9 | use crate::schema::{Block, Db, Transaction}; | 10 | use crate::schema::{Block, Db, Transaction}; |
10 | 11 | ||
@@ -63,11 +64,10 @@ pub async fn propose_transaction( | |||
63 | /// POST /transaction, authenticated | 64 | /// POST /transaction, authenticated |
64 | /// The transaction arrived in this method has been authored by the public key in the source | 65 | /// The transaction arrived in this method has been authored by the public key in the source |
65 | pub async fn propose_authenticated_transaction( | 66 | pub async fn propose_authenticated_transaction( |
66 | pubkey: Pubkey, | 67 | pubkey: BoxedFilter<(Pubkey,)>, |
67 | new_transaction: Transaction, | 68 | new_transaction: Transaction, |
68 | db: Db, | 69 | db: Db, |
69 | ) -> Result<impl warp::Reply, warp::Rejection> { | 70 | ) -> Result<impl warp::Reply, warp::Rejection> { |
70 | |||
71 | // auth logic | 71 | // auth logic |
72 | debug!("new transaction request {:?}", new_transaction); | 72 | debug!("new transaction request {:?}", new_transaction); |
73 | 73 | ||
@@ -79,7 +79,6 @@ pub async fn propose_authenticated_transaction( | |||
79 | Ok(StatusCode::CREATED) | 79 | Ok(StatusCode::CREATED) |
80 | } | 80 | } |
81 | 81 | ||
82 | |||
83 | /// POST /block | 82 | /// POST /block |
84 | /// Proposes a new block for the next round | 83 | /// Proposes a new block for the next round |
85 | /// Can reject the block | 84 | /// Can reject the block |
diff --git a/src/routes.rs b/src/routes.rs index 871fd9c..b389919 100644 --- a/src/routes.rs +++ b/src/routes.rs | |||
@@ -8,7 +8,8 @@ use crate::schema::{Db, Transaction}; | |||
8 | /// Root, all routes combined | 8 | /// Root, all routes combined |
9 | pub fn consensus_routes(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { | 9 | pub fn consensus_routes(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { |
10 | transaction_list(db.clone()) | 10 | transaction_list(db.clone()) |
11 | .or(transaction_propose(db.clone())) | 11 | // .or(transaction_propose(db.clone())) |
12 | .or(authenticated_transaction_propose(db.clone())) | ||
12 | .or(block_propose(db.clone())) | 13 | .or(block_propose(db.clone())) |
13 | .or(block_list(db.clone())) | 14 | .or(block_list(db.clone())) |
14 | } | 15 | } |
@@ -51,16 +52,16 @@ pub fn authenticated_transaction_propose( | |||
51 | // // header/jwt logic to propose_transaction but that doesn't feel right either | 52 | // // header/jwt logic to propose_transaction but that doesn't feel right either |
52 | // // good luck <10-04-21, yigit> // | 53 | // // good luck <10-04-21, yigit> // |
53 | 54 | ||
55 | let db1 = db.clone(); | ||
54 | warp::path("transaction") | 56 | warp::path("transaction") |
55 | .and(warp::path::end()) | 57 | .and(warp::path::end()) |
56 | .and(warp::post()) | 58 | .and(warp::post()) |
57 | .and(custom_filters::transaction_json_body()) // returns transaction | 59 | .and(custom_filters::transaction_json_body()) // returns transaction |
58 | .map(|t: Transaction| { | 60 | .map(move |t: Transaction| { |
59 | // what do these do? | 61 | with_auth(db1.clone(), t).boxed() |
60 | with_auth(db.clone(), t) | ||
61 | }) | 62 | }) |
62 | .and(custom_filters::transaction_json_body()) // returns transaction | 63 | .and(custom_filters::transaction_json_body()) // returns transaction |
63 | .and(custom_filters::with_db(db)) // wraps db | 64 | .and(custom_filters::with_db(db.clone())) // wraps db |
64 | .and_then(handlers::propose_authenticated_transaction) // uses db, transaction and authenticated | 65 | .and_then(handlers::propose_authenticated_transaction) // uses db, transaction and authenticated |
65 | } | 66 | } |
66 | 67 | ||
diff --git a/src/schema.rs b/src/schema.rs index f680bbf..46d49cd 100644 --- a/src/schema.rs +++ b/src/schema.rs | |||
@@ -72,8 +72,8 @@ pub struct Block { | |||
72 | 72 | ||
73 | #[derive(Serialize, Deserialize, Debug)] | 73 | #[derive(Serialize, Deserialize, Debug)] |
74 | pub struct User { | 74 | pub struct User { |
75 | username: String, | 75 | pub username: String, |
76 | token: String | 76 | pub pubkey: String |
77 | } | 77 | } |
78 | 78 | ||
79 | // TODO: write schema tests using the original repo <09-04-21, yigit> // | 79 | // TODO: write schema tests using the original repo <09-04-21, yigit> // |