-
-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Read JWT from query params #556
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f249c80
feat(py-client): Embed read-only token in object_url; accept os_auth …
lcian 72bd080
ref(py-client): Simplify object_url docs and query-auth tests
lcian 0bb7a53
improve
lcian 40b0714
improvel
lcian bc31e8b
improve
lcian 85d7109
improve
lcian f97d8d7
ref(server): Drop redundant base64 wrapping of os_auth query token
lcian 21ba90b
improve
lcian 0673bf6
test(py-client): Parse object_url query params instead of substring m…
lcian 01b4114
ref(server): Parse os_auth via Query extractor instead of manual split
lcian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,107 @@ | ||
| //! End-to-end tests for the `os_auth` query parameter authentication path. | ||
| //! | ||
| //! A JWT can be supplied either via the `x-os-auth`/`Authorization` header or, | ||
| //! as-is, via the `os_auth` query parameter. The header takes precedence when | ||
| //! both are present. | ||
|
|
||
| use anyhow::Result; | ||
| use http::header; | ||
| use jsonwebtoken::{Algorithm, EncodingKey, Header, encode, get_current_timestamp}; | ||
| use objectstore_server::config::{AuthZ, Config}; | ||
| use objectstore_test::server::{TEST_EDDSA_KID, TEST_EDDSA_PRIVKEY, TestServer}; | ||
|
|
||
| /// Object path used across the tests: usecase `test`, scope `org=1`, key `query-auth-key`. | ||
| const OBJECT_PATH: &str = "/v1/objects/test/org=1/query-auth-key"; | ||
|
|
||
| async fn test_server() -> TestServer { | ||
| TestServer::with_config(Config { | ||
| auth: AuthZ { | ||
| enforce: true, | ||
| ..Default::default() | ||
| }, | ||
| ..Default::default() | ||
| }) | ||
| .await | ||
| } | ||
|
|
||
| /// Builds a JWT for `test`/`org=1` with the given permissions. | ||
| fn jwt(permissions: &[&str]) -> String { | ||
| let mut header = Header::new(Algorithm::EdDSA); | ||
| header.kid = Some(TEST_EDDSA_KID.into()); | ||
|
|
||
| let claims = serde_json::json!({ | ||
| "exp": get_current_timestamp() + 300, | ||
| "res": {"os:usecase": "test", "org": "1"}, | ||
| "permissions": permissions, | ||
| }); | ||
|
|
||
| let key = EncodingKey::from_ed_pem(TEST_EDDSA_PRIVKEY.as_bytes()).unwrap(); | ||
| encode(&header, &claims, &key).unwrap() | ||
| } | ||
|
|
||
| /// Seeds the object at [`OBJECT_PATH`] with the given body via an authorized `PUT`. | ||
| async fn seed_object(server: &TestServer, body: &'static str) -> Result<()> { | ||
| let resp = reqwest::Client::new() | ||
| .put(server.url(OBJECT_PATH)) | ||
| .header( | ||
| header::AUTHORIZATION.as_str(), | ||
| format!("Bearer {}", jwt(&["object.read", "object.write"])), | ||
| ) | ||
| .body(body) | ||
| .send() | ||
| .await?; | ||
| assert_eq!(resp.status(), reqwest::StatusCode::OK); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn query_auth_get_succeeds() -> Result<()> { | ||
| let server = test_server().await; | ||
| seed_object(&server, "hello").await?; | ||
|
|
||
| let token = jwt(&["object.read"]); | ||
| let url = format!("{}?os_auth={token}", server.url(OBJECT_PATH)); | ||
| let resp = reqwest::Client::new().get(url).send().await?; | ||
|
|
||
| assert_eq!(resp.status(), reqwest::StatusCode::OK); | ||
| assert_eq!(resp.text().await?, "hello"); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn query_auth_tampered_token_is_unauthorized() -> Result<()> { | ||
| let server = test_server().await; | ||
|
|
||
| // Flip the last character of the JWT signature so verification fails. | ||
| let mut token = jwt(&["object.read"]); | ||
| let last = token.pop().unwrap(); | ||
| token.push(if last == 'A' { 'B' } else { 'A' }); | ||
|
|
||
| let url = format!("{}?os_auth={token}", server.url(OBJECT_PATH)); | ||
| let resp = reqwest::Client::new().get(url).send().await?; | ||
|
|
||
| assert_eq!(resp.status(), reqwest::StatusCode::UNAUTHORIZED); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn header_takes_precedence_over_query() -> Result<()> { | ||
| let server = test_server().await; | ||
| seed_object(&server, "hello").await?; | ||
|
|
||
| // Valid header token, garbage query token: the header must win, so the | ||
| // request succeeds despite the unusable query value. | ||
| let url = format!("{}?os_auth=not-a-valid-jwt", server.url(OBJECT_PATH)); | ||
| let resp = reqwest::Client::new() | ||
| .get(url) | ||
| .header( | ||
| header::AUTHORIZATION.as_str(), | ||
| format!("Bearer {}", jwt(&["object.read"])), | ||
| ) | ||
| .send() | ||
| .await?; | ||
|
|
||
| assert_eq!(resp.status(), reqwest::StatusCode::OK); | ||
| assert_eq!(resp.text().await?, "hello"); | ||
| Ok(()) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.