Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Commit

Permalink
Load and install the authorization system in the CLI.
Browse files Browse the repository at this point in the history
References #20.
  • Loading branch information
mcollina committed Apr 18, 2013
1 parent 533e3c9 commit aec4b6f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 4 deletions.
5 changes: 4 additions & 1 deletion bin/mosca.js
@@ -1,3 +1,6 @@
#! /usr/bin/env node

require("../lib/cli")(process.argv);
require("../lib/cli")(process.argv, function(err) {
console.log(err);
process.exit(1);
});
1 change: 1 addition & 0 deletions lib/authorizer.js
Expand Up @@ -2,6 +2,7 @@

var hasher = require("./hasher");
var minimatch = require("minimatch");
var debug = require("debug");

/**
* mosca.Authorizer's responsibility is to give an implementation
Expand Down
24 changes: 23 additions & 1 deletion lib/cli.js
Expand Up @@ -17,14 +17,16 @@ module.exports = function cli(argv, callback) {
var server = null;
var runned = false;

callback = callback || function() {};

program
.version(pkg.version)
.option("-p, --port <n>", "the port to listen to", parseInt)
.option("--parent-port <n>", "the parent port to connect to", parseInt)
.option("--parent-host <s>", "the parent host to connect to")
.option("--parent-prefix <s>", "the prefix to use in the parent broker")
.option("--credentials <file>", "the file containing the credentials", null, "./credentials.json")
.option("-c, --config <c>", "the config file to use (override every other options)")
.option("-c, --config <c>", "the config file to use (override every other option)")
.option("-v, --verbose", "equal to DEBUG=mosca")
.option("--very-verbose", "equal to DEBUG=mosca,ascoltatori:*");

Expand Down Expand Up @@ -68,6 +70,26 @@ module.exports = function cli(argv, callback) {
}

server = new Server(opts);

if (program.credentials) {
fs.readFile(program.credentials, function(err, data) {
if (err) {
cb(err);
return;
}

var authorizer = new Authorizer();
authorizer.users = JSON.parse(data);
server.authenticate = authorizer.authenticate;
server.authorizeSubscribe = authorizer.authorizeSubscribe;
server.authorizePublish = authorizer.authorizePublish;
callback(server);
});
} else {
callback(server);
}

return server;
};

var adduser = function (username, password) {
Expand Down
3 changes: 1 addition & 2 deletions lib/server.js
Expand Up @@ -254,8 +254,7 @@ Server.prototype.serve = function(client) {
client.id = packet.clientId;

that.authenticate(client, packet.username, packet.password,

function(err, verdict) {
function(err, verdict) {
if (err) {
debug("The authentication errored");
client.stream.end();
Expand Down
57 changes: 57 additions & 0 deletions test/cli_spec.js
@@ -1,6 +1,7 @@
var async = require("async");
var tmp = require('tmp');
var fs = require("fs");
var mqtt = require("mqtt");

describe("mosca.cli", function() {

Expand Down Expand Up @@ -174,4 +175,60 @@ describe("mosca.cli", function() {
});
});
});

it("should support authorizing an authorized client", function(done) {
args.push("--credentials");
args.push("test/credentials.json");
async.waterfall([
function(cb) {
server = mosca.cli(args);
server.on("ready", cb);
},
function(cb) {
var options = { username: "test", password: "test" };
var client = mqtt.createClient(1883, "localhost", options);
cb = cb.bind(null, null, client);
client.on("connect", cb);
},
function(client, cb) {
client.on("close", cb);
client.end();
}
], function(err) {
if(err) {
done(err);
return;
}
done();
});
});

it("should support negating an unauthorized client", function(done) {
args.push("--credentials");
args.push("test/credentials.json");
async.waterfall([
function(cb) {
server = mosca.cli(args);
server.on("ready", cb);
},
function(cb) {
var options = { username: "bad", password: "bad" };
var client = mqtt.createClient(1883, "localhost", options);
client.on("error", cb);
client.on("connect", function() {
cb(null, client);
});
},
function(client, cb) {
client.once("close", cb);
client.end();
}
], function(err) {
if(err) {
done();
return;
}
done(new Error("No error thrown"));
});
});
});
1 change: 1 addition & 0 deletions test/credentials.json
@@ -0,0 +1 @@
{"test":{"salt":"RtdMQo57Qj/RHdiKZWlsuCX5++KvucbIOGVN1EXPe5zA8M2n4uHcESEEwP+bhkyjhbU6sRShrPtgi6VKaNHsKA==","hash":"sJDtCL9MqCO2Ln3bDTuy4inhA9ayHxIoSjchXnkhzmT0cuvxTcWhF6q2uiiTBqqOKNp57wWrKKQtFKduR515nw==","authorizePublish":"**","authorizeSubscribe":"**"}}

0 comments on commit aec4b6f

Please sign in to comment.