Skip to content

Commit

Permalink
Merge pull request #31 from claroty/reuvens-patch-marshmallow
Browse files Browse the repository at this point in the history
Fix dependency versions
Remove unused deps
Fix warnings
  • Loading branch information
mixmind authored Sep 15, 2022
2 parents c5aa6f4 + f68854e commit 6c0b7bf
Show file tree
Hide file tree
Showing 5 changed files with 1,509 additions and 31 deletions.
7 changes: 4 additions & 3 deletions jwthenticator/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from environs import Env

DAYS_TO_SECONDS = lambda x: x * 60 * 24
def days_to_seconds(days: int) -> int:
return days * 60 * 24

env = Env()

Expand All @@ -20,8 +21,8 @@
JWT_AUDIENCE = env.list("JWT_AUDIENCE", [])

# Token consts
KEY_EXPIRY = env.int("KEY_EXPIRY", DAYS_TO_SECONDS(120)) # In seconds
REFRESH_TOKEN_EXPIRY = env.int("REFRESH_TOKEN_EXPIRY", DAYS_TO_SECONDS(60)) # In seconds
KEY_EXPIRY = env.int("KEY_EXPIRY", days_to_seconds(120)) # In seconds
REFRESH_TOKEN_EXPIRY = env.int("REFRESH_TOKEN_EXPIRY", days_to_seconds(60)) # In seconds

# Keys from env
RSA_PUBLIC_KEY = env("RSA_PUBLIC_KEY", None)
Expand Down
3 changes: 1 addition & 2 deletions jwthenticator/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from aiohttp import web, ClientSession
from aiohttp.client import ClientSession as ClientSessionType
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop, TestClient
from aiohttp.test_utils import AioHTTPTestCase, TestClient

from jwthenticator.server import Server
from jwthenticator.client import Client, InternalClient
Expand Down Expand Up @@ -65,7 +65,6 @@ async def setUpAsync(self) -> None:

await self.client.start_server()

@unittest_run_loop
async def test_client_and_authenticated_server(self) -> None:
key = await random_key()
uuid_identifier = uuid4()
Expand Down
24 changes: 1 addition & 23 deletions jwthenticator/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from http import HTTPStatus
from unittest.mock import MagicMock

from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
from aiohttp.test_utils import AioHTTPTestCase
from aiohttp.web import Application
from jwt import PyJWKClient

Expand Down Expand Up @@ -59,7 +59,6 @@ async def perform_auth(self) -> TokenResponse:


# Sanity Tests
@unittest_run_loop
async def test_full_flow(self) -> None:
key = await random_key()
uuid_identifier = uuid4()
Expand Down Expand Up @@ -107,14 +106,12 @@ async def test_full_flow(self) -> None:
assert jwks_client.get_signing_key_from_jwt(token)


@unittest_run_loop
async def test_bad_json_request(self) -> None:
for route in POST_ROUTES:
response = await self.client.post(route, json="{")
assert response.status == HTTPStatus.BAD_REQUEST

# Authenticate Tests
@unittest_run_loop
async def test_authentication_bad_request(self) -> None:
# Missing field
request = {"key": await random_key()}
Expand All @@ -126,13 +123,11 @@ async def test_authentication_bad_request(self) -> None:
response = await self.client.post("/authenticate", json=request)
assert response.status == HTTPStatus.BAD_REQUEST

@unittest_run_loop
async def test_authentication_unknown_key(self) -> None:
request = self.auth_request_schema.dump(AuthRequest(key=await random_key(), identifier=uuid4()))
response = await self.client.post("/authenticate", json=request)
assert response.status == HTTPStatus.UNAUTHORIZED

@unittest_run_loop
async def test_authentication_expired_key(self) -> None:
key = await self.register_key()
request = self.auth_request_schema.dump(AuthRequest(key=key, identifier=uuid4()))
Expand All @@ -142,7 +137,6 @@ async def test_authentication_expired_key(self) -> None:
assert response.status == HTTPStatus.UNAUTHORIZED

# Refresh Tests
@unittest_run_loop
async def test_refresh_bad_request(self) -> None:
# Missing field
request = {"refresh_token": await random_refresh_token()}
Expand All @@ -154,13 +148,11 @@ async def test_refresh_bad_request(self) -> None:
response = await self.client.post("/refresh", json=request)
assert response.status == HTTPStatus.BAD_REQUEST

@unittest_run_loop
async def test_refresh_unknown_refresh_token(self) -> None:
request = self.refresh_request_schema.dump(RefreshRequest(refresh_token=await random_refresh_token(), identifier=uuid4()))
response = await self.client.post("/refresh", json=request)
assert response.status == HTTPStatus.UNAUTHORIZED

