This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
generated from WalletConnect/rust-http-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: project validation * fix: tests * fix: clippy and fmt * fix: get rid of unwraps * fix: move auth into extractor * fix: fmt and clippy * fix: change to auth header * fix: add extractors.rs * Update src/registry.rs Co-authored-by: Chris Smith <1979423+chris13524@users.noreply.github.com> * fix: fmt --------- Co-authored-by: Chris Smith <1979423+chris13524@users.noreply.github.com>
- Loading branch information
1 parent
ac6fc45
commit ded66ce
Showing
21 changed files
with
688 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use { | ||
crate::state::AppState, | ||
async_trait::async_trait, | ||
axum::{ | ||
extract::{FromRequestParts, Path}, | ||
headers::{authorization::Bearer, Authorization}, | ||
http::request::Parts, | ||
TypedHeader, | ||
}, | ||
hyper::StatusCode, | ||
serde_json::json, | ||
std::{collections::HashMap, sync::Arc}, | ||
tracing::warn, | ||
}; | ||
|
||
/// Extracts project_id from uri and project_secret from Authorization header. | ||
/// Verifies their correctness against registry and returns AuthedProjectId | ||
/// struct. | ||
pub struct AuthedProjectId(pub String, pub String); | ||
|
||
#[async_trait] | ||
impl FromRequestParts<Arc<AppState>> for AuthedProjectId { | ||
type Rejection = (StatusCode, String); | ||
|
||
async fn from_request_parts( | ||
parts: &mut Parts, | ||
state: &Arc<AppState>, | ||
) -> Result<Self, Self::Rejection> { | ||
let Path(path_args) = Path::<HashMap<String, String>>::from_request_parts(parts, state) | ||
.await | ||
.map_err(|_| { | ||
( | ||
StatusCode::BAD_REQUEST, | ||
json!({ | ||
"reason": "Invalid project_id. Please make sure to include project_id in uri. " | ||
}).to_string(), | ||
) | ||
})?; | ||
|
||
let TypedHeader(project_secret) = TypedHeader::<Authorization::<Bearer>>::from_request_parts(parts, state).await.map_err(|_| { | ||
( | ||
StatusCode::UNAUTHORIZED, | ||
json!({ | ||
"reason": "Unauthorized. Please make sure to include project secret in Authorization header. " | ||
}).to_string(), | ||
) | ||
})?; | ||
|
||
let project_id = path_args | ||
.get("project_id") | ||
.ok_or(( | ||
StatusCode::BAD_REQUEST, | ||
json!({"reason": "Invalid data for authentication".to_string()}).to_string(), | ||
))? | ||
.to_string(); | ||
|
||
let authenticated = state | ||
.registry | ||
.is_authenticated(&project_id, project_secret.token()) | ||
.await | ||
.map_err(|e| { | ||
warn!(?e, "Failed to authenticate project"); | ||
( | ||
StatusCode::BAD_REQUEST, | ||
"Invalid data for authentication".to_string(), | ||
) | ||
})?; | ||
|
||
if !authenticated { | ||
return Err(( | ||
StatusCode::UNAUTHORIZED, | ||
json!({ | ||
"reason": "Invalid project_secret. Please make sure to include proper project secret in Authorization header." | ||
}) | ||
.to_string(), | ||
)); | ||
}; | ||
|
||
Ok(AuthedProjectId( | ||
project_id, | ||
project_secret.token().to_string(), | ||
)) | ||
} | ||
} |
Oops, something went wrong.