<?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: Ram Nad</title>
    <description>The latest articles on DEV Community by Ram Nad (@ramnad1999).</description>
    <link>https://dev.to/ramnad1999</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%2F320201%2F1fec2e9a-c115-4324-8ab8-a63b67dc49db.jpeg</url>
      <title>DEV Community: Ram Nad</title>
      <link>https://dev.to/ramnad1999</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ramnad1999"/>
    <language>en</language>
    <item>
      <title>C++ Templates and SFINAE</title>
      <dc:creator>Ram Nad</dc:creator>
      <pubDate>Wed, 05 Feb 2020 17:42:28 +0000</pubDate>
      <link>https://dev.to/iiits-iota/c-templates-and-sfinae-1dd4</link>
      <guid>https://dev.to/iiits-iota/c-templates-and-sfinae-1dd4</guid>
      <description>&lt;p&gt;Recently, I came across a problem while looking for &lt;code&gt;good first issues&lt;/code&gt; on GitHub. I would like to share the problem and its solution with you in this post. Just for clarity, we would work with a simpler version of the problem.&lt;/p&gt;

&lt;p&gt;Suppose there is a template class &lt;code&gt;Container&lt;/code&gt; that takes a &lt;code&gt;class&lt;/code&gt; as Backend conforming to a particular interface. This &lt;code&gt;Container&lt;/code&gt; acts as a wrapper on the Backend providing it with extra functionalities.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;Backend&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;private:&lt;/span&gt;
    &lt;span class="n"&gt;Backend&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;Container&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;data1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;data2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&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;Now all the Backends need to have the following particular function,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Backend1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&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;This function is called by a &lt;code&gt;get()&lt;/code&gt; function inside our container. The problem is that we need to change this interface to add up new functionalities. So, we now need to call &lt;code&gt;getValue()&lt;/code&gt; with 2 parameters, instead of 1. So, our interface should now be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We cannot make this change suddenly, because many users and backends depend upon the old functionality. Therefore, for the backends that support new interface we will call &lt;code&gt;getValue&lt;/code&gt; with two parameters else we would call it with one.&lt;/p&gt;

&lt;p&gt;We need to somehow statically(that is at compile-time) detect if any backend supports the new interface we use it, else we should stick with older interface.&lt;/p&gt;

&lt;p&gt;Now, this is where the magic of C++ templates come to picture.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SupportedBackend&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;data2&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UnsupportedBackend&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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, among above two backends, &lt;code&gt;SupportedBackend&lt;/code&gt; supports new interface whereas &lt;code&gt;UnsupportedBackend&lt;/code&gt; does not. (I could not come up with any other descriptive name)&lt;/p&gt;

&lt;p&gt;Here, is the code that can do the work for us,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;type_traits&amp;gt;
&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;check_new_interface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;decltype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;declval&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

  &lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;decltype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&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;&lt;code&gt;check_new_interface&amp;lt;T&amp;gt;::value&lt;/code&gt; would be &lt;em&gt;true&lt;/em&gt; if &lt;code&gt;T&lt;/code&gt; supports new interface else it will be &lt;em&gt;false&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let us now understand the above code.&lt;/p&gt;

&lt;p&gt;So, &lt;code&gt;check_new_interface&lt;/code&gt; class has two template member functions with same name (but different signatures). Now the value of &lt;code&gt;value&lt;/code&gt; depends upon the following expression: &lt;code&gt;sizeof(decltype(check&amp;lt;T&amp;gt;(0))) == sizeof(int)&lt;/code&gt;. This is where we need to understand a lot of things.&lt;/p&gt;

