<?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: Achraf Affes</title>
    <description>The latest articles on Forem by Achraf Affes (@darken).</description>
    <link>https://forem.com/darken</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%2F713591%2F5a8dff1a-af06-482e-b85d-3d44c9e6c198.jpg</url>
      <title>Forem: Achraf Affes</title>
      <link>https://forem.com/darken</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/darken"/>
    <language>en</language>
    <item>
      <title>JavaScript and the event queue!</title>
      <dc:creator>Achraf Affes</dc:creator>
      <pubDate>Wed, 13 Oct 2021 18:23:45 +0000</pubDate>
      <link>https://forem.com/darken/javascript-and-the-event-loop-5dpb</link>
      <guid>https://forem.com/darken/javascript-and-the-event-loop-5dpb</guid>
      <description>&lt;p&gt;So we all know the definition of JavaScript, it is a single threaded synchronous language. &lt;br&gt;
This means it has one call stack and one memory heap, it executes code in order and must finish executing a piece of code before moving onto the next and hence the language is blocking in nature.&lt;/p&gt;

&lt;p&gt;Again JavaScript is synchronous and single-threaded so if we execute a JavaScript block of code on a page then no other JavaScript code on that same page will parallelly be executed!&lt;/p&gt;

&lt;p&gt;The definitions seems &lt;strong&gt;absurd&lt;/strong&gt;, since we all use asynchronous stuff using JS or is it all an illusion?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;so from the various resources I've read on this topic, here's what I understand:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript itself is &lt;strong&gt;synchronous&lt;/strong&gt; but the &lt;strong&gt;browser&lt;/strong&gt; makes it possible to code in an asynchronous way, How is that?&lt;/p&gt;

&lt;p&gt;Well the answer is : &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;by using the Browser APIs, such as Fetch API, setTimeout, Promises, Geolocation API etc ..&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Whenever an asynchronous function is called, it is sent to a browser API, these are APIs built into the browser. &lt;br&gt;
Based on the commands received from the call stack, the API starts its own single-threaded operation.&lt;/p&gt;

&lt;p&gt;An example of this is the setTimeout method. &lt;br&gt;
When a setTimeout operation is processed in the call stack, it is sent to the corresponding API which waits till the specified time to send this operation back in for processing. &lt;br&gt;
Where does it send the operation? &lt;strong&gt;The event queue&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;The event loop constantly checks whether or not the call stack is empty, if it is empty, new functions are added from the event queue, if it is not, then the current function call is processed. &lt;/p&gt;

&lt;p&gt;So lets dive deeper into the event queue itself.&lt;br&gt;
To make sense out of all of this, we need to talk about some terminology first:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tasks&lt;/strong&gt; : any block of code (literally)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Browser APIs&lt;/strong&gt; : APIs that usually start their own threads to run code parallelly ( most of them requires a callback which will later on be pushed to the event queue )&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Micro Tasks&lt;/strong&gt; : any callback related to a promise&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;rendering process&lt;/strong&gt; : anything related to rendering such as style calculation, or any requestAnimationFrame callback&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;call stack&lt;/strong&gt; : a mechanism for an interpreter to keep track of its place in a script that calls multiple functions ( what function is currently being run and what function initially called it ).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Heap&lt;/strong&gt; : an area of pre-reserved computer main storage ( memory ) that a program process can use to store data&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thread&lt;/strong&gt; : a sequence of programmed instructions that can be managed independently by a scheduler it got its own heap and stack.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Alright time to make sense of it all!&lt;br&gt;
So we said that the main thread runs JavaScript synchronously, when we use some certain BrowserAPI when that command is executed in the stack a new thread is initiated that runs its code independently&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbhqfok6galmgm8w6xjmg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbhqfok6galmgm8w6xjmg.png" alt="What’s happening under the hood in the main thread"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s take setTimeout as an example, the new thread will keep tracking sysTick till the X ms runs out, the new thread sends a message to the main Thread telling it to enqueue (push) its attached callback to the event queue, the event loop then waits till the call stack is empty to dequeue some callback into the stack, which will then be executed.&lt;/p&gt;

