Repository navigation
feat(py-client)!: Split SecretKey and TokenGenerator #721
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
3 commits
Select commit
Hold shift + click to select a range
9c52b30
feat(py-client)!: Split SecretKey and TokenGenerator
sentry-junior[bot] caf576f
Merge remote-tracking branch 'origin/main' into feat/py-client-split-…
sentry-junior[bot] 846ddc5
ref(py-client): Add Token alias and move signing test to test_auth
sentry-junior[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import base64 | ||
| import os | ||
|
|
||
| import jwt | ||
| import pytest | ||
| from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey | ||
| from cryptography.hazmat.primitives.serialization import load_pem_public_key | ||
| from objectstore_client import presign, utils | ||
| from objectstore_client.auth import Permission, SecretKey, TokenGenerator | ||
| from objectstore_client.scope import Scope | ||
|
|
||
| TEST_EDDSA_PRIVKEY_PATH = os.path.join(os.path.dirname(__file__), "ed25519.private.pem") | ||
| TEST_EDDSA_PUBKEY_PATH = os.path.join(os.path.dirname(__file__), "ed25519.public.pem") | ||
|
|
||
|
|
||
| def _key() -> SecretKey: | ||
| with open(TEST_EDDSA_PRIVKEY_PATH) as f: | ||
| return SecretKey("test_kid", f.read()) | ||
|
|
||
|
|
||
| def test_token_generator_signs_with_key_and_defaults() -> None: | ||
| generator = TokenGenerator(_key(), permissions=[Permission.OBJECT_READ]) | ||
| token = generator.token_for_scope("testing", Scope(org=1)) | ||
|
|
||
| assert jwt.get_unverified_header(token)["kid"] == "test_kid" | ||
| with open(TEST_EDDSA_PUBKEY_PATH) as f: | ||
| claims = jwt.decode(token, f.read(), algorithms=["EdDSA"]) | ||
| assert claims["res"]["os:usecase"] == "testing" | ||
| assert claims["permissions"] == [Permission.OBJECT_READ] | ||
|
|
||
|
|
||
| def test_token_generator_rejects_escalated_permissions() -> None: | ||
| generator = TokenGenerator(_key(), permissions=[Permission.OBJECT_READ]) | ||
| with pytest.raises(ValueError, match="not granted to this token generator"): | ||
| generator.token_for_scope( | ||
| "testing", Scope(org=1), permissions=[Permission.OBJECT_WRITE] | ||
| ) | ||
|
|
||
|
|
||
| def test_sign_canonical_form_roundtrips_with_public_key() -> None: | ||
| with open(TEST_EDDSA_PUBKEY_PATH, "rb") as f: | ||
| public_key = load_pem_public_key(f.read()) | ||
| assert isinstance(public_key, Ed25519PublicKey) | ||
|
|
||
| canonical = presign.build_canonical_form( | ||
| "GET", | ||
| utils.encode_path("/v1/objects/testing/_/key"), | ||
| utils.encode_query("os_kid=test_kid&os_duration=3600"), | ||
| ) | ||
| signature_b64 = _key().signature_for_canonical_form(canonical) | ||
|
|
||
| # Decode the base64url-no-pad signature and verify with the public key. | ||
| padding = "=" * (-len(signature_b64) % 4) | ||
| signature = base64.urlsafe_b64decode(signature_b64 + padding) | ||
| assert len(signature) == 64 | ||
| public_key.verify(signature, canonical.encode()) # raises on mismatch | ||
Oops, something went wrong.
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.