<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sam Jesso on Medium]]></title>
        <description><![CDATA[Stories by Sam Jesso on Medium]]></description>
        <link>https://medium.com/@jamsesso?source=rss-46892d12359c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*F6eX8c66EIt4zWGgY3Im0Q.jpeg</url>
            <title>Stories by Sam Jesso on Medium</title>
            <link>https://medium.com/@jamsesso?source=rss-46892d12359c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 20:08:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jamsesso/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Simple React Finite State Machine Design Pattern]]></title>
            <link>https://medium.com/@jamsesso/a-simple-react-finite-state-machine-design-pattern-9078e343bdd7?source=rss-46892d12359c------2</link>
            <guid isPermaLink="false">https://medium.com/p/9078e343bdd7</guid>
            <category><![CDATA[fsm]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Sam Jesso]]></dc:creator>
            <pubDate>Tue, 07 Mar 2017 01:23:39 GMT</pubDate>
            <atom:updated>2017-07-10T11:31:58.451Z</atom:updated>
            <content:encoded><![CDATA[<p>If you search for a React finite state machine, you’ll find libraries on top of Redux, MobX, and state machines modeled as components. While these all have their place, I was looking for a simple pattern instead of a library.</p><p>One pattern that has worked well for me is the following:</p><pre>const States = {<br>  INIT: 0,<br>  SECOND: 1,<br>  THIRD: 2<br>};</pre><pre>class StateMachine extends Component {<br>  state = {<br>    current: States.INIT<br>  };</pre><pre>  transition(to) {<br>    this.setState({current: to});<br>  }</pre><pre>  render() {<br>    switch(this.state.current) {<br>      case States.SECOND:<br>        return this.renderSecond();<br>      case States.THIRD:<br>        return this.renderThird();<br>      case States.INIT:<br>      default:<br>        return this.renderInit();<br>    }<br>  }</pre><pre>  renderInit() {<br>    return (<br>      &lt;button onClick={() =&gt; this.transition(States.SECOND)}&gt;<br>        Go to state 2<br>      &lt;/button&gt;<br>    );<br>  }</pre><pre>  renderSecond() {<br>    return (<br>      &lt;button onClick={() =&gt; this.transition(States.THIRD)}&gt;<br>        Go to state 3<br>      &lt;/button&gt;<br>    );<br>  }</pre><pre>  renderThird() {<br>    return (<br>      &lt;button onClick={() =&gt; this.transition(States.INIT)}&gt;<br>        Go back to the initial state<br>      &lt;/button&gt;<br>    );<br>  }<br>}</pre><ul><li>Create a States object to identify states. You can just use strings if you’d like.</li><li>Create one render method per state.</li><li>The render method switches between the available states.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9078e343bdd7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Alternate Java Lambda Type Definition Syntax]]></title>
            <link>https://medium.com/@jamsesso/alternate-java-lambda-type-definition-syntax-75a568daa3f6?source=rss-46892d12359c------2</link>
            <guid isPermaLink="false">https://medium.com/p/75a568daa3f6</guid>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[lamda]]></category>
            <dc:creator><![CDATA[Sam Jesso]]></dc:creator>
            <pubDate>Mon, 12 Dec 2016 17:28:09 GMT</pubDate>
            <atom:updated>2016-12-12T17:28:09.624Z</atom:updated>
            <content:encoded><![CDATA[<p>After working with Java 8 for about a year now, I appreciate that lambdas were added to the language. However, I’m not a huge fan of applying the traditional type system to them — especially since it usually involves generics.</p><p>Defining lambda types with generics wouldn’t be so bad if type erasure wasn’t an issue, but having multiple overloaded Function methods in a class doesn’t work so great.</p><h4>Current Syntax</h4><pre>public Function&lt;Integer, Integer&gt; adder(int a) {<br>  return b -&gt; a + b;<br>}</pre><h4>Alternate Syntax</h4><pre>public int -&gt; int adder(int a) {<br>  return b -&gt; a + b;<br>}</pre><p>There are a few advantages to this syntax. One being that you solve the overloaded method issue <em>(which, to be fair, isn’t a largely limiting practical issue)</em> and curried functions are a lot cleaner to define — the intent is clearer (at least it is for me).</p><pre>// New:<br>public int -&gt; (int -&gt; int) adder(int a) {<br>  return b -&gt; c -&gt; a + b + c;<br>}</pre><pre>adder(1)(2)(3); // returns 6</pre><pre>// Old:<br>public Function&lt;Integer, Function&lt;Integer, Integer&gt;&gt; adder(int a) {<br>  return b -&gt; c -&gt; a + b + c;<br>}</pre><pre>adder(1).apply(2).apply(3); // returns 6</pre><p>Declaring a lambda as a local variable could look something like:</p><pre>public boolean useless(String s) {<br>   (String -&gt; boolean) predicate = String::isEmpty;<br>   return predicate(s);<br>}</pre><p>Obviously there are a lot of use-cases I haven’t considered, but a programmer can dream, right? What are your thoughts on this lambda type definition syntax?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75a568daa3f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Transforming React Context into Properties]]></title>
            <link>https://medium.com/@jamsesso/transforming-react-context-into-properties-487a26cf0f0b?source=rss-46892d12359c------2</link>
            <guid isPermaLink="false">https://medium.com/p/487a26cf0f0b</guid>
            <category><![CDATA[higher-order-components]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Sam Jesso]]></dc:creator>
            <pubDate>Sun, 10 Jul 2016 16:38:03 GMT</pubDate>
            <atom:updated>2016-07-10T16:38:03.228Z</atom:updated>
            <cc:license>https://creativecommons.org/publicdomain/mark/1.0/</cc:license>
            <content:encoded><![CDATA[<p>It’s no secret that the React Context API will probably change in the near future. If you use context like myself, this might be worrisome.</p><blockquote>Context is an advanced and experimental feature. The API is likely to change in future releases. — <a href="https://facebook.github.io/react/docs/context.html">React Documentation</a></blockquote><p>However, sometimes context is a necessary evil. Why is context considered bad? When you use context your components become <em>impure. </em>If you need to use context, the general recommendation is to deal with it in a higher-order component.</p><h3>Sebastian Markbåge on Twitter</h3><p>@dan_abramov I think a single HoC is probably the safest way to consume context, in terms of upgradability.</p><p>So, that’s our goal. Use a single higher-order component to convert context into regular ‘ol props — an API that we can rely on.</p><h4>Why use Context at all?</h4><p>In my case I am using React Router, particularly the route children feature. My parent route is responsible for loading data from the server, but the child routes need that data as well.</p><p>Does this absolutely require context? Nope. Previously, I was communicating this state to child components using Redux. The problem I had with that approach was that this particular state was completely tied to the component lifecycle — so it felt more ‘natural’ to put the state back into the parent route component. Because we cannot pass props from parent routes to child routes, context is necessary.</p><h3>What We’re Dealing With</h3><p>If you use the context API you probably understand why the API is likely to change: it requires a lot of ceremony and has a lot of gotchas. The setup that we’re playing is semantically similar to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/daba85007c1776aada1ec5a71d38d53d/href">https://medium.com/media/daba85007c1776aada1ec5a71d38d53d/href</a></iframe><p>It’s clunky. It’s unstable. Let’s fix that.</p><h3>Implementing our Higher Order Component</h3><p>Going back to our goal, we want to transform context passed from the Parent into props on Child. To do this, we’re going to use a <a href="https://medium.com/@franleplant/react-higher-order-components-in-depth-cf9032ee6c3e#.hxduolqku">higher-order component</a>. Before we see the code, let’s review what we want to do. We want to create a component that wraps another component, but passes it’s context onto the wrapped component as props. It’s actually not very complicated!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e31d168071ecc4c90b1ac7e71c46ad0d/href">https://medium.com/media/e31d168071ecc4c90b1ac7e71c46ad0d/href</a></iframe><p>Let’s break this HoC down with a bottom-up approach.</p><ul><li>The last thing we do in the <em>withContext</em> function is return a component called <em>ContextComponent</em>.</li><li>Ignoring the functions around it for a moment, <em>ContextComponent</em> just declares the context that it is looking for in the same way a regular, non-higher-order component would.</li><li>When <em>ContextComponent</em> is rendered, it spreads the context that it has received onto it’s child. <strong>This passes the context to the child as props.</strong></li><li>Next, instead of hard-coding the children as a non-higher-order component would do, we wrap <em>ContextComponent</em> in a function that accepts any component as input — this becomes the child to <em>ContextComponent</em>. If you’re unfamiliar with HoCs, this is the distinguishing feature of one.</li><li>Finally, we wrap our previous function in<em> yet another</em> function. This is not required, but the intent will become clear in a moment.</li></ul><p>Great, now we have a mechanism for transforming context into props! Let’s apply it to our original example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05166904f8731dd9ac86a9b25cb1b41e/href">https://medium.com/media/05166904f8731dd9ac86a9b25cb1b41e/href</a></iframe><p>Using our <em>withContext</em> HoC, we were able to remove all notion of context from the <em>Child</em> component and read that data from the props — hooray!</p><p>But, <em>withSomeContext1(withSomeContext2(Child))</em> looks pretty hairy, especially if we need many more keys from context. We can borrow an idea called ‘threading’ from Clojure to make this look a little neater:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cef39e5f0757374f19c46197811bc26e/href">https://medium.com/media/cef39e5f0757374f19c46197811bc26e/href</a></iframe><p>This new function <em>thread</em> takes an argument and successively passes it to each function in <em>funcs</em>. Here’s an example usage outside of React for clarity:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1d02ed63452f7b3f1002ba3b669bada/href">https://medium.com/media/c1d02ed63452f7b3f1002ba3b669bada/href</a></iframe><p>Utilizing thread in our previous example, we end up with the final product:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3103a3cb89469be296a4411bb8428f3/href">https://medium.com/media/f3103a3cb89469be296a4411bb8428f3/href</a></iframe><h3>“Wrapping” Up</h3><p>After all of that, what was the point? What did we gain? We gained two benefits:</p><ol><li>Our child component is pure again!</li><li>When the React context API changes, we have a single place, <em>withContext</em>, to upgrade. Remember Sebastian’s tweet?</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=487a26cf0f0b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ES6 Directives with AngularJS 1.x]]></title>
            <link>https://medium.com/@jamsesso/es6-directives-with-angularjs-1-x-7adbc3fd275f?source=rss-46892d12359c------2</link>
            <guid isPermaLink="false">https://medium.com/p/7adbc3fd275f</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[directives]]></category>
            <category><![CDATA[angularjs]]></category>
            <dc:creator><![CDATA[Sam Jesso]]></dc:creator>
            <pubDate>Sun, 27 Mar 2016 17:25:06 GMT</pubDate>
            <atom:updated>2016-03-27T17:25:06.869Z</atom:updated>
            <content:encoded><![CDATA[<p>This article is meant to be a (quick) extension to Jesus Rodriguez’s article <a href="http://angular-tips.com/blog/2015/06/using-angular-1-dot-x-with-es6-and-webpack/">Using Angular 1.x With ES6 and Webpack</a>. I wanted to write about using ES6 syntax with AngularJS 1.x directives.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*tMcxUmzDAghCXvySBqTyGg.jpeg" /><figcaption>An anonymous factory.</figcaption></figure><h4>Understanding Directives &amp; Dependency Injection</h4><p>Angular’s <em>directive </em>registration method is similar to the <em>controller </em>and <em>service </em>registration methods, but it does not accept a directive directly. Rather the directive registration method accepts a <strong>name</strong> and a <strong>factory</strong>. The factory object is responsible for passing dependencies into the directive which is typically done through the use of closures (as in Jesus’ article). This is fine for most things, but I wanted to express directives in an ES6 syntax to keep consistency with my controllers and services without being a leaky workaround.</p><p>A factory is simply an object that accepts dependencies as input and creates an object as output. In this case, the output is a directive. Registering controllers and services looked very similar, that is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2acad0d263b050650bee8342135bbca0/href">https://medium.com/media/2acad0d263b050650bee8342135bbca0/href</a></iframe><p>Basically:</p><ul><li>Loop over each service that was exported from the <em>module.js </em>file. (Just a list of <strong><em>export * from ‘./myservice’</em></strong>)</li><li>Get the name from some metadata injected by a decorator.</li><li>Pass the service to the Angular module.</li></ul><p>The decorators I used to wrap services and controllers looked like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e88a6bdef1f14cc505493ff33c7380f5/href">https://medium.com/media/e88a6bdef1f14cc505493ff33c7380f5/href</a></iframe><p>Decorators are only responsible for assignment metadata to objects. It is the registration process that was responsible for reading this metadata and using it to register the service/controller/directive. Of course, because directives themselves must be constructed by factories, I cannot use the same pattern as I did for services and controllers (not shown).</p><h4>Anonymous Factory Creation</h4><p>Because a directive would end up with a <em>Directive </em>and possibly an <em>Inject </em>decorator, all of the metadata is available for us to construct an anonymous factory during directive registration. Consider this function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91b81f78df3ea05576f2c6f20b4696be/href">https://medium.com/media/91b81f78df3ea05576f2c6f20b4696be/href</a></iframe><p>The function <em>factoryize </em>takes a directive as input, and returns a factory that constructs that directive based on the dependencies declared by the <em>Inject </em>decorator. Here’s an example of what would happen with a specific directive passed into <em>factoryize</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fefe04e55f687e96066631e1a4ff95db/href">https://medium.com/media/fefe04e55f687e96066631e1a4ff95db/href</a></iframe><h4>Glue it all Together</h4><p>With <em>factoryize</em>, we can create anonymous factories to construct our ES6 directives during directive registration. This is less of a workaround and more of a generic way of creating directives to begin with. One of the main differences is that dependencies are passed to the directive constructor instead of being accessed through closure scope. Here is what the final registration process for directives looks like (hint: it is very similar to service registration):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d098d758e6ecadb0faaaddf17889890c/href">https://medium.com/media/d098d758e6ecadb0faaaddf17889890c/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7adbc3fd275f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Webpack Proxy with http-proxy-middleware]]></title>
            <link>https://medium.com/@jamsesso/webpack-proxy-with-http-proxy-middleware-14331e7562d2?source=rss-46892d12359c------2</link>
            <guid isPermaLink="false">https://medium.com/p/14331e7562d2</guid>
            <category><![CDATA[proxy]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[webpack]]></category>
            <dc:creator><![CDATA[Sam Jesso]]></dc:creator>
            <pubDate>Sat, 02 Jan 2016 23:16:50 GMT</pubDate>
            <atom:updated>2016-01-12T14:29:39.875Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>UPDATE Jan 11, 2016:</strong> The PR was merged, webpack-dev-server now uses http-proxy-middleware, so the following code is not needed.</p><p><strong>UPDATE Jan 3, 2016:</strong> I implemented http-proxy-middleware into the webpack-dev-server project. You can comment on/contribute to the PR here: <a href="https://github.com/webpack/webpack-dev-server/pull/359/files">https://github.com/webpack/webpack-dev-server/pull/359/files</a></p><p>I’ve been using <a href="https://webpack.github.io/">Webpack</a> recently and I am super fond of it. It is the easiest bundler I have used by far. I ran across a problem today however, and want to briefly write about it.</p><p>The project I am working on has a back end REST API running on port 8080, and a front end website running on port 8081. Because the project may need to support older browsers (such as IE8), I can’t rely on <a href="https://en.wikipedia.org/wiki/Cross-origin_resource_sharing">CORS</a> headers on the API side. The best way to make calls to the API, then, is to use a proxy.</p><p>It turns out the Webpack has a proxy functionality build on top of <a href="https://github.com/nodejitsu/node-http-proxy">node-http-proxy</a> (NHP), great! <strong>Not so fast</strong>. My specific use case was that all requests to <em>frontend:8081/api</em> would be forwarded to <em>backend:8080</em>. For example, a request to <em>frontend:8081/api/users/1 </em>should proxy the request to <em>backend:8080/users/1</em>. It turns out that this can’t be done using any combination of configuration at the time of writing.</p><p>Well, so much for that. Time to write my own proxy. Luckily, Webpack offers <em>webpack-dev-middleware</em> which allows one to pretty much brew their own <em>webpack-dev-server</em>, though it doesn’t support the <em>inline</em> command line option.</p><p>My implementation of the dev server is not super-complete, but it does the job. I’m using <em>http-proxy-middleware</em> as it is a much more configurable proxy library for Node. I’m also using <em>webpack-hot-middleware</em> (not provided by Webpack) for hot reloading and to mimic the <em>inline</em> functionality of the <em>webpack-dev-server</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c112e3f8d388a070cdf6ff7815ed404/href">https://medium.com/media/2c112e3f8d388a070cdf6ff7815ed404/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=14331e7562d2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Internet of Things: Authenticating “Things”]]></title>
            <link>https://medium.com/@jamsesso/internet-of-things-authenticating-things-97816eb74a83?source=rss-46892d12359c------2</link>
            <guid isPermaLink="false">https://medium.com/p/97816eb74a83</guid>
            <category><![CDATA[internet-of-things]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[iot]]></category>
            <dc:creator><![CDATA[Sam Jesso]]></dc:creator>
            <pubDate>Sun, 25 Oct 2015 18:50:04 GMT</pubDate>
            <atom:updated>2015-12-22T01:56:29.260Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*qT-GRx1kvOKeDf7UYLge8g.jpeg" /><figcaption>GPRS Module for Cellular Connectivity</figcaption></figure><p>Usually, when talking about Internet of Things (IoT) security, we talk about it from the point of view of the consumer. That is, how we should prevent connected devices (<em>things</em>) from being used maliciously.</p><p>From a development standpoint, having a lot of these <em>things </em>as part of your system infrastructure is another story. To give this article some context without diving too deep into the roots, the system we’re building reads data from hardware (the <em>thing</em>) and sends it to our application so it can be analysed.</p><p>While working on the project, <a href="http://autobit.ca/">Autobit</a>, we came across an interesting problem: <strong>How do we know if the data sent to our application is legitimate?</strong></p><h4>Trusting Data from Things</h4><p>We spent some time researching methods other people used, but had a hard time finding any ideas, let alone example implementations. My guess is that most of these security systems are proprietary. Anyway, we needed a way to authenticate the data sent from the hardware. It’s an interesting problem to think about because “authentication” in a normal, pre-IoT context usually means “ask for a username and password” or some other credential, but authenticating a <em>thing</em> is much different than authenticating a person.</p><p>Before we started designing the authentication system, we came up with a set of questions that would help us determine the quality of any such system:</p><ul><li>What if the <em>thing </em>needs to be replaced? How difficult is it to swap?</li><li>If someone compromises the system, does it put the customer at risk? Does it put other <em>things </em>at risk?</li><li>Are the credentials passed across the internet with every request made by the <em>thing </em>to the application?</li></ul><h4>Designing the Authentication System</h4><p>Our solution borrows from a few concepts that are common, specifically ideas from generating <a href="https://dev.twitter.com/oauth/overview/creating-signatures">OAuth signatures</a>, <a href="https://en.wikipedia.org/wiki/Hash-based_message_authentication_code">Hash based Message Authentication Codes (HMAC)</a> and ideas <a href="http://www.thebuzzmedia.com/designing-a-secure-rest-api-without-oauth-authentication/">inspired by</a> Amazon Web Services (AWS) REST API authentication system.</p><p>The method for determining data authenticity starts by assigning each <em>thing </em>a unique public key and a unique secret key. Both keys are symmetric, so they need to be known by the application and the <em>thing</em>. It is important that, given a public key, your application can determine the secret key otherwise this particular authentication method will not work.</p><p>The idea is to package up data from <em>things </em>into messages. Fundamentally, messages include a time stamp, public key, a type, a payload, and a signature. Other metadata can be included as well (a good idea would be to implement an <a href="https://en.wikipedia.org/wiki/Cryptographic_nonce">nonce</a>).</p><ul><li>The time stamp is the time when the message was dispatched from the <em>thing </em>to the application.</li><li>The public key is some unique identifier that is assigned to the <em>thing.</em></li><li>The type is a flag assigned by the <em>thing </em>and used by the application to determine the appropriate method for reading the payload.</li><li>The payload is the actual data that the <em>thing </em>wants to send. This can be in any format.</li><li>The signature is a hash-based message authentication code (HMAC) created using some algorithm. I will be using SHA256.</li></ul><h4>Sending Data from Things</h4><p>To send a message from your <em>thing</em>, consider the following pseudo-code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/abdd5a7856a3eddbe8d0e49350f73995/href">https://medium.com/media/abdd5a7856a3eddbe8d0e49350f73995/href</a></iframe><p>A message is constructed using the create_message function, which appends a signature to the message that is generated using the HMAC SHA256 digest of the data using the secret key. Having the secret key programmed onto the <em>thing </em>means that the secret key is never actually sent in plain text over the internet, keeping it secret. Note that the signature string is constructed using all of the available message data in alphabetical order; the order is important because the receiver of this message needs to be able to reconstruct the signature in the exact same way.</p><h4>Determining Message Authenticity on the Other Side</h4><p>Receiving the message on the application side means parsing the message into a form where the data is easily accessible. The idea is that we will:</p><ol><li>Based on the public key provided in the message metadata, get the secret key.</li><li>Using the same method as the <em>thing</em>, but in reverse, reconstruct the signature of the message independently of the signature that was sent in the message. Because the signature was generated using the parameters in a known (alphabetical) order, we can accurately reconstruct the signature.</li><li>If the signature you generate matches the signature sent in the message, you can be confident that the data was sent by a source that knows the secret key. As a side affect, you also know that the data was not tampered with during it’s transmission.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aec2483bb6f852d00db24d9065f52570/href">https://medium.com/media/aec2483bb6f852d00db24d9065f52570/href</a></iframe><h4>Answering our Questions</h4><p>As I mentioned earlier, we had a set of questions that we used to determine the quality of any authentication system we developed.</p><ul><li>What if the <em>thing </em>needs to be replaced? How difficult is it to swap? — If the <em>thing </em>needs to be replaced, a new one can easily be either programmed with the decommissioned <em>thing</em>’s public and secret keys or it can be programmed with it’s own keys. This method will work with either architecture. The difficulty is determined by the difficulty of programming keys onto <em>things</em>.</li><li>If someone compromises the system, does it put the customer at risk? Does it put other <em>things </em>at risk? — Tricky question because it depends on the nature of the messages being sent. In our application, we only collect sensor data. We aren’t sending any control messages. Secret keys are not used for any other purpose other than writing data to the application, so if any one secret key was compromised, the only thing an attacker could do is send bogus data. They would not be able to read or modify previously sent sensor data or access user data because the user authentication system works separately.</li><li>Are the credentials passed across the internet with every request made by the <em>thing </em>to the application? — Using this mechanism, the only credential to ever cross the internet in plain text is the public key, which is exactly what public keys are meant to do. The sensitive secret key is never transmitted making it effectively impossible to find.</li></ul><p>We also have the added bonus of knowing that our data has not been tampered with during the transmission (as mentioned earlier). It is possible to prevent <a href="https://en.wikipedia.org/wiki/Replay_attack">replay attacks</a> by adding an nonce mechanism like I mentioned earlier.</p><h4>Conclusions</h4><p>As far as we could tell, the proposed authentication system seems to work well for our application. None of us are security experts and we would all appreciate any comments on the security of the idea.</p><p>I hope that this article can serve as at least a starting point for anyone else who might be facing a similar development challenge. If you come up with something better, we’d love to hear about it.</p><h3>An Example Java Implementation</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f53e4794a36867f80fb459f6153f0da6/href">https://medium.com/media/f53e4794a36867f80fb459f6153f0da6/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97816eb74a83" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Starting out with Jersey & Apache Tomcat using IntelliJ]]></title>
            <link>https://medium.com/@jamsesso/starting-out-with-jersey-apache-tomcat-using-intellij-6338d93ffd40?source=rss-46892d12359c------2</link>
            <guid isPermaLink="false">https://medium.com/p/6338d93ffd40</guid>
            <dc:creator><![CDATA[Sam Jesso]]></dc:creator>
            <pubDate>Thu, 18 Dec 2014 01:56:00 GMT</pubDate>
            <atom:updated>2014-12-18T01:56:00.950Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re reading this article, it is likely that you’re having a bit of trouble using Jersey to create a RESTful API and running it on Apache Tomcat.</p><p>You might be thinking that I am a broken record: there must be <em>millions</em> of articles on this very same topic. However, in my own quest to get started using Jersey to create a REST API in Java, I was bombarded with article upon article that assumed too much knowledge on my behalf. Any programmer who’s getting started with a new technology doesn&#39;t want to read the textbook, they want to jump in and <strong>build something</strong>.</p><p>The motivation for creating this tutorial is for those who want to kick-start their REST API development using Jersey.</p><h3>0. Preamble</h3><p>Before we begin, I’d like to introduce a few technologies that we’ll be dealing with during this tutorial. If you’re familiar with these, feel free to skip this section.</p><p>For this tutorial I will be using <strong>IntelliJ IDEA 14 Ultimate</strong> edition, but the community edition should work just as well. This is my IDE of choice; I’m not a fan of Eclipse. No, I’m not going to explain what REST stands for or explain what a Unique Resource Identifier is.</p><h4>JAX-RS</h4><p>JAX-RS is a Java API developed by Oracle. There are multiple reference implementations of this API including Jersey, Apache CXF, RESTeasy, etc…</p><h4>Jersey</h4><p>Jersey, as previously stated, is a reference implementation of the JAX-RS 2.0 API. Jersey is extensible and provides additional functionality to the API.</p><h4>Maven &amp; The Central Repository</h4><p>Maven is a tool used to model projects/modules: it keeps tracks of different aspects such as building or managing a Java based project. For this tutorial, it will be used mainly to manage our project dependencies.</p><p>The Central Repository is a glorious place where artifacts can be found for use in your project. An <em>artifact</em> is a file (usually a JAR) that is created by a Maven build.</p><h4>Apache Tomcat</h4><p>Apache Tomcat is a popular, open source implementation of the Java Servlet API. A <em>servlet</em> is “a Java class used to extend the capabilities of servers.”</p><h3>1. Installing Apache Tomcat &amp; Maven</h3><p>Depending on your operating system, this step will vary. The goal is to install Apache Tomcat and Maven on your system.</p><p>I am using Fedora 21, so this was as painless as <strong>sudo yum install tomcat maven</strong></p><h3>2. Creating a New Project</h3><p>Though it sounds trivial, I was actually having quite a difficult time starting a new project. As a first time Maven/Jersey user, I was not in a great position to figure out why I was encountering these issues.</p><p>To create a new project, open up the New Project dialog in IntelliJ. In the left menu, select <strong>Java Enterprise</strong>. To the right, select <strong>RESTful Web Service</strong>. Below, change the radio selection from <strong>Download</strong> to <strong>Set up library later</strong>. We will be using Maven to download and manage our libraries.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*NLDknYVjoYRJhQ3e08e5sA.png" /><figcaption>New Project dialog</figcaption></figure><p>You can continue through the rest of the steps as per normal.</p><h3>3. Adding Maven Support</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*1z91jWks7kJoO1jycgUGYw.png" /></figure><p>In order to add Maven to your project, you can avoid the command line completely by right-clicking on your project’s name in the <strong>Project </strong>sidebar and selecting <strong>Add Framework Support.</strong></p><p>In the Add Frameworks Support dialog, select <strong>Maven.</strong></p><p>Once this is done, the <strong>pom.xml</strong> file will open in the editor. You will need to enter <em>at least</em> a <strong>groupId</strong>, usually your package name. For this tutorial, I will be using <strong>com.example.jersey</strong>. At this point, it is helpful to enable auto-importing for library dependencies.</p><h3>4. Adding Web Support</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/327/1*kqhDWZXUZDBdeD-5WSZNSw.png" /></figure><p>Similarly to Maven, you’ll want to open the <strong>Add Frameworks Support </strong>dialog again. This time, select <strong>Web Application </strong>and ensure that <strong>Create web.xml </strong>is checked.</p><p>Once complete, your project structure should look similar to this.</p><h3>5. Adding Jersey with Maven</h3><p>Before we can use Jersey, we need to add the library to our project. We will do this with Maven by adding the following to our <strong>pom.xml</strong> file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5060f12bf8f77b06af1e512b64a5197/href">https://medium.com/media/d5060f12bf8f77b06af1e512b64a5197/href</a></iframe><p>The <strong>jersey-bundle </strong>artifact can be found at <a href="http://search.maven.org/#artifactdetails%7Ccom.sun.jersey%7Cjersey-bundle%7C1.18.3%7Cjar">http://search.maven.org/#artifactdetails%7Ccom.sun.jersey%7Cjersey-bundle%7C1.18.3%7Cjar</a></p><h3>6. Creating a Resource</h3><p>Inside of the <strong>src/main/java</strong> directory, create a new package matching your groupId.</p><p>Inside of this package, create a resource such as <strong>HelloWorld.java</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c211ce8debb4b7082097c9977d9ad62/href">https://medium.com/media/0c211ce8debb4b7082097c9977d9ad62/href</a></iframe><h3>7. Setting up Tomcat</h3><p>The next step is making IntelliJ aware of Tomcat so that it can deploy your applications seamlessly. This is accomplished by navigating through <strong>File</strong> &gt; <strong>Build, Execution, Deployment</strong> &gt; <strong>Application Servers</strong> &gt; <strong>“+”</strong> &gt; <strong>Tomcat Server</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*Sr60-HvrgUSm8dfO_x4hEQ.png" /></figure><p>In the Tomcat Server dialog, navigate to the installation directory of Tomcat.</p><p>After this is done, the correct version should be reported by the dialog. Exit both dialogs by selecting <strong>OK</strong>.</p><h3>8. Creating a Run &amp; Debug Configuration</h3><p>Before Tomcat can serve your application, you’ll need to set it up. Do this by creating a new run configuration. This is done by navigating through <strong>Run &gt;</strong> <strong>Edit Configurations… &gt;</strong> “<strong>+” &gt;</strong> <strong>Tomcat Server &gt;</strong> <strong>Local</strong>. Name this whatever you like.</p><p>During this step, we need to select the artifact for Tomcat to deploy. Choose the <strong>Deployment</strong> tab, clicking<strong> “+”</strong>, and choosing <strong>Artifact…</strong></p><p>Select the only artifact listed then choose <strong>Apply</strong> and finally <strong>OK</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/893/1*iAZQ1ZAopwFm4dhhu4PhSg.png" /><figcaption>The final Run/Debug configuration for Tomcat</figcaption></figure><h3>9. Configuring your Servlet</h3><p>Inside of your <strong>web/WEB-INF/web.xml</strong> file, you’ll need to add the following contents:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1860f03fc9eb42b88d2087df2819b84f/href">https://medium.com/media/1860f03fc9eb42b88d2087df2819b84f/href</a></iframe><p>I will refrain from going into much detail about this configuration file, but it is wise to investigate additional configuration options for your REST service.</p><p>The last step is to let your application server (Tomcat) know about your configuration file. Navigate through <strong>File</strong> &gt; <strong>Project Structure</strong> &gt; <strong>Artifacts</strong>. After choosing all of the<strong> Available Elements</strong> listed, right-click and choose <strong>Put in Default Location</strong>.</p><h3>10. Run your Application</h3><p>That’s everything! From here, you can run your application through IntelliJ. If you get a permissions error when running, be sure that Tomcat’s <strong>conf </strong>directory is accessible from the system user that is running IntelliJ.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*IaZHAXzfcQ3CTDk6pQtRdQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6338d93ffd40" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Versioning REST API routes in Express.js]]></title>
            <link>https://medium.com/@jamsesso/versioning-rest-api-routes-in-express-js-f7287e1c8886?source=rss-46892d12359c------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7287e1c8886</guid>
            <dc:creator><![CDATA[Sam Jesso]]></dc:creator>
            <pubDate>Sat, 30 Aug 2014 20:31:45 GMT</pubDate>
            <atom:updated>2014-08-30T20:31:45.942Z</atom:updated>
            <content:encoded><![CDATA[<p>Over the past few months I have been building a RESTful web service that is to be consumed from both the web as well as an HTML5 hybrid mobile app powered by the <a href="http://ionicframework.com/">Ionic framework.</a> The web consumer doesn’t present much of a problem since the API will only be consumed by a first-party web client. On the other hand, I cannot leave mobile app users dead in the water if they decide against upgrading.</p><p>The clear solution to this problem is API versioning. It works as simply as offering different versions of your API to consumers while still allowing you to add new features and (potentially) break stuff in the process.</p><p>I have been writing the API in Javascript using Express.js and was looking for a clean and easy way to implement versioning. The method that was inspired by a few hours of reading is to simply have the version specified in the request URI, with the default version being the latest. So, say I have a couple of routes:</p><ul><li><strong>GET /v1/user/:id</strong></li><li><strong>GET /v2/user/:id</strong></li><li><strong>GET /user/:id -&gt; GET /v2/user/:id</strong></li></ul><p>Two versions of the API exist (in this example, a REST call for consuming a single user record), version 1 &amp; 2. Since version 2 is the latest version, any call to the default API version (latest) is simply redirected* to the latest implementation of that REST call in my API; in this case, version 2.</p><p>Express, being as awesome as it is, is actually very easily configured for this sort of thing. The implementation of the above routes are as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9906dab2a9eb6822c325b61dd3fd9988/href">https://medium.com/media/9906dab2a9eb6822c325b61dd3fd9988/href</a></iframe><p>With this approach, it is also possible to implement different middleware depending on the API version which is quite a powerful capability — think beta user authentication, etc…</p><p>Hopefully this is of some use to any future developers working on an Express.js REST API.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7287e1c8886" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>