diff options
author | Yigit Sever | 2021-04-10 14:44:40 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-10 14:44:40 +0300 |
commit | c03321bc059ed733970d8a696acb461428b1d284 (patch) | |
tree | a79e48c3a55ac1957932a9b80ce25b39088a9253 /src/auth.rs | |
parent | ed53fbc9097370feeda1c5507878933643a9bcc5 (diff) | |
download | gradecoin-c03321bc059ed733970d8a696acb461428b1d284.tar.gz gradecoin-c03321bc059ed733970d8a696acb461428b1d284.tar.bz2 gradecoin-c03321bc059ed733970d8a696acb461428b1d284.zip |
dunno
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 | ||