Skip to content Skip to sidebar Skip to footer

Sleekxmpp Threaded Authentication

so... I have a simple chat client like so: class ChatClient(sleekxmpp.ClientXMPP): def __init__(self, jid, password, server): sleekxmpp.ClientXMPP.__init__(self, jid, p

Solution 1:

This would be a good reason for changing the .authenticated and related fields to be threading.Event objects so that you could use wait() for situations like this, but I'm not sure how much that would break existing user code.

But short of modifying SleekXMPP, what you will need to do is wait for certain events to fire. For example, if your client successfully authenticated, then there will be a session_start event (I may add an auth_success or similar event later). Likewise, if authentication failed for a single mechanism, there will be a failed_auth event. If no authentication methods at all succeeded (which is what you'd be interested in), there will be a no_auth event.

So you can add handlers for these events, and have those handlers place a token in a queue, and then wait for the desired token to arrive.

classChatClient(ClientXMPP):
    def__init__(self, ...):
        ...
        self.auth_queue = queue.Queue()
        self.add_event_handler('no_auth', self.failed)

    defstart(self, event):
        self.auth_queue.put('success')
        ...

    deffailed(self, event):
        self.auth_queue.put('failed')


defauthenticate(username, password, server):
    xmppuser = username + '@' + server
    passTester = ChatClient(xmppuser, password)
    try:
        result = passTester.auth_queue.get(timeout=10)
    except queue.Empty:
        result = 'failed'
    passTester.disconnect()
    return result == 'success'

Don't forget that we also have the chat room at sleek@conference.jabber.org.

-- Lance

Post a Comment for "Sleekxmpp Threaded Authentication"