{"id":10432,"date":"2021-09-06T10:30:03","date_gmt":"2021-09-06T01:30:03","guid":{"rendered":"http:\/\/www.gigas-jp.com\/appnews\/?p=10432"},"modified":"2021-09-03T20:14:24","modified_gmt":"2021-09-03T11:14:24","slug":"the-structure-of-jwt","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/10432","title":{"rendered":"The structure of JWT"},"content":{"rendered":"\n<p>I talked about JWT last week and we get the basic idea of what is JWT, how it works etc. Today I will talk about the structure of JWT and its pros and cons.<\/p>\n\n\n\n<p>If you haven&#8217;t read about JWT, you can read it <a href=\"\/appnews\/archives\/10413\">here<\/a>. So lets get started.<\/p>\n\n\n\n<p>A JWT contains 3 parts separated by a &#8220;.&#8221; sign. These are header, payload and signature. <\/p>\n\n\n\n<p>You can also take a look and can play debugging of these three parts at the official website of JWT called <a href=\"https:\/\/jwt.io\/\">jwt.io<\/a><\/p>\n\n\n\n<h3 id=\"header\">Header<\/h3>\n\n\n\n<p>Header contains the type of token and algorithm used for signing and encoding. Algorithms can be HMAC, SHA256, RSA, HS256 or RS256.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{ \n  \"typ\": \"JWT\",    \n  \"alg\": \"HS256\"\n}<\/code><\/pre>\n\n\n\n<h3 id=\"payload\">Payload<\/h3>\n\n\n\n<p>Payload contains the data we are exchange through client and server. Here is the sample payload.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"uid\": \"1234567890\",\n  \"name\": \"yuuma\",\n  \"iat\": 1231313123\n}<\/code><\/pre>\n\n\n\n<p>We can also add expiration payload to add expiration date of that token. We have to be careful about sensitive informations since, JWT can be decoded easily.<\/p>\n\n\n\n<h3>Signature<\/h3>\n\n\n\n<p>Signatures are the most important part of JWT. It is calculated by encoding the header and payload using the Base64url encoding and concatenating them with a dot sign. This is then passed to the encryption algorithm. If he header or payload changes, signature has to calculated again.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/sample from jwt.io\nHMACSHA256(\n  base64UrlEncode(header) + \".\" +\n  base64UrlEncode(payload),\n  your-256-bit-secret\n) secret base64 encoded<\/code><\/pre>\n\n\n\n<h3>Tips<\/h3>\n\n\n\n<p>We have to be careful about these facts if we are using JWT token in your authorization mechanism <\/p>\n\n\n\n<ul><li>Use HTTPS to protect the Authorization header.<\/li><li> Better to prepare with blocklist tokens as the attacker might get JWT token before it&#8217;s expiration date.<\/li><li>If the JWT is cookie-persistent, you need to create an HttpOnlyCookie. This restricts third-party JavaScript from reading the JWT token from the cookie.<\/li><li>For XSS, the server side should always sanitize user-generated data.<\/li><li>For CSRF, have to mitigate CSRF by using the source of the request and special request headers.<\/li><\/ul>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10432\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/10432\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"The structure of JWT\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10432\" data-text=\"The structure of JWT\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10432\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10432\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>I talked about JWT last week and we get the basic idea of what is JWT, how it works etc. Today I will talk abo [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[35],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10432"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=10432"}],"version-history":[{"count":1,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10432\/revisions"}],"predecessor-version":[{"id":10433,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10432\/revisions\/10433"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=10432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=10432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=10432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}