<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prakhar Rai</title>
    <description>The latest articles on DEV Community by Prakhar Rai (@prakharrai1609).</description>
    <link>https://dev.to/prakharrai1609</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1185106%2F174145d0-3034-4267-82f3-5da9e861e40b.jpg</url>
      <title>DEV Community: Prakhar Rai</title>
      <link>https://dev.to/prakharrai1609</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prakharrai1609"/>
    <language>en</language>
    <item>
      <title>The magic of JWT tokens ✨</title>
      <dc:creator>Prakhar Rai</dc:creator>
      <pubDate>Sat, 14 Oct 2023 17:36:13 +0000</pubDate>
      <link>https://dev.to/prakharrai1609/the-magic-of-jwt-tokens-193g</link>
      <guid>https://dev.to/prakharrai1609/the-magic-of-jwt-tokens-193g</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DrMpavix--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/st3qduea6pfwi10avir0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DrMpavix--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/st3qduea6pfwi10avir0.png" alt="Image description" width="800" height="697"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How does a web app know you are authorized to use a resource or not after authentication?&lt;/p&gt;

&lt;p&gt;That's where JWT tokens come into play ⚡️&lt;/p&gt;

&lt;p&gt;Traditionally what used to happen?&lt;/p&gt;

&lt;p&gt;When a client makes an authentication request (username/pass) to the server, the server stores the user info in the memory, generates an id which points to that location and sends that id as a session id (usually in browser cookies).&lt;/p&gt;

&lt;p&gt;After that every time you request another resource, the browser verifies the session id and then sends a response.&lt;/p&gt;

&lt;p&gt;Problems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Traditional session IDs require server-side storage to maintain session state. This can be resource-intensive and challenging to scale in distributed systems.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CORS restrictions can make it difficult to share session IDs securely across different domains or APIs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since the information mapping to session id is stored in server, managing session state across multiple servers or instances can be complex.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;How does JWT solve these problems? ⚡&lt;/p&gt;

&lt;p&gt;Structure of a JWT token: [𝘏𝘌𝘈𝘋𝘌𝘙].[𝘗𝘈𝘠𝘓𝘖𝘈𝘋].[𝘚𝘐𝘎𝘕𝘈𝘛𝘜𝘙𝘌]&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;JWT tokens are stateless and contain all necessary information, like user roles and permissions, in the token itself. Servers can verify tokens without needing to store session information.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since they contain all the info, they don't need to be stored on the server!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JWT tokens can be used as a standardized authentication mechanism, allowing users to access multiple services or APIs with a single token.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The signature is used to determine if the token was changed on the client's end.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;How is the signature made:
Algorithm: 𝘏𝘔𝘈𝘊𝘚𝘏𝘈256(𝘣𝘢𝘴𝘦64𝘜𝘳𝘭𝘌𝘯𝘤𝘰𝘥𝘦(𝘩𝘦𝘢𝘥𝘦𝘳𝘴) + "." + 𝘣𝘢𝘴𝘦64𝘜𝘳𝘭𝘌𝘯𝘤𝘰𝘥𝘦(𝘱𝘢𝘺𝘭𝘰𝘢𝘥), [𝘺𝘰𝘶𝘳&lt;em&gt;𝘴𝘦𝘤𝘳𝘦𝘵&lt;/em&gt;𝘬𝘦𝘺]).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is how the server validates the token, it decodes the &lt;code&gt;header&lt;/code&gt; and &lt;code&gt;payload&lt;/code&gt; and it uses the algo above to create the &lt;code&gt;new_signature&lt;/code&gt; and then verifies the &lt;code&gt;new_signature&lt;/code&gt; with the &lt;code&gt;signature&lt;/code&gt; in the JWT token.&lt;/p&gt;

&lt;p&gt;~ &lt;a class="mentioned-user" href="https://dev.to/prakharrai1609"&gt;@prakharrai1609&lt;/a&gt; &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>security</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
