<?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: Antonio Perez</title>
    <description>The latest articles on DEV Community by Antonio Perez (@antonioperez).</description>
    <link>https://dev.to/antonioperez</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F131075%2F6affa8bb-5fda-4fed-b171-9a3f42ad2455.png</url>
      <title>DEV Community: Antonio Perez</title>
      <link>https://dev.to/antonioperez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/antonioperez"/>
    <language>en</language>
    <item>
      <title>Create a HMAC Authorization Header in NodeJS</title>
      <dc:creator>Antonio Perez</dc:creator>
      <pubDate>Mon, 29 Jul 2019 03:09:37 +0000</pubDate>
      <link>https://dev.to/antonioperez/create-a-hmac-authorization-header-in-nodejs-4ajg</link>
      <guid>https://dev.to/antonioperez/create-a-hmac-authorization-header-in-nodejs-4ajg</guid>
      <description>&lt;p&gt;There are a ton of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization"&gt;HTTP Authorization methods&lt;/a&gt; to use when trying to authenticate against an API. One such is the &lt;a href="https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-00"&gt;&lt;strong&gt;Hash Message Authentication Code (HMAC)&lt;/strong&gt;&lt;/a&gt; authentication scheme. This method provides a way for the server to cryptographically verify the request by hashing the request itself, and then creating the MAC out of the HTTP method, request URI, hostname, and the request body hash.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/antonioperez/2c17117eb34bf8d6c2a1be05155699a4"&gt;HMAC NODEJS EXAMPLE GIST LINK&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The server you are attempting to authenticate against will provide you with three key pieces of data.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The MAC key id&lt;/li&gt;
&lt;li&gt;The MAC secret key with the expected hash algorithm. If the key is base 64 encoded. You will have to decode it when generating the HMAC.&lt;/li&gt;
&lt;li&gt;Issue time of your credentials in EPOCH. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Generally, the server will use the MAC key id to look up your issued credentials on their end. The MAC secret key will be used to generate the HMAC just before sending the request to the server. The server will use this same secret key to compute the mac. If there is a mis-match, the request will be rejected.&lt;/p&gt;

&lt;p&gt;A resulting HMAC header may look like this..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Authorization: "MAC id='SERVER-PROVIDED-ID', nonce='6573561:WINTERBOOTS', bodyhash='pWZ7PIoST1E8QP70NAbNfSVfl/U0BFWk596zZuBuh84=', mac='qh4lj5GpmJd5yvxX8BUXU5j6orDUsCYBMNhjIFOA21E='"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Lets break down the MAC header into the main components&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;id&lt;/strong&gt; – The server provided ID&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;nonce&lt;/strong&gt; – The number of seconds since the provided issued time concatentated with a random string, separated by a ‘:’ character.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;bodyhash&lt;/strong&gt; – The result of &lt;code&gt;=BASE64(HASH(requestText))&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mac&lt;/strong&gt; – &lt;code&gt;=BASE64(HMAC(macString, secretKey))&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The MAC before it is created needs to be formatted and preserved as a normal text string so the server and client can calculate the request MAC over the exact same value. Each piece of data is separated by a new line character (\n). Let’s review each of the 7 pieces of data. This order must be maintained in the string.&lt;/p&gt;

&lt;h2&gt;
  
  
  MAC normalize string
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;The nonce value generated for the request. &lt;/li&gt;
&lt;li&gt;The HTTP request method in upper case. For example: “HEAD”, “GET”, “POST”, etc. &lt;/li&gt;
&lt;li&gt;The HTTP request route ex) ‘/users’&lt;/li&gt;
&lt;li&gt;The hostname included in the HTTP request &lt;/li&gt;
&lt;li&gt;The port ex) 80 for HTTP or 443 for HTTPS &lt;/li&gt;
&lt;li&gt;The request payload body hash, otherwise, an empty string. &lt;/li&gt;
&lt;li&gt;The value of the “ext” “Authorization” request header field, otherwise, an empty string.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;${nonce}\n ${method}\n ${route}\n ${hostname}\n ${PORT}\n ${bodyHash}\n ${ext}\n
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Great. Now that we have the string, we can now calculate the MAC portion of the request. The result is the base 64 encoded result of the HMAC(string, secretKey). Remember, if the secret key is base64 encoded, you will have to decode it. Below is a Node js gist that takes advantage of the crypto libray to generate the hash, HMAC, and auth header for your API integration.&lt;/p&gt;

&lt;p&gt;The code sample creates an HMAC for a /users POST endpoint being hosted at example.com. The result is a header object that can be used in Axios or most HTTP libraries. The request is converted to stringified json.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/antonioperez/2c17117eb34bf8d6c2a1be05155699a4"&gt;HMAC NODEJS EXAMPLE GIST LINK&lt;/a&gt;&lt;/p&gt;

