<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ajinkya Bhuwad on Medium]]></title>
        <description><![CDATA[Stories by Ajinkya Bhuwad on Medium]]></description>
        <link>https://medium.com/@securitybyte?source=rss-447071b7c46------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*UlS1GbS4I_fQgRdrPlvtlg.png</url>
            <title>Stories by Ajinkya Bhuwad on Medium</title>
            <link>https://medium.com/@securitybyte?source=rss-447071b7c46------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 20:12:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@securitybyte/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[All You Need to Know About JSON Web Token (JWT)]]></title>
            <link>https://medium.com/swlh/all-you-need-to-know-about-json-web-token-jwt-8a5d6131157f?source=rss-447071b7c46------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a5d6131157f</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[security-token]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[json-web-token]]></category>
            <dc:creator><![CDATA[Ajinkya Bhuwad]]></dc:creator>
            <pubDate>Sat, 28 Nov 2020 22:51:34 GMT</pubDate>
            <atom:updated>2020-12-08T15:15:11.294Z</atom:updated>
            <content:encoded><![CDATA[<h3>Best Guide to JSON Web Token (JWT)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*td9R_B000pPpmNusVJx3HQ.png" /><figcaption>Picture Credit: <a href="https://www.loginradius.com/engineering/blog/jwt/">https://www.loginradius.com</a></figcaption></figure><blockquote>“A JSON Web Token (JWT), pronounced ‘jot’, is an open standard (<a href="https://tools.ietf.org/html/rfc7519">RFC 7519</a>) which is used for securely transmitting information between entities as a JSON object.”</blockquote><p>It is a compact and secure way of exchanging information over the network. JSON Web Token helps to maintain the integrity and authenticity of the information because it is digitally signed using secret or public/private key pair using <a href="https://en.wikipedia.org/wiki/RSA_(cryptosystem)">RSA</a> or <a href="https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm">ECDSA</a>.</p><p>An important thing to keep in mind about JWT is that it is a signed token and not an encrypted one. Therefore, even though JWT can verify the <em>integrity</em> of the claims contained within it, it cannot hide that information. And because of that, it is advisable not to put any sensitive information within the token.</p><h3>Why we need JSON Web Token?</h3><p>HTTP is a stateless protocol that means a new request does not remember anything about the previous one. So for each request, you need to login and authenticate yourself (figure 1). Now, this sounds like a lot of work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*HLorcU6jUip57XC7l_61hQ.png" /><figcaption>Figure 1</figcaption></figure><p>So, the solution to deal with this is the use of what’s called <em>a session</em>. A session is an object stored on the server that helps the user to stay logged in or to save any reference to their account. Figure 2 shows the overall flow of this process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*PN408M_2RdnMRopUWlcgsw.png" /><figcaption>Figure 2</figcaption></figure><p>First, the user submits a username and a password that are authenticated by the server. If the authentication is successful a session ID is generated for the respective client. The generated session ID is returned to the client and is stored on the server-side as well.</p><p>Now, the client just needs to send its session ID along with the request to authenticate itself and retrieve necessary information. The server will then check if the session ID is valid or not. If the session is still valid, it will respond with the requested webpage/data. And if not, the server will respond with an error message stating that the request made is unauthorized.</p><h4>Problems with Session-based Authentication</h4><p><strong>Scalability : </strong>The sessions need to be stored on the server, either in memory or in a database. In the current API era, a server can receive a large number of requests, and hence the server needs to be scaled up. Adding new resources can increase the complexity of the infrastructure as well.</p><p><strong>Session Management: </strong>The server needs to manage the sessions by keeping track of all the active, inactive (expired, invalidated) sessions. The expired or invalidated sessions need to be removed from the memory.</p><p><strong>Performance: </strong>For every request, the server needs to perform a lookup into the memory to check if the provided session object is valid or not. This back and forth can mark down the server.</p><h3>The better and effective solution</h3><p>The JSON Web Token (JWT) does not use sessions and hence prevents the above problems. When you send your credentials to the server instead of making a session, the server will return a JSON Web Token. You can use that JWT to do whatever you want with the server (Of course, the things that you are authorized to do).</p><p>Consider a JWT like a hotel key: When you enter the hotel, first you need to register yourself at the reception to receive your key card. You can use that key card to open and close your room, access common amenities like Bar, Fitness Centre, etc. But you cannot use that key card to access someone else’s room or Manager’s office since you are not authorized to do so. The key card comes with an expiration date, and it becomes useless once your stay has ended at the hotel.</p><p>Similarly, you can use your JWT token generated from one server to access resources on different servers. The JWT token contains claims like expiration date/time that can be used to check its validity..</p><h3>Structure of JSON Web Token</h3><p>A JSON Web Token is nothing but a long encoded text string which is made up of three parts separated by a period. These parts are:</p><ol><li>Header</li><li>Payload</li><li>Signature</li></ol><p>A typical JWT looks like the following:</p><pre>header.payload.signature</pre><p>Let’s see detailed break down of all the three parts.</p><h4>Header</h4><p>The header consists of two parts i.e. the type of token and the algorithm used for signing (such as HMAC SHA256 or RSA). The token type helps to interpret the token and in this case it’s JWT. For example,</p><pre>{<br>&quot;typ&quot;: &quot;JWT&quot;,<br>&quot;alg&quot;: &quot;HS256&quot;<br>}</pre><p>This header is then encoded into base64 to form the first part of the JWT.</p><h4>Payload</h4><p>The payload consists of the session data called as claims. Claims provide information about the client/user. There are three types of claims: <em>registered</em>, <em>public</em>, and <em>private</em> claims.</p><p><strong>Registered Claims: </strong>These type of claims are predefined claims which can<strong> </strong>be used for increasing the security. These claims are not mandatory but recommended. Some of these claims are:</p><ul><li><strong>iss (Issuer) Claim:</strong> The “iss” claim helps to identify the issuer of the token.</li><li><strong>sub (Subject) Claim: </strong>The “sub” claim identifies the subject of the JWT.</li><li><strong>aud (Audience) Claim: </strong>The “aud” claim identifies the recipients that the JWT is intended for.</li><li><strong>exp (Expiration Time) Claim: </strong>The “exp” claim is used to identify the expiration time on or after which the JWT must not be valid. Its value must be a number containing a NumericDate value. One important thing is that the current date/time must be before the expiration date/time.</li><li><strong>nbf (Not Before) Claim: </strong>The “nbf” claim identifies the time before which the JWT must not be accepted for processing. The current date/time must be after or equal to the not-before date/time</li><li><strong>iat (Issued At) Claim: </strong>The “iat” claim is used to identify the the time at which the JWT was issued. This claim can be used to determine the age of the JWT.</li><li><strong>jti (JWT ID) Claim: </strong>The “jti” claim gives a unique identifier for the JWT. The “jti” value is a case-sensitive string and it should be assigned in such a manner that ensures that there is a negligible probability that the same value will be repeated. The “jti” claim can be used to prevent the JWT from being replayed.</li></ul><p><strong>Public Claims: </strong>These type of claims can be defined by group of people using the JWTs. Whenever any new claim name is defined it is necessary that it should be registered in the IANA “JSON Web Token Registry” or it should contain a collision resistant name to avoid collisions.</p><p><strong>Private Claims: </strong>These are custom claims defined and created by two parties in order to exchange information between them.</p><p>An example of payload is as follows:</p><pre>{<br>&quot;sub&quot;: &quot;user123&quot;,<br>&quot;name&quot;: &quot;John&quot;,<br>&quot;role&quot;: &quot;developer&quot;,<br>&quot;exp&quot;: &quot;1606595460&quot;,<br>}</pre><p>The payload is then Base64Url encoded to get the second part of the JWT.</p><h4>Signature</h4><p>Signature is the most important part of JWT which helps to verify if the information within the token has been tampered with or not. It can be also used to verify that the sender of the JWT is who it says it is.</p><p>In order to calculate the signature, you require three things: an encoded header, an encoded payload, and a secret. First, you will take the encoded header and encoded payload and concatenate them with a period separator to form a string. This concatenated string will be hashed using an algorithm specified in the header and a secret key to calculate the signature.</p><p>Consider the following example where we will be using the RSA algorithm to generate a signature.</p><pre>RSA(<br>base64UrlEncode(header) + &quot;.&quot; + base64UrlEncode(payload), secret<br>)</pre><p>Now let’s put all three parts together to get a JSON Web Token.</p><p>We have three Base64-URL strings, which can be concatenated with a period between each. The JWT is very compact and can be easily exchanged in HTML and HTTP environments.</p><blockquote>The header and payload can be easily decoded (since it’s just base64) to retrieve information contained within the token. The signature can be just used to maintain the integrity of the token and not to secure the contained information. Therefore, you should pass any sensitive information in the token and if you want to pass make sure you encrypt the token to secure it.</blockquote><h3>How does a JSON Web Token work?</h3><p>When a user sends his credentials to the server to login, the server authenticates the user. If the authorization is successful, the server sends a JSON Web Token to the user. The user can use the JWT to request any protected services/resources from the server by including the JWT in the <strong>Authorization</strong> header using the <strong>Bearer</strong> schema.</p><pre>Authorization: Bearer &lt;token&gt;</pre><p>When the server gets a request from the user to access any protected content, the protected routes of the server will look for a valid JWT in the Authorization header. If the token is present and is valid the server will allow access to the user.</p><p>The JWT contains necessary information about the user that can be used to identify the user, know the user’s privilege, and serve the user accordingly. Because of JWT, the server does not need to query the database every time a request comes in to check if the user has the necessary rights or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*dLhSE6Rd255MME2IH5EEsA.png" /><figcaption>Figure 3</figcaption></figure><p>Every token assigned by the server is signed by a secret key known to the server only. Therefore, only the server can use the secret key to verify the token and to check if the token has tampered. If an attacker tries to make any changes in the token (like granting admin privileges), the signature of the token needs to be calculated again that will require the secret key. Since the attacker does not have a secret key making any changes to the token will invalid it. The server will discard such requests to prevent unauthorized access.</p><h3>Advantages of JSON Web Token</h3><ul><li><strong>Compactness: </strong>JSON is less verbose than XML and therefore when it is encoded it takes up less space making JWT more compact then SAML.</li><li><strong>No need of Session: </strong>The JWT can contain all the necessary information about the user and therefore there is no need to maintain a session object on the server, saving up server memory.</li><li><strong>Built-in Expiration:</strong> The JWT has claims that can be used to assign it a expiration date/time. Therefore, the token can become invalid on its own after the expiration period.</li><li><strong>No need of Cookies: </strong>The token can be stored in the localStorage, indexDB, or some native store. This will provide a protection against CORS and CSRF attacks.</li><li><strong>Compatibility: </strong>In most programming languages, JSON parsers are popular because they map directly to objects. Contrary, there is no natural document-to-object mapping in XML. This makes it simpler than SAML assertions to operate with JWT.</li></ul><p>That’s it! Now you know pretty much everything about JSON Web Token.</p><p>If you find this article useful, please leave a comment. Also, don’t forget to share this article with your friends. Stay tuned for more articles in future. Thank you!</p><p><em>Written by: Ajinkya Bhuwad</em></p><h3>References</h3><ul><li><a href="https://jwt.io/introduction/">jwt.io</a></li><li><a href="https://medium.com/myplanet-musings/what-is-a-json-web-token-2193f383e963">Myplanet Musings</a></li><li><a href="https://medium.com/swlh/why-do-we-need-the-json-web-token-jwt-in-the-modern-web-8490a7284482">The Startup</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a5d6131157f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/all-you-need-to-know-about-json-web-token-jwt-8a5d6131157f">All You Need to Know About JSON Web Token (JWT)</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>