diff options
author | Yigit Sever | 2021-04-10 15:14:37 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-10 15:14:37 +0300 |
commit | 38a928a47f94d9f456c9c53f5db4fcd19165e7ac (patch) | |
tree | dfeeb91b3761d767962cc446ce7e31461ce3c124 /src/auth.rs | |
parent | 52b95ace5c67e37300c976f344bc0aacaa278639 (diff) | |
download | gradecoin-38a928a47f94d9f456c9c53f5db4fcd19165e7ac.tar.gz gradecoin-38a928a47f94d9f456c9c53f5db4fcd19165e7ac.tar.bz2 gradecoin-38a928a47f94d9f456c9c53f5db4fcd19165e7ac.zip |
agliyo
Diffstat (limited to 'src/auth.rs')
-rw-r--r-- | src/auth.rs | 31 |
1 files changed, 5 insertions, 26 deletions
diff --git a/src/auth.rs b/src/auth.rs index ced9e8e..03930f0 100644 --- a/src/auth.rs +++ b/src/auth.rs | |||
@@ -63,29 +63,10 @@ struct Claims { | |||
63 | puk: String, | 63 | puk: String, |
64 | } | 64 | } |
65 | 65 | ||
66 | // #[derive(Error, Debug)] | 66 | #[derive(Debug)] |
67 | // pub enum Nope { | 67 | struct RateLimited; |
68 | // #[error("Invalid header")] | ||
69 | // InvalidHeader { | ||
70 | // expected: String, | ||
71 | // found: String, | ||
72 | // }, | ||
73 | // } | ||
74 | // impl warp::reject::Reject for Nope {} | ||
75 | 68 | ||
76 | #[derive(Error, Debug)] | 69 | impl Reject for RateLimited {} |
77 | pub enum DataStoreError { | ||
78 | #[error("invalid header")] | ||
79 | InvalidHeader {}, | ||
80 | } | ||
81 | |||
82 | impl Reject for DataStoreError {} | ||
83 | |||
84 | // impl From<LessThanTenError> for Rejection { | ||
85 | // fn from(other: LessThanTenError) -> Self { | ||
86 | // warp::reject::custom(other) | ||
87 | // } | ||
88 | // } | ||
89 | 70 | ||
90 | pub fn with_auth( | 71 | pub fn with_auth( |
91 | db: Db, | 72 | db: Db, |
@@ -94,10 +75,9 @@ pub fn with_auth( | |||
94 | headers_cloned() | 75 | headers_cloned() |
95 | .map(move |headers: HeaderMap<HeaderValue>| (db.clone(), headers)) | 76 | .map(move |headers: HeaderMap<HeaderValue>| (db.clone(), headers)) |
96 | .and_then(authorize) | 77 | .and_then(authorize) |
97 | .recover(handle_rejection()) | ||
98 | } | 78 | } |
99 | 79 | ||
100 | async fn authorize((db, headers): (Db, HeaderMap<HeaderValue>)) -> Result<String, Infallible> { | 80 | async fn authorize((db, headers): (Db, HeaderMap<HeaderValue>)) -> Result<String, Rejection> { |
101 | match jwt_from_header(&headers) { | 81 | match jwt_from_header(&headers) { |
102 | Ok(jwt) => { | 82 | Ok(jwt) => { |
103 | let decoded = decode::<Claims>( | 83 | let decoded = decode::<Claims>( |
@@ -111,8 +91,7 @@ async fn authorize((db, headers): (Db, HeaderMap<HeaderValue>)) -> Result<String | |||
111 | 91 | ||
112 | Ok(decoded.claims.puk) | 92 | Ok(decoded.claims.puk) |
113 | } | 93 | } |
114 | Err(e) => return (StatusCode::UNAUTHORIZED, e.to_string()), | 94 | Err(e) => return Err(warp::reject::custom(RateLimited)), |
115 | // warp error | ||
116 | } | 95 | } |
117 | } | 96 | } |
118 | 97 | ||