&lt;p&gt;The Scheme explains it perfectly.&lt;br&gt;
What is important to note is that not all callbacks got the same priority and the same order of execution or enqueueing.&lt;br&gt;
A funny representation that I saw in JSconf presentation, describes the event loop as following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;


&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getNextQueue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="nc"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;microtaskQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasTasks&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="nf"&gt;doMicrotask&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isRepaintTime&lt;/span&gt;&lt;span class="p"&gt;()){&lt;/span&gt;
      &lt;span class="nx"&gt;animationTasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;animationQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copyTasks&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;task&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;animationTasks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="nf"&gt;doAnimationTask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;repaint&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;MicroTasks as many sources explains them, are usually promises callbacks, please notice that when pushing them to the event queue we push the entire MicroTask queue, while when pushing Tasks we only push the first-in callback in the tasks queue.&lt;/p&gt;

&lt;p&gt;We also push the entire render queue to the event queue when it's time to render ( usually browsers repaints the screen every 16/17ms since most of them runs with a frequency of 60Hz )&lt;br&gt;
So a good practice is to use requestAnimationFrame to run animations rather than running it in simple tasks or microtasks, since its pointless to repaint it in higher frequency cause the the human eye can see between 30 and 60 frames per second (30/60Hz).&lt;/p&gt;

&lt;p&gt;Another presentation in the JSConf visualizes the event loop as following&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq8h02s6lg3wtwc3t8mpp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq8h02s6lg3wtwc3t8mpp.png" alt="Event loop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So as a conclusion:&lt;/strong&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The event loop is the secret behind JavaScript's asynchronous programming. JavaScript executes all operations on a single thread, but using a few smart data structures, it gives us the illusion of multi-threading, so describing JavaScript as asynchronous is arguably misleading.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I hope this made sense in anyway, if not I highly recommend you to check these presentations to watch, I guaranty you will understand it much better:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=cCOL7MC4Pl0" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=cCOL7MC4Pl0&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ&amp;amp;t=594s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=8aGhZQkoFbQ&amp;amp;t=594s&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=u1kqx6AenYw&amp;amp;t=861s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=u1kqx6AenYw&amp;amp;t=861s&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Please feel free to explain it in your own way in the comments section or to provide us with more links about the subject.&lt;br&gt;
Thanks for reading.&lt;/p&gt;

</description>
      <category>eventloop</category>
      <category>javascript</category>
      <category>synchronous</category>
      <category>asynchronous</category>
    </item>
    <item>
      <title>JWT how does it work and is it secure?</title>
      <dc:creator>Achraf Affes</dc:creator>
      <pubDate>Mon, 11 Oct 2021 20:48:57 +0000</pubDate>
      <link>https://forem.com/darken/jwt-how-does-it-work-and-is-it-secure-37n</link>
      <guid>https://forem.com/darken/jwt-how-does-it-work-and-is-it-secure-37n</guid>
      <description>&lt;p&gt;&lt;strong&gt;JWT stands for JSON web token&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the common definition says that it is an open industry standard RFC 7519 method for representing claims securely between two parties &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;so lets break it up into a simpler logic to understand its utility and the way it works!&lt;br&gt;
So JWT was built by some developers in Microsoft, they built it initially for information exchange, and later on it was repurposed for authorization.&lt;/p&gt;

&lt;p&gt;In security processes, authentication validates a user's identity, it also grants that user permission to access a resource.&lt;br&gt;
JWT is a stateless session, so it does not need to be saved in a database in the server-side like cookies, it only exists in the client side.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A JWT is composed by :&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;header . payload . signature&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c&lt;/p&gt;

&lt;p&gt;The Header is the metadata about the token, its the result of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;base64Url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;base64-url&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="c1"&gt;// used for Base64 and URL Encoding Decoding &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;header&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;base64Url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;alg&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;HS256&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// algorithm : none, HS256, RS256, PS256 etc ..&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JWT&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//outputs : eyJhbGciOiJQUzI1NiIsInR5cCI6IkpXVCJ9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;please notice that it is not encrypted it's just encoded which means you can use base64 decode and you will get the JSON object in clear.&lt;/p&gt;

&lt;p&gt;the payload contains the message we want to send alongside with different information about the token itself&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;base64Url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;base64-url&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="c1"&gt;// used for Base64 and URL Encoding Decoding &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;header&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;base64Url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; 
    &lt;span class="na"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1234567890&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//subject&lt;/span&gt;
    &lt;span class="na"&gt;iss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Darken&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//issuer&lt;/span&gt;
    &lt;span class="na"&gt;aud&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;My API&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//audience used for auth as well &lt;/span&gt;
    &lt;span class="na"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1633895355&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//expiration datetime&lt;/span&gt;
    &lt;span class="na"&gt;iat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1633895235&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//issued at datetime&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//outputs : eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9&lt;/span&gt;
&lt;span class="c1"&gt;//lIiwiaWF0IjoxNTE2MjM5MDIyfQ&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again it is not encrypted it's just encoded which means you can use base64 decode and you will get the JSON object in clear.&lt;br&gt;
So far we are not securing information, so you may be wondering, how is this secure, and where is the authentication in all of this?&lt;br&gt;
And that's where the signature plays it's role!&lt;/p&gt;

&lt;p&gt;A signature is the result of some function that uses the header, the payload a secret key and hash function.&lt;br&gt;
The secret key is the most important part, a good advice is to use a 256bit key and don't hard code it ( save it in process.env )&lt;br&gt;
Please note that if we are using asymmetric encryption, when calculating the signature the algorithm uses both keys ( private and public )&lt;/p&gt;

&lt;p&gt;So the signature is usually calculated like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;crypto&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// cryptography library&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;base64Url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;base64-url&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SECRET&lt;/span&gt;
&lt;span class="c1"&gt;//Again ! please use a 256bit secret key&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;${header}.${payload}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="c1"&gt;//used for Base64 and URL Encoding Decoding &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;signature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;base64Url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;escape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createHmac&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sha256&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;base64&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//outputs : SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this creates an HMAC encryption (Hash-based message authentication code) a cryptographic technique that combines the key and a hash into a mix hackers can't unpack.&lt;/p&gt;

&lt;p&gt;So the authentication part shows up here! Have the content of this message been manipulated?&lt;/p&gt;

&lt;p&gt;Remember that the token is equal to :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;${header}.${payload}.${signature}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;eyJzdWIiOiIxMjM0NTY3ODkwIiwib&lt;/span&gt;
&lt;span class="nx"&gt;mFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SflKxwRJSMeKKF2QT4fw&lt;/span&gt;
&lt;span class="nx"&gt;pMeJf36POk6yJV_adQssw5c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the hacker can change the signature but can't guess the right signature( he doesn't know the secret key ) then when the attacker changes the payload or the header, the signature no longer matches the data.&lt;br&gt;
So lets suppose the hacker decoded the payload and changed it to :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sub&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This was changed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;AchrafAffes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;iat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1516239022&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//The payload encoded will then be changed to :&lt;/span&gt;
&lt;span class="nx"&gt;eyJzdWIiOiJUaGlzIHdhcyBjaGFuZ2VkIiwibmFtZSI6IkFjaHJhZkFmZmVzIiwiaW&lt;/span&gt;
&lt;span class="nx"&gt;F0IjoxNTE2MjM5MDIyfQ&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And again! since the hacker can't guess the right signature for the new encoded payload ( no secret key ) then when the server decodes the header and the payload, and recalculates the new signature it will be : do3cSS2wLRUM6cmqVqvFZVpCwJkeO0BieF0h0oTWaBE&lt;br&gt;
which is impossible for the hacker to guess unless he knows the secret key ( remember when using single symmetrical key to use a 256 bit key) and here the server will predict that the payload or the header were changed and therefore it will ignore the request.&lt;/p&gt;

