-
Notifications
You must be signed in to change notification settings - Fork 22
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
Open
denizs
wants to merge
8
commits into
workos:main
Choose a base branch
from
denizs:minor/expose-api-key-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−6
Open
Changes from all commits
Commits
Show all changes
8 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 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
Some comments aren't visible on the classic Files Changed page.
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,52 @@ | ||
| # 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 | ||
| from workos.exceptions import AuthenticationException | ||
|
|
||
|
|
||
| @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, | ||
| {"message": "Invalid API key", "error": "invalid_api_key"}, | ||
| 401, | ||
| ) | ||
|
|
||
| with pytest.raises(AuthenticationException): | ||
| syncify(module_instance.validate_api_key(value="invalid-key")) |
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,54 @@ | ||
| from typing import 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" | ||
|
|
||
|
|
||
| class ApiKeysModule(Protocol): | ||
| def validate_api_key(self, *, value: str) -> SyncOrAsync[ApiKey]: | ||
| """Validates the configured API key. | ||
|
|
||
| Returns: | ||
| ApiKey: The validated API key details containing | ||
| information about the key's name and usage | ||
|
|
||
| Raises: | ||
| AuthenticationException: If the API key is invalid or | ||
| unauthorized (401) | ||
| NotFoundException: If the API key is not found (404) | ||
| ServerException: If the API server encounters an error | ||
| (5xx) | ||
| """ | ||
| ... | ||
|
|
||
|
|
||
| class ApiKeys(ApiKeysModule): | ||
| _http_client: SyncHTTPClient | ||
|
|
||
| def __init__(self, http_client: SyncHTTPClient): | ||
| self._http_client = http_client | ||
|
|
||
| def validate_api_key(self, *, value: str) -> ApiKey: | ||
| response = self._http_client.request( | ||
| API_KEY_VALIDATION_PATH, method=REQUEST_METHOD_POST, json={ | ||
| "value": value} | ||
| ) | ||
| return ApiKey.model_validate(response["api_key"]) | ||
|
|
||
|
|
||
| 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) -> ApiKey: | ||
| response = await self._http_client.request( | ||
| API_KEY_VALIDATION_PATH, method=REQUEST_METHOD_POST, json={ | ||
| "value": value} | ||
| ) | ||
| return ApiKey.model_validate(response["api_key"]) | ||
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 | ||||
|
|
||||
| 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: str | None = None | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line caused some issues for me with a test project. I see a different approach in another type file, would this work here?
|
||||
| permissions: list[str] | ||||
| created_at: str | ||||
| updated_at: str | ||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I passed an invalid value into this method, I saw this error message:
From the docs:
Do we need to handle the invalid case differently?