</description>
      <category>api</category>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>An AWS Code Build spec for your typescript node project</title>
      <dc:creator>Antonio Perez</dc:creator>
      <pubDate>Sat, 15 Jun 2019 17:00:49 +0000</pubDate>
      <link>https://dev.to/antonioperez/an-aws-code-build-spec-for-your-typescript-node-project-51c</link>
      <guid>https://dev.to/antonioperez/an-aws-code-build-spec-for-your-typescript-node-project-51c</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VMTS7yDV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://softwaresurvivor.com/wp-content/uploads/2019/07/baccab28f77c594ee6b73de0224e75bd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VMTS7yDV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://softwaresurvivor.com/wp-content/uploads/2019/07/baccab28f77c594ee6b73de0224e75bd.png" alt="AWS Code Build"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aws.amazon.com/codebuild/"&gt;AWS Code Build&lt;/a&gt; is a great addition to your &lt;a href="https://aws.amazon.com/"&gt;AWS&lt;/a&gt; infrastructure. Today, we are going to focus on one aspect of the software deployment process. &lt;strong&gt;Building!&lt;/strong&gt; Before we dive in, let me provide some context about the architecture and my plan for the CI/CD process. The project behind this post is a &lt;a href="https://nodejs.org/en/"&gt;node.js&lt;/a&gt; &lt;a href="https://expressjs.com/"&gt;express&lt;/a&gt; project written in &lt;a href="https://www.typescriptlang.org/"&gt;typescript&lt;/a&gt;. The API is served on &lt;a href="https://aws.amazon.com/elasticbeanstalk/"&gt;Elastic Bean Stalk&lt;/a&gt;. We want to auto deploy our code versions to elastic beanstalk by using AWS Codebuild and then later &lt;a href="https://aws.amazon.com/codepipeline/"&gt;Code Pipeline&lt;/a&gt; to push the resulting build to the Elastic Bean Stalk instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deployments suck&lt;/strong&gt;. You build this great product on your local machine and now you have to deploy it to the masses. You spent so much time focusing on the product, that you forgot to scope out the deployment process. It’s DevOps! How hard can it be?! … err.. guilty. There are so many ways to do deployment, as is common in this field. You can easily spend a whole day researching different deployment tools for your environment. You try to understand why certain products exists, weigh the pros and cons of each based on opinionated words of others. Perhaps integrations between different platforms are possible. Travis for AWS? How are those set up? Death by a thousand choices and words. Now with the setup above, we can focus on our code, and let automation tools worry about the builds. I also wanted to keep it all on AWS.&lt;/p&gt;

&lt;p&gt;Let’s worry about Git. Branch management is important, but that’s a different topic for another day. In my project, I have two special branches, a ‘staging’  and a ‘production’ one. I have two AWS Code Build configurations watching these two branches for any changes. If a change is detected, for example, I merged my development branch into staging or staging into production, a code build will fire and the pipeline is executed. A minute later, our project has a new release! Magic!&lt;/p&gt;

&lt;p&gt;For AWS Code Build, you need a buildspec.yml in your git repo to tell the server how to install and build your project. In this case, my package json file has an npm command called build-ts that runs &lt;code&gt;tsc&lt;/code&gt;.  You can also include your tests command here, but I’ll come back to that at a later post.&lt;/p&gt;

&lt;p&gt;The artifacts section is also important. Elastic Bean stalk expects us to zip the contents of the result, not the resulting build folder. So I am grabbing three things here. The package.json file, the read me, and the contents of the dist folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//buildspec.yml
version: 0.2
phases:
    install:
        commands:
            - 'npm install'
    build:
        commands:
            - 'npm run build-ts'
artifacts:
    type: zip
    files:
        - package.json
        - package-lock.json
        - 'dist/**/*'
        - LICENSE
        - README.md
        - .npmrc

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



&lt;p&gt;Head on over to AWS Pipeline to setup a pipeline. When you set up a pipeline, you can set the source, and later a build project. It’s easier than going to each step, setting it up individually, and later connecting them. I plan on showing each step in detail in my next post, but hopefully you’re at a point where you either need the build spec, or just a general overview of this type of AWS infrastructure. Happy coding!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lWNhPAuS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://softwaresurvivor.com/wp-content/uploads/2019/07/a6f8ed3dbf2b27748e0f7ae332f821b6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lWNhPAuS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://softwaresurvivor.com/wp-content/uploads/2019/07/a6f8ed3dbf2b27748e0f7ae332f821b6.png" alt="Code Pipeline"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>aawscodebuildspe</category>
      <category>howtosetupcicdf</category>
      <category>typescriptbuildon</category>
    </item>
  </channel>
</rss>