&lt;p&gt;First, &lt;code&gt;sizeof&lt;/code&gt; and &lt;code&gt;decltype&lt;/code&gt; do not evaluate the expression they are called with. So, the following program would only print &lt;code&gt;4&lt;/code&gt; (that is sizeof(int)) instead of &lt;code&gt;hello4&lt;/code&gt;. (Did you notice that we did not define the &lt;code&gt;check&lt;/code&gt; functions although we called it the above expression)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;cstdio&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%lu"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&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;Second, when C++ sees &lt;code&gt;check&amp;lt;T&amp;gt;(0)&lt;/code&gt; expression, it trys to find a proper function declaration that matches this call. There are two options, so template function overload resolution comes into picture. As, &lt;code&gt;0&lt;/code&gt; is an integer literal, so C++ would first try to associate this call with function that takes integer as its parameter. If it cannot do so it will then check other options. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;echo_with_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"double: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;first_echo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;echo_with_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;echo_with_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"int: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;second_echo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;echo_with_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;first_echo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;second_echo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&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;If, you run the above code, you would find that first output would be &lt;code&gt;double: 2&lt;/code&gt; and second would be &lt;code&gt;int: 2&lt;/code&gt;. This is because when &lt;code&gt;first_echo&lt;/code&gt; function is called, it does not find any &lt;code&gt;echo_with_type&lt;/code&gt; function with integer parameter so it calls the one which takes double as parameter, but second time it finds one with integer and therefore call its.&lt;/p&gt;

&lt;p&gt;Similarly, in our situation for &lt;code&gt;check&amp;lt;T&amp;gt;(0)&lt;/code&gt; compiler would try to substitute &lt;code&gt;T&lt;/code&gt; into the first template before the second one (for similar reasons as above program). Now, this is where the real magic happens. When any type is substituted in place of R, then only if that type has a member function with signature &lt;code&gt;getValue(int, int)&lt;/code&gt; the substitution would succeed. Else it will fail. So, in our case when we will substitute &lt;code&gt;SupportedBackend&lt;/code&gt; then this substitution would be succeed but in case of &lt;code&gt;UnsupportedBackend&lt;/code&gt; it will fail. (Sorry, I am skipping few details here, like what is &lt;code&gt;declval&lt;/code&gt; and why we use it but we can get into that sometimes later.)&lt;/p&gt;

&lt;p&gt;Now, if C++ did not support &lt;code&gt;SFINAE&lt;/code&gt;, then the compiler would have thrown an error here. But luckily it does so we can continue.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;SFINAE - Substitution Failure is not an Error&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Basically &lt;code&gt;SFINAE&lt;/code&gt; means that if a template substitution fails then compiler does not throw error but continues to find a valid template substitution until it succeeds. (If it does not find any then ofcourse it will throw error.)&lt;/p&gt;

