Security

JWT Token Generator

Generate JWT tokens with custom payloads, secrets, and algorithms.

Advertisement

Understanding JWT Tokens

JSON Web Tokens (JWTs) have become the de facto standard for stateless authentication and secure information exchange in modern web applications. A JWT is a compact, URL-safe string that encodes claims — statements about an entity and additional metadata — as a JSON object. Its self-contained nature means that a server can verify a user identity without querying a database or session store on every request, making JWTs ideal for distributed systems, microservices, and single-page applications.

A JWT consists of three parts separated by dots: the Header, the Payload, and the Signature. The Header identifies the token type (JWT) and the signing algorithm. The Payload contains the claims, which include registered claims like issuer (iss), subject (sub), audience (aud), expiration time (exp), and not-before time (nbf), as well as custom claims specific to your application. The Signature ensures that the token has not been tampered with by cryptographically signing the header and payload with a secret key.

The appeal of JWTs lies in their portability and efficiency. Once a user authenticates, the server issues a JWT that the client stores and presents with subsequent requests. The server validates the signature, checks that the token has not expired, and trusts the claims inside. This statelessness enables horizontal scaling: any server instance can validate the token without shared session storage, simplifying load balancing and microservice communication.

  • Compact, self-contained tokens for stateless authentication
  • Three-part structure: Header, Payload, and Signature
  • Claims encode identity, permissions, and token metadata
  • No server-side session storage required per request
  • Widely supported across programming languages and platforms

How JWT Generation Works

Generating a JWT involves three steps: constructing the header, constructing the payload, and creating the signature. Each step requires careful attention to detail to produce a valid, secure token.

The header is a JSON object containing at minimum two fields: `alg`, which specifies the signing algorithm (such as HS256 for HMAC with SHA-256), and `typ`, which is set to "JWT". This header is Base64URL-encoded to produce the first segment of the token. The choice of algorithm matters: symmetric algorithms like HS256 use a single shared secret, while asymmetric algorithms like RS256 use public-private key pairs.

The payload contains the claims you want to transmit. At minimum, you should include `exp` (expiration time) to limit the token lifetime, `iat` (issued at) to record when the token was created, and `sub` (subject) to identify the user or entity. Custom claims carry application-specific data such as user roles, permissions, or tenant identifiers. Keep the payload small — every byte increases request header size and network overhead.

The signature is generated by joining the encoded header and payload with a dot, then signing the result using the chosen algorithm and secret key. For HMAC algorithms, the secret is a shared key known to both the issuer and the verifier. The signature proves that the token was issued by a trusted party and that its contents have not been altered since issuance.

  • Construct a header specifying the algorithm and token type
  • Build a payload with registered and custom claims
  • Set an expiration time to limit token lifetime
  • Sign the encoded header and payload with a secret key
  • Use Base64URL encoding for all token segments

HMAC Signing with Crypto Subtle

Our JWT Generator uses the Web Crypto API — specifically the `crypto.subtle` interface — to perform HMAC signing entirely within your browser. The Web Crypto API provides native, high-performance cryptographic operations that are significantly faster and more secure than pure JavaScript implementations.

The HMAC (Hash-based Message Authentication Code) algorithm combines a cryptographic hash function with a secret key to produce a signature. Our generator supports HMAC-SHA-256 (HS256), which is the industry standard for JWT signing. HS256 offers an excellent balance of security and performance: it produces a 256-bit signature that is resistant to forgery without the secret key, while remaining fast enough to generate and verify on every request.

Using `crypto.subtle` ensures that your secret key never leaves the browser during generation. You enter your payload claims and secret, and the signing operation happens locally using the browser built-in cryptography module. This client-side approach eliminates the risk of exposing your signing key to third-party servers, which is critical when testing tokens for production systems or working with sensitive credentials.

  • Uses the native Web Crypto API for secure HMAC operations
  • Supports HS256 signing with 256-bit signatures
  • Processes entirely client-side with no server interaction
  • Faster and more secure than pure JavaScript crypto libraries
  • Secret keys remain in your browser for maximum security

Security Considerations for JWTs

JWTs are powerful but easy to misuse. Understanding their security properties and limitations is essential for any developer implementing token-based authentication.

First and foremost, JWT payloads are not encrypted — they are only Base64URL-encoded. Anyone who intercepts a token can decode and read the header and payload. Never include sensitive information such as passwords, credit card numbers, or personal health information in a JWT payload. If you need confidentiality, encrypt the token using JWE (JSON Web Encryption) or transmit it exclusively over TLS.

The security of an HMAC-signed JWT depends entirely on the secrecy and strength of the signing key. Use a long, randomly generated secret of at least 256 bits. Short or predictable secrets can be brute-forced, allowing attackers to forge valid tokens with arbitrary payloads. Rotate signing keys periodically and have a plan for key revocation if a compromise is suspected.

Token lifetime is a critical security control. Short-lived access tokens (15 minutes to 1 hour) limit the window of opportunity if a token is stolen. Pair them with refresh tokens that have longer lifetimes but can be revoked independently. This pattern balances security (stolen access tokens expire quickly) with usability (users do not have to re-authenticate constantly).

  • Never store sensitive data in JWT payloads — they are readable
  • Use long, randomly generated secrets of 256 bits or more
  • Rotate signing keys and have a revocation plan
  • Keep access tokens short-lived (15-60 minutes)
  • Use refresh tokens for long-lived sessions with independent revocation

Best Practices for JWT Implementation

Implementing JWT authentication correctly requires attention to detail at every stage — generation, transmission, verification, and revocation.

Always verify tokens on the server side before trusting their claims. Client-side verification is useful for debugging but provides no security, since an attacker can trivially bypass it. Server verification must check the signature using the secret key, validate the expiration time, verify the issuer and audience if specified, and ensure the token has not been revoked if you maintain a revocation list.

Store tokens securely on the client. For web applications, HttpOnly cookies provide the best protection against XSS attacks because JavaScript cannot read them. If storing in localStorage or sessionStorage is necessary, be aware that XSS vulnerabilities can expose these tokens to attackers. For mobile applications, use the platform secure storage APIs (Keychain on iOS, Keystore on Android).

Include only essential claims in access tokens. Every claim increases token size, and tokens travel with every request. Fetch additional user data from your database or cache when needed rather than embedding it in the token. A minimal payload reduces bandwidth, improves latency, and limits information exposure if a token is intercepted.

  • Verify signatures and claims on the server side only
  • Store tokens in HttpOnly cookies when possible
  • Validate exp, iss, aud, and nbf claims on every request
  • Keep payloads minimal — fetch additional data from the server
  • Implement token revocation for logout and security incidents