Skip to content

Commit 6f5c6aa

Browse files
authored
Add type hints to complete developer interface (#7)
1 parent 3ab2307 commit 6f5c6aa

File tree

5 files changed

+13
-6
lines changed

5 files changed

+13
-6
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ lint: # Run code linters
1111
isort --profile black --check --diff .
1212
black --check --diff --color .
1313
flake8 --max-line-length 88 --max-complexity 8 --select C,E,F,W,B,B950,S --ignore E203,E501 ninja_apikey
14-
# TODO: mypy ninja_apikey
14+
mypy --strict ninja_apikey/security.py
1515

1616
test: # Run tests
1717
pytest --ds=sample_project.settings -v sample_project ninja_apikey/tests.py

ninja_apikey/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .security import generate_key
55

66

7-
@admin.action(description="Revoke selected API keys")
7+
@admin.action(description="Revoke selected API keys") # type: ignore
88
def revoke_key(modeladmin, request, queryset):
99
queryset.update(revoked=True)
1010

@@ -24,7 +24,7 @@ class APIKeyAdmin(admin.ModelAdmin):
2424
actions = [revoke_key]
2525
list_filter = ["revoked"]
2626

27-
@admin.display
27+
@admin.display # type: ignore
2828
def is_active(self, obj: APIKey):
2929
return obj.is_valid
3030

ninja_apikey/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# type: ignore
12
from django.contrib.auth import get_user_model
23
from django.db import models
34
from django.utils import timezone

ninja_apikey/security.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from collections import namedtuple
2+
from typing import Any, Optional
23

34
from django.contrib.auth.hashers import check_password, make_password
5+
from django.http import HttpRequest
46
from django.utils.crypto import get_random_string
57
from ninja.security import APIKeyHeader
68

7-
from .models import APIKey
9+
from .models import APIKey # type: ignore
810

911
KeyData = namedtuple("KeyData", "prefix key hashed_key")
1012

@@ -16,7 +18,7 @@ def generate_key() -> KeyData:
1618
return KeyData(prefix, key, hashed_key)
1719

1820

19-
def check_apikey(api_key: str):
21+
def check_apikey(api_key: str) -> Any:
2022
if not api_key:
2123
return False
2224

@@ -53,7 +55,10 @@ def check_apikey(api_key: str):
5355
class APIKeyAuth(APIKeyHeader):
5456
param_name = "X-API-Key"
5557

56-
def authenticate(self, request, key):
58+
def authenticate(self, request: HttpRequest, key: Optional[str]) -> Any:
59+
if not key:
60+
return False
61+
5762
user = check_apikey(key)
5863

5964
if not user:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ test = [
5353
"flake8-bugbear",
5454
"flake8-bandit",
5555
"mypy",
56+
"django-stubs",
5657
]

0 commit comments

Comments
 (0)