@unittest_run_loop
async def test_refresh_expired_refresh_token(self) -> None:
token_response_obj = await self.perform_auth()
request = self.refresh_request_schema.dump(RefreshRequest(refresh_token=token_response_obj.refresh_token, identifier=uuid4())) # type: ignore
Expand All @@ -171,7 +163,6 @@ async def test_refresh_expired_refresh_token(self) -> None:
assert response.status == HTTPStatus.UNAUTHORIZED

# Validate Tests
@unittest_run_loop
async def test_validate_bad_request(self) -> None:
# Missing field
response = await self.client.post("/validate", json={})
Expand All @@ -182,7 +173,6 @@ async def test_validate_bad_request(self) -> None:
response = await self.client.post("/validate", json=request)
assert response.status == HTTPStatus.BAD_REQUEST

@unittest_run_loop
async def test_validate_bad_jwt(self) -> None:
token_response_obj = await self.perform_auth()
request = self.jwt_validate_request_schema.dump(JWTValidateRequest(jwt=token_response_obj.jwt[:-2]))
Expand All @@ -193,7 +183,6 @@ async def test_validate_bad_jwt(self) -> None:
response = await self.client.post("/validate", json=request)
assert response.status == HTTPStatus.UNAUTHORIZED

@unittest_run_loop
async def test_validate_expired_jwt(self) -> None:
token_response_obj = await self.perform_auth()
request = self.jwt_validate_request_schema.dump(JWTValidateRequest(jwt=token_response_obj.jwt))
Expand All @@ -205,7 +194,6 @@ async def test_validate_expired_jwt(self) -> None:


# Register Key Tests
@unittest_run_loop
async def test_register_key_bad_request(self) -> None:
# Missing field
response = await self.client.post("/register_key", json={})
Expand All @@ -216,7 +204,6 @@ async def test_register_key_bad_request(self) -> None:
response = await self.client.post("/register_key", json=request)
assert response.status == HTTPStatus.BAD_REQUEST

@unittest_run_loop
async def test_register_key_already_registered(self) -> None:
# Already registered (and still valid) key
key = await self.register_key()
Expand All @@ -232,7 +219,6 @@ async def test_register_key_already_registered(self) -> None:


# Is Key Registered Tests
@unittest_run_loop
async def test_is_key_registered_bad_request(self) -> None:
# Missing field
response = await self.client.post("/is_key_registered", json={})
Expand All @@ -245,14 +231,12 @@ async def test_is_key_registered_bad_request(self) -> None:


# Validate request tests
@unittest_run_loop
async def test_validate_request(self) -> None:
token_response_obj = await self.perform_auth()
headers = {"Authorization": f"Bearer {token_response_obj.jwt}"}
response = await self.client.get("/validate_request", headers=headers)
assert response.status == HTTPStatus.OK

@unittest_run_loop
async def test_validate_request_expired_token(self) -> None:
token_response_obj = await self.perform_auth()
headers = {"Authorization": f"Bearer {token_response_obj.jwt}"}
Expand All @@ -262,7 +246,6 @@ async def test_validate_request_expired_token(self) -> None:
response = await self.client.get("/validate_request", headers=headers)
assert response.status == HTTPStatus.UNAUTHORIZED

@unittest_run_loop
async def test_validate_request_bad_header(self) -> None:
# No Authorization header
response = await self.client.get("/validate_request")
Expand All @@ -285,12 +268,10 @@ async def get_application(self) -> Application:
self.app = self.api_server.app
return self.app

@unittest_run_loop
async def test_external_api_sanity(self) -> None:
response = await self.client.post("/validate", json={})
assert response.status == HTTPStatus.BAD_REQUEST

@unittest_run_loop
async def test_disabled_internal_api(self) -> None:
response = await self.client.post("/register_key", json={})
assert response.status == HTTPStatus.NOT_FOUND
Expand All @@ -310,12 +291,10 @@ async def get_application(self) -> Application:
self.app = self.api_server.app
return self.app

@unittest_run_loop
async def test_internal_api_sanity(self) -> None:
response = await self.client.post("/register_key", json={})
assert response.status == HTTPStatus.BAD_REQUEST

@unittest_run_loop
async def test_disabled_external_api(self) -> None:
response = await self.client.post("/authenticate", json={})
assert response.status == HTTPStatus.NOT_FOUND
Expand All @@ -326,7 +305,6 @@ async def test_disabled_external_api(self) -> None:
response = await self.client.post("/validate", json={})
assert response.status == HTTPStatus.NOT_FOUND

@unittest_run_loop
async def test_health_check(self) -> None:
response = await self.client.get("/health")
assert response.status == HTTPStatus.OK
Loading

0 comments on commit 6c0b7bf

Please sign in to comment.