aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/auth.rs25
-rw-r--r--src/handlers.rs4
-rw-r--r--src/routes.rs12
3 files changed, 26 insertions, 15 deletions
diff --git a/src/auth.rs b/src/auth.rs
index e22262c..95f2378 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -1,12 +1,14 @@
1use crate::error::Error; 1use crate::error::Error;
2use crate::schema::{Db, Transaction}; 2use crate::schema::{Db, Transaction};
3use anyhow::{anyhow, Context, Result};
3use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; 4use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
4use serde::{Deserialize, Serialize}; 5use serde::{Deserialize, Serialize};
6use thiserror::Error;
5use warp::header::headers_cloned; 7use warp::header::headers_cloned;
6use warp::http::header::{HeaderMap, HeaderValue, AUTHORIZATION}; 8use warp::http::header::{HeaderMap, HeaderValue, AUTHORIZATION};
7use warp::{reject, Filter, Rejection}; 9use warp::reject;
8use thiserror::Error; 10use warp::reject::Reject;
9use anyhow::*; 11use warp::{Filter, Rejection};
10 12
11const BEARER: &str = "Bearer "; 13const BEARER: &str = "Bearer ";
12const PUBLIC_KEY_PEM: &str = "-----BEGIN PUBLIC KEY----- 14const PUBLIC_KEY_PEM: &str = "-----BEGIN PUBLIC KEY-----
@@ -65,6 +67,18 @@ struct Claims {
65// found: String, 67// found: String,
66// }, 68// },
67// } 69// }
70// impl warp::reject::Reject for Nope {}
71
72#[derive(Debug)]
73struct LessThanTenError {}
74
75impl Reject for LessThanTenError {}
76
77// impl From<LessThanTenError> for Rejection {
78// fn from(other: LessThanTenError) -> Self {
79// warp::reject::custom(other)
80// }
81// }
68 82
69pub fn with_auth( 83pub fn with_auth(
70 db: Db, 84 db: Db,
@@ -75,8 +89,6 @@ pub fn with_auth(
75 .and_then(authorize) 89 .and_then(authorize)
76} 90}
77 91
78impl warp::reject::Reject for Nope {}
79
80async fn authorize((db, headers): (Db, HeaderMap<HeaderValue>)) -> Result<String, Error> { 92async fn authorize((db, headers): (Db, HeaderMap<HeaderValue>)) -> Result<String, Error> {
81 match jwt_from_header(&headers) { 93 match jwt_from_header(&headers) {
82 Ok(jwt) => { 94 Ok(jwt) => {
@@ -92,7 +104,8 @@ async fn authorize((db, headers): (Db, HeaderMap<HeaderValue>)) -> Result<String
92 104
93 Ok(decoded.claims.puk) 105 Ok(decoded.claims.puk)
94 } 106 }
95 Err(e) => return Err(anyhow!("missing!")); 107 Err(e) => return Err(anyhow!("missing!")),
108 // warp error
96 } 109 }
97} 110}
98 111
diff --git a/src/handlers.rs b/src/handlers.rs
index 256e72a..89905a3 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -61,10 +61,12 @@ pub async fn propose_transaction(
61/// POST /transaction, authenticated 61/// POST /transaction, authenticated
62/// The transaction arrived in this method has been authored by the public key in the source 62/// The transaction arrived in this method has been authored by the public key in the source
63pub async fn propose_authenticated_transaction( 63pub async fn propose_authenticated_transaction(
64 pubkey: String, 64 header: HeaderMap<HeaderName, HeaderValue>,
65 new_transaction: Transaction, 65 new_transaction: Transaction,
66 db: Db, 66 db: Db,
67) -> Result<impl warp::Reply, warp::Rejection> { 67) -> Result<impl warp::Reply, warp::Rejection> {
68
69 // auth logic
68 debug!("new transaction request {:?}", new_transaction); 70 debug!("new transaction request {:?}", new_transaction);
69 71
70 // let mut transactions = db.lock().await; 72 // let mut transactions = db.lock().await;
diff --git a/src/routes.rs b/src/routes.rs
index 499ba35..b48fdb2 100644
--- a/src/routes.rs
+++ b/src/routes.rs
@@ -47,14 +47,10 @@ pub fn authenticated_transaction_propose(
47 warp::path("transaction") 47 warp::path("transaction")
48 .and(warp::path::end()) 48 .and(warp::path::end())
49 .and(warp::post()) 49 .and(warp::post())
50 .and(custom_filters::transaction_json_body()) 50 .and(custom_filters::transaction_json_body()) // returns transaction
51 .map(|t: Transaction| { 51 .and(custom_filters::transaction_header()) // returns Transaction
52 with_auth(db.clone(), t) 52 .and(custom_filters::with_db(db)) // wraps db
53 }) 53 .and_then(handlers::propose_authenticated_transaction) // uses db, transaction and authenticated
54 .untuple_one()
55 .and(custom_filters::transaction_json_body())
56 .and(custom_filters::with_db(db))
57 .and_then(handlers::propose_authenticated_transaction)
58 54
59 // .and(custom_filters::transaction_json_body()) 55 // .and(custom_filters::transaction_json_body())
60 // // TODO: you might have to restore this 56 // // TODO: you might have to restore this