&lt;p&gt;Now that you understand how the JWT works, how do we use it in action ?&lt;/p&gt;

&lt;p&gt;For me I use it as following, the user logs in, the server checks for the credentials whether this user coords exists or not, if it does, the server generates a token and sends it to the user ( the server does not save a copy ) the user then saves the token in its localstorage ( the token should have a short expiration datetime since it is vulnerable for XSS attacks which I'll explain in another post in the future )&lt;br&gt;
Whenever the user wants to access something, it sends the token in its header, and the server verifies it, if its verified then the server responds else the server responds with a 403 Forbidden error.&lt;/p&gt;

&lt;p&gt;In some other solutions, we implement an authentication server(AS), the user passes by the AS first and then it is redirected to the resource server (API) which will verify the token with each request.&lt;/p&gt;

&lt;p&gt;If you are working with nodeJs you can use the jsonwebtoken package, to easily implement the JWT&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jsonwebtoken&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;secretkey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="c1"&gt;//please make sure to use a 265bit key&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;achraf&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;other&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;stuffHere&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//to generate the data we use&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;expiresIn&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2 min&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;//other options can be used&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//and to verify it you can use&lt;/span&gt;
&lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;tokendata&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Unauthorized request&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tokendata&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;verified&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so lets talk quickly about the most recommended algorithms that can be used :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;HS256 : HMAC + SHA-265&lt;/strong&gt; &lt;br&gt;
(relies on the shared symmetrical key)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RS256 / RSASSA + SHA-256&lt;/strong&gt;&lt;br&gt;
(relies on private/public RSA key pair, but it may overload the network and uses more CPU for calculation )&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ES256 : ECDSA using p-265 and SHA-265&lt;/strong&gt;&lt;br&gt;
(relies on private/public RSA key pair but much shorter )&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I'll try to talk about these algorithms in details in the future&lt;/p&gt;

&lt;p&gt;finally I wanna talk about the difference between cookies and JWT:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Cookies need to be stored on the server-side while JWT are stateless&lt;/li&gt;
&lt;li&gt;since cookies require a database, this database will be queried on each client request&lt;/li&gt;
&lt;li&gt;cookies are vulnerable for both CSRF and XSS attacks, while JWT is only vulnerable to XSS attack.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

</description>
      <category>token</category>
      <category>react</category>
      <category>jwt</category>
      <category>security</category>
    </item>
    <item>
      <title>DHCP and static IPs ??</title>
      <dc:creator>Achraf Affes</dc:creator>
      <pubDate>Sat, 02 Oct 2021 23:07:16 +0000</pubDate>
      <link>https://forem.com/darken/dhcp-and-static-ips-okk</link>
      <guid>https://forem.com/darken/dhcp-and-static-ips-okk</guid>
      <description>&lt;p&gt;I'm not gonna talk about how does a DHCP server work exactly, &lt;br&gt;
this Post is destined for those who understands that already but still got some dark spots with their knowledge about the topic,&lt;br&gt;
this is simply an explanation for the famous problem : &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;If one of the devices in the network is configured with a static IP address, when the DHCP server starts distributing the IP addresses to other machines, what happens exactly ? does it skip it ? or does it duplicate it and therefore there would be a conflict ?&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So the answer is, &lt;strong&gt;NOPE&lt;/strong&gt; the DHCP server will not distribute that IP address, you may be wondering, but how ?&lt;/p&gt;

&lt;p&gt;Remember that the DHCP server sends an ARP Broadcast asking about the IP address about to be distributed &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Who has x.x.x.x"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At that moment the machine with that static IP address will respond with an ARP saying that it got that IP and that its MAC address is X:X:X:X:X:X so the DHCP server will continue to the next IP address asking "Who has y.y.y.y"&lt;br&gt;
and therefore there will be no duplication.&lt;/p&gt;

&lt;p&gt;But here comes another problem, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;what if the local network is placed behind a router, and we all know that the routers doesn't allow broadcasting, then what happens ?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;well simply instead of ARP Broadcasting, it uses ICMP Packet type 8 code 0 which is the ECHO request ( ping ) and if it gets no answer than it distributes it, else it simply continues to the next IP address.&lt;/p&gt;

&lt;p&gt;And here comes the last question, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;And what if a device that is configured with a static IP address was shutdown, and the DHCP distributed its IP address, what happens if the machine is up again?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;well there are two solutions for this, some DHCP servers are advanced enough and caches the static IP address and therefore this conflict will never occurs, but if the DHCP server doesn't provide this feature, than it should be configured to exclude these IP addresses or else there will be an IP Duplication.&lt;/p&gt;

&lt;p&gt;And this pretty much sums up the main problems of dealing with a DHCP server.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>beginners</category>
      <category>dhcp</category>
      <category>networking</category>
    </item>
  </channel>
</rss>
