-
Notifications
You must be signed in to change notification settings - Fork 25
Expose api key validation #491
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
14 commits
Select commit
Hold shift + click to select a range
b4d4ff4
introduce ApiKey module alongside with types
denizs 600f046
wire ApiKey module through clients
denizs aa51996
rename api_key modules to api_keys
denizs 6622211
extend ApiKey response model by missing fields
denizs 1566ff0
pluralize ApiKeyModule, ApiKey and AsyncApiKey
denizs 1239d82
remove AI slop, actually validate API key
denizs 683d508
make api key validation path a constant
denizs b445127
adapt tests
denizs 686c98e
backport | None notation to Optional[T]
denizs 0e65d46
return None on invalid API key
denizs bccd817
improve naming
denizs 63e833a
black .
nholden 7610c60
fix type
nholden 246acf2
Merge branch 'main' into minor/expose-api-key-validation
nholden 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # type: ignore | ||
| import pytest | ||
|
|
||
| from tests.utils.fixtures.mock_api_key import MockApiKey | ||
| from tests.utils.syncify import syncify | ||
| from workos.api_keys import API_KEY_VALIDATION_PATH, ApiKeys, AsyncApiKeys | ||
|
|
||
|
|
||
| @pytest.mark.sync_and_async(ApiKeys, AsyncApiKeys) | ||
| class TestApiKeys: | ||
| @pytest.fixture | ||
| def mock_api_key(self): | ||
| return MockApiKey().dict() | ||
|
|
||
| @pytest.fixture | ||
| def api_key(self): | ||
| return "sk_my_api_key" | ||
|
|
||
| def test_validate_api_key_with_valid_key( | ||
| self, | ||
| module_instance, | ||
| api_key, | ||
| mock_api_key, | ||
| capture_and_mock_http_client_request, | ||
| ): | ||
| response_body = {"api_key": mock_api_key} | ||
| request_kwargs = capture_and_mock_http_client_request( | ||
| module_instance._http_client, response_body, 200 | ||
| ) | ||
|
|
||
| api_key_details = syncify(module_instance.validate_api_key(value=api_key)) | ||
|
|
||
| assert request_kwargs["url"].endswith(API_KEY_VALIDATION_PATH) | ||
| assert request_kwargs["method"] == "post" | ||
| assert api_key_details.id == mock_api_key["id"] | ||
| assert api_key_details.name == mock_api_key["name"] | ||
| assert api_key_details.object == "api_key" | ||
|
|
||
| def test_validate_api_key_with_invalid_key( | ||
| self, | ||
| module_instance, | ||
| mock_http_client_with_response, | ||
| ): | ||
| mock_http_client_with_response( | ||
| module_instance._http_client, | ||
| {"api_key": None}, | ||
| 200, | ||
| ) | ||
|
|
||
| assert syncify(module_instance.validate_api_key(value="invalid-key")) is None |
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,19 @@ | ||
| import datetime | ||
|
|
||
| from workos.types.api_keys import ApiKey | ||
|
|
||
|
|
||
| class MockApiKey(ApiKey): | ||
| def __init__(self, id="api_key_01234567890"): | ||
| now = datetime.datetime.now().isoformat() | ||
| super().__init__( | ||
| object="api_key", | ||
| id=id, | ||
| owner={"type": "organization", "id": "org_1337"}, | ||
| name="Development API Key", | ||
| obfuscated_value="api_..0", | ||
| permissions=[], | ||
| last_used_at=now, | ||
| created_at=now, | ||
| updated_at=now, | ||
| ) |
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,53 @@ | ||
| from typing import Optional, Protocol | ||
|
|
||
| from workos.types.api_keys import ApiKey | ||
| from workos.typing.sync_or_async import SyncOrAsync | ||
| from workos.utils.http_client import AsyncHTTPClient, SyncHTTPClient | ||
| from workos.utils.request_helper import REQUEST_METHOD_POST | ||
|
|
||
| API_KEY_VALIDATION_PATH = "api_keys/validations" | ||
| RESOURCE_OBJECT_ATTRIBUTE_NAME = "api_key" | ||
|
|
||
|
|
||
| class ApiKeysModule(Protocol): | ||
| def validate_api_key(self, *, value: str) -> SyncOrAsync[Optional[ApiKey]]: | ||
| """Validate an API key. | ||
|
|
||
| Kwargs: | ||
| value (str): API key value | ||
|
|
||
| Returns: | ||
| Optional[ApiKey]: Returns ApiKey resource object | ||
| if supplied value was valid, None if it was not | ||
| """ | ||
| ... | ||
|
|
||
|
|
||
| class ApiKeys(ApiKeysModule): | ||
| _http_client: SyncHTTPClient | ||
|
|
||
| def __init__(self, http_client: SyncHTTPClient): | ||
| self._http_client = http_client | ||
|
|
||
| def validate_api_key(self, *, value: str) -> Optional[ApiKey]: | ||
| response = self._http_client.request( | ||
| API_KEY_VALIDATION_PATH, method=REQUEST_METHOD_POST, json={"value": value} | ||
| ) | ||
| if response.get(RESOURCE_OBJECT_ATTRIBUTE_NAME) is None: | ||
| return None | ||
| return ApiKey.model_validate(response[RESOURCE_OBJECT_ATTRIBUTE_NAME]) | ||
|
|
||
|
|
||
| class AsyncApiKeys(ApiKeysModule): | ||
| _http_client: AsyncHTTPClient | ||
|
|
||
| def __init__(self, http_client: AsyncHTTPClient): | ||
| self._http_client = http_client | ||
|
|
||
| async def validate_api_key(self, *, value: str) -> Optional[ApiKey]: | ||
| response = await self._http_client.request( | ||
| API_KEY_VALIDATION_PATH, method=REQUEST_METHOD_POST, json={"value": value} | ||
| ) | ||
| if response.get(RESOURCE_OBJECT_ATTRIBUTE_NAME) is None: | ||
| return None | ||
| return ApiKey.model_validate(response[RESOURCE_OBJECT_ATTRIBUTE_NAME]) | ||
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 @@ | ||
| from .api_keys import ApiKey as ApiKey # noqa: F401 |
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,20 @@ | ||
| from typing import Literal, Optional, Sequence | ||
|
|
||
| from workos.types.workos_model import WorkOSModel | ||
|
|
||
|
|
||
| class ApiKeyOwner(WorkOSModel): | ||
| type: str | ||
| id: str | ||
|
|
||
|
|
||
| class ApiKey(WorkOSModel): | ||
| object: Literal["api_key"] | ||
| id: str | ||
| owner: ApiKeyOwner | ||
| name: str | ||
| obfuscated_value: str | ||
| last_used_at: Optional[str] = None | ||
| permissions: Sequence[str] | ||
| created_at: str | ||
| updated_at: str |
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.