Skip to content

Commit

Permalink
Rename some things
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Aug 28, 2023
1 parent b151e58 commit bb8ac38
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions apitally/django_ninja.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ninja import NinjaAPI


__all__ = ["ApitallyMiddleware", "AuthorizationAPIKeyHeader", "KeyInfo"]
__all__ = ["ApitallyMiddleware", "APIKeyAuth", "KeyInfo"]


class ApitallyMiddleware(_ApitallyMiddleware):
Expand All @@ -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 <code>Authorization</code> header and the scheme prefix <code>ApiKey</code>.<br>Example: <pre>Authorization: ApiKey your_api_key_here</pre>"

Expand Down
4 changes: 2 additions & 2 deletions apitally/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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()
4 changes: 2 additions & 2 deletions apitally/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from starlette.responses import Response


__all__ = ["ApitallyMiddleware", "ApitallyKeysBackend"]
__all__ = ["ApitallyMiddleware", "APIKeyBackend"]


class ApitallyMiddleware(BaseHTTPMiddleware):
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tests/django_ninja_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions tests/test_django_ninja.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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


Expand Down

0 comments on commit bb8ac38

Please sign in to comment.