Skip to content

Commit

Permalink
django.contrib.auth: Relax ModelBackend -> BaseBackend (#2141)
Browse files Browse the repository at this point in the history
`BaseBackend` is the base interface of django backends, `ModelBackend`
has more specific signatures, such as `.authenticate()` including
`username` and `password`, not all authentication backends use those.

For instance, since the `authenticate()` method in BaseBackend already includes
request (`HttpRequest`), it can read a `Bearer` token or cookie, and already accepts
`**kwargs` for additional parameters.

See also:
- BaseBackend: https://github.com/django/django/blob/5.0.6/django/contrib/auth/backends.py#L8-L28
  - ModelBackend: https://github.com/django/django/blob/5.0.6/django/contrib/auth/backends.py#L31-L160
- django.contrib.auth.authenticate(): https://github.com/django/django/blob/5.0.6/django/contrib/auth/__init__.py#L65-L93
- Django Docs for "Writing an authentication backend": https://docs.djangoproject.com/en/5.0/topics/auth/customizing/#writing-an-authentication-backend
  • Loading branch information
tony authored May 9, 2024
1 parent 5216eaf commit a26f96a
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions django-stubs/contrib/auth/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any

from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.backends import BaseBackend
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import AnonymousUser
from django.db.models.options import Options
Expand All @@ -16,15 +16,15 @@ BACKEND_SESSION_KEY: str
HASH_SESSION_KEY: str
REDIRECT_FIELD_NAME: str

def load_backend(path: str) -> ModelBackend: ...
def get_backends() -> list[ModelBackend]: ...
def load_backend(path: str) -> BaseBackend: ...
def get_backends() -> list[BaseBackend]: ...
def authenticate(request: HttpRequest | None = ..., **credentials: Any) -> AbstractBaseUser | None: ...
async def aauthenticate(request: HttpRequest | None = ..., **credentials: Any) -> AbstractBaseUser | None: ...
def login(
request: HttpRequest, user: AbstractBaseUser | None, backend: type[ModelBackend] | str | None = ...
request: HttpRequest, user: AbstractBaseUser | None, backend: type[BaseBackend] | str | None = ...
) -> None: ...
async def alogin(
request: HttpRequest, user: AbstractBaseUser | None, backend: type[ModelBackend] | str | None = ...
request: HttpRequest, user: AbstractBaseUser | None, backend: type[BaseBackend] | str | None = ...
) -> None: ...
def logout(request: HttpRequest) -> None: ...
async def alogout(request: HttpRequest) -> None: ...
Expand Down

0 comments on commit a26f96a

Please sign in to comment.