Skip to main content

Command Palette

Search for a command to run...

JWT, JWE, and JWKS Explained: A Developerโ€™s Guide to Token-Based Security

Understand How Token-Based Authentication and Encryption Work Behind the Scenes

Updated
โ€ข4 min read
JWT, JWE, and JWKS Explained: A Developerโ€™s Guide to Token-Based Security
F

Principal Technical Consultant at GeekyAnts.

Bootstrapping our own Data Centre services.

I lead the development and management of innovative software products and frameworks at GeekyAnts, leveraging a wide range of technologies including OpenStack, Postgres, MySQL, GraphQL, Docker, Redis, API Gateway, Dapr, NodeJS, NextJS, and Laravel (PHP).

With over 9 years of hands-on experience, I specialize in agile software development, CI/CD implementation, security, scaling, design, architecture, and cloud infrastructure. My expertise extends to Metal as a Service (MaaS), Unattended OS Installation, OpenStack Cloud, Data Centre Automation & Management, and proficiency in utilizing tools like OpenNebula, Firecracker, FirecrackerContainerD, Qemu, and OpenVSwitch.

I guide and mentor a team of engineers, ensuring we meet our goals while fostering strong relationships with internal and external stakeholders. I contribute to various open-source projects on GitHub and share industry and technology insights on my blog at blog.faizahmed.in.

I hold an Engineer's Degree in Computer Science and Engineering from Raj Kumar Goel Engineering College and have multiple relevant certifications showcased on my LinkedIn skill badges.

๐Ÿง  What is JWT?

JWT (JSON Web Token) is a compact, URL-safe token format used to transmit claims securely between parties. Itโ€™s the backbone of stateless authentication and is often signed using JWS (JSON Web Signature) or encrypted using JWE (JSON Web Encryption).

๐Ÿ”‘ JWT = Header + Payload + Signature

eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.
eyJ1c2VySWQiOiIxMjM0Iiwicm9sZSI6InVzZXIifQ.
ZHVtbXktc2lnbmF0dXJl
SectionPurpose
HeaderMetadata (algorithm, type)
PayloadClaims (e.g., userId, role)
SignatureVerifies token integrity

โœ… JWT (JWS) โ€“ Signed Token

JWS = JSON Web Signature
Itโ€™s the most common JWT. The payload is not encrypted โ€” just signed to ensure authenticity.

๐Ÿ“Œ Use Case:

  • User login tokens

  • API access tokens

  • OAuth2 flows

๐Ÿ” JWE โ€“ Encrypted JWT

JWE = JSON Web Encryption
Unlike JWS, JWE encrypts the payload so itโ€™s not visible to intermediaries. It includes more sections than JWS.

<Header>.<EncryptedKey>.<IV>.<Ciphertext>.<AuthTag>
ComponentDescription
HeaderAlgorithm & encryption method
Encrypted KeyEncrypted symmetric key using RSA
IVInitialization Vector
CiphertextEncrypted payload
AuthTagAuth tag for integrity/authentication

๐Ÿ“Œ Use Case:

  • Financial data

  • Healthcare apps

  • B2B confidential communication

๐Ÿ”„ JWT vs JWE

FeatureJWS (Signed) ๐Ÿ”JWE (Encrypted) ๐Ÿ”
Payload Visibleโœ… YesโŒ No
Integrityโœ… Ensured via signatureโœ… Ensured via encryption
ConfidentialityโŒ Not secureโœ… Encrypted
Use CaseAuth tokensSensitive data transfer

๐Ÿงฉ What is JWKS?

JWKS = JSON Web Key Set
Itโ€™s a public endpoint that exposes public keys in a JSON format. It's how services like Auth0, Google, and Okta let you verify JWTs without sharing the private key.

๐Ÿง  JWKS provides a way to rotate keys without breaking consumers.

๐Ÿ“Œ URL Example:

https://your-auth-server.com/.well-known/jwks.json

๐Ÿ“„ Example Response:

{
  "keys": [
    {
      "kty": "RSA",
      "kid": "abc123",
      "use": "sig",
      "n": "...base64url...",
      "e": "AQAB"
    }
  ]
}

๐Ÿ—‚๏ธ How it All Works Together

๐Ÿ› ๏ธ Verifying JWTs with JWKS in Node.js

โœ… Install Dependencies

npm install jwks-rsa jsonwebtoken express

๐Ÿ”ง Verify JWT with JWKS

const jwt = require("jsonwebtoken");
const jwksClient = require("jwks-rsa");

const client = jwksClient({
  jwksUri: "https://your-auth-server.com/.well-known/jwks.json"
});

function getKey(header, callback) {
  client.getSigningKey(header.kid, (err, key) => {
    const signingKey = key.getPublicKey();
    callback(null, signingKey);
  });
}

function verifyToken(token) {
  jwt.verify(token, getKey, {
    algorithms: ["RS256"]
  }, (err, decoded) => {
    if (err) return console.error("โŒ Invalid Token");
    console.log("โœ… Verified Token Payload:", decoded);
  });
}

โœ… Best Practices

PracticeWhy It Matters
Use RS256 (asymmetric) for signingSafer than HMAC in distributed systems
Always verify iss, aud, expProtect against spoofed/expired tokens
Enable key rotation using JWKSImproves security without breaking apps
Use JWE only when confidentiality is requiredSaves performance otherwise

๐Ÿš€ Final Thoughts

JWTs are everywhere โ€” but understanding the difference between JWS, JWE, and JWKS is key to building secure, scalable, and standards-compliant systems.

โœ… Use JWS for API authentication
โœ… Use JWE for encrypting sensitive data
โœ… Use JWKS for secure key distribution and rotation

Let me know if you'd like a follow-up tutorial to implement JWKS-based auth with Auth0, Google Identity, or AWS Cognito! ๐Ÿ‘‡


About Me ๐Ÿ‘จโ€๐Ÿ’ป

I'm Faiz A. Farooqui. Software Engineer from Bengaluru, India.
Find out more about me @ faizahmed.in

F

Thanks for sharing!

Mastering Encryption: A Practical Guide for Developers

Part 2 of 13

Learn encryption fundamentals, from Symmetric vs Asymmetric Encryption to Envelope Encryption and AWS KMS implementation. Clear explanations, real-world use cases, and easy-to-follow diagrams to help developers secure their data.

Up next

Homomorphic Encryption: Performing Computations on Encrypted Data

The Future of Privacy-Preserving Computation