Skip to content

Commit

Permalink
Add API Key authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
barreiro committed Nov 8, 2024
1 parent 4455c32 commit 9ef334c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/horreum/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
class HorreumCredentials:
username: str = None
password: str = None
apikey: str = None

class AuthMethod(Enum):
BEARER = 1
BASIC = 2
API_KEY = 3

@dataclass
class ClientConfiguration:
Expand Down
7 changes: 6 additions & 1 deletion src/horreum/horreum_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ async def setup(self):
"""

if self.__credentials:
if self.__credentials.username is not None:
if self.__credentials.apikey is not None and (self.__client_config is None or self.__client_config.auth_method == AuthMethod.API_KEY):
# API key authentication
self.auth_provider = ApiKeyAuthenticationProvider(KeyLocation.Header, self.__credentials.apikey, "X-Horreum-API-Key")
logger.info('Using API Key authentication')

elif self.__credentials.username is not None:
if self.__client_config is None or self.__client_config.auth_method == AuthMethod.BEARER:
# Bearer token authentication
access_provider = await setup_auth_provider(self.__base_url, self.__credentials.username, self.__credentials.password)
Expand Down
24 changes: 24 additions & 0 deletions test/horreum_client_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from horreum import HorreumCredentials, ClientConfiguration, AuthMethod
from horreum.horreum_client import new_horreum_client, HorreumClient
from horreum.raw_client.api.test.test_request_builder import TestRequestBuilder
from horreum.raw_client.api.user.apikey.apikey_post_request_body import ApikeyPostRequestBody
from horreum.raw_client.models.key_type import KeyType
from horreum.raw_client.models.protected_type_access import ProtectedType_access
from horreum.raw_client.models.test import Test

Expand Down Expand Up @@ -108,6 +110,28 @@ async def test_check_no_tests(authenticated_client: HorreumClient):
assert (await authenticated_client.raw_client.api.test.get(config)).count == 0


@pytest.mark.asyncio
async def test_api_key(custom_authenticated_client: HorreumClient):
key_request = ApikeyPostRequestBody(name="python test key", type=KeyType.USER)
key = await custom_authenticated_client.raw_client.api.user.apikey.post(key_request)
assert key is not None

key_client = await new_horreum_client(base_url="http://localhost:8080",
credentials=HorreumCredentials(apikey=key),
client_config=ClientConfiguration(auth_method=AuthMethod.API_KEY))

# use key to retrieve list of keys
assert len(await key_client.raw_client.api.user.apikey.get()) >= 1

wrong_key_client = await new_horreum_client(base_url="http://localhost:8080",
credentials=HorreumCredentials(apikey=key.swapcase()),
client_config=ClientConfiguration(auth_method=AuthMethod.API_KEY))

# wrong key does not authenticate
with pytest.raises(APIError) as ex: (await wrong_key_client.raw_client.api.user.apikey.get())
assert ex.value.response_status_code == 401


@pytest.mark.asyncio
async def test_check_create_test(custom_authenticated_client: HorreumClient):
# Create new test
Expand Down

0 comments on commit 9ef334c

Please sign in to comment.