moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 508 forks source link

Adding credentials.json to mosca settings #588

Closed bacteria323 closed 7 years ago

bacteria323 commented 7 years ago

I am trying to add credentials.json to the mosca settings file but I am getting the error:

/home/User/Desktop/node_modules/mosca/lib/server.js:109
  throw new Error(errMessage);
  ^

Error: is not of a type(s) object
at EventEmitter.Server (/home/User/Desktop/node_modules/mosca/lib/server.js:109:13)
at Object.<anonymous> (/home/User/Desktop/MOSCA/mosca-app.js:50:14)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3

My mosca settings is:

var moscaSettings = {
  credentials: "credentials.json", // This line is causing the error
  port: 1883,
  backend: ascoltatore,
  persistence: {
    factory: mosca.persistence.Mongo,
    url: 'mongodb://localhost:27017/mqtt'
  }
};
mcollina commented 7 years ago

You should put the content of the JSON file, not the name of the file.

bacteria323 commented 7 years ago

But can't I add the file to the settings like shown here: https://github.com/mcollina/mosca/wiki/Authentication-&-Authorization ?

mcollina commented 7 years ago

I think that works only from the CLI.

bigbenz8 commented 6 years ago

do not put "credentials" field into mosca server setting obj , just define a setting var only for userAuth, like :

const authSetting = {credentials: "credentials.json" } 

and pass it to setup func :

function setup() {
    // setup authorizer
    loadAuthorizer(authSetting, function(err, authorizer) {
        if (err) {
            // handle error here
        }

        if (authorizer) {
            server.authenticate = authorizer.authenticate;
            server.authorizeSubscribe = authorizer.authorizeSubscribe;
            server.authorizePublish = authorizer.authorizePublish;
        }
    });

    // you are good to go!
}

then put the setup func in server ready call back func. it works fine . Good luck.

see here: https://github.com/mcollina/mosca/wiki/Authentication-&-Authorization

Using Mosca's standalone authorizer with an embedded Mosca