JWT
what is JWT
?
JWT
stands for JSON Web Token
-is an open standard used to share security information between two parties , Each JWT contains encoded JSON objects, including a set of claims. JWTs are signed using a cryptographic algorithm to ensure that the claims cannot be altered after the Token
is issued.
It stores information in an easy-to-access manner, both for developers and computers. It can be used as a data format by any programming language and is quickly becoming the preferred syntax for APIs, surpassing XML.
Token
so what do we mean by the term Token
?
- is a string of data that represents something else, such as an identity. In the case of authentication, a non-JWT based token is a string of characters that allow the receiver to validate the sender’s identity. The important distinction here is lack of meaning within the characters themselves.
JWT structure?
A JWT is a string made up of three parts
, separated by dots (.), and serialized using base64. In the most common serialization format, compact serialization, the JWT looks something like this: xxxxx.yyyyy.zzzzz
.
1.Header
2.Payload
3.Signature
How JWT Works ?
- Once decoded, you will get
two JSON strings
:
1.The header and the payload. 2.The signature.
The JOSE (JSON Object Signing and Encryption) header contains the type of token — JWT
in this case — and the signing algorithm
.
The payload contains the claims
. This is displayed as a JSON string
, usually containing no more than a dozen fields to keep the JWT compact. This information is typically used by the server to verify that the user has permission to perform the action they are requesting.
There are no mandatory claims for a JWT, but overlaying standards may make claims mandatory. For example, when using JWT as bearer access token under OAuth2.0, iss, sub, aud, and exp must be present. some are more common than others.
The signature
ensures that the token hasn’t been altered. The party that creates the JWT signs the header and payload with a secret that is known to both the issuer and receiver, or with a private key known only to the sender. When the token is used, the receiving party verifies that the header and payload match the signature
.
Why should we use JSON Web Tokens?
As JSON is less verbose than XML. when it is encoded its size is also smaller. JWT and SAML tokens can use a public/private key pair in the form of a X.509 certificate for signing JSON parsers are common in most programming languages because they map directly to objects. JWT is used at Internet scale.
Header: consists of two parts
the type of the token, which is JWT, the signing algorithm being used, such as HMAC SHA256 or RSA.
For example:
{
"alg": "HS256",
"typ": "JWT"
}
Then, this JSON is Base64Url encoded to form the first part of the JWT.
Payload : contains the claims
registered
, public
, and private
claims.
Notice that the claim names are only three characters long as JWT is meant to be compact.
example payload :
{
"sub": "1234567890",
"name": "John Doe",
"admin": true
}
The payload is then Base64Url encoded to form the second part of the JSON Web Token.
Signature
To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.
For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret)
The signature is used to verify the message wasn’t changed along the way,
Putting all together
The output is three Base64-URL strings separated by dots that can be easily passed in HTML and HTTP environments, while being more compact when compared to XML-based standards such as SAML.