-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor middleware and move sending trace to examples directory
- Loading branch information
Showing
6 changed files
with
32 additions
and
47 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Manually submitting a trace | ||
|
||
The following curl command shows how you can send a trace to the worker. Be sure | ||
to update the secret token in the command. | ||
|
||
```bash | ||
curl -X POST http://localhost:8787/v1/traces \ | ||
-H "Authorization: Bearer your-secret-token-here" \ | ||
-H "Content-Type: application/json" \ | ||
--data-binary @trace.json | ||
``` |
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 |
---|---|---|
|
@@ -83,4 +83,4 @@ | |
] | ||
} | ||
] | ||
} | ||
} |
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,47 +1,25 @@ | ||
use axum::{ | ||
body::Body, | ||
http::{Request, StatusCode}, | ||
middleware::Next, | ||
response::Response, | ||
}; | ||
|
||
use tracing::debug; | ||
use axum::body::Body; | ||
use axum::http::{Request, StatusCode}; | ||
use axum::middleware::Next; | ||
use axum::response::Response; | ||
|
||
pub async fn auth_middleware( | ||
expected_token: String, | ||
request: Request<Body>, | ||
env: worker::Env, | ||
next: Next, | ||
) -> Result<Response, StatusCode> { | ||
let expected_token = env | ||
.var("API_TOKEN") | ||
.map_err(|_| { | ||
debug!("Failed to get API_TOKEN"); | ||
StatusCode::INTERNAL_SERVER_ERROR | ||
})? | ||
.to_string(); | ||
|
||
if expected_token.is_empty() { | ||
debug!("API_TOKEN is empty"); | ||
return Err(StatusCode::INTERNAL_SERVER_ERROR); | ||
} | ||
debug!("Expected token: {}", expected_token); | ||
|
||
// Retrieve the Authorization from the request. If we are not able to parse | ||
// the Header or it is missing, then the variable will be a empty &str. | ||
let auth_header = request | ||
.headers() | ||
.get("Authorization") | ||
.and_then(|header| header.to_str().ok()); | ||
|
||
match auth_header { | ||
Some(auth) if auth.starts_with("Bearer ") => { | ||
let token = &auth[7..]; | ||
debug!("Received token: {}", token); | ||
.and_then(|header| header.to_str().ok()) | ||
.unwrap_or(""); | ||
|
||
if token == expected_token { | ||
Ok(next.run(request).await) | ||
} else { | ||
Err(StatusCode::UNAUTHORIZED) | ||
} | ||
} | ||
// Split on the first space and make sure the first part matches 'Bearer' | ||
// and the second part matches the expected_token. | ||
match auth_header.split_once(' ') { | ||
Some(("Bearer", token)) if token == expected_token => Ok(next.run(request).await), | ||
_ => Err(StatusCode::UNAUTHORIZED), | ||
} | ||
} |
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