<?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: Wadi</title>
    <description>The latest articles on DEV Community by Wadi (@wadid).</description>
    <link>https://dev.to/wadid</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%2F984528%2Fe9805fbd-c2a1-4e4a-88eb-047767233356.png</url>
      <title>DEV Community: Wadi</title>
      <link>https://dev.to/wadid</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wadid"/>
    <language>en</language>
    <item>
      <title>JWT Authentication - What Is It and How Do You Use It With Amplication?</title>
      <dc:creator>Wadi</dc:creator>
      <pubDate>Tue, 20 Dec 2022 11:22:40 +0000</pubDate>
      <link>https://dev.to/amplication/jwt-authentication-what-is-it-and-how-do-you-use-it-with-amplication-7dp</link>
      <guid>https://dev.to/amplication/jwt-authentication-what-is-it-and-how-do-you-use-it-with-amplication-7dp</guid>
      <description>&lt;p&gt;JSON Web Token (JWT) is now supported by Amplication, the open source platform for &lt;a href="https://amplication.com/"&gt;Node.js app development&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This article gives you an overview of how JWT works and how you can use it in your Amplication-generated app.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is JWT?
&lt;/h2&gt;

&lt;p&gt;JWT is an open standard security token that transmits information securely as a JSON object, useful for authorization and information exchange. It contains all essential information about an entity, meaning that no database queries are necessary, and the session doesn’t need to be saved on the server. You can sign the token using a private secret or a public/private key. Its short messages can be encrypted and securely convey the identity of the sender and whether they have the necessary access rights.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Most programming languages have a library for generating JWT, so you don’t have to do it manually.&lt;/p&gt;

&lt;h2&gt;
  
  
  JWT structure
&lt;/h2&gt;

&lt;p&gt;JWT contains three parts: &lt;strong&gt;Header&lt;/strong&gt;, &lt;strong&gt;Payload&lt;/strong&gt;, and &lt;strong&gt;Signature&lt;/strong&gt; as described in the following sections.&lt;/p&gt;

&lt;h2&gt;
  
  
  Header
&lt;/h2&gt;

&lt;p&gt;The header provides information about the type of token and the signing/encryption algorithm being used.&lt;/p&gt;

&lt;p&gt;The header typically consists of two parts:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;alg&lt;/strong&gt; - the signing algorithm used, such as HMAC SHA256 or RSA&lt;br&gt;
&lt;strong&gt;typ&lt;/strong&gt; - the type of token (which is JWT)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "alg": "HS256",
  "typ": "JWT"
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Payload
&lt;/h2&gt;

&lt;p&gt;The payload contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three classes of claim names; &lt;strong&gt;Registered&lt;/strong&gt;, &lt;strong&gt;Public&lt;/strong&gt;, and &lt;strong&gt;Private&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Registered claims
&lt;/h2&gt;

&lt;p&gt;Registered claims are defined by the &lt;a href="https://datatracker.ietf.org/doc/html/rfc7519"&gt;JWT specification&lt;/a&gt;. JWT defines a set of &lt;a href="https://techdocs.akamai.com/api-definitions/docs"&gt;seven reserved claims&lt;/a&gt; that are not obligatory, but it is recommended that you use them to allow interoperability with third-party applications.&lt;/p&gt;

&lt;p&gt;Note: Public claims and private claims are both considered custom claims, created to share information between parties that agree to use them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Public claims
&lt;/h2&gt;

&lt;p&gt;You can define public claims however you want, but to avoid collisions they should be defined in the &lt;a href="https://www.iana.org/assignments/jwt/jwt.xhtml"&gt;IANA JSON Web Token Registry&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Private claims
&lt;/h2&gt;

&lt;p&gt;You can create private claims to share information specific to your application. Unlike public claims, private claims might collide as they are not registered, so use them with care. Private claims should not share names with registered or public claims.&lt;/p&gt;

&lt;p&gt;The following example includes a private claim &lt;strong&gt;loggedInAs&lt;/strong&gt;, and a registered claim &lt;strong&gt;iat&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "loggedInAs": "admin",
  "iat": 1422779638
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Signature
&lt;/h2&gt;

