DEV Community

Roy
Roy

Posted on

What is JWT? how to create one?

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA.

What is the JSON Web Token structure?

In its compact form, JSON Web Tokens consist of three parts separated by dots (.), which are:
Header
Payload
Signature

Therefore, a JWT typically looks like the following.
xxxxx.yyyyy.zzzzz

Let's break down the different parts.

Header
The header typically consists of two parts: the type of the token, which is JWT, and the signing algorithm being used, such as HMAC SHA256 or RSA.

For example:

{
"alg": "HS256",
"typ": "JWT"
}
Then, this JSON is Base64Url encoded to form the first part of the JWT.

Payload
The second part of the token is the payload, which contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims: registered, public, and private claims.

Registered claims: These are a set of predefined claims which are not mandatory but recommended, to provide a set of useful, interoperable claims. Some of them are: iss (issuer), exp (expiration time), sub (subject), aud (audience), and others.

Notice that the claim names are only three characters long as JWT is meant to be compact.

Public claims: These can be defined at will by those using JWTs. But to avoid collisions they should be defined in the IANA JSON Web Token Registry or be defined as a URI that contains a collision resistant namespace.

Private claims: These are the custom claims created to share information between parties that agree on using them and are neither registered or public claims.

An example payload could be:

{
"sub": "1234567890",
"name": "John Doe",
"admin": true
}
The payload is then Base64Url encoded to form the second part of the JSON Web Token.

Do note that for signed tokens this information, though protected against tampering, is readable by anyone. Do not put secret information in the payload or header elements of a JWT unless it is encrypted.

Signature
To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.

For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:

HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret)
The signature is used to verify the message wasn't changed along the way, and, in the case of tokens signed with a private key, it can also verify that the sender of the JWT is who it says it is.

Note that JWT is not encryption method but digital signature.

More info about JWT: jwt.io

So we understood what is JWT..
For using it you will need to implement JWT receiving in your API.

Today we will see how to create the JWT itself..

Python have dedicated library for that.
*** Let's get started! ***

  1. Install PyJWT.
pip install pyjwt
  1. Import it.
import jwt
  1. Create a payload
payload = {"userId":1234}
  1. Define your secret
secret= [your secret key]
  1. Define JWT encryption algorithm (optional).
JWT_ALGORITHM = "HS256"
  1. Encode.
jwtToken = jwt.encode(payload, secret, JWT_ALGORITHM)
  1. Put it all together:
import jwt

payload = {"userId":1234}
secret= "secret"
JWT_ALGORITHM = "HS256"

jwtToken = jwt.encode(payload, secret, JWT_ALGORITHM)
print(jwtToken)

Enjoy :)

Top comments (0)