the1laz / cgateweb

MQTT interface for C-Bus written in Node.js
MIT License
21 stars 20 forks source link

How do you set MQTT username and password? #1

Closed poldim closed 7 years ago

poldim commented 7 years ago

I've got an MQTT running in a docker and have it exposed to the web so I've enabled password protection to keep intruders out. How can I pass through my credentials from cgateweb to the MQTT server?

the1laz commented 7 years ago

I've added password support, you need to add it in the settings file. Let me know if it works fine for you.

poldim commented 7 years ago

Thanks for working on this!

I've updated the index and settings files and went form a Error: Connection refused: Not authorized to the error below. I also tried intentionally using the wrong username/password and the error was the same.


root@Homebridge:/usr/local/bin/cgateweb# node index.js
/usr/local/bin/cgateweb/index.js:5
<!DOCTYPE html>
^
SyntaxError: Unexpected token <
    at Object.exports.runInThisContext (vm.js:78:16)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3
the1laz commented 7 years ago

Just in case, could you double check that the index.js on your machine matches what's in github? It looks like somehow somewhere a js file has some html in it, but I'm not exactly sure where or how... https://github.com/the1laz/cgateweb/blob/master/index.js

Maybe worth checking settings.js as well.

poldim commented 7 years ago

I decided to start over and removed the cgateweb folder to eliminate any update conflicts. C-Gate registers the connection from node and then instantly closes it as it fails to connect to the MQTT broker. I tried to see if there is an option for "trusted connections" in mosquitto to see if I can get around it that way but did not find anything.

root@Homebridge:/usr/local/bin/cgateweb# node index.js
CONNECTED TO C-GATE COMMAND PORT: 127.0.0.1:20023
CONNECTED TO C-GATE EVENT PORT: 127.0.0.1:20025
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: Connection refused: Not authorized
    at MqttClient._handleConnack (/usr/local/bin/cgateweb/node_modules/mqtt/lib/client.js:524:9)
    at Connection.<anonymous> (/usr/local/bin/cgateweb/node_modules/mqtt/lib/client.js:191:10)
    at emitOne (events.js:96:13)
    at Connection.emit (events.js:188:7)
    at Connection._write (/usr/local/bin/cgateweb/node_modules/mqtt/lib/connection.js:176:12)
    at doWrite (_stream_writable.js:332:12)
    at writeOrBuffer (_stream_writable.js:318:5)
    at Connection.Writable.write (_stream_writable.js:245:11)
    at Socket.ondata (_stream_readable.js:555:20)
    at emitOne (events.js:96:13)
the1laz commented 7 years ago

Hmm, weird. I've tested on mosquitto here and it works. I image there's possibilities of version differences, but I'd expect some different error. You've definitely got both your username and password in settings.js and uncommented those two lines?

poldim commented 7 years ago

yessir

poldim commented 7 years ago

my password has an ! and # in it - could that be breaking it or are the single quotes around it making it a whole var?

the1laz commented 7 years ago

Alright. I might try a fresh install on a different pi when I'm back at my computer then, see if I'm missing something.

the1laz commented 7 years ago

No, I don't think there's any problem with ! or #. As long as there are no quotes in the password and as long as you've removed the slashes from in front of those lines, should work. I just tested on mine using password!# and it connected fine.

the1laz commented 7 years ago

I just tried this on a different pi:

git clone https://github.com/the1laz/cgateweb.git cd cgateweb npm install nano settings.js -- changed the cbus ip -- changed the mqtt ip -- deleted the // from username and password, entered my testing usernames and password: exports.mqttusername = 'user1'; exports.mqttpassword = 'password!#'; then ran it with node index.js

Connected without issue. Did you do anything different with your install?

I set up password on mosquitto by creating a password file with this in it: user1:password!# then ran mosquitto_passwd -u passwordfilename then copied the file into /etc/mosquitto edited /etc/mosquitto/mosquitto.conf -- added the line password_file /etc/mosquitto/passwordfilename -- added the line allow_anonymous false restarted the service.

Any differences there?

I'm not sure where else to look, since we'll be using the same version of the mqtt module and we're both using a rasppberry pi.

the1laz commented 7 years ago

Haven't got any more feedback, working for me.

lenlch commented 7 years ago

use can use this. var mqtt = require('mqtt'), url = require('url'); // Parse var mqtt_url = url.parse(process.env.CLOUDMQTT_URL || 'mqtt://localhost:1883'); var auth = (mqtt_url.auth || ':').split(':'); var url = "mqtt://" + mqtt_url.host;

var options = { port: mqtturl.port, clientId: 'mqttjs' + Math.random().toString(16).substr(2, 8), username: auth[0], password: auth[1], };

// Create a client connection var client = mqtt.connect(url, options);