-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add X_API_KEY environment variable and with_api_key middleware
- Loading branch information
1 parent
17cda97
commit 9c793ad
Showing
6 changed files
with
42 additions
and
3 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
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ data | |
.env | ||
.env.local | ||
private_key.pem | ||
.errorviz-version |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod res_log; | ||
pub mod res_log; | ||
pub mod with_api_key; |
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,33 @@ | ||
use axum::{ | ||
body::Body, | ||
http::{Request, StatusCode}, | ||
middleware::Next, | ||
response::Response, | ||
}; | ||
use std::env; | ||
|
||
pub async fn with_api_key(req: Request<Body>, next: Next) -> Result<Response, StatusCode> { | ||
println!("Checking for API key..."); | ||
|
||
let api_key = req | ||
.headers() | ||
.get("x-api-key") | ||
.and_then(|key| key.to_str().ok()); | ||
|
||
let expected_api_key = env::var("X_API_KEY").expect("X_API_KEY must be set"); | ||
|
||
|
||
if let Some(key) = api_key { | ||
if key == expected_api_key { | ||
return Ok(next.run(req).await); | ||
} | ||
} | ||
// error with message of x-api-key not match | ||
|
||
Ok(Response::builder() | ||
.status(StatusCode::UNAUTHORIZED) | ||
.body(Body::from("Invalid API key")) | ||
.unwrap()) | ||
|
||
|
||
} |