diff options
Diffstat (limited to 'src/custom_filters.rs')
| -rw-r--r-- | src/custom_filters.rs | 18 | 
1 files changed, 13 insertions, 5 deletions
| diff --git a/src/custom_filters.rs b/src/custom_filters.rs index f93f572..dfdae04 100644 --- a/src/custom_filters.rs +++ b/src/custom_filters.rs | |||
| @@ -1,30 +1,38 @@ | |||
| 1 | use gradecoin::schema::{AuthRequest, Block, Db, Transaction}; | 1 | /// Functions that extracts Structs to be used in warp routines | 
| 2 | use crate::schema::{AuthRequest, Block, Db, Transaction}; | ||
| 2 | use std::convert::Infallible; | 3 | use std::convert::Infallible; | 
| 3 | use warp::{Filter, Rejection}; | 4 | use warp::{Filter, Rejection}; | 
| 4 | 5 | ||
| 5 | // Database context for routes | 6 | /// Wraps the database to be used in warp routes | 
| 6 | pub fn with_db(db: Db) -> impl Filter<Extract = (Db,), Error = Infallible> + Clone { | 7 | pub fn with_db(db: Db) -> impl Filter<Extract = (Db,), Error = Infallible> + Clone { | 
| 7 | warp::any().map(move || db.clone()) | 8 | warp::any().map(move || db.clone()) | 
| 8 | } | 9 | } | 
| 9 | 10 | ||
| 10 | // Accept only json encoded User body and reject big payloads | 11 | /// Extracts an `AuthRequest` JSON body from the request | 
| 12 | /// Accepts only JSON encoded `AuthRequest` body and rejects big payloads | ||
| 13 | /// | ||
| 11 | // 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> // | 
| 12 | pub fn auth_request_json_body() -> impl Filter<Extract = (AuthRequest,), Error = Rejection> + Clone | 15 | pub fn auth_request_json_body() -> impl Filter<Extract = (AuthRequest,), Error = Rejection> + Clone | 
| 13 | { | 16 | { | 
| 14 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) | 17 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) | 
| 15 | } | 18 | } | 
| 16 | 19 | ||
| 17 | // Accept only json encoded Transaction body and reject big payloads | 20 | /// Extracts an `Transaction` JSON body from the request | 
| 21 | /// Accepts only JSON encoded `Transaction` body and rejects big payloads | ||
| 18 | // TODO: find a good limit for this <11-04-21, yigit> // | 22 | // TODO: find a good limit for this <11-04-21, yigit> // | 
| 19 | pub fn transaction_json_body() -> impl Filter<Extract = (Transaction,), Error = Rejection> + Clone { | 23 | pub fn transaction_json_body() -> impl Filter<Extract = (Transaction,), Error = Rejection> + Clone { | 
| 20 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) | 24 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) | 
| 21 | } | 25 | } | 
| 22 | 26 | ||
| 27 | /// Extracts the value of the `Authorization` header field, hopefully a valid JWT | ||
| 28 | /// Used in Authorization for `Block` and `Transaction` proposals | ||
| 29 | /// Rejects the request if the Authorization header does not exist | ||
| 23 | pub fn auth_header() -> impl Filter<Extract = (String,), Error = Rejection> + Clone { | 30 | pub fn auth_header() -> impl Filter<Extract = (String,), Error = Rejection> + Clone { | 
| 24 | warp::header::header::<String>("Authorization") | 31 | warp::header::header::<String>("Authorization") | 
| 25 | } | 32 | } | 
| 26 | 33 | ||
| 27 | // Accept only json encoded Block body and reject big payloads | 34 | /// Extracts an `Block` JSON body from the request | 
| 35 | /// Accepts only JSON encoded `Block` body and rejects big payloads | ||
| 28 | // TODO: find a good limit for this <11-04-21, yigit> // | 36 | // TODO: find a good limit for this <11-04-21, yigit> // | 
| 29 | pub fn block_json_body() -> impl Filter<Extract = (Block,), Error = Rejection> + Clone { | 37 | pub fn block_json_body() -> impl Filter<Extract = (Block,), Error = Rejection> + Clone { | 
| 30 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) | 38 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) | 
