<?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: Adam</title>
    <description>The latest articles on DEV Community by Adam (@adimension_io).</description>
    <link>https://dev.to/adimension_io</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%2F152236%2Fc7776d3d-256e-4f7a-bd01-7a43b4345813.png</url>
      <title>DEV Community: Adam</title>
      <link>https://dev.to/adimension_io</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adimension_io"/>
    <language>en</language>
    <item>
      <title>One JWT to rule them all.</title>
      <dc:creator>Adam</dc:creator>
      <pubDate>Mon, 26 Oct 2020 02:59:05 +0000</pubDate>
      <link>https://dev.to/adimension_io/one-jwt-to-rule-them-all-55ac</link>
      <guid>https://dev.to/adimension_io/one-jwt-to-rule-them-all-55ac</guid>
      <description>&lt;p&gt;&lt;em&gt;Okay let's setup the scenario here.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's say you have a Node.js API and some front end framework, and you are using AWS Cognito to handle the authentication dance 💃 via the client side SDK either through Amplify or some other means.&lt;/p&gt;

&lt;p&gt;Now you also want to &lt;strong&gt;authenticate&lt;/strong&gt; your API calls against the &lt;strong&gt;same user&lt;/strong&gt;, against the &lt;strong&gt;same token&lt;/strong&gt;. This easily done by using that JWT and a JWKS on the server side to validate the JWT signature. &lt;/p&gt;

&lt;p&gt;I'll show you how via a typical middleware.&lt;/p&gt;

&lt;p&gt;First head over to &lt;a href="https://jwt.io"&gt;jwt.io&lt;/a&gt;, this amazing tool will decode your JWT into something readable like 👇&lt;/p&gt;

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

&lt;p&gt;&lt;em&gt;On to the Node.js middleware&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's start with a class so we can define some protected functions. You could also just do functional programming here too. Whatever floats your boat ⛵&lt;/p&gt;

&lt;p&gt;Using &lt;a href="https://github.com/panva/jose"&gt;Jose&lt;/a&gt; it would look something like this to authenticate the token and either return the validated signature or fail.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MPkDlovX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z8brq5fo3mv850o1z1fu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MPkDlovX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z8brq5fo3mv850o1z1fu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may need to get the token from the head in a more specific way like so.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    let token = context?.request?.headers().authorization || ''
    // Remove "Bearer " from start of token
    return (token = token.substring(7, token.length)) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From here is just a matter of &lt;strong&gt;context&lt;/strong&gt; depending on what language you are using.&lt;/p&gt;

&lt;p&gt;I hope this help some one who has read the below AWS cognito guide and found that to be underwhelming.&lt;/p&gt;

&lt;p&gt;🖖&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;References&lt;br&gt;
&lt;a href="https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html"&gt;AWS Guide&lt;/a&gt; - &lt;a href="https://github.com/panva/jose"&gt;Jose&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>cognito</category>
      <category>jwt</category>
      <category>jwks</category>
      <category>auth</category>
    </item>
  </channel>
</rss>
