<?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>Forem: Ayomide Akinbo</title>
    <description>The latest articles on Forem by Ayomide Akinbo (@aytheotaku).</description>
    <link>https://forem.com/aytheotaku</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%2F435254%2F44aab54b-6444-4ca0-9071-87237fe08838.png</url>
      <title>Forem: Ayomide Akinbo</title>
      <link>https://forem.com/aytheotaku</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aytheotaku"/>
    <language>en</language>
    <item>
      <title>Authentication using JSON Web Tokens.</title>
      <dc:creator>Ayomide Akinbo</dc:creator>
      <pubDate>Thu, 11 Apr 2024 22:24:59 +0000</pubDate>
      <link>https://forem.com/aytheotaku/authentication-using-json-web-tokens-ahg</link>
      <guid>https://forem.com/aytheotaku/authentication-using-json-web-tokens-ahg</guid>
      <description>&lt;p&gt;Why did the JSON Web Token need a break?&lt;br&gt;
Because it had too many "claims" to make! &lt;/p&gt;

&lt;p&gt;A JWT (JSON Web Token) is simply a base64url &lt;strong&gt;encoded&lt;/strong&gt; string which is short and efficient enough to transport over the internet.  This string looks something like this: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyMjExMyIsIm5hbWUiOiJyb2JpbidzIGNsaWVudCIsImlhdCI6MTUxNjIzOTAyMn0.pmV2EoDtuoSn1tn9yV0FDrV2x-W_ixcEVAb4Zkjdqms&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lol, doesn't look like it makes much sense, does it?  As you can see the string is split into 3 parts all concatenated with a period ‘.’ &lt;/p&gt;

&lt;p&gt;These parts are respectively the: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Header&lt;/li&gt;
&lt;li&gt;Payload and the&lt;/li&gt;
&lt;li&gt;Signature&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is important to know what a JWT should contain as we now go into the flow of authenticating users using JWTs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Flow
&lt;/h2&gt;

&lt;p&gt;The flow of using JWTs to authenticate users typically divided into two processes. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating the JWT&lt;/li&gt;
&lt;li&gt;Verifying the JWT.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Creating/Signing the JWT
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz21sdr3ja3q09i20q3w6.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz21sdr3ja3q09i20q3w6.gif" alt="Image description" width="498" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s assume we have a robot named Robin. Robin’s job is to create JWTs.&lt;/p&gt;

&lt;p&gt;To do this, Robin first requires some things from us, she needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Header JSON. This JSON should typically contain two name-value pairs. The first is the &lt;code&gt;alg&lt;/code&gt; (short for algorithm) property which has a value specifying the type of algorithm needed to sign and verify the JWT and the second is the &lt;code&gt;typ&lt;/code&gt; (short for type) which has a value specifying the type of token it is.&lt;/li&gt;
&lt;li&gt;A Payload JSON. This payload should also contain name-value pairs which are called &lt;em&gt;‘claims. A claim is a statement about an entity (usually the user) that includes information like their identity, role, or permissions. Claims can be used to convey a variety of data, such as user information, permission data, and other metadata.&lt;/em&gt; Claims can be anything you want to store about a client. There are already predefined claims though that help to provide consistency across different JWT implementations, known as Registered Claims. Some examples are&lt;/li&gt;
&lt;li&gt;"iss" (issuer): This claim identifies the entity that issued the JWT. The value of this claim should be a string that identifies the issuer, such as a URL or a unique identifier.&lt;/li&gt;
&lt;li&gt;"sub" (subject): This claim identifies the subject of the JWT. The value of this claim should be a string that identifies the user or entity that the JWT represents.&lt;/li&gt;
&lt;li&gt;"exp" (expiration time): This claim specifies the expiration time of the JWT.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Naturally, we store a client’s id in the sub claim. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TOP SECRET key(s). The last thing Robin needs from us, (depending on the value of the &lt;code&gt;alg&lt;/code&gt; property in the header JSON) is a key/set of keys needed to Sign the JWT. By Signing a JWT we are basically taking the Header and Payload data and hashing them with a secret key or pair of keys using the algorithm specified in &lt;code&gt;alg&lt;/code&gt; property&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AND THAT’S IT. Robin will take these and create a JWT. So Robin does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Accepts the Header and Payload JSON provided.&lt;/li&gt;
&lt;li&gt;Encodes each JSON into a base64url string.&lt;/li&gt;
&lt;li&gt;Concatenates them with a period ‘.’&lt;/li&gt;
&lt;li&gt;Hashes the result of the concatenation using the algorithm specified Header and TOP SECRET key(s) provided.&lt;/li&gt;
&lt;li&gt;Returns the JWT in the form &lt;code&gt;base64urlencoded(header).base64urlencoded(payload).base64urlencoded(hash_result_of_step_3)&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;NOTE: Never store sensitive information about a client in the payload as the JWT is just &lt;strong&gt;encoded&lt;/strong&gt; and not &lt;strong&gt;encrypted.&lt;/strong&gt; You can paste the JWT I gave as an example above in this cool site which basically allows you to see in decoded. &lt;a href="https://jwt.io/"&gt;JSON Web Tokens - jwt.io&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Some packages like the NPM &lt;code&gt;jsonwebtoken&lt;/code&gt; package help by abstracting the creation of JWTs.&lt;/p&gt;

&lt;p&gt;This process of creating) a JWT should occurs when a client has successfully logged into your system (this could be through a username and password implementation).&lt;/p&gt;

&lt;h2&gt;
  
  
  Verifying the JWT
&lt;/h2&gt;

&lt;p&gt;Verifying a JWT is actually pretty straight forward. Naturally, when a client sends a request to a protected route, they attach the JWT to the request, it’s mostly in the Authorization header. The following steps then take place to verify the JWT.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Splitting the JWT string concatenated by periods '.' to extract each component. (the header, payload and signature)&lt;/li&gt;
&lt;li&gt;Using an algorithm (specified by the algorithm in header) and the secret key/public key to compare the resulting hash to the signature of the JWT. If they match, then we know the JWT has not been tampered with.&lt;/li&gt;
&lt;li&gt;If the results of the hashes do not match, or the JWT has expired (info at expiry time gotten from payload), the JWT gets rejected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reasons why JWTs are used.&lt;/p&gt;

&lt;p&gt;A simple google search will show you a lot of reasons why JWTs are used but a couple stood out to me:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Stateless: JWT tokens are stateless, meaning that the server does not need to keep track of the user's session. The server does not need to have a database where it stores sessions.&lt;/li&gt;
&lt;li&gt;Scalability: JWT tokens can be used across multiple servers for authentication. In a microservices architecture where a client needs to visit multiple servers, each server does not need a session store, it just needs to verify the client’s JWT.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>security</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
