diff options
author | Yigit Sever | 2021-04-10 15:49:26 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-10 15:49:26 +0300 |
commit | a3bc90d6755acbe3670dfcf23f35acdaf9b00ffc (patch) | |
tree | 28b354bac254c1c92e8a13b8b2bf6f3d0dcde021 | |
parent | 38a928a47f94d9f456c9c53f5db4fcd19165e7ac (diff) | |
download | gradecoin-a3bc90d6755acbe3670dfcf23f35acdaf9b00ffc.tar.gz gradecoin-a3bc90d6755acbe3670dfcf23f35acdaf9b00ffc.tar.bz2 gradecoin-a3bc90d6755acbe3670dfcf23f35acdaf9b00ffc.zip |
%(date +'%Y%m%d')
-rw-r--r-- | src/auth.rs | 17 | ||||
-rw-r--r-- | src/handlers.rs | 4 | ||||
-rw-r--r-- | src/routes.rs | 20 | ||||
-rw-r--r-- | src/schema.rs | 2 |
4 files changed, 25 insertions, 18 deletions
diff --git a/src/auth.rs b/src/auth.rs index 03930f0..65d639b 100644 --- a/src/auth.rs +++ b/src/auth.rs | |||
@@ -66,18 +66,25 @@ struct Claims { | |||
66 | #[derive(Debug)] | 66 | #[derive(Debug)] |
67 | struct RateLimited; | 67 | struct RateLimited; |
68 | 68 | ||
69 | #[derive(Debug, Clone)] | ||
70 | pub struct Pubkey { | ||
71 | a: String, | ||
72 | } | ||
73 | |||
69 | impl Reject for RateLimited {} | 74 | impl Reject for RateLimited {} |
70 | 75 | ||
71 | pub fn with_auth( | 76 | pub fn with_auth( |
72 | db: Db, | 77 | db: Db, |
73 | t: Transaction, | 78 | t: Transaction, |
74 | ) -> impl Filter<Extract = (String,), Error = Rejection> + Clone { | 79 | ) -> impl Filter<Extract = (Pubkey,), Error = Rejection> + Clone { |
75 | headers_cloned() | 80 | headers_cloned() |
76 | .map(move |headers: HeaderMap<HeaderValue>| (db.clone(), headers)) | 81 | .map(move |headers: HeaderMap<HeaderValue>| (db.clone(), headers, t.source.clone())) |
77 | .and_then(authorize) | 82 | .and_then(authorize) |
78 | } | 83 | } |
79 | 84 | ||
80 | async fn authorize((db, headers): (Db, HeaderMap<HeaderValue>)) -> Result<String, Rejection> { | 85 | async fn authorize( |
86 | (db, headers, source): (Db, HeaderMap<HeaderValue>, String), | ||
87 | ) -> Result<Pubkey, Rejection> { | ||
81 | match jwt_from_header(&headers) { | 88 | match jwt_from_header(&headers) { |
82 | Ok(jwt) => { | 89 | Ok(jwt) => { |
83 | let decoded = decode::<Claims>( | 90 | let decoded = decode::<Claims>( |
@@ -89,7 +96,9 @@ async fn authorize((db, headers): (Db, HeaderMap<HeaderValue>)) -> Result<String | |||
89 | .map_err(|_| reject::custom(Error::JWTTokenError)) | 96 | .map_err(|_| reject::custom(Error::JWTTokenError)) |
90 | .unwrap(); | 97 | .unwrap(); |
91 | 98 | ||
92 | Ok(decoded.claims.puk) | 99 | Ok(Pubkey { |
100 | a: decoded.claims.puk, | ||
101 | }) | ||
93 | } | 102 | } |
94 | Err(e) => return Err(warp::reject::custom(RateLimited)), | 103 | Err(e) => return Err(warp::reject::custom(RateLimited)), |
95 | } | 104 | } |
diff --git a/src/handlers.rs b/src/handlers.rs index 8908bfc..0bcbd49 100644 --- a/src/handlers.rs +++ b/src/handlers.rs | |||
@@ -3,6 +3,8 @@ use log::debug; // this is more useful than debug! learn how to use this | |||
3 | use parking_lot::RwLockUpgradableReadGuard; | 3 | use parking_lot::RwLockUpgradableReadGuard; |
4 | use std::convert::Infallible; | 4 | use std::convert::Infallible; |
5 | use warp::{http::StatusCode, reply}; | 5 | use warp::{http::StatusCode, reply}; |
6 | use warp::reject::Rejection; | ||
7 | use crate::auth::Pubkey; | ||
6 | 8 | ||
7 | use crate::schema::{Block, Db, Transaction}; | 9 | use crate::schema::{Block, Db, Transaction}; |
8 | 10 | ||
@@ -61,7 +63,7 @@ pub async fn propose_transaction( | |||
61 | /// POST /transaction, authenticated | 63 | /// POST /transaction, authenticated |
62 | /// The transaction arrived in this method has been authored by the public key in the source | 64 | /// The transaction arrived in this method has been authored by the public key in the source |
63 | pub async fn propose_authenticated_transaction( | 65 | pub async fn propose_authenticated_transaction( |
64 | pubkey: String, | 66 | pubkey: Pubkey, |
65 | new_transaction: Transaction, | 67 | new_transaction: Transaction, |
66 | db: Db, | 68 | db: Db, |
67 | ) -> Result<impl warp::Reply, warp::Rejection> { | 69 | ) -> Result<impl warp::Reply, warp::Rejection> { |
diff --git a/src/routes.rs b/src/routes.rs index e2e068a..871fd9c 100644 --- a/src/routes.rs +++ b/src/routes.rs | |||
@@ -44,28 +44,24 @@ pub fn transaction_propose(db: Db) -> impl Filter<Extract = impl Reply, Error = | |||
44 | pub fn authenticated_transaction_propose( | 44 | pub fn authenticated_transaction_propose( |
45 | db: Db, | 45 | db: Db, |
46 | ) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { | 46 | ) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { |
47 | // // TODO: you might have to restore this | ||
48 | // // what we're trying to do is knowing which public key to use to decode the jwt in the | ||
49 | // // header of the request, we will either request it through a header (ugly, ugh) or get it | ||
50 | // // from json (then how do we chain these ugh) or we can just validate/check (move the | ||
51 | // // header/jwt logic to propose_transaction but that doesn't feel right either | ||
52 | // // good luck <10-04-21, yigit> // | ||
53 | |||
47 | warp::path("transaction") | 54 | warp::path("transaction") |
48 | .and(warp::path::end()) | 55 | .and(warp::path::end()) |
49 | .and(warp::post()) | 56 | .and(warp::post()) |
50 | .and(custom_filters::transaction_json_body()) // returns transaction | 57 | .and(custom_filters::transaction_json_body()) // returns transaction |
51 | .map(|t: Transaction| { | 58 | .map(|t: Transaction| { |
59 | // what do these do? | ||
52 | with_auth(db.clone(), t) | 60 | with_auth(db.clone(), t) |
53 | }) | 61 | }) |
54 | .and(custom_filters::transaction_json_body()) // returns transaction | 62 | .and(custom_filters::transaction_json_body()) // returns transaction |
55 | .and(custom_filters::with_db(db)) // wraps db | 63 | .and(custom_filters::with_db(db)) // wraps db |
56 | .and_then(handlers::propose_authenticated_transaction) // uses db, transaction and authenticated | 64 | .and_then(handlers::propose_authenticated_transaction) // uses db, transaction and authenticated |
57 | |||
58 | // .and(custom_filters::transaction_json_body()) | ||
59 | // // TODO: you might have to restore this | ||
60 | // // what we're trying to do is knowing which public key to use to decode the jwt in the | ||
61 | // // header of the request, we will either request it through a header (ugly, ugh) or get it | ||
62 | // // from json (then how do we chain these ugh) or we can just validate/check (move the | ||
63 | // // header/jwt logic to propose_transaction but that doesn't feel right either | ||
64 | // // good luck <10-04-21, yigit> // | ||
65 | // .map(|t: Transaction| with_auth(db.clone(), t)) | ||
66 | // .and(custom_filters::transaction_json_body()) | ||
67 | // .and(custom_filters::with_db(db)) | ||
68 | // .and_then(handlers::propose_transaction) | ||
69 | } | 65 | } |
70 | 66 | ||
71 | /// POST /block warp route | 67 | /// POST /block warp route |
diff --git a/src/schema.rs b/src/schema.rs index c4917ab..f680bbf 100644 --- a/src/schema.rs +++ b/src/schema.rs | |||
@@ -46,7 +46,7 @@ impl Db { | |||
46 | /// A transaction between `source` and `target` that moves `amount` Note: | 46 | /// A transaction between `source` and `target` that moves `amount` Note: |
47 | /// https://serde.rs/container-attrs.html might be valueable to normalize the serialize/deserialize | 47 | /// https://serde.rs/container-attrs.html might be valueable to normalize the serialize/deserialize |
48 | /// conventions as these will be hashed | 48 | /// conventions as these will be hashed |
49 | #[derive(Serialize, Deserialize, Debug)] | 49 | #[derive(Serialize, Deserialize, Debug, Clone)] |
50 | pub struct Transaction { | 50 | pub struct Transaction { |
51 | pub source: String, | 51 | pub source: String, |
52 | pub target: String, | 52 | pub target: String, |