JSON Web Tokens
JSON Web Tokens
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.
TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
{
"alg": "HS256",
"typ": "JWT"
}
{
"sub": "1234567890",
"name": "John Doe",
"admin": true
}
Claims?
Definitions or assertions made about a certain party or object.Some are part of the
JWT spec, others are user defined.
JWTs can also be signed using JWS 'S' for signature) or encrypting them, JWE
Encryption)
JWS, JWE with JWTs provide powerful and secure solution to many problems.
Authentication
Federated Identity
Authorization
Client-Side session ("Stateless" sessions)
Client side secrets.
History
JOSE JSON object signing and Encryption Group formed in 2011.
Objective: "Standardize the mechanism for integrity protection(signature and MAC
and encryption as well as the format for key and algorithm identifiers to support
interoperability of security services for protocols that use JSON"
In 2013 series of drafts, cookbook with examples available.Later these drafts would
become JWT,JWS,JWE,JWK and JWA RFCs.
PRACTICAL APPLICATIONS
1. Client-Side/Stateless Sessions
Stateless Sessions: nothing but client-side data
JWTs, by the virtue of JWS and JWE can provide various types of signature and
encryption.
2. Federated Identity
Federated Identity systems allow different, possibly unrelated parties to share
authentication and authorization services with other parties.
User's identity is centralized.
JWTs can be used for this purpose.
Essential Flow of the authorization process is:
Steps:
Credentials returned from Authorization Server to the User can be encoded with JWT.
Access Tokens
Tokens that give those who have them access to protected resources.
Usually short lived, may have expiration date embedded with them.
May carry extra info like IP addr from which requests are allowed.
Refresh Tokens
Allow clients to request new access tokens. It is required when the access token has
expired and client wants a new access token from the server.
These are usually long lived.
Signed JWTs make good access tokens as:
They encode all the neccessary data to differentiate access levels to a resource.
Can carry expiration date.
Are signed to avoid validation queries against the authorization server.
JWTs may also be used as refresh tokens. But there is less reason for this purpose as -
Most of the time a simple UUID will suffice as no need for token to carry a
payload.
The Header
The Payload
The Signature / encryption data (depending upon the algo used for signing or
encryption, ommitted in case of unencrypted JWTs)
NOTE: JWT uses a variant of Base64 encoding ie safe for URLs basically
sustituting '+' and '/' for '-' and '_'.
Padding also removed. Also reffered to as "base64url".
{
"alg": "HS256",
"typ": "JWT"
}
{
"sub": "1234567890",
"name": "John Doe",
"admin": true
}
The Header
Every JWT carries a header also called JOSE header, which claims about itself.
It establishes the algorithms used, whether the JWT is signed or encrypted and how to
parse the JWT.
alg : main algorithm in use for signing and/or decrypting the JWT.
{
"alg": "none"
}
The Payload
All interesting user data is added here. It is a JSON object like header.
{
"sub": "1234567890",
"name": "John Doe",
"admin": true
}
Registered Claims:
-iss: (from issuer): case-sensitive str or URI uniquely identifying the JWT issuer party.
-sub: (from subject): case-sensitive str or URI uniquely identifying the party about
whom information is in the JWT.Must be unique in context of the issuer.
aud: (from audience): case sensitive str, URI or array of such values uniquely
identifying the recipients of this JWT.
exp:(from expiration time): Specific date and time in format "seconds since
epoch" as definition in POSIX. Sets the exact moment from which the JWT is
considered invalid.
nbf:(not before time): Opposite of expiration claim. Exact moment from which the
JWT is considered valid.
iat:(issued at): specific date and time at which this JWT was issued.
jti:(from JWT ID string representation unique identifier for this JWT.
Unsecured JWTs
These are the simplest JWTs, formed by a simple(usually static) header.
{
"alg": "none"
}
{
"sub": "user123",
"session": "ch72gsb320000udocl363eofy",
"name": "Pretty Name",
"last page": "/views/settings"
}
eyJhbGciOiJub25lIn0.
eyJzdWIiOiJ1c2VyMTIzIiwic2Vzc2lvbiI6ImNoNzJnc2IzMjAwMDB1ZG9jbDM2M
2VvZnkiLCJuYW1lIjoiUHJldHR5IE5hbWUiLCJsYXN0cGFnZSI6Ii92aWV3cy9zZXR0aW5ncyJ9
Take the header as a byte array of its UTF8 representation.No need to minify or
strip the JSON.
Encode the byte array using the Base64URL algorithm, removing trailing equal
signs(=)
Take the payload as a byte array of its UTF8 representation.No need to minify or
strip the JSON before encoding.
Encode the byte array using the Base64URL algorithm, removing trailing equal
signs(=).
Concatenate the resulting string, putting first the header followed by a '.'
character, followed by the payload.
Validation of both header and payload (wrt to presence of required claims and the
correct use of each claims) must be performed before encoding.
Add flowchart here
See the example in this section in the handbook.
Find the first period '.' character.Take the string before it.
Decode the string using the Base64URL algorithm.Result is the JWT header.
Take the string after the period from step 1.
Decode the string using the Base64URL algorithm.Result is the JWT payload.