Get a Pentest and security assessment of your IT network.

Cyber Security

JWT Authorization Check

TL;DR

This guide shows you how to make sure a client is allowed to use a JSON Web Token (JWT) before letting them access your system. We’ll cover checking the token’s signature, expiry time, and any extra claims you need.

Checking JWT Authorization

  1. Understand the JWT Structure
    • A JWT has three parts: Header, Payload, and Signature.
    • The Header contains information about the signing algorithm (e.g., HS256).
    • The Payload holds the claims – data about the user or client. This is where you’ll find things like user ID, roles, and permissions.
    • The Signature verifies that the token hasn’t been tampered with. It’s created using a secret key.
  2. Verify the Signature
  3. This is the most important step! You need to use the same secret key used to sign the token.

    • Many libraries exist for different programming languages (e.g., python-jose in Python, jsonwebtoken in Node.js).
    • Here’s an example using a hypothetical library:

      token = "your_jwt_token"
      secret_key = "your_super_secret_key"
      try:
        decoded_payload = verify_jwt(token, secret_key)
      except InvalidSignatureError:
        print("Token signature is invalid")
        # Reject the token
      except ExpiredSignatureError:
        print("Token has expired")
        # Reject the token
      except Exception as e:
        print(f"An error occurred: {e}")
        #Reject the token
    • The verify_jwt function is a placeholder; replace it with your library’s equivalent.
  4. Check Expiry Time (exp claim)
  5. JWTs have an expiry time to limit their validity. Always check this!

    • The exp claim in the payload specifies the expiration timestamp (in seconds since epoch).
    • After verification, access the exp claim from the decoded payload.

      import time
      current_time = int(time.time())
      if decoded_payload['exp'] < current_time:
        print("Token has expired")
        # Reject the token
  6. Validate Custom Claims
  7. You might add extra claims to control access (e.g., role, permissions).

    • After successful signature and expiry checks, examine these custom claims.

      if decoded_payload['role'] != 'admin':
        print("User does not have admin privileges")
        # Reject the token or limit access
  8. Consider Using a Revocation List
  9. Sometimes you need to revoke tokens before their expiry time (e.g., user logs out).

    • Maintain a list of revoked JWTs (e.g., in a database or cache).
    • Before accepting a token, check if it’s on the revocation list.

  10. Store Secrets Securely
  11. Your secret key is critical! Never hardcode it directly into your code.

    • Use environment variables or a secure secrets management system.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation