pythonxmppsleekxmpp

SleekXMPP-How to check whether the user is authenticated or not?


As the question says, how can I do it? following is the code-

import logging
from sleekxmpp import ClientXMPP

logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s')


def on_session(event):
    xmpp.get_roster()

xmpp = ClientXMPP(jid, password)
xmpp.add_event_handler('session_start', on_session)
if xmpp.connect():
    print xmpp.authenticated  # Always prints `False`
    xmpp.process(block=True)

I can see that printing in the logs, but I rather want to check it in the code as well. How can I do it?


Solution

  • In XMPP, the authentication is handled by SASL which usually starts once the client is connected.

    In your case, mere connection success doesn't imply that authentication was done. You need to put event handler for SASL authentication success and failure cases. Going through the sleekxmpp events, it makes me believe that it can be done by monitoring auth_success and failed_auth events. Possible code will be :

    xmpp.add_event_handler("failed_auth", on_failed_auth)
    xmpp.add_event_handler("auth_success", on_auth_success)