aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYigit Sever2021-04-10 15:04:27 +0300
committerYigit Sever2021-04-10 15:04:27 +0300
commit52b95ace5c67e37300c976f344bc0aacaa278639 (patch)
treecc3d8d2bd039787db9b190ebc35d6a9c152a66ea
parentc03321bc059ed733970d8a696acb461428b1d284 (diff)
downloadgradecoin-52b95ace5c67e37300c976f344bc0aacaa278639.tar.gz
gradecoin-52b95ace5c67e37300c976f344bc0aacaa278639.tar.bz2
gradecoin-52b95ace5c67e37300c976f344bc0aacaa278639.zip
infallible
-rw-r--r--src/auth.rs29
1 files changed, 18 insertions, 11 deletions
diff --git a/src/auth.rs b/src/auth.rs
index 95f2378..ced9e8e 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -1,14 +1,18 @@
1use crate::error::Error; 1use crate::error::{handle_rejection, Error};
2use crate::schema::{Db, Transaction}; 2use crate::schema::{Db, Transaction};
3use anyhow::{anyhow, Context, Result}; 3use std::convert::Infallible;
4// use anyhow::{anyhow, Context, Result};
4use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; 5use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
5use serde::{Deserialize, Serialize}; 6use serde::{Deserialize, Serialize};
6use thiserror::Error; 7// use std::fmt::Display;
8// use thiserror::Error;
7use warp::header::headers_cloned; 9use warp::header::headers_cloned;
8use warp::http::header::{HeaderMap, HeaderValue, AUTHORIZATION}; 10use warp::http::header::{HeaderMap, HeaderValue, AUTHORIZATION};
11use warp::http::StatusCode;
9use warp::reject; 12use warp::reject;
10use warp::reject::Reject; 13use warp::reject::Reject;
11use warp::{Filter, Rejection}; 14use warp::reject::Rejection;
15use warp::Filter;
12 16
13const BEARER: &str = "Bearer "; 17const BEARER: &str = "Bearer ";
14const PUBLIC_KEY_PEM: &str = "-----BEGIN PUBLIC KEY----- 18const PUBLIC_KEY_PEM: &str = "-----BEGIN PUBLIC KEY-----
@@ -69,10 +73,13 @@ struct Claims {
69// } 73// }
70// impl warp::reject::Reject for Nope {} 74// impl warp::reject::Reject for Nope {}
71 75
72#[derive(Debug)] 76#[derive(Error, Debug)]
73struct LessThanTenError {} 77pub enum DataStoreError {
78 #[error("invalid header")]
79 InvalidHeader {},
80}
74 81
75impl Reject for LessThanTenError {} 82impl Reject for DataStoreError {}
76 83
77// impl From<LessThanTenError> for Rejection { 84// impl From<LessThanTenError> for Rejection {
78// fn from(other: LessThanTenError) -> Self { 85// fn from(other: LessThanTenError) -> Self {
@@ -87,15 +94,15 @@ pub fn with_auth(
87 headers_cloned() 94 headers_cloned()
88 .map(move |headers: HeaderMap<HeaderValue>| (db.clone(), headers)) 95 .map(move |headers: HeaderMap<HeaderValue>| (db.clone(), headers))
89 .and_then(authorize) 96 .and_then(authorize)
97 .recover(handle_rejection())
90} 98}
91 99
92async fn authorize((db, headers): (Db, HeaderMap<HeaderValue>)) -> Result<String, Error> { 100async fn authorize((db, headers): (Db, HeaderMap<HeaderValue>)) -> Result<String, Infallible> {
93 match jwt_from_header(&headers) { 101 match jwt_from_header(&headers) {
94 Ok(jwt) => { 102 Ok(jwt) => {
95 let decoded = decode::<Claims>( 103 let decoded = decode::<Claims>(
96 &jwt, 104 &jwt,
97 // TODO: what key are we using here? pass db/pw store here to get the claimant's 105 // TODO: what key are we using here? pass db/pw store here to get the claimant's public key <10-04-21, yigit> //
98 // public key <10-04-21, yigit> //
99 &DecodingKey::from_rsa_pem(PUBLIC_KEY_PEM.as_bytes()).unwrap(), 106 &DecodingKey::from_rsa_pem(PUBLIC_KEY_PEM.as_bytes()).unwrap(),
100 &Validation::new(Algorithm::HS512), 107 &Validation::new(Algorithm::HS512),
101 ) 108 )
@@ -104,7 +111,7 @@ async fn authorize((db, headers): (Db, HeaderMap<HeaderValue>)) -> Result<String
104 111
105 Ok(decoded.claims.puk) 112 Ok(decoded.claims.puk)
106 } 113 }
107 Err(e) => return Err(anyhow!("missing!")), 114 Err(e) => return (StatusCode::UNAUTHORIZED, e.to_string()),
108 // warp error 115 // warp error
109 } 116 }
110} 117}