diff options
Diffstat (limited to 'src/auth.rs')
| -rw-r--r-- | src/auth.rs | 25 |
1 files changed, 19 insertions, 6 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 @@ | |||
| 1 | use crate::error::Error; | 1 | use crate::error::Error; |
| 2 | use crate::schema::{Db, Transaction}; | 2 | use crate::schema::{Db, Transaction}; |
| 3 | use anyhow::{anyhow, Context, Result}; | ||
| 3 | use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; | 4 | use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; |
| 4 | use serde::{Deserialize, Serialize}; | 5 | use serde::{Deserialize, Serialize}; |
| 6 | use thiserror::Error; | ||
| 5 | use warp::header::headers_cloned; | 7 | use warp::header::headers_cloned; |
| 6 | use warp::http::header::{HeaderMap, HeaderValue, AUTHORIZATION}; | 8 | use warp::http::header::{HeaderMap, HeaderValue, AUTHORIZATION}; |
| 7 | use warp::{reject, Filter, Rejection}; | 9 | use warp::reject; |
| 8 | use thiserror::Error; | 10 | use warp::reject::Reject; |
| 9 | use anyhow::*; | 11 | use warp::{Filter, Rejection}; |
| 10 | 12 | ||
| 11 | const BEARER: &str = "Bearer "; | 13 | const BEARER: &str = "Bearer "; |
| 12 | const PUBLIC_KEY_PEM: &str = "-----BEGIN PUBLIC KEY----- | 14 | const 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)] | ||
| 73 | struct LessThanTenError {} | ||
| 74 | |||
| 75 | impl 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 | ||
| 69 | pub fn with_auth( | 83 | pub 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 | ||
| 78 | impl warp::reject::Reject for Nope {} | ||
| 79 | |||
| 80 | async fn authorize((db, headers): (Db, HeaderMap<HeaderValue>)) -> Result<String, Error> { | 92 | async 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 | ||
