Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax typing of backend in django.contrib.auth (ModelBackend -> BaseBackend) #2141

Merged
merged 1 commit into from
May 9, 2024

Conversation

tony
Copy link
Contributor

@tony tony commented May 9, 2024

Summary

BaseBackend is the base interface of django backends, ModelBackend has more specific signatures, such as .authenticate() including username and password, which isn't true for all backends, such as custom backends. ModelBackend is simply a backend included by Django it uses by default.

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

Related issues

`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
@tony tony changed the title django.contrib.auth: Relax ModelBackend -> BaseBackend Relax typing of backend in django.contrib.auth (ModelBackend -> BaseBackend) May 9, 2024
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sobolevn sobolevn merged commit a26f96a into typeddjango:master May 9, 2024
36 checks passed
@tony tony deleted the auth-backend-types branch May 9, 2024 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

django.contrib.auth.__init__'s backend param: Too specific?
2 participants