django / django/new-features

moving the process of filling request.user to authentication backends

Open
#193 0 comments 4 reactions 0 assignees View on GitHub
Authentication/Users Django Core Middleware
Dominant language
No language data
Stars
188
Forks
7
PR merge metrics
No merged PRs in 30d

Description

### Code of Conduct

- [x] I agree to follow Django's Code of Conduct

### Feature Description

hello
in the process of filling request.user in the [AuthenticationMiddleware](https://github.com/django/django/blob/main/django/contrib/auth/middleware.py#L19), it calls `auth.get_user`
and that calls [_get_user_session_key](https://github.com/django/django/blob/main/django/contrib/auth/__init__.py#L287) which is a hardcoded function in django, and as the name suggests, it only supports session base authentication

but django has authentication backends, which are plugable, and allow writers of auth libraries to plug-in their own logic, why would this part be excluded?
this limits django's auth system to only support session based authentication, and a lot of the toolings django provides don't work in other situations (such as tokens)

i propose moving this logic to backends, this way, auth libraries can come in and fill request.user, even if the user is authenticated using other methods, such as tokens and JWT.

### Problem

if we look at API libraries in django ecosystem, they are all making their own auth system, that is not compatible with each other nor django,
and we have to maintain multiple versions of libraries that support tokens or JWT for each of them.
i would argue, since django already has an auth system, authentication should go through this system and libraries should be compatible.
(or atleast have the ability to be compatible)

### Request or proposal

request

### Additional Details

_No response_

### Implementation Suggestions

_No response_

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with AuthenticationMiddleware in django/contrib/auth/middleware.py, then trace auth.get_user and _get_user_session_key in django/contrib/auth/__init__.py. Review how authentication backends currently participate and define how backend-provided authentication would populate request.user for non-session methods such as tokens or JWT. Done should include a decided backend API and compatible behavior for existing session authentication.

Written by the indexing model from the issue text.

Assessment

Tech stack
django
Domain
authentication
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.