umqtt.simple and user/password authentication

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
spynappels
Posts: 10
Joined: Wed Jan 25, 2017 11:34 am

umqtt.simple and user/password authentication

Post by spynappels » Thu Jul 20, 2017 12:10 pm

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

User avatar
pythoncoder
Posts: 2160
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: umqtt.simple and user/password authentication

Post by pythoncoder » Fri Jul 21, 2017 5:44 am

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.
Peter Hinch

spynappels
Posts: 10
Joined: Wed Jan 25, 2017 11:34 am

Re: umqtt.simple and user/password authentication

Post by spynappels » Fri Jul 21, 2017 5:55 am

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

User avatar
pythoncoder
Posts: 2160
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: umqtt.simple and user/password authentication

Post by pythoncoder » Fri Jul 21, 2017 6:10 am

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.
Last edited by pythoncoder on Fri Jul 21, 2017 6:25 am, edited 1 time in total.
Peter Hinch

spynappels
Posts: 10
Joined: Wed Jan 25, 2017 11:34 am

Re: umqtt.simple and user/password authentication

Post by spynappels » Fri Jul 21, 2017 6:22 am

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

User avatar
pythoncoder
Posts: 2160
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: umqtt.simple and user/password authentication

Post by pythoncoder » Fri Jul 21, 2017 6:32 am

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.
Peter Hinch

spynappels
Posts: 10
Joined: Wed Jan 25, 2017 11:34 am

Re: umqtt.simple and user/password authentication

Post by spynappels » Fri Jul 21, 2017 6:35 am

Good point, well taken.
I'll wait until I have the SSL/TLS component working too before raising a PR.

Thanks again.

User avatar
rdagger
Posts: 56
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: umqtt.simple and user/password authentication

Post by rdagger » Sun Dec 10, 2017 7:45 pm

Did you get your sample with SSL/TLS working?
If so, would it be ESP32 compatible?

spynappels
Posts: 10
Joined: Wed Jan 25, 2017 11:34 am

Re: umqtt.simple and user/password authentication

Post by spynappels » Sun Dec 10, 2017 8:28 pm

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.

Post Reply