<?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: João Victor Santos</title>
    <description>The latest articles on DEV Community by João Victor Santos (@devictorsilva).</description>
    <link>https://dev.to/devictorsilva</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%2F309003%2F974d8bb8-da96-43cb-98f7-fc5d1a3f77c2.png</url>
      <title>DEV Community: João Victor Santos</title>
      <link>https://dev.to/devictorsilva</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devictorsilva"/>
    <language>en</language>
    <item>
      <title>JWT Golang Template </title>
      <dc:creator>João Victor Santos</dc:creator>
      <pubDate>Sun, 05 Jan 2020 18:35:19 +0000</pubDate>
      <link>https://dev.to/devictorsilva/jwt-golang-template-7k7</link>
      <guid>https://dev.to/devictorsilva/jwt-golang-template-7k7</guid>
      <description>&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;This is a series of posts divided into 4 parts. The same posts are being posted on different IT blogs like Hackernoon, Devto and Medium.&lt;/p&gt;

&lt;h3&gt;
  
  
  Big Picture
&lt;/h3&gt;

&lt;p&gt;So you could be skipping the whole series of posts and seeing the final &lt;a href="https://github.com/devictorsilva/jwt-template"&gt;repository&lt;/a&gt; and how it works.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;IMPORTANT&lt;/strong&gt;: This is not a code for production, it is for understanding concepts. I really don't recommend using it in production.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Seen the warning above, the code does not include any configuration and all keys and passwords are stored in the repository.&lt;/p&gt;

&lt;p&gt;In the repository you can find examples and commented code. Not everything in the garden is rosy, but we have a good start. Feel free to create pull-request or ask more.&lt;/p&gt;

&lt;p&gt;So enough litany and let's get down to business!&lt;/p&gt;

&lt;h3&gt;
  
  
  PART 1 - What the heck is a JWT?
&lt;/h3&gt;

&lt;p&gt;Many people may have already seen or used JWT without even knowing what a it really is, here whe have a good start &lt;a href="https://jwt.io/"&gt;JWT.io&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  In The Idiomatic Way
&lt;/h4&gt;

&lt;p&gt;JSON Web Token or popularly known as JWT is a set of base64url encoded string separated by &lt;code&gt;.&lt;/code&gt;'s. Something similar to &lt;code&gt;aaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbb.ccccccccccccccccccc&lt;/code&gt;, - but what is this parts ? -, the first two parts are a set of public information in JSON Object and the third is a signature, usually signed with some private key or password ( depending on the security level implemented ). The general use of JWT is authentication and authorization.&lt;/p&gt;




&lt;h4&gt;
  
  
  In The Technical Way
&lt;/h4&gt;

&lt;p&gt;JWT refers to &lt;a href="https://tools.ietf.org/html/rfc7519"&gt;RFC7519&lt;/a&gt; and it's a community accepted and discussed standard and after published by the Internet Engineering Steering Group ( IESG ) as an Internet Standards.&lt;/p&gt;

&lt;p&gt;Usually used by OAuth Servers or IdentityProviders carrying client information ( one user or application ).&lt;/p&gt;

&lt;p&gt;JWT consists of JSON objects that are Header, Payload and one Signature. Usually Header and Payload contain public information, while Signature contains some private information.&lt;/p&gt;

&lt;p&gt;Let's see what they really look like ?&lt;/p&gt;

&lt;p&gt;An encoded JWT ( with line breaks for display purposes only ):&lt;/p&gt;

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

&lt;h5&gt;
  
  
  Decoded Header:
&lt;/h5&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;p&gt;Here we have a JOSE Header that declares that the encoded object is a JWT and the signature is based on HMAC encryption. We can confirm this by looking at the &lt;code&gt;typ&lt;/code&gt;  and &lt;code&gt;alg&lt;/code&gt;, where &lt;code&gt;alg&lt;/code&gt; refers to the algorithm used for signature and &lt;code&gt;typ&lt;/code&gt; refers to the token type.&lt;/p&gt;

&lt;h5&gt;
  
  
  Decoded Payload:
&lt;/h5&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;The payload is usually a set of claims that contain information about the client, the generation of this token, and more.&lt;/p&gt;

&lt;p&gt;The JWT specification defines seven reserved claims that are not required, but are recommended to allow interoperability with third-party applications. They are:&lt;/p&gt;

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

&lt;p&gt;You can see more standards internet claims &lt;a href="https://www.iana.org/assignments/jwt/jwt.xhtml#claims"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Signature:
&lt;/h5&gt;

&lt;p&gt;In this case Signature it's composed by:&lt;/p&gt;

&lt;p&gt;HMACSHA256( base64UrlEncode( header ) + "." + base64UrlEncode( payload ), &lt;code&gt;your-256-bit-secret&lt;/code&gt; )&lt;/p&gt;

&lt;p&gt;Where &lt;code&gt;your-256-bit-secret&lt;/code&gt; is a password to validate and generate this token. Depending on the algorithm used, we could use private certificates for token generation and public certificates for validation of the token like in RSA Algorithms or EDCSA Algorithms.&lt;/p&gt;

&lt;p&gt;For example in RSA:&lt;/p&gt;

&lt;p&gt;RSASHA512( base64UrlEncode(header) + "." + base64UrlEncode(payload), &amp;lt; Public Key or Certificate &amp;gt; , &amp;lt; Private Key or Certificate &amp;gt; )&lt;/p&gt;

&lt;h4&gt;
  
  
  PART 2 - Generating a token
&lt;/h4&gt;

&lt;p&gt;WIP - Work In Progress&lt;/p&gt;

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

&lt;p&gt;A go implementation of JSON Web Tokens:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/dgrijalva/jwt-go"&gt;https://github.com/dgrijalva/jwt-go&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Internet Standard Claims:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://auth0.com/docs/tokens/jwt-claims"&gt;https://auth0.com/docs/tokens/jwt-claims&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.iana.org/assignments/jwt/jwt.xhtml#claims"&gt;https://www.iana.org/assignments/jwt/jwt.xhtml#claims&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Javascript Object Signing and Encryption (JOSE):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jose.readthedocs.io/en/latest/"&gt;https://jose.readthedocs.io/en/latest/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://datatracker.ietf.org/wg/jose/documents/"&gt;https://datatracker.ietf.org/wg/jose/documents/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;RFC2104 - HMAC: Keyed-Hashing for Message Authentication:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tools.ietf.org/html/rfc2104"&gt;https://tools.ietf.org/html/rfc2104&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;RFC6979 - Deterministic Usage of the Digital Signature Algorithm (DSA) and Elliptic Curve Digital Signature Algorithm (ECDSA):&lt;/p&gt;

&lt;p&gt;&lt;a href="http://tools.ietf.org/html/rfc6979"&gt;http://tools.ietf.org/html/rfc6979&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;RFC7519 - JSON Web Token (JWT):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tools.ietf.org/html/rfc7519"&gt;https://tools.ietf.org/html/rfc7519&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;RFC8017 - PKCS #1: RSA Cryptography Specifications Version 2.2:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tools.ietf.org/html/rfc8017"&gt;https://tools.ietf.org/html/rfc8017&lt;/a&gt;&lt;/p&gt;

</description>
      <category>jwt</category>
      <category>authentication</category>
      <category>go</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
