plugin XEP-0077: fixed RegisteringAuthenticator to use TLS if possible

This commit is contained in:
Goffi 2018-07-04 07:26:20 +02:00
commit 653b1f1155

View file

@ -23,8 +23,7 @@ from sat.core import exceptions
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 @@ PLUGIN_INFO = {
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 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()
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),
]
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)