Julian A.
Julian A.

Reputation: 11470

How can I set up token-based authentication for web services using Python?

I'm relatively new to web authentication, Python and Django. That said, I need to set up per-user token-based authentication for RESTful services and I'm not sure where to start. Any pointers would be greatly appreciated.

Upvotes: 4

Views: 2133

Answers (2)

Thierry Lam
Thierry Lam

Reputation: 46294

I've done something similar where 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 login a Django User as mentioned in the doc. Once the user has been successfully authenticated, create or update the UserProfile to store the token which you will get from calling an authentication WS or another WS which will return you a unique token.

While the current user is authenticated, you should be able to use the stored token to call other WS.

In your settings.py:

AUTH_PROFILE_MODULE = 'userprofiles.userprofile'

From the view code, it goes something like:

request.user.get_profile().token

Upvotes: 2

There's a django-tokenapi package. Its homepage/README looks quite informative. Did you already take a look at that? If so, please clarify the question as to what you have tried and what problems you've faced.

Note that I'm not very familiar with Django, so I might not be able to help further.

Upvotes: 2

Related Questions