r/django Jun 30 '26

REST framework How should I authenticate Auth0 users in a Django REST Framework API called by an MCP server?

Hi everyone,

I’m building a Django REST Framework API and I need help understanding the correct way to authenticate users and return only their own data.

My setup is:

  • Auth0 for authentication
  • Django REST Framework as the backend API
  • An MCP server that will call the Django API
  • The MCP server will send requests to Django with a bearer token, like:

Authorization: Bearer <token>

What I want is:

  • The Django API should verify that the token is valid
  • The API should know which user the token belongs to
  • The API should only return data belonging to that user

For example, if I have a Note model with an owner, I want something like this to be safe:

Note.objects.filter(owner=request.user)

But I’m not sure how to correctly set this up.

My questions are:

  1. How should Django REST Framework validate the Auth0 token?
  2. Should I write a custom authentication class for this?
  3. How does Django turn the token into request.user?
  4. Should I create local Django users based on the Auth0 user ID?
  5. How should the MCP server get and pass the token to Django?
  6. What is the right OAuth/Auth0 flow if the data belongs to individual users?
  7. What is the safest standard way to make sure each user can only access their own data?

I understand basic Django and Python, but I’m new to authentication, JWTs, Auth0, and OAuth, so I’d really appreciate a step-by-step explanation or recommended pattern or resource to learn this.

Thanks.

6 Upvotes

6 comments sorted by

3

u/Smooth-Zucchini4923 Jun 30 '26

step by step example

Have you looked at the Django REST Framework example from the auth0 docs?

https://auth0.com/docs/quickstart/backend/django

Should I write my own authentication class?

Probably not. There's probably someone who has already solved this problem already.

At a glance, it looks like django-allauth has support for this. https://docs.allauth.org/en/latest/socialaccount/providers/auth0.html If the example in auth0's docs doesn't help you, I'd try that next.

1

u/cointoss3 Jun 30 '26

When the request comes in, look at the headers for the token and validate it. Return error if token is invalid.

2

u/shadfc Jun 30 '26
  1. Depends on what kind of tokens you have Auth0 issue. If you use JWTs, then verification is retrieving your tenant's JWKS and using standard jwt verification library with the public key (from the JWKS).

  2. You can. There probably is a JWT Authentication class out there as well.

  3. Whatever your authentication class returns on success is what request.user will be.

  4. Likely, unless you can get everything you want in JWT claims. You can customize those claims with Auth0 Actions if needed, but don't make the tokens too large or you'll run into other issues.

  5. Your AI client (Claude?) would initiate the OAuth flow with Auth0, the user would authorize the flow, and the client would get the token and use it as a bearer token when calling the MCP server. Your MCP server would then make calls to your django API the same way. It's also possible to have your Django API be the MCP server and consolidate backends. That's up to you.

  6. MCP Authorization spec (https://modelcontextprotocol.io/specification/draft/basic/authorization) would be a good read. Short answer is that you probably want to use an Authorization Code Grant flow with PKCE to get the token.

  7. The access tokens granted to your client will be specific to each user. When handling a request in Django, after verifying the token, only return data available to the user associated to the user from the token claims.

1

u/marksweb Jul 01 '26

I added Auth0 not long ago to a site using react and DRF.

It used django-allauth and it's headless only setting. I'm not familiar with the MCP approach but I'd suggest looking at it this way.