What is inside a JWT and how to validate one
A signed JWT is three base64url strings joined by periods. Decoding it needs no key, so a service must verify the signature and the claims before it acts on anything inside.

Header, payload and signature
RFC 7519 represents a JWT as a sequence of URL-safe parts separated by periods, each one base64url-encoded. A signed JWT has three: the header, the payload and the signature.
GitHub's documentation shows the decoded token that GitHub Actions issues to a workflow job. This is its header:
{
"typ": "JWT",
"alg": "RS256",
"x5t": "example-thumbprint",
"kid": "example-key-id"
}These are the registered claims from its payload:
{
"jti": "example-id",
"sub": "repo:octo-org/octo-repo:environment:prod",
"aud": "https://github.com/octo-org",
"iss": "https://token.actions.githubusercontent.com",
"nbf": 1632492967,
"exp": 1632493867,
"iat": 1632493567
}The signature is computed over the encoded header and the encoded payload, joined by a period. Time claims count seconds since 1 January 1970 UTC. In this token exp is 300 seconds after iat.
Encoding is not encryption
Base64url is base64 with a URL-safe character set and the trailing padding omitted. Decoding it needs no key. This command decodes the header of the example token in RFC 7519:
python3 -c 'import base64, sys; print(base64.urlsafe_b64decode(sys.argv[1]))' \
eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9It prints the JSON header, with typ set to JWT and alg set to HS256.
RFC 7515 says its signatures provide integrity protection. For privacy-sensitive claims, RFC 7519 gives two measures: use an encrypted JWT, or send the token only over a protocol that encrypts, such as TLS. It adds that leaving such information out of the token is the simplest way.
How the header selects a key
alg identifies the algorithm used to secure the token. kid is a hint that indicates which key was used. A JSON Web Key Set is a JSON object with a keys array, and the kid in the header is matched against the kid of a key in the set. With OpenID Connect, the issuer's discovery document gives the address of the set in its jwks_uri field.
RFC 8725 has a rule named "Do Not Trust Received Claims". It says to validate or sanitize kid before using it in a lookup, because it can carry an SQL or LDAP injection. It also says that blindly following a jku or x5u URL from the header can result in server-side request forgery.
The checks to run
- Algorithm. RFC 8725 requires a library to let the caller specify the supported algorithms and to use no others. Each key must be used with exactly one algorithm.
- Signature. If any cryptographic operation fails to validate, the entire token must be rejected.
- Issuer. When the token has an
issclaim, validate that the keys used belong to that issuer. - Audience. When one issuer issues tokens for more than one application, the token must contain
aud. Reject it ifaudis absent or does not identify your application. - Time. The current time must be before
exp, and equal to or afternbf. RFC 7519 lets implementers allow some small leeway for clock skew. - Subject. When the token has a
subclaim, validate that it corresponds to a valid subject at your application.
A validation example in Python
This example uses PyJWT. For RSA signatures it needs the cryptography library, which pip install pyjwt[crypto] installs. Replace the three values in angle brackets with your own.
import jwt
ISSUER = "<issuer>" # the iss value you trust
AUDIENCE = "<audience>" # the identifier of your own service
JWKS_URL = "<jwks-uri>" # the jwks_uri from the issuer's discovery document
jwks_client = jwt.PyJWKClient(JWKS_URL)
def validate(token: str) -> dict:
signing_key = jwks_client.get_signing_key_from_jwt(token)
return jwt.decode(
token,
signing_key,
algorithms=["RS256"],
issuer=ISSUER,
audience=AUDIENCE,
options={"require": ["exp", "iss", "aud"]},
)algorithms is the allow-list. issuer and audience are the values PyJWT compares iss and aud with. PyJWT's documentation warns that exp and nbf are verified only if present, and require lists the claims that must be present. Run with PyJWT 2.15.1, the same call with no require accepted a token that had no exp.
Common mistakes
- Accepting
algset tonone. RFC 7519 calls a token withalgnoneand an empty signature an Unsecured JWT. RFC 8725 reports libraries that trusted the value and accepted the token with no signature check. - Letting the token pick the algorithm. RFC 8725 describes an attacker changing
RS256toHS256, after which some libraries verify the signature with the RSA public key as the HMAC secret. For both attacks it points to its rules on algorithm verification, the first check above, and on appropriate algorithms. - Reading an unverified payload. PyJWT returns the claims unchecked when
verify_signatureisFalse. Its documentation says the integrity or authenticity of the claimset cannot be trusted that way. - Skipping the audience. RFC 8725 describes a recipient that takes a token intended for itself and presents it to another recipient.
- Long lifetimes.
expis optional in RFC 7519. RFC 9700 notes that access tokens with a short lifetime reduce the impact of a leaked token. Requireexpand keep it short. - Weak HMAC secrets. RFC 8725 forbids a human-memorizable password as the key for
HS256. Such keys are open to offline brute-force attacks once an attacker holds a token.
Tests to write
Run with PyJWT 2.15.1, the validate function above raised an exception for each of these tokens. Add the same negative tests to your own service:
- a token with
algset tonone - a token signed with another key
- an
HS256token signed with the RSA public key as the secret - an expired token, and one whose
nbfis in the future - a token with another service's
aud - a token from another issuer
- a token with no
exp
Sources
Want a second pair of eyes on yours? We do this work for a living.
More guides

OpenID Connect for engineers: tokens, claims and trust
Authentication · 4 min read
Three parties, two tokens, one discovery document, and the five claims a relying party must check.

OAuth 2.0 flows: which grant fits which client
Authentication · 4 min read
Which OAuth 2.0 grant to use for a web app, a single-page app, a mobile app, a CLI and a service.

Workload identity on EKS, AKS and GKE: pods without cloud keys
Authentication · 4 min read
The role association, annotation, label or IAM binding that each of the three platforms needs.