Konstantin Paulus
Konstantin Paulus

Reputation: 2195

Django REST Framework Authentication keyword

I'm trying to rename the Rest_framework TokenAuthentication keyword from "Token" to "Bearer" as suggested in the Documentation I have subclassed the TokenAuthentication class like this:

in module: user/authentication.py

from rest_framework import authentication

class TokenAuthentication(authentication.TokenAuthentication):
    """
    Simple token based authentication.
    Clients should authenticate by passing the token key in the "Authorization"
    HTTP header, prepended with the string "Token ".  For example:
    Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a
    """

    keyword = 'Bearer'

in module app/settings.py

 REST_FRAMEWORK = {
     'DEFAULT_AUTHENTICATION_CLASSES': (
         'user.authentication.TokenAuthentication',
     ),
 }

It is still sending me a 401 Unauthorized when im using 'Authorization: Bearer ...token...' but not with 'Authorization: Token ...token...'

What am I doing wrong ?

Upvotes: 3

Views: 1143

Answers (2)

Yareth Sanchez
Yareth Sanchez

Reputation: 61

from rest_framework import authentication
    
class TokenAuthentication(authentication.TokenAuthentication):
    authentication.TokenAuthentication.keyword = 'Bearer'

Upvotes: 6

Shahab Seyedi
Shahab Seyedi

Reputation: 66

You are missing one last step. Import your TokenAuthentication class that contains the new keyword in your View class instead of the default TokenAuthentication class.

Upvotes: 1

Related Questions