Skip to content

Commit 0e65d46

Browse files
committed
return None on invalid API key
1 parent 686c98e commit 0e65d46

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

tests/test_api_keys.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from tests.utils.fixtures.mock_api_key import MockApiKey
55
from tests.utils.syncify import syncify
66
from workos.api_keys import API_KEY_VALIDATION_PATH, ApiKeys, AsyncApiKeys
7-
from workos.exceptions import AuthenticationException
87

98

109
@pytest.mark.sync_and_async(ApiKeys, AsyncApiKeys)
@@ -44,9 +43,8 @@ def test_validate_api_key_with_invalid_key(
4443
):
4544
mock_http_client_with_response(
4645
module_instance._http_client,
47-
{"message": "Invalid API key", "error": "invalid_api_key"},
48-
401,
46+
{"api_key": None},
47+
200,
4948
)
5049

51-
with pytest.raises(AuthenticationException):
52-
syncify(module_instance.validate_api_key(value="invalid-key"))
50+
assert syncify(module_instance.validate_api_key(value="invalid-key")) is None

workos/api_keys.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
from typing import Protocol
1+
from typing import Optional, Protocol
22

33
from workos.types.api_keys import ApiKey
44
from workos.typing.sync_or_async import SyncOrAsync
55
from workos.utils.http_client import AsyncHTTPClient, SyncHTTPClient
66
from workos.utils.request_helper import REQUEST_METHOD_POST
77

88
API_KEY_VALIDATION_PATH = "api_keys/validations"
9+
RESOURCE_OBJECT_PATH = "api_key"
910

1011

1112
class ApiKeysModule(Protocol):
12-
def validate_api_key(self, *, value: str) -> SyncOrAsync[ApiKey]:
13-
"""Validates the configured API key.
13+
def validate_api_key(self, *, value: str) -> SyncOrAsync[Optional[ApiKey]]:
14+
"""Validate an API key.
15+
16+
Kwargs:
17+
value (str): API key value
1418
1519
Returns:
16-
ApiKey: The validated API key details containing
17-
information about the key's name and usage
18-
19-
Raises:
20-
AuthenticationException: If the API key is invalid or
21-
unauthorized (401)
22-
NotFoundException: If the API key is not found (404)
23-
ServerException: If the API server encounters an error
24-
(5xx)
20+
Optional[ApiKey]: Returns ApiKey resource object
21+
if supplied value was valid, None if it was not
2522
"""
2623
...
2724

@@ -32,12 +29,13 @@ class ApiKeys(ApiKeysModule):
3229
def __init__(self, http_client: SyncHTTPClient):
3330
self._http_client = http_client
3431

35-
def validate_api_key(self, *, value: str) -> ApiKey:
32+
def validate_api_key(self, *, value: str) -> Optional[ApiKey]:
3633
response = self._http_client.request(
37-
API_KEY_VALIDATION_PATH, method=REQUEST_METHOD_POST, json={
38-
"value": value}
34+
API_KEY_VALIDATION_PATH, method=REQUEST_METHOD_POST, json={"value": value}
3935
)
40-
return ApiKey.model_validate(response["api_key"])
36+
if response.get(RESOURCE_OBJECT_PATH) is None:
37+
return None
38+
return ApiKey.model_validate(response[RESOURCE_OBJECT_PATH])
4139

4240

4341
class AsyncApiKeys(ApiKeysModule):
@@ -46,9 +44,10 @@ class AsyncApiKeys(ApiKeysModule):
4644
def __init__(self, http_client: AsyncHTTPClient):
4745
self._http_client = http_client
4846

49-
async def validate_api_key(self, *, value: str) -> ApiKey:
47+
async def validate_api_key(self, *, value: str) -> Optional[ApiKey]:
5048
response = await self._http_client.request(
51-
API_KEY_VALIDATION_PATH, method=REQUEST_METHOD_POST, json={
52-
"value": value}
49+
API_KEY_VALIDATION_PATH, method=REQUEST_METHOD_POST, json={"value": value}
5350
)
54-
return ApiKey.model_validate(response["api_key"])
51+
if response.get(RESOURCE_OBJECT_PATH) is None:
52+
return None
53+
return ApiKey.model_validate(response[RESOURCE_OBJECT_PATH])

0 commit comments

Comments
 (0)