Happy New Year everyone!
First I’ll describe the webservice I’m building. Then we’ll configure our environment for Typescript and Mocha testing.
Poor man’s dual factor authentication via a password of the day
I’m the technical lead for an enterprise application which is in use by about 100 large multinational corporates. As part of the installation process, we ask for a registration code which is based on the date. The customer has to call us to get the password of the day. This gives us an opportunity to engage with the customer and also gives us little more control. It’s a simple form of dual factor authorization where one of the factors requires a phone call.
In the old days, the routine for checking the validity of the password was part of the source code, but we’ve since moved the checking function to a web service.
It’s my goal to replace this ‘password of the day’ check function with a Serverless module. The service will take a password as input and check that it matches the password of the day.
It’s a tiny, simple, rarely-used web service but AWS lambda is still a great fit for it. Although lambda can scale if necessary, in this case it’s about not having the hassle of administering a server.
Mocha and TypeScript
New version 0.3.1
Edit: since the original version of this post, a new version 0.3.1 of Serverless was released. I have updated the tutorial below to reflect the newer version. Also, TSD has been deprecated in favour of Typings so I’ve updated to use Typings instead.
Let’s do things properly and set up a testing framework.
Make sure you’re in the component folder.
$ cd nodejscomponent
Then we’ll install Mocha.
$ npm install mocha --save-dev firstname.lastname@example.org node_modules/mocha ├── email@example.com ├── firstname.lastname@example.org ├── email@example.com ├── firstname.lastname@example.org ├── email@example.com ├── firstname.lastname@example.org (email@example.com) ├── firstname.lastname@example.org (email@example.com) ├── firstname.lastname@example.org (email@example.com, firstname.lastname@example.org) └── email@example.com (firstname.lastname@example.org, email@example.com, firstname.lastname@example.org)
$ npm install typescript --save email@example.com node_modules/typescript $ npm install typings -g /usr/local/bin/typings -> /usr/local/lib/node_modules/typings/dist/bin/typings.js firstname.lastname@example.org /usr/local/lib/node_modules/typings ├── email@example.com ├── firstname.lastname@example.org ├── email@example.com ├── firstname.lastname@example.org ... etc ...
$ typings init -> written typings.json
Now we add the type definitions for Mocha.
$ typings install mocha --ambient --save ? Found mocha typings for DefinitelyTyped. Continue? Yes Installing mocha@~2.2.5 (DefinitelyTyped)... mocha └── (No dependencies)
We need a config file for the TypeScript compiler. This goes in the same module directory (back/modules/potd in my case).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Now make a subdirectory for our TypeScript source files.
$ mkdir src $ mkdir src/test
Next up we need to modify the package.json file to add a scripts section. Only the
"scripts" section needs changing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
We’ve finished setting up everything for TypeScript and Mocha. Whenever you run
npm test will compile and then run any Mocha tests in /lib/test.
The source code so far is on GitHub. Note the default .gitignore file skips the admin.env file which contains the (sensitive) AWS keys in it so don’t forget to add your own.
In the next post we’ll create a TypeScript class for the guts of the lambda function which checks the password of the day, along with some corresponding Mocha tests, also written in TypeScript.