JWT

Also known as: JSON Web Token

On this page 7

A JWT, or JSON Web Token, is a compact, URL-safe string that carries a set of claims in JSON form and is protected by a digital signature or message authentication code. Defined in RFC 7519, it is the most common token format for authentication and authorization in APIs, where a client presents the token with each request instead of a session identifier.

The value of a JWT is that any service holding the right key can verify the token locally, without querying a central session store. That property makes JWTs attractive for distributed systems, but it also creates revocation and expiry challenges that every implementation must address deliberately.

The three-part structure of a JWT

A signed JWT consists of three Base64URL-encoded parts separated by dots: a header, a payload and a signature. The result looks like xxxxx.yyyyy.zzzzz and fits easily into an HTTP header, a cookie or a query parameter.

The table below describes what each part contains and what it is used for.

PartContentPurpose
HeaderJSON with alg and usually typ, sometimes kidTells the verifier which algorithm and key to use
PayloadJSON object of claims such as sub, exp, issCarries identity, permissions and validity data
SignatureSignature or MAC over header and payloadProves integrity and origin of the token
Parts of a signed JWT

A crucial point is that the payload of a signed token is encoded, not encrypted. Anyone who intercepts the token can decode and read the claims, so sensitive data must never be placed in the payload unless the token is encrypted with JWE.

How JWT authentication works in an API

In a typical flow, an authorization server issues the token after the user signs in, and the client then sends it to resource servers in the Authorization: Bearer header. Each API verifies the signature, checks the time-based claims and reads the permissions before serving the request.

  1. The user authenticates with credentials, a single sign-on provider or another factor.
  2. The issuer creates a JWT with claims such as subject, audience and expiration time, then signs it.
  3. The client stores the token and attaches it to every API call.
  4. The API validates the signature, issuer, audience and expiry, then authorizes the action based on the claims.
  5. When the access token expires, the client obtains a new one, usually with a refresh token.

Because verification needs only a key and a clock, JWTs support stateless authorization across microservices and API gateways. In OAuth 2.0 deployments they commonly serve as access tokens, and in OpenID Connect the ID token is always a JWT.

Registered claims and signing algorithms

RFC 7519 defines a set of registered claims with short three-letter names. They are optional by specification, but a secure API should require and check the ones that bound the token in time and scope.

ClaimMeaningValidation rule
issIssuer of the tokenMust match a trusted issuer
subSubject, usually the user or client identifierUsed to identify the principal
audIntended audienceMust include the receiving API
expExpiration time in Unix secondsReject if the current time is later
nbfNot-before timeReject if the current time is earlier
iatIssued-at timeUseful for age checks and auditing
jtiUnique token identifierEnables replay detection and deny lists
Registered JWT claims

The signing algorithm determines who can create and who can verify tokens. Symmetric HMAC algorithms use one shared secret for both operations, while asymmetric algorithms let the issuer keep the private key and publish the public key, often through a JWKS endpoint.

AlgorithmTypeTypical use
HS256HMAC with SHA-256, shared secretSingle service that both issues and verifies
RS256RSA signature with SHA-256Widely supported default for identity providers
ES256ECDSA on P-256 with SHA-256Smaller signatures, mobile and modern stacks
EdDSAEd25519 signatureFast, deterministic signing in newer libraries
Common JWT signing algorithms

JWT compared with other API credentials

A JWT is one of several ways an API can identify a caller. The choice depends on whether instant revocation or verification without a lookup matters more.

CredentialVerificationStrengthsWeaknesses
JWTLocal signature checkStateless, carries claims, scales wellHard to revoke before expiry
Opaque access tokenLookup or introspection callInstant revocation, no data exposureExtra network call per request
Session cookieServer-side session storeSimple for browser applicationsShared state across servers
API keyDatabase lookup of a static keyEasy for server-to-server accessLong-lived, no user context
JWT and alternative credentials

Many architectures combine these approaches: an API gateway introspects an opaque token at the edge and forwards a short-lived internal JWT to backend services, gaining revocation control externally and stateless checks internally.

Security pitfalls and best practices

Most JWT vulnerabilities come from lax validation in the verifying code, not from weaknesses in the format itself. Libraries that trust the header too much have historically allowed attackers to forge tokens.

  • Reject the none algorithm and pin the expected algorithm on the server instead of reading it from the token header.
  • Prevent algorithm confusion, in which an RS256 public key is misused as an HS256 secret.
  • Keep access token lifetimes short, typically 5 to 15 minutes, and rotate refresh tokens on use.
  • Use a strong HMAC secret of at least 256 bits, never a guessable password.
  • Always check aud, iss and exp, and allow only a small clock skew of seconds.
  • Store browser tokens in HttpOnly, Secure cookies where possible to reduce exposure to cross-site scripting.

Revocation remains the hardest problem. Practical options include short expiry with refresh tokens, a deny list keyed on jti, or a per-user token version stored centrally, each of which reintroduces some state in exchange for control.

In everyday language

A JWT works like a stamped wristband at a festival. At the entrance, staff check your ticket once and give you a wristband that says who you are and which areas you may enter, with a tamper-proof seal.

After that, every stage door only looks at the wristband and its seal instead of calling the ticket office. That is fast, but if a wristband is stolen, it keeps working until it expires, which is why good systems make the wristbands expire quickly.

Seen in the real world

A mobile banking app sends users through an identity provider when they sign in. The provider returns an RS256-signed access token valid for 10 minutes and a refresh token valid for 30 days. The access token contains the user identifier, the audience of the payments API and scopes such as reading balances and initiating transfers.

Each of the bank’s twelve backend services downloads the provider’s public keys once an hour and verifies every request locally, which removes thousands of introspection calls per second. When a customer reports a stolen phone, support revokes the refresh token, and the stolen session stops working within ten minutes at most, a window the security team accepted as a deliberate trade-off between performance and immediate revocation.