changeset 2634:6c89cf856d28

plugin XEP-0077: fixed RegisteringAuthenticator to use TLS if possible
author Goffi <goffi@goffi.org>
date Wed, 04 Jul 2018 07:26:20 +0200
parents f3bbb6822ab3
children eecd84a2530a
files sat/plugins/plugin_xep_0077.py
diffstat 1 files changed, 20 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0077.py	Wed Jul 04 07:24:42 2018 +0200
+++ b/sat/plugins/plugin_xep_0077.py	Wed Jul 04 07:26:20 2018 +0200
@@ -23,8 +23,7 @@
 from sat.core.log import getLogger
 
 log = getLogger(__name__)
-from twisted.words.protocols.jabber import jid
-from twisted.words.protocols.jabber import xmlstream
+from twisted.words.protocols.jabber import jid, xmlstream, client
 from twisted.internet import defer, reactor
 from sat.tools import xml_tools
 
@@ -46,23 +45,35 @@
 
 
 class RegisteringAuthenticator(xmlstream.ConnectAuthenticator):
-    # FIXME: request IQ is not send to check available fields, while XEP recommand to use it
+    # FIXME: request IQ is not send to check available fields,
+    #        while XEP recommand to use it
     # FIXME: doesn't handle data form or oob
+    namespace = 'jabber:client'
 
     def __init__(self, jid_, password, email=None):
+        log.debug(_(u"Registration asked for {jid}").format(jid=jid_))
         xmlstream.ConnectAuthenticator.__init__(self, jid_.host)
         self.jid = jid_
         self.password = password
         self.email = email
         self.registered = defer.Deferred()
-        log.debug(_(u"Registration asked for {jid}").format(jid=jid_))
+
+    def associateWithStream(self, xs):
+        xmlstream.ConnectAuthenticator.associateWithStream(self, xs)
+        xs.addObserver(xmlstream.STREAM_AUTHD_EVENT, self.register)
+
+        xs.initializers = [client.CheckVersionInitializer(xs)]
+        inits = [ (xmlstream.TLSInitiatingInitializer, False),
+                ]
 
-    def connectionMade(self):
-        log.debug(_(u"Connection made with {server}".format(server=self.jid.host)))
-        self.xmlstream.otherEntity = jid.JID(self.jid.host)
-        self.xmlstream.namespace = C.NS_CLIENT
-        self.xmlstream.sendHeader()
+        for initClass, required in inits:
+            init = initClass(xs)
+            init.required = required
+            xs.initializers.append(init)
 
+    def register(self, xmlstream):
+        log.debug(_(u"Stream started with {server}, now registering"
+                    .format(server=self.jid.host)))
         iq = XEP_0077.buildRegisterIQ(self.xmlstream, self.jid, self.password, self.email)
         d = iq.send(self.jid.host).addCallbacks(self.registrationCb, self.registrationEb)
         d.chainDeferred(self.registered)