JWT authorization in Python, Part 2: THEORY.
OUR Blog
Python developer
Alexey Milogradov
Python developer
Mar 30 2016

JWT authorization in Python, Part 2: THEORY.

Plenty of services exist in modern information technology world. The concept of sharing commonly needed information among those services is both powerful and dangerous at the same time. That's why information privacy the user stores on the internet requires protection and careful permissions management.

A whole industry of information security arose to handle this problem and a lot of concepts, principles and tools were developed to split the problem into smaller ones and solve them separately. One of such tools is JSON Web Token standard, one of the approaches to token-based authentication.

Token-based authentication

Token based authentication is a mechanism, along with session-based authentication and simple login/password scheme. The core concept of token-based approach is simple: user enters his login and password, then receives token which he can use to get access to allowed resources for an allowed amount of time.

Json Web Token

JWT is a standard. It is written down in RFC 7519 and relies on two other standards: RFC 7515 JSON Web Encryption (JWE) and RFC 7516 JSON Web Signature (JWS).

The standard contains specification of reserved keywords, creation and validation process description, implementation requirements, examples of tokens and other information. To get the most reliable information, please, use the standard specification

JWT usage overview

As it was mentioned above, process of using JWT is rather simple even without additional authentication framework. Here is the short overview of steps required:

  1. Desired information is encoded using specified algorithm and secret phrase. Special information (defined in Registered Claim Names section of standard) may be added, like expiration time. Some of this information, such as exp, is handled automatically by specific implementations. The output is a token of the following type aaaaa.bbbbb.ccccccc.
  2. The generated token is passed to a client. There is a few choices of where to store tokens in the client. For web-application it can be a local storage or cookies. There is the discussion on the internet on this topic, such as [1].
  3. On requests, token is attached to headers or passed in cookies, depending on chosen storage. Server app is responsible for decoding and validation of data encoded, as well as for resolving permissions.

The key concept of JWT is that data stores in token itself. It allows to decentralize storage and avoid explicit storing data on server side.

Encoding and decoding using JWT

JWT structure

Token itself consists of three parts: header, payload and signature:

  • header contains encoded type of the token and algorithm
  • payload contains encoded data and additional metadata
  • signature is encoded header + encoded data + secret phrase, encoded with algorithm

jwt structure

JWT vs Cookie-based Sessions

JWT brings some benefits compared to cookie-based authentication:

  1. Easy cross-domain requests.
  2. Server-side scalability. Since JWT token carries all information inside, token and secret phrase is all that is required to authenticate a user.
  3. Weak coupling. As JWT is stateless and supported by standard, it is possible to use it with different services, possibly written with several languages.
  4. Easily usable in mobile development. No cookie emulation is required for mobile clients.
  5. No CSRF. No automatic cookie is passed by browser - no vulnerability.
  6. Standard-based. JWT is well supported by all major languages.

These benefits are discussed in detail in 2


JWT is a great alternative to cookie-based authentication approach. It brings some benefits over cookie-based auth approach, but it also has its drawbacks.

Useful links

  1. Where to store JWT on web client
  2. Cookies vs Tokens. Getting auth right with Angular.JS
  3. The Ins and Outs of Token Based Authentication

Missed the first part of this article? It’s here: JWT authorization python: Part 1, Practise"

Sep 29 2016
This article will tell you why you need to keep Changelog. The history of the changelog management process on the example of python / django project using git instruments.
Aug 01 2016
Brief outline of the advantages of Python as a coding language and why we choose to work with Django, what we consider to be the best web framework for your project.
Jul 14 2016
This repo contains examples from article Emacs configuration for working with Python. It collects information for creating a usable Python programming environment in Emacs. Emacs already has out-of-the-box Python support via ‘python-mode’. The settings contained in this repo can be used to set up a more complete environment quickly.