diff --git a/apitally/django_ninja.py b/apitally/django_ninja.py index d40b1a4..e36a721 100644 --- a/apitally/django_ninja.py +++ b/apitally/django_ninja.py @@ -15,7 +15,7 @@ from ninja import NinjaAPI -__all__ = ["ApitallyMiddleware", "AuthorizationAPIKeyHeader", "KeyInfo"] +__all__ = ["ApitallyMiddleware", "APIKeyAuth", "KeyInfo"] class ApitallyMiddleware(_ApitallyMiddleware): @@ -38,7 +38,7 @@ class PermissionDenied(AuthError): pass -class AuthorizationAPIKeyHeader(APIKeyHeader): +class APIKeyAuth(APIKeyHeader): param_name = "Authorization" openapi_description = "Provide your API key using the Authorization header and the scheme prefix ApiKey.
Example:
Authorization: ApiKey your_api_key_here
" diff --git a/apitally/fastapi.py b/apitally/fastapi.py index a32a6aa..62d7097 100644 --- a/apitally/fastapi.py +++ b/apitally/fastapi.py @@ -16,7 +16,7 @@ __all__ = ["ApitallyMiddleware", "KeyInfo", "api_key_auth"] -class AuthorizationAPIKeyHeader(SecurityBase): +class APIKeyAuth(SecurityBase): def __init__(self, *, auto_error: bool = True): self.model = APIKey( **{"in": APIKeyIn.header}, # type: ignore[arg-type] @@ -52,4 +52,4 @@ async def __call__(self, request: Request, security_scopes: SecurityScopes) -> O return key_info -api_key_auth = AuthorizationAPIKeyHeader() +api_key_auth = APIKeyAuth() diff --git a/apitally/starlette.py b/apitally/starlette.py index c76d128..3ab3a41 100644 --- a/apitally/starlette.py +++ b/apitally/starlette.py @@ -32,7 +32,7 @@ from starlette.responses import Response -__all__ = ["ApitallyMiddleware", "ApitallyKeysBackend"] +__all__ = ["ApitallyMiddleware", "APIKeyBackend"] class ApitallyMiddleware(BaseHTTPMiddleware): @@ -92,7 +92,7 @@ def get_path_template(request: Request) -> Tuple[str, bool]: return request.url.path, False -class ApitallyKeysBackend(AuthenticationBackend): +class APIKeyBackend(AuthenticationBackend): async def authenticate(self, conn: HTTPConnection) -> Optional[Tuple[AuthCredentials, BaseUser]]: if "Authorization" not in conn.headers: return None diff --git a/tests/django_ninja_urls.py b/tests/django_ninja_urls.py index 80f592f..381586c 100644 --- a/tests/django_ninja_urls.py +++ b/tests/django_ninja_urls.py @@ -2,28 +2,28 @@ from django.urls import path from ninja import NinjaAPI -from apitally.django_ninja import AuthorizationAPIKeyHeader +from apitally.django_ninja import APIKeyAuth api = NinjaAPI() -@api.get("/foo", auth=AuthorizationAPIKeyHeader()) +@api.get("/foo", auth=APIKeyAuth()) def foo(request: HttpRequest) -> str: return "foo" -@api.get("/foo/{bar}", auth=AuthorizationAPIKeyHeader(scopes=["foo"])) +@api.get("/foo/{bar}", auth=APIKeyAuth(scopes=["foo"])) def foo_bar(request: HttpRequest, bar: int) -> str: return f"foo: {bar}" -@api.post("/bar", auth=AuthorizationAPIKeyHeader(scopes=["bar"])) +@api.post("/bar", auth=APIKeyAuth(scopes=["bar"])) def bar(request: HttpRequest) -> str: return "bar" -@api.put("/baz", auth=AuthorizationAPIKeyHeader()) +@api.put("/baz", auth=APIKeyAuth()) def baz(request: HttpRequest) -> str: raise ValueError("baz") diff --git a/tests/test_django_ninja.py b/tests/test_django_ninja.py index b0102e0..164c544 100644 --- a/tests/test_django_ninja.py +++ b/tests/test_django_ninja.py @@ -58,7 +58,7 @@ def client() -> Client: def test_middleware_requests_ok(client: Client, mocker: MockerFixture): mock = mocker.patch("apitally.client.base.RequestLogger.log_request") - mocker.patch("apitally.django_ninja.AuthorizationAPIKeyHeader.authenticate") + mocker.patch("apitally.django_ninja.APIKeyAuth.authenticate") response = client.get("/api/foo/123") assert response.status_code == 200 @@ -78,7 +78,7 @@ def test_middleware_requests_ok(client: Client, mocker: MockerFixture): def test_middleware_requests_error(client: Client, mocker: MockerFixture): mock = mocker.patch("apitally.client.base.RequestLogger.log_request") - mocker.patch("apitally.django_ninja.AuthorizationAPIKeyHeader.authenticate") + mocker.patch("apitally.django_ninja.APIKeyAuth.authenticate") response = client.put("/api/baz") assert response.status_code == 500 diff --git a/tests/test_starlette.py b/tests/test_starlette.py index 5ddb735..c8416bf 100644 --- a/tests/test_starlette.py +++ b/tests/test_starlette.py @@ -48,7 +48,7 @@ def app_with_auth() -> Starlette: from starlette.responses import JSONResponse, PlainTextResponse from starlette.routing import Route - from apitally.starlette import ApitallyKeysBackend + from apitally.starlette import APIKeyBackend @requires(["authenticated", "foo"]) def foo(request: Request): @@ -75,7 +75,7 @@ def baz(request: Request): Route("/baz/", baz), ] app = Starlette(routes=routes) - app.add_middleware(AuthenticationMiddleware, backend=ApitallyKeysBackend()) + app.add_middleware(AuthenticationMiddleware, backend=APIKeyBackend()) return app