-
-
Notifications
You must be signed in to change notification settings - Fork 218
Auth error handling with status codes #841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
dfbf006
b7b9580
6dd9048
81ca3ee
100acb8
2295660
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ use database::mungos::mongodb::bson::doc; | |
| use komodo_client::entities::{komodo_timestamp, user::User}; | ||
| use reqwest::StatusCode; | ||
| use serde::Deserialize; | ||
| use serror::AddStatusCode; | ||
| use serror::AddStatusCodeError; | ||
|
|
||
| use crate::{ | ||
| helpers::query::get_user, | ||
|
|
@@ -37,17 +37,15 @@ pub async fn auth_request( | |
| mut req: Request, | ||
| next: Next, | ||
| ) -> serror::Result<Response> { | ||
| let user = authenticate_check_enabled(&headers) | ||
| .await | ||
| .status_code(StatusCode::UNAUTHORIZED)?; | ||
| let user = authenticate_check_enabled(&headers).await?; | ||
| req.extensions_mut().insert(user); | ||
| Ok(next.run(req).await) | ||
| } | ||
|
|
||
| #[instrument(level = "debug")] | ||
| pub async fn get_user_id_from_headers( | ||
| headers: &HeaderMap, | ||
| ) -> anyhow::Result<String> { | ||
| ) -> serror::Result<String> { | ||
| match ( | ||
| headers.get("authorization"), | ||
| headers.get("x-api-key"), | ||
|
|
@@ -59,6 +57,7 @@ pub async fn get_user_id_from_headers( | |
| auth_jwt_get_user_id(jwt) | ||
| .await | ||
| .context("failed to authenticate jwt") | ||
| .map_err(|e| e.status_code(StatusCode::UNAUTHORIZED)) | ||
| } | ||
| (None, Some(key), Some(secret)) => { | ||
| // USE API KEY / SECRET | ||
|
|
@@ -67,26 +66,33 @@ pub async fn get_user_id_from_headers( | |
| auth_api_key_get_user_id(key, secret) | ||
| .await | ||
| .context("failed to authenticate api key") | ||
| .map_err(|e| e.status_code(StatusCode::UNAUTHORIZED)) | ||
|
||
| } | ||
| _ => { | ||
| // AUTH FAIL | ||
| Err(anyhow!( | ||
| "must attach either AUTHORIZATION header with jwt OR pass X-API-KEY and X-API-SECRET" | ||
| )) | ||
| Err( | ||
| anyhow!("must attach either AUTHORIZATION header with jwt OR pass X-API-KEY and X-API-SECRET") | ||
| .status_code(StatusCode::BAD_REQUEST) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[instrument(level = "debug")] | ||
| pub async fn authenticate_check_enabled( | ||
| headers: &HeaderMap, | ||
| ) -> anyhow::Result<User> { | ||
| ) -> serror::Result<User> { | ||
| let user_id = get_user_id_from_headers(headers).await?; | ||
| let user = get_user(&user_id).await?; | ||
| let user = get_user(&user_id).await.map_err(serror::Error::from)?; | ||
| if user.enabled { | ||
| Ok(user) | ||
| } else { | ||
| Err(anyhow!("user not enabled")) | ||
| Err( | ||
| serror::Error::from( | ||
| anyhow!("user not enabled") | ||
| .status_code(StatusCode::FORBIDDEN) | ||
| ) | ||
|
||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can
.status_code()directly after.context()There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I didn't think so, sure