From 52b95ace5c67e37300c976f344bc0aacaa278639 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Sat, 10 Apr 2021 15:04:27 +0300 Subject: infallible --- src/auth.rs | 29 ++++++++++++++++++----------- 1 file 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 @@ -use crate::error::Error; +use crate::error::{handle_rejection, Error}; use crate::schema::{Db, Transaction}; -use anyhow::{anyhow, Context, Result}; +use std::convert::Infallible; +// use anyhow::{anyhow, Context, Result}; use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; use serde::{Deserialize, Serialize}; -use thiserror::Error; +// use std::fmt::Display; +// use thiserror::Error; use warp::header::headers_cloned; use warp::http::header::{HeaderMap, HeaderValue, AUTHORIZATION}; +use warp::http::StatusCode; use warp::reject; use warp::reject::Reject; -use warp::{Filter, Rejection}; +use warp::reject::Rejection; +use warp::Filter; const BEARER: &str = "Bearer "; const PUBLIC_KEY_PEM: &str = "-----BEGIN PUBLIC KEY----- @@ -69,10 +73,13 @@ struct Claims { // } // impl warp::reject::Reject for Nope {} -#[derive(Debug)] -struct LessThanTenError {} +#[derive(Error, Debug)] +pub enum DataStoreError { + #[error("invalid header")] + InvalidHeader {}, +} -impl Reject for LessThanTenError {} +impl Reject for DataStoreError {} // impl From for Rejection { // fn from(other: LessThanTenError) -> Self { @@ -87,15 +94,15 @@ pub fn with_auth( headers_cloned() .map(move |headers: HeaderMap| (db.clone(), headers)) .and_then(authorize) + .recover(handle_rejection()) } -async fn authorize((db, headers): (Db, HeaderMap)) -> Result { +async fn authorize((db, headers): (Db, HeaderMap)) -> Result { match jwt_from_header(&headers) { Ok(jwt) => { let decoded = decode::( &jwt, - // TODO: what key are we using here? pass db/pw store here to get the claimant's - // public key <10-04-21, yigit> // + // TODO: what key are we using here? pass db/pw store here to get the claimant's public key <10-04-21, yigit> // &DecodingKey::from_rsa_pem(PUBLIC_KEY_PEM.as_bytes()).unwrap(), &Validation::new(Algorithm::HS512), ) @@ -104,7 +111,7 @@ async fn authorize((db, headers): (Db, HeaderMap)) -> Result return Err(anyhow!("missing!")), + Err(e) => return (StatusCode::UNAUTHORIZED, e.to_string()), // warp error } } -- cgit v1.2.3-70-g09d2