How to configure token-based authentication for web services using Python?

I am relatively new to web authentication, Python, and Django. However, I need to configure token-based authentication for each user for RESTful services, and I'm not sure where to start. Any pointers would be greatly appreciated.

+4
source share
2 answers

I did something similar when each user will have their own unique token. I created a UserProfile to store the token:

from django.contrib.auth.models import User class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) token = models.CharField(max_length=100, blank=True) 

The idea is to log in to a Django user as indicated in the document. After successful user authentication, create or update a UserProfile to save the token you receive from the WS WS call, or another WS that will return a unique token to you.

While the current user is authenticated, you can use the saved token to call another WS.

In your settings.py :

 AUTH_PROFILE_MODULE = 'userprofiles.userprofile' 

From the view code, it looks something like this:

 request.user.get_profile().token 
+2
source

There is django-tokenapi . Its homepage / README looks pretty informative. Have you looked at this already? If so, please clarify the question of what you tried and what problems you encountered.

Please note that I am not very familiar with Django, so I may not help anymore.

+2
source

Source: https://habr.com/ru/post/1346120/


All Articles