Introduction
JWT helps safely share information between two parties. It makes sure the data is real and not changed.
Jump into concepts and practice - no test required
JWT helps safely share information between two parties. It makes sure the data is real and not changed.
header.payload.signature
The JWT has three parts separated by dots.
Each part is base64 encoded text.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxMjM0NSIsIm5hbWUiOiJKb2huIERvZSJ9.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
{
"alg": "HS256",
"typ": "JWT"
}.
{
"userId": "12345",
"name": "John Doe"
}.
SignatureThis program creates a JWT token with user info, then decodes it back to show the data.
import jwt # Secret key to sign the token secret = 'mysecretkey' # Data to include in the token payload = {'userId': '12345', 'name': 'John Doe'} # Create a JWT token encoded_jwt = jwt.encode(payload, secret, algorithm='HS256') print('JWT Token:', encoded_jwt) # Decode the JWT token decoded_payload = jwt.decode(encoded_jwt, secret, algorithms=['HS256']) print('Decoded Payload:', decoded_payload)
The header tells what algorithm is used to sign the token.
The payload holds the data you want to share.
The signature proves the token is not changed and is from a trusted source.
JWT has three parts: header, payload, and signature.
It is used to safely share data between systems.
Tokens are signed to prevent tampering.
{"sub":"1234567890","name":"John Doe","iat":1516239022}, what does the iat field represent?