{"id":233,"date":"2017-03-31T05:56:47","date_gmt":"2017-03-31T12:56:47","guid":{"rendered":"http:\/\/vinodsr.com\/myblog\/?p=233"},"modified":"2020-04-24T10:08:20","modified_gmt":"2020-04-24T17:08:20","slug":"json-web-tokens-intro","status":"publish","type":"post","link":"https:\/\/vinodsr.com\/myblog\/2017\/03\/json-web-tokens-intro\/","title":{"rendered":"JWT &#8211; Security with Simplicity"},"content":{"rendered":"<p style=\"text-align: right;\"><strong><span style=\"font-family: 'Advent Pro';\">Reading time : [est_time]<\/span><\/strong><\/p>\n<p><span style=\"font-family: 'Scope One';\"><span style=\"font-size: 36px;\">I<\/span>n today&#8217;s world , security is&nbsp;the prime priority.<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">Throughout the evolution of computers, security also evolved from simple password based authentication to multi factor based authentication, from simple text encryption&nbsp;to two key encryption. &nbsp;The latest in this evolution&nbsp;is JWT&nbsp;(JSON Web Tokens), which handles secure message passing between two clients \/ parties.<\/span><\/p>\n<h1>Why we need JWT ?<\/h1>\n<p><span style=\"font-family: 'Scope One';\">In a normal token based authentication, a token is granted for a authenticated user. All the subsequent service calls uses this token and identifies the user. The identification process takes place at server where this token is validated against a token store data. Same is the case for a SAML authentication. The idea of JWT is to make this token self contained with out loosing the compact nature. So the identity will also lies with in the token . This will reduce the time to look up in the token store and also makes the program code straight forward.<\/span><\/p>\n<p><!--more--><\/p>\n<h1>How JWT Works ?<\/h1>\n<p><span style=\"font-family: 'Scope One';\">JWT has got three parts; a header, a payload and a signature separated by a dot(.) .<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">A typical JWT token looks like<\/span><\/p>\n<pre class=\"lang:default decode:true\">xxxx.yyyy.zzzz\n\n&lt;header&gt;.&lt;payload&gt;.&lt;signature&gt;<\/pre>\n<h2><span style=\"font-family: Aclonica;\">1. Header<\/span><\/h2>\n<p><span style=\"font-family: 'Scope One';\">The header consist of two parts. The type of the token which is &nbsp;JWT and the hashing algorithm used eg: HMAC SHA256 or RSA. This JSON&nbsp;block is base64URL encoded.<\/span><\/p>\n<pre class=\"lang:js decode:true\">{ \n \"alg\": \"HS256\",\n \"typ\": \"JWT\" \n}<\/pre>\n<h2><span style=\"font-family: Aclonica;\">2. Payload<\/span><\/h2>\n<p><span style=\"font-family: 'Scope One';\">This section contains the information of the user with the associated meta data. The payload can also store relevant information as per the context. It is advised to make it short as possible (avoid using long key values).<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">Like the header the payload is also base64URL encoded.<\/span><\/p>\n<pre class=\"lang:js decode:true\">{\n \"name\" : \"Peter\",\n \"id\" : \"S190F34\"\n}<\/pre>\n<h2><span style=\"font-family: Aclonica;\">3. Signature<\/span><\/h2>\n<p><span style=\"font-family: 'Scope One';\">To create a signature, take the header and the payload and sign it using the mentioned algorithm with a secret.<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">It will look like this.<\/span><\/p>\n<pre class=\"lang:default decode:true\">HMACSHA256(\n  base64UrlEncode(header) + \".\" +\n  base64UrlEncode(payload),\n  secret)<\/pre>\n<p><span style=\"font-family: 'Scope One';\">Secret can be any string agreed between the two parties. The signature is used to verify the authenticity of the message.<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">Finally the JWT will look like&nbsp;<\/span><\/p>\n<p><span style=\"color: #ff00ff;\"><code>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiUGV0ZXIiLCJpZCI6IlMxOTBGMzQifQ.ZHMsgv28QW3wt3cnyd3pAF3Uzyw0dbzmKF9nZfH9rS8<\/code><\/span><\/p>\n<p>More details about JWT can be found at&nbsp;<a href=\"https:\/\/jwt.io\/introduction\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/jwt.io\/introduction\/<\/a>. You can use the <a href=\"https:\/\/jwt.io\/#debugger-io\" target=\"_blank\" rel=\"noopener noreferrer\">jwt.io debugger <\/a>to generate, decode and verify the tokens.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reading time : [est_time] In today&#8217;s world , security is&nbsp;the prime priority. Throughout the evolution of computers, security also evolved from simple password based authentication&hellip;<\/p>\n","protected":false},"author":1,"featured_media":318,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","has-post-thumbnail-archive"],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts\/233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/comments?post=233"}],"version-history":[{"count":15,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":319,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts\/233\/revisions\/319"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/media\/318"}],"wp:attachment":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}