Mosca lib/authorizer.js

    Description

    Copyright (c) 2013-2014 Matteo Collina, http://matteocollina.com

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.

    Source

    "use strict";
    
    var hasher = require("./hasher");
    var minimatch = require("minimatch");

    Authorizer

    function
    Authorizer()
    • @param: {Object}usersThe user hash, as created by this class
    • @: (optional)

    Description

    mosca.Authorizer's responsibility is to give an implementation
    of mosca.Server callback of authorizations, against a JSON file.

    Source

    function Authorizer(users) {
      this.users = users || {};
    }
    module.exports = Authorizer;

      Description

      It returns the authenticate function to plug into mosca.Server.

      Source

      Authorizer.prototype.__defineGetter__("authenticate", function() {
        var that = this;
        return function(client, user, pass, cb) {
          that._authenticate(client, user, pass, cb);
        };
      });

        Description

        It returns the authorizePublish function to plug into mosca.Server.

        Source

        Authorizer.prototype.__defineGetter__("authorizePublish", function() {
          var that = this;
          return function(client, topic, payload, cb) {
            cb(null, minimatch(topic, that.users[client.user].authorizePublish));
          };
        });

          Description

          It returns the authorizeSubscribe function to plug into mosca.Server.

          Source

          Authorizer.prototype.__defineGetter__("authorizeSubscribe", function() {
            var that = this;
            return function(client, topic, cb) {
              cb(null, minimatch(topic, that.users[client.user].authorizeSubscribe));
            };
          });

          addUser

          method
          Authorizer.prototype.addUser()
          • @param: {String}userThe username
          • @param: {String}passThe password
          • @param: {String}authorizePublishThe authorizePublish pattern
          • @: (optional)
          • @param: {String}authorizeSubscribeThe authorizeSubscribe pattern
          • @: (optional)
          • @param: {Function}cbThe callback that will be called after the
          • @: insertion.

          Description

          An utility function to add an user.

          Source

          Authorizer.prototype.addUser = function(user, pass, authorizePublish,
                                                  authorizeSubscribe, cb) {
            var that = this;
          
            if (typeof authorizePublish === "function") {
              cb = authorizePublish;
              authorizePublish = "**";
              authorizeSubscribe = "**";
            } else if (typeof authorizeSubscribe == "function") {
              cb = authorizeSubscribe;
              authorizeSubscribe = "**";
            }
          
            hasher({
              password: pass.toString()
            }, function(err, pass, salt, hash) {
              if (!err) {
                that.users[user] = {
                  salt: salt,
                  hash: hash,
                  authorizePublish: authorizePublish,
                  authorizeSubscribe: authorizeSubscribe
                };
              }
              cb(err);
            });
            return this;
          };

          rmUser

          method
          Authorizer.prototype.rmUser()
          • @param: {String}userThe username
          • @param: {String}passThe password
          • @param: {Function}cbThe callback that will be called after the
          • @: deletion.

          Description

          An utility function to delete a user.

          Source

          Authorizer.prototype.rmUser = function(user, cb) {
            delete this.users[user];
            cb();
            return this;
          };