49

I am building a real world application where users will access the app primarily from Android, iOS devices as well as Desktops.

From my elementary research, I have realized that token based authentication mechanism is more better and elegant for client-server models as compared to session based authentication.

In Django, I have found two popular ways to do this -

  1. http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication
  2. http://getblimp.github.io/django-rest-framework-jwt/

From what I understood, option 2] is an extension of 1] except that the Token is in the form of JSON(serialized). I would like to understand what other differences there are between option 1] and 2] and the advantages/disadvantages of choosing either.

anon
  • 879
  • 1
  • 9
  • 15
  • 1
    I have somewhat similar setup. What I have done is that for my app client Token authentication works but for my web client session authentication works. Not sure about what advantage JWT will provide ? – Rajesh Kaushik Jul 24 '15 at 09:33
  • possible duplicate of [Appropriate choice of authentication class for python REST API used by web app](http://stackoverflow.com/questions/27578726/appropriate-choice-of-authentication-class-for-python-rest-api-used-by-web-app) – Kevin Brown Jul 31 '15 at 20:19
  • 2
    FWIW, [`django-rest-framework-simplejwt`](https://github.com/davesque/django-rest-framework-simplejwt) seems to be maintained while [`django-rest-framework-jwt`](https://github.com/GetBlimp/django-rest-framework-jwt) is not. – phoenix Jun 26 '19 at 14:42

1 Answers1

82

They both carrying out similar tasks with few differences.

Token

DRF's builtin Token Authentication

  1. One Token for all sessions
  2. No time stamp on the token

DRF JWT Token Authentication

  1. One Token per session
  2. Expiry timestamp on each token

Database access

DRF's builtin Token Authentication

  1. Database access to fetch the user associated with the token
  2. Verify user's status
  3. Authenticate the user

DRF JWT Token Authentication

  1. Decode token (get payload)
  2. Verify token timestamp (expiry)
  3. Database access to fetch user associated with the id in the payload
  4. Verify user's status
  5. Authenticate the user

Pros

DRF's builtin Token Authentication

  1. Allows forced-logout by replacing the token in the database (ex: password change)

DRF JWT Token Authentication

  1. Token with an expiration time
  2. No database hit unless the token is valid

Cons

DRF's builtin Token Authentication

  1. Database hit on all requests
  2. Single token for all sessions

DRF JWT Token Authentication

  1. Unable to recall the token without tracking it in the database
  2. Once the token is issued, anyone with the token can make requests
  3. Specs are open to interpretations, no consensus on how to do refresh
ChrisCrossCrash
  • 147
  • 1
  • 11
un33k
  • 15,254
  • 13
  • 59
  • 64
  • 1
    Sorry, I don't understand your answer. Could you clarify? Do you mean with option 1 you need to remember the user in a session, while for option 2 you just check the username in the URL of the request, so no session is required for option 2? – Sander Vanden Hautte Mar 05 '18 at 13:31
  • @SanderVandenHautte I added more details to my answer. Hope it helps – un33k Mar 06 '18 at 16:21
  • Thanks! That's helpful. – Sander Vanden Hautte Mar 06 '18 at 17:44
  • @un33k can you elaborate this? > Specs are open to interpretations, no consensus on how to do refresh – sphoenix Apr 07 '19 at 16:14
  • 3
    @sphoenix this is a complex topic and stackoverflow is not the right place for a detailed analysis. Please use the above as a reference for the Pros & Cons. Then you can refer to your design needs and the packages to figure out what is best for you. Please note that there is no silver bullet for authentication and anything you pick will have side effects. The mission is to minimize the side effects for your requirements. If I were to recommend something it would be signed-http cookies for authentication and JWT for authorization. Cookie valid for 2 weeks, JWT refreshed every 15 min. – un33k Jan 08 '20 at 15:17