To give ourselves assurance that the user authentication server works, let's write a couple of scripts to exercise the API. Because we're not going to take the time to write an administrative backend to the Notes application, these scripts will let us add and delete users who are allowed access to Notes. These scripts will live within the user authentication server package directory.
The Restify package supports coding REST servers. For the REST clients, we're using a companion library, restify-clients, which has been spun out of Restify.
Create a file named users-add.js, containing the following code:
'use strict';
const util = require('util');
const restify = require('restify-clients');
var client = restify.createJsonClient({
url: 'http://localhost:'+process.env.PORT,
version: '*'
});
client.basicAuth('them', 'D4ED43C0-8BD6-4FE2-B358-7C0E230D11EF');
client.post('/create-user', {
username: "me", password: "w0rd", provider: "local",
familyName: "Einarrsdottir", givenName: "Ashildr", middleName: "",
emails: [], photos: []
},
(err, req, res, obj) => {
if (err) console.error(err.stack);
else console.log('Created '+ util.inspect(obj));
});
This is the basic structure of a Restify client. We create the Client object – we have a choice between the JsonClient, as used here, the StringClient, and the HttpClient. The HTTP basicAuth credentials are easy to set, as shown here.
Then we make the request, in this case a POST request on /create-user. Because it is a POST request, the object we specify here is formatted by Restify into HTTP POST body parameters. As we saw earlier, the server has the bodyParser handler function configured, which converts those body parameters into the req.param object.
In the Restify client, as for the Restify server, we use the various HTTP methods by calling client.METHOD. Because it is a POST request, we use client.post. When the request finishes, the callback function is invoked.
Before running these scripts, start the authentication server in one window using the following command:
$ npm start
Now run the test script using the following command:
$ PORT=3333 node users-add.js
Created { id: 1, username: 'me', password: 'w0rd', provider: 'local',
familyName: 'Einarrsdottir', givenName: 'Ashildr',
middleName: '',
emails: '[]', photos: '[]',
updatedAt: '2016-02-24T02:34:41.661Z',
createdAt: '2016-02-24T02:34:41.661Z' }
We can inspect our handiwork using the following command:
$ sqlite3 users-sequelize.sqlite3 SQLite version 3.10.2 2016-01-20 15:27:19 Enter ".help" for usage hints. sqlite> .schema users CREATE TABLE `Users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `username` VARCHAR(255) UNIQUE, `password` VARCHAR(255), `provider` VARCHAR(255), `familyName` VARCHAR(255), `givenName` VARCHAR(255), `middleName` VARCHAR(255), `emails` VARCHAR(2048), `photos` VARCHAR(2048), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, UNIQUE (`username`));
sqlite> select * from users; 2|me|w0rd|local|Einarrsdottir|Ashildr||[]|[]|2018-01-21 05:34:56.629 +00:00|2018-01-21 05:34:56.629 +00:00 sqlite> ^D
Now let's write a script, users-find.js, to look up a given user:
'use strict';
const util = require('util');
const restify = require('restify-clients');
var client = restify.createJsonClient({
url: 'http://localhost:'+process.env.PORT,
version: '*'
});
client.basicAuth('them', 'D4ED43C0-8BD6-4FE2-B358-7C0E230D11EF');
client.get('/find/'+ process.argv[2],
(err, req, res, obj) => {
if (err) console.error(err.stack);
else console.log('Found '+ util.inspect(obj));
});
This simply calls the /find URL, specifying the username that the user supplies as a command-line argument. Note that the get operation does not take an object full of parameters. Instead, any parameters would be added to the URL.
It's run as follows:
$ PORT=3333 node users-find.js me
Found { username: 'me', provider: 'local',
familyName: 'Einarrsdottir', givenName: 'Ashildr',
middleName: '',
emails: '[]', photos: '[]' }
Similarly, we can write scripts against the other REST functions. But we need to get on with the real goal of integrating this into the Notes application.