Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is purpose of changing token key name in DjangoRestFramework

In DjangoRestFramework you can change keyword in header for token authentification.

From docs

Note: If you want to use a different keyword in the header, such as Bearer, simply subclass TokenAuthentication and set the keyword class variable.

What is the purpose of changing default keyword? I've seen 'Bearer', 'Basic' and a few other variants, but just don't understand the purpose of doing so. Can anyone explain?

like image 815
Myroslav Hryshyn Avatar asked Aug 17 '17 14:08

Myroslav Hryshyn


People also ask

What is token authentication in DRF?

Token authentication refers to exchanging username and password for a token that will be used in all subsequent requests so to identify the user on the server side. This article revolves about implementing token authentication using Django REST Framework to make an API.

What is Knox authentication?

Knox authentication is token based, similar to the TokenAuthentication built in to DRF. However, it overcomes some problems present in the default implementation: DRF tokens are limited to one per user. This does not facilitate securely signing in from multiple devices, as the token is shared.

How does Django session authentication work?

With session-based auth, a session is generated and the ID is stored in a cookie. After logging in, the server validates the credentials. If valid, it generates a session, stores it, and then sends the session ID back to the browser.


1 Answers

I found how we need to pass the keyword to TokenAuthentication.

class BearerAuthentication(authentication.TokenAuthentication):
    '''
    Simple token based authentication using utvsapitoken.

    Clients should authenticate by passing the token key in the 'Authorization'
    HTTP header, prepended with the string 'Bearer '.  For example:

    Authorization: Bearer 956e252a-513c-48c5-92dd-bfddc364e812
    '''
    keyword = 'Bearer'

Then instead of using authentication.TokenAuthentication in the settings.py we will use BearerAuthentication

# settings.py
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (                                                                                                                                                                                       
        'your.models.BearerAuthentication',
    )
}
like image 159
Druta Ruslan Avatar answered Dec 31 '22 15:12

Druta Ruslan