&lt;p&gt;The signature is used to verify that the message wasn’t changed in transit. If the token is signed with a private key, it can also verify the identity of the sender. To create the signature part, sign the encoded header, the encoded payload, a secret, and the algorithm specified in the header. The following example uses the HMAC SHA256 algorithm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HMAC_SHA256(
  secret,
  base64urlEncoding(header) + '.' +
  base64urlEncoding(payload)
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JWT workflow
&lt;/h2&gt;

&lt;p&gt;Users have only indirect contact with the token, for example, when they enter usernames and passwords. The actual communication takes place between the client and the server.&lt;/p&gt;

&lt;p&gt;Before using JWT, you must define a secret key. As soon as a user has successfully entered their login information, the JWT will be returned with the key and saved locally. This transfer should take place over HTTPS to ensure that the data is protected. These steps are described as follows:&lt;/p&gt;

&lt;p&gt;The user logs in to the client using a username and password.&lt;/p&gt;

&lt;p&gt;The server checks if the hashed password is the same as the hashed password stored in the database for this user.&lt;/p&gt;

&lt;p&gt;If the hashed passwords are the same, the JWT service in the server stores the data in the JWT payload section and signs it.&lt;/p&gt;

&lt;p&gt;The server sends the signed JWT to the client, and the client saves it locally.&lt;/p&gt;

&lt;p&gt;The next time the user sends a request for data, the client sends the token to the server in the authorization header of the HTTP request using the Bearer scheme.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a bearer token?
&lt;/h2&gt;

&lt;p&gt;Bearer authentication is an HTTP authentication scheme using Bearer tokens, so-named because it gives access to the bearer of the token. The Bearer token is a cryptic string, usually generated by the server in response to a login request. The client must send this token in the Authorization header when making requests to protected resources. After a user has been authenticated, the application validates the user’s Bearer token.&lt;/p&gt;

&lt;p&gt;You must provide the token using &lt;strong&gt;Header&lt;/strong&gt;, &lt;strong&gt;Body&lt;/strong&gt;, or &lt;strong&gt;Query&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This example shows you how to set the value of the authorization header as Bearer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Authorization : Bearer cn389ncoiwuencr

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

&lt;/div&gt;



&lt;p&gt;If you want to send the token in the body or as a query, add access_token to your required option, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "access_token": "eyJhb...",
  "token_type": "Bearer",
  "expires_in": 3600
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Selecting JWT as the authentication method in Amplication
&lt;/h2&gt;

&lt;p&gt;Support for JWT is built-in to Amplication.&lt;/p&gt;

&lt;p&gt;To select JWT authorization for your Amplication app, go to your project dashboard, select &lt;strong&gt;Auth Settings&lt;/strong&gt; and choose &lt;strong&gt;JWT&lt;/strong&gt; from the dropdown list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TwTw-0ve--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcowkf92xxn3lkze9po2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TwTw-0ve--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcowkf92xxn3lkze9po2.png" alt="Select JWT Authentication" width="880" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting more information about using JWT in Amplication
&lt;/h2&gt;

&lt;p&gt;For more details about using JWT in Amplication, check out the &lt;a href="https://docs.amplication.com/authentication/"&gt;Authentication article&lt;/a&gt; in Amplication Docs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get the full story
&lt;/h2&gt;

&lt;p&gt;This has been just a quick overview of JWT. If you want the full picture check out the &lt;a href="https://docs.amplication.com/"&gt;Amplication docs&lt;/a&gt;, and these other sites:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jwt.io/"&gt;Autho - JSON Web Tokens&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/JSON_Web_Token"&gt;Wikipedia - JSON Web Token&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://flaviocopes.com/jwt/"&gt;flaviocopes - JSON Web Token (JWT) Explained&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#authentication_schemes"&gt;Mozilla – Authentication Schemes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.rfc-editor.org/rfc/rfc7519.html"&gt;JSON Web Token - (IETF)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.rfc-editor.org/rfc/rfc6750.html"&gt;Bearer Token Usage - (IETF)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.ionos.com/digitalguide/websites/web-development/json-web-token-jwt/"&gt;ionos – JSON Web Tokens&lt;/a&gt;&lt;/p&gt;

</description>
      <category>jwt</category>
      <category>amplication</category>
      <category>opensource</category>
      <category>authentication</category>
    </item>
  </channel>
</rss>
