Let's understand how we can verify whether a token (say ID Token) is valid and not tampered.
Different ways to parse and validate JWT Tokens
1. Manual Parsing and Validation
In this approach, we have to manually parse the JWT token by splitting it into its three components (header, payload, and signature) using a base64 decoding mechanism. Once split, we have to inspect the token's claims and validate the signature using the token's signing algorithm and the corresponding key. We have to write the logic by ourselves with the help of
2. JWT Libraries
Utilize JWT libraries available in your programming language or framework. These libraries provide built-in methods to parse and validate JWT tokens, making the process easier and more robust. Libraries for different framework/language is available at
3. Identity Provider SDKs
Many identity providers offer SDKs that handle JWT parsing and validation as part of their authentication libraries. For example, libraries like Auth0 SDKs, Okta SDKs, or Azure AD libraries often include methods to validate JWT tokens issued by their respective identity providers.
4. Framework Integration
Some web frameworks have built-in support for JWT token handling and validation. These frameworks provide middleware or modules that handle the parsing, validation, and authentication of JWT tokens automatically.
5. Online Validation Tools
Use online JWT validation tools or libraries to perform validation checks without writing code. For example using this site -
Example using Java JWT Library: Nimbus-JOSE-JWT
sample Spring Boot project to verify ID Token Signature of a Valid and Forged Token.
Let's start the keycloak and mysql service using docker-compose.
version: "3.9"
# https://docs.docker.com/compose/compose-file/
services:
# If mysql volume is already created and need to change the initial setup,
# remove the volume and restart the container to reflect
# docker-compose down -v
mysql:
container_name: mysql
image: mysql:8.0.29
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: identity
MYSQL_USER: keycloak
MYSQL_PASSWORD: keycloak
MYSQL_ROOT_PASSWORD: root
volumes:
- mysql-data:/var/lib/mysql
# access url - http://localhost:1010/
keycloak:
image: quay.io/keycloak/keycloak:21.0
: jboss/keycloak (Does not support ARM 64 image)
command: ["start-dev"]
ports:
- 1010:8080
- 1011:8443
environment:
KC_HEALTH_ENABLED: true
KC_METRICS_ENABLED: true
KC_DB: mysql
KC_DB_URL: jdbc:mysql://mysql:3306/identity?useSSL=false&allowPublicKeyRetrieval=true&cacheServerConfiguration=true&createDatabaseIfNotExist=true
KC_DB_USERNAME: root
KC_DB_PASSWORD: root
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: admin
KEYCLOAK_FRONTEND_URL: http://localhost:1010/auth
volumes:
#- ./data:/opt/jboss/keycloak/standalone/data
#- ./themes:/opt/jboss/keycloak/standalone/themes
#- ./config:/opt/jboss/keycloak/standalone/configuration
- ./log:/opt/jboss/keycloak/standalone/log
depends_on:
- mysql
volumes:
mysql-data:
driver: local
networks:
default:
name: company_default