Page 1 of 1

umqtt.simple and user/password authentication

Posted: Thu Jul 20, 2017 12:10 pm
by spynappels
I've been playing with umqtt on an esp8266 running MicroPython 1.9.1
I have been able to get it working fine with the broker accepting anonymous connections, but when I wanted to force the use of username and password for authentication, I was unable to make that work.

Is this currently supported, or is this something which is in the roadmap?

If it is currently supported, I'll keep trying on it, and if I get it working I'll likely write it up, or add an example Python script to the lib repo, but there is precious little information on umqtt and authentication out there.

Thanks in advance.
Stefan

Re: umqtt.simple and user/password authentication

Posted: Fri Jul 21, 2017 5:44 am
by pythoncoder
The code for umqtt.simple supports authentication but I don't know to what extent it's been tested.

Have you checked that the broker configuration is correct? I'd run a client on a PC to check that password authentication is working. Are you using SSL/TLS? What broker are you using?

This link https://mosquitto.org/man/mosquitto-conf-5.html has some information but it is specific to mosquitto.

Re: umqtt.simple and user/password authentication

Posted: Fri Jul 21, 2017 5:55 am
by spynappels
Thanks for the response Peter.

I am using Mosquitto on a RasPi and it is configured correctly for password auth (but not SSL yet) as I can use mosquitto_pub to send messages and both publishing and subscribing from the Android client I am testing with works.

A snippet of the code I'm using is this, which is basically an edit of the example script in the micropython-lib repo:

Code: Select all

SERVER = "192.168.10.180"
PORT = 1883
USER = "testesp8266"
PASSWORD = "xxxxxxx"
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
TOPIC = b"test/led"

.....

def main(server=SERVER, port=PORT, user=USER, password=PASSWORD):
    c = MQTTClient(CLIENT_ID, server, port, user, password)
    # Subscribed messages will be delivered to this callback
    c.set_callback(sub_cb)
    c.connect()
    c.subscribe(TOPIC)
    print("Connected to %s, subscribed to %s topic" % (server, TOPIC))
.....
I suspect that passing in the username and password in

Code: Select all

c = MQTTClient(CLIENT_ID, server, port, user, password)
is failing somewhere, so I think I need to work on that, but is that the right direction to be going in?

I've done some basic Python, but I think that I need to do some more reading up on passing parameters into functions.

Regards,
Stefan

Re: umqtt.simple and user/password authentication

Posted: Fri Jul 21, 2017 6:10 am
by pythoncoder
Hi Stefan,

what you're doing looks correct. Strictly the user and password strings should be bytes objects declared thus:

Code: Select all

USER = b"testesp8266"
PASSWORD = b"xxxxxxx"
Please try this and report the outcome. It's quite possible that you're the first to test this.

[EDIT]
I've looked at the MQTT spec and the library code and it looks correct. Username and password are handled similarly to the Last Will arguments and I have tested those.

Re: umqtt.simple and user/password authentication

Posted: Fri Jul 21, 2017 6:22 am
by spynappels
Aaaaahhhhhhh!

That'll teach me to actually Google the error messages I get properly!

It was working, but I was getting MQTT Exception: 4 which is Auth Failed.
Ensuring the username and password were actually correct made it work.

Thanks for the sanity check though.

Do you think it might be a good idea for me to raise a PR with a simple example script with authentication against the umqtt.simple module in the micropython-lib repo?

Kindest regards,
Stefan

Re: umqtt.simple and user/password authentication

Posted: Fri Jul 21, 2017 6:32 am
by pythoncoder
Hi Stefan,

glad you got it working. A sample script with username, password, and SSL/TLS would be very useful. In practice username and password on a plaintext internet connection isn't secure.

Re: umqtt.simple and user/password authentication

Posted: Fri Jul 21, 2017 6:35 am
by spynappels
Good point, well taken.
I'll wait until I have the SSL/TLS component working too before raising a PR.

Thanks again.

Re: umqtt.simple and user/password authentication

Posted: Sun Dec 10, 2017 7:45 pm
by rdagger
Did you get your sample with SSL/TLS working?
If so, would it be ESP32 compatible?

Re: umqtt.simple and user/password authentication

Posted: Sun Dec 10, 2017 8:28 pm
by spynappels
rdagger wrote:
Sun Dec 10, 2017 7:45 pm
Did you get your sample with SSL/TLS working?
If so, would it be ESP32 compatible?
I have not, but to be fair, I've not come back to it yet as the username and password usage inside my test LAN was sufficient.
I will try to get back to it and in terms of ESP32 compatibility, if it runs micropython it should work.