1. OverviewJSON Web Token or JWT (jot) for short is an open standard (RFC 7519) that defines a compact, URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object that is used as the payload of a JSON Web Signature (JWS) structure or as the plaintext of a JSON Web Encryption (JWE) structure, enabling the claims to be digitally signed or integrity protected with a Message Authentication Code (MAC) and/or encrypted.
2. StructureThe compacted representation of a signed JWT is a string that has three parts, each separated by a dots (.) :
Each section is Base64Encoded and the first section is called header, the second section is payload and the third section is Signature.
2.1 HeaderIt consists of two parts: Hashing algorithm (HMAC SHA256 or RSA) and type of the token and to form the first part of the JWT, you need to do Base64Url encoded.
2.2 PayloadPayload contains the Claims. JWT Claims Set represents a JSON object whose members are the claims conveyed by the JWT i.e. Data to be shared between the applications.
There are three types of claims: reserved, public, and private claims.
2.2.1 Reserved ClaimsThese are standard predefined claims defined in the specification which can be used to provide a set of useful claims. Some of them are :
a. "iss" (Issuer) Claim
b. "sub" (Subject) Claim
c. "aud" (Audience) Claim
d. "exp" (Expiration Time) Claim
e. "nbf" (Not Before) Claim
f. "iat" (Issued At) Claim
g. "jti" (JWT ID) Claim
2.2.2 Public ClaimsThese can be defined at will by those using JWTs. But to avoid collisions they should be defined in the IANA JSON Web Token Registry or be defined as a URI that contains a collision resistant namespace.
2.2.3 Private ClaimsAll the custom claims come under private claims and it is created to share information between parties that agree on using them.
2.3 SignatureTo create the signature part, you have to take the encoded header, the encoded payload, and a SECRET_KEY, the algorithm specified in the header, and sign that.
if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:
base64UrlEncode(header) + "." +
The signature is used to verify that the sender of the JWT is who it says it is and to ensure that the message wasn't changed along the way.
You will get the below JWT token if you combine above header and payload and will use secret as SECRET_KEY.
3. How do JSON Web Tokens work?
2. Authorization Server will authenticate the user and will create the JWT token signed by its key and will return in the response.
3. Now, Browser will access the Application Server by JWT token as Authorization bearer <token> in the header.
4. Application Server will verify JWT using secret key (In such scenario, Key must be shared between Authorization Server and Application Server), Decode the String, verify the claims and will return the response.
4. Open Source libraries:
There are already open source libraries available in the market using which you can create and verify the access token.
Source code for implementation of above library can be downloaded from GITHUB
We went over what JWT are, how they are created and validated, and how they can be used to ensure trust between an application and its users. This is a starting point for understanding the fundamentals of JWT and why they are useful.
Post a Comment