&lt;p&gt;So, in our case as well compiler continues to find a valid substitution for &lt;code&gt;check&amp;lt;T&amp;gt;(0) [when T = UnsupportedBackend]&lt;/code&gt;. Because first template substitution failed, it will move to the second template and succeed. But when it succeeds for the second template then return type of &lt;code&gt;check&amp;lt;T&amp;gt;&lt;/code&gt; would be &lt;code&gt;double&lt;/code&gt; instead of &lt;code&gt;int&lt;/code&gt;. (Where as in case of &lt;code&gt;T = SupportedBackend&lt;/code&gt; it would be &lt;code&gt;int&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;So, it is clear that the &lt;code&gt;check_new_interface::value&lt;/code&gt; would be &lt;em&gt;true&lt;/em&gt; in case of &lt;code&gt;SupportedBackend&lt;/code&gt; and &lt;em&gt;false&lt;/em&gt; in case of &lt;code&gt;UnsupportedBackend&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, we may be tempted to implement &lt;code&gt;get()&lt;/code&gt; as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;get&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="n"&gt;check_new_interface&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Backend&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;::&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&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;But, compiler would give errors if we try to do this. The reason is although compiler knows for any given &lt;code&gt;Backend&lt;/code&gt; whether if part will exceute or else part but it needs to generate code for both of them. So, obviously it will fail.&lt;/p&gt;

&lt;p&gt;The solution again would be to use function overloading here. &lt;code&gt;std::true_type&lt;/code&gt; and &lt;code&gt;std::false_type&lt;/code&gt; are specialized types of class template &lt;code&gt;std::integral_constant&lt;/code&gt; defined as following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;integral_constant&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;true_type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;integral_constant&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;false_type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="nl"&gt;private:&lt;/span&gt;

&lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;false_type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;true_type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;integral_constant&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;check_new_interface&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Backend&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;::&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;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;Now, this would work flawlessly because, when compiler generates code for &lt;code&gt;get()&lt;/code&gt; function then, it only needs to compile one of &lt;code&gt;get(std::false_type)&lt;/code&gt; or get(std::true_type) depending upon the value of &lt;code&gt;check_new_interface&amp;lt;Backend&amp;gt;::value&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, finally we have our solution ready. You can find the complete code &lt;a href="https://gist.github.com/ram-nad/1b9eca2a078b35998833c8d7b07e04b3"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope that you got to learn something new in this post. Thanks for reading.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>templates</category>
      <category>sfinae</category>
      <category>metaprogramming</category>
    </item>
    <item>
      <title>JSON Web Tokens (Part 1)</title>
      <dc:creator>Ram Nad</dc:creator>
      <pubDate>Tue, 04 Feb 2020 10:01:54 +0000</pubDate>
      <link>https://dev.to/iiits-iota/json-web-tokens-part-1-5c83</link>
      <guid>https://dev.to/iiits-iota/json-web-tokens-part-1-5c83</guid>
      <description>&lt;p&gt;JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA.&lt;/p&gt;

&lt;p&gt;In this post I would like to explain how can we use JWTs for authentication in web applications. In the next part, we will be using &lt;code&gt;NodeJS&lt;/code&gt;, &lt;code&gt;Express&lt;/code&gt; and &lt;code&gt;jsonwebtoken&lt;/code&gt; library for implementing a simple JWT based authentication. Note that, we would use the word system and server interchangeably.&lt;/p&gt;

&lt;p&gt;So, a JWT looks typically like this:&lt;/p&gt;


&lt;center&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yPh40ROp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8cgd55dhl60n8o430f1e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yPh40ROp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8cgd55dhl60n8o430f1e.png" alt="JWT"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Image Source: &lt;a href="https://jwt.io/"&gt;jwt.io&lt;/a&gt;&lt;/p&gt;


&lt;/center&gt;

&lt;p&gt;So, this token can be broken down into 3 parts each separted by a &lt;strong&gt;&lt;code&gt;.(dot)&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Header (Red colour)&lt;/li&gt;
&lt;li&gt;Payload (Pink colour)&lt;/li&gt;
&lt;li&gt;Signature (Light Blue colour)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The header and payload sections are just normal JSON strings that is encoded in &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/Base64"&gt;base64&lt;/a&gt;&lt;/strong&gt;. Now, there is a difference between &lt;code&gt;encoding&lt;/code&gt; and &lt;code&gt;encryption&lt;/code&gt;. Encryption is when you use a secret key to transform message into a cipher which can only be transformed back to original message if you have the key. But in encoding, we just transform the message into a format that is easy to transfer between computers. Anyone with an encoded message can transform it back and read its content(there is no key involved).&lt;/p&gt;

&lt;p&gt;If you use a &lt;a href="https://www.base64decode.org/"&gt;base64 decoder&lt;/a&gt; for decoding the header or payload section here is what you get,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"alg"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"HS256"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"typ"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"JWT"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"sub"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1234567890"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"John Doe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"iat"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1516239022&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Third and final part, signature is also &lt;code&gt;base64&lt;/code&gt; encoded but if you try to decode it, you would get a gibberish text.&lt;/p&gt;

&lt;h3&gt;
  
  
  Header
&lt;/h3&gt;

&lt;p&gt;Header section contains some information about the nature of this token. For example, in the particular token above, &lt;code&gt;typ&lt;/code&gt; contains information about the type of this token(which is JWT) and &lt;code&gt;alg&lt;/code&gt; says that the algorithm used to sign this token that is &lt;code&gt;HMCASHA256&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: We will explain what does &lt;em&gt;sign&lt;/em&gt; means later in this post. But for now you can take the analogy of &lt;em&gt;signature&lt;/em&gt; that people do on documents to say &lt;em&gt;that I have verified it and I approve of it.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Payload
&lt;/h3&gt;

&lt;p&gt;Payload section contains of a set of &lt;code&gt;claims&lt;/code&gt; that a party makes with this token. So, the contents of payload section says when, where, why and by whom this token can be used. Again continuing with the example given, &lt;code&gt;sub&lt;/code&gt; stands for &lt;strong&gt;subject&lt;/strong&gt; and it contains information about whom is this token concerned with, &lt;code&gt;name&lt;/code&gt; is evidently name of the person and &lt;code&gt;iat&lt;/code&gt; means &lt;strong&gt;issued at&lt;/strong&gt;, It is Unix timestamp of the moment when this token was issued.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Side Note: Unix Timestamp is number of seconds that has passed since January 1, 1970. considering that every day has 86400 seconds. Also since this is calculated relative to UTC, it is same for all computers around the world.&lt;/p&gt;

&lt;p&gt;Side Note: You will come across names John Doe quite frequently if you read posts like this. This is the frequently used name to refer to a male whose name is not known or is being concealed. Jane Doe is used for females.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now there are certain standard claims that is specified in &lt;a href="https://tools.ietf.org/html/rfc7519"&gt;RC 7519&lt;/a&gt; for a particular purpose:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;iss (issuer): Issuer of the JWT&lt;/li&gt;
&lt;li&gt;sub (subject): Subject of the JWT (the user)&lt;/li&gt;
&lt;li&gt;aud (audience): Recipient for which the JWT is intended&lt;/li&gt;
&lt;li&gt;exp (expiration time): Time after which the JWT expires&lt;/li&gt;
&lt;li&gt;nbf (not before time): Time before which the JWT must not be accepted for processing&lt;/li&gt;
&lt;li&gt;iat (issued at time): Time at which the JWT was issued; can be used to determine age of the JWT&lt;/li&gt;
&lt;li&gt;jti (JWT ID): Unique identifier; can be used to prevent the JWT from being replayed (allows a token to be used only once)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now before we get to the third and most important part of JWTs, let us first consider a scenerio where JWT is used for user authentication.&lt;/p&gt;

&lt;p&gt;Consider how user's are authenticated normally(that is without the use of tokens). So, when user logs in for the first time, he gives an username and password. The system checks if username and password is correct. If it matches with the database records, then systems creates a random string (called session identifier), stores it in the database along with user's identification and sends the identifier back to the user.(It is usually send as a cookie, but that does not matter for our disscussion)&lt;/p&gt;

&lt;p&gt;Next time when user visits the website, he sends this identifier along with the request. When system recieves this identifier, it matches that with information in the database. If the identifier matches, then the system knows that user is genuine. To log out a user, system simply deletes this identifier from the database.&lt;/p&gt;


&lt;center&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HhRwlXRT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t5ozjwmmkvyrvcnk6f6u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HhRwlXRT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t5ozjwmmkvyrvcnk6f6u.png" alt="Session based authentication"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Image Source: &lt;a href="https://medium.com/@sherryhsu/session-vs-token-based-authentication-11a6c5ac45e4"&gt;Medium&lt;/a&gt;&lt;/p&gt;




&lt;/center&gt;

&lt;p&gt;Now why does the system believes in this identifier? The reason is that this identifier is usually a long and random string. So, it is not possible for anyone to know it. Also if someone tries to randomly generate all the possible tokens, it might take him 10&lt;sup&gt;18&lt;/sup&gt; billion years to test every combination.(Assuming that identifier is 128 bits long and it takes 1 second for testing 10,000 strings) For reference, current age of universe is 13.8 billion years.&lt;/p&gt;

&lt;p&gt;There are few problem with this session based approach though. First one which is that for every request the system needs to hit database to check if &lt;code&gt;session identiifer&lt;/code&gt; is valid. (This is problematic because database access takes time) Other similar but related problem is with microservice architecture. In this case, identifiers need to be stored in a central server, because different servers need to access this information.(Therefore, this would take even more time and is much more difficult to manage) Both of this problems can be solved with the help of the JWTs.&lt;/p&gt;

&lt;p&gt;So in the case of JWTs, when user first logs in the system generates one of this tokens and send it to the user. The claims that system usually put in this token is &lt;code&gt;sub&lt;/code&gt; to identify the user and other details like the user's privilege level. So, when user makes a request to the server, then system checks if the token is well formed JWT and just by decoding the payload section it can verify the user which this token identifies.&lt;/p&gt;


&lt;center&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RAVeEl4w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0206uz1lkv14rd95smo6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RAVeEl4w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0206uz1lkv14rd95smo6.png" alt="JWT based authentication"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Image Source: &lt;a href="https://medium.com/@sherryhsu/session-vs-token-based-authentication-11a6c5ac45e4"&gt;Medium&lt;/a&gt;&lt;/p&gt;




&lt;/center&gt;

&lt;p&gt;Now, the only problem is anyone can generate this token. (Remember that header and payload are just encoded) So, anyone in this world can generate a token containing anyone else's identification and send it to the server. The system would then not be able to distinguish wether the token came from the right person or someone else. This is the place where the signature comes into picture.&lt;/p&gt;

&lt;h3&gt;
  
  
  Signature
&lt;/h3&gt;

&lt;p&gt;Signature is used to verify that this token (JWT) is issued only by the server and no one else could generate it.&lt;/p&gt;

&lt;p&gt;The idea is very simple. Each system has a long and unique &lt;code&gt;secret key&lt;/code&gt; with itself. This key cannot be guessed by anyone else. (Because of similar reasons of session identifier) When the system generates a token it then sends it through a Message Authentication Code Algorithm(HMACSHA256 is one of them. There are many others) along with the secret key. Now this algorithm gives a pattern of bits as its result. This bit pattern is then encoded into base64 format to form the signature.&lt;/p&gt;

&lt;p&gt;The bit pattern(which we will call signature from now) given out by the algorithm is not random but it has two important properties.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Given the signature, one cannot guess the value of secret key or the contents of the message.(This is to say that the algorithm computes a one-way function)&lt;/li&gt;
&lt;li&gt;Second if we change the contents of message or the secret key even slightly the signature would change completely.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This two properties make sure that only way to guess the correct signature for any given message is to randomly try all possible combination. (Which would again take 10&lt;sup&gt;18&lt;/sup&gt; billion years) Therefore it is highly unlikely that anyone would be able to do it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Scientists say that sun would burn out within 4-5 billion years from now.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, when someone sends this token back to the server, it just generates the signature again using the same key and algorithm. If the resulting signature matches with the signature that came with the token, then server can be pretty sure that this is a valid token.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: For this particular &lt;code&gt;HMACSHA256&lt;/code&gt; algorithm the signature is 256 bits long. If you want a rough idea of how unlikely it is to guess a signature watch this &lt;a href="https://www.youtube.com/watch?v=S9JGmA5_unY"&gt;3Blue1Brown video&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I hope that you have an idea of how JWT is used. I feel that this post is getting a bit long. So, we will continue with how to implement JWT using NodeJS in next part of this post.&lt;/p&gt;

&lt;p&gt;You can verify the example signature using &lt;a href="https://jwt.io/"&gt;jwt.io&lt;/a&gt;. Just enter the header and payload given above. The secret I used is &lt;code&gt;hello&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://tools.ietf.org/html/rfc7519"&gt;https://tools.ietf.org/html/rfc7519&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://jwt.io/introduction/"&gt;https://jwt.io/introduction/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/HMAC"&gt;https://en.wikipedia.org/wiki/HMAC&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scotch.io/tutorials/the-ins-and-outs-of-token-based-authentication"&gt;https://scotch.io/tutorials/the-ins-and-outs-of-token-based-authentication&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@sherryhsu/session-vs-token-based-authentication-11a6c5ac45e4"&gt;https://medium.com/@sherryhsu/session-vs-token-based-authentication-11a6c5ac45e4&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>jwt</category>
      <category>node</category>
      <category>authentication</category>
    </item>
  </channel>
</rss>
