Mercurial > libervia-backend
comparison src/plugins/plugin_misc_register_account.py @ 2172:545a1261ac3b
core, plugin XEP-0077: in-band registration fix and move:
in-band was partially in core for historical reason, it has been moved to XEP-0077, and fixed. It is still incomplete, but should work for basic accounts creation.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 08 Mar 2017 20:59:31 +0100 |
parents | 33c8c4973743 |
children | 75002ac33801 |
comparison
equal
deleted
inserted
replaced
2171:978011533892 | 2172:545a1261ac3b |
---|---|
20 | 20 |
21 from sat.core.i18n import _, D_ | 21 from sat.core.i18n import _, D_ |
22 from sat.core.log import getLogger | 22 from sat.core.log import getLogger |
23 log = getLogger(__name__) | 23 log = getLogger(__name__) |
24 from sat.core.constants import Const as C | 24 from sat.core.constants import Const as C |
25 from twisted.words.protocols.jabber import jid, xmlstream | 25 from twisted.words.protocols.jabber import jid |
26 from sat.core import xmpp | |
27 from sat.memory.memory import Sessions | 26 from sat.memory.memory import Sessions |
28 from twisted.internet import reactor, defer | |
29 from sat.tools import xml_tools | 27 from sat.tools import xml_tools |
30 from sat.tools.xml_tools import SAT_FORM_PREFIX, SAT_PARAM_SEPARATOR | 28 from sat.tools.xml_tools import SAT_FORM_PREFIX, SAT_PARAM_SEPARATOR |
31 | 29 |
32 | 30 |
33 PLUGIN_INFO = { | 31 PLUGIN_INFO = { |
34 C.PI_NAME: "Register Account Plugin", | 32 C.PI_NAME: "Register Account Plugin", |
35 C.PI_IMPORT_NAME: "REGISTER-ACCOUNT", | 33 C.PI_IMPORT_NAME: "REGISTER-ACCOUNT", |
36 C.PI_TYPE: "MISC", | 34 C.PI_TYPE: "MISC", |
37 C.PI_PROTOCOLS: [], | 35 C.PI_PROTOCOLS: [], |
38 C.PI_DEPENDENCIES: [], | 36 C.PI_DEPENDENCIES: ["XEP-0077"], |
39 C.PI_RECOMMENDATIONS: [], | 37 C.PI_RECOMMENDATIONS: [], |
40 C.PI_MAIN: "RegisterAccount", | 38 C.PI_MAIN: "RegisterAccount", |
41 C.PI_HANDLER: "no", | 39 C.PI_HANDLER: "no", |
42 C.PI_DESCRIPTION: _(u"""Register XMPP account""") | 40 C.PI_DESCRIPTION: _(u"""Register XMPP account""") |
43 } | 41 } |
44 | 42 |
45 | 43 |
46 class RegisterAccount(object): | 44 class RegisterAccount(object): |
45 # FIXME: this plugin is messy and difficult to read, it needs to be cleaned up and documented | |
47 | 46 |
48 def __init__(self, host): | 47 def __init__(self, host): |
49 log.info(_(u"Plugin Register Account initialization")) | 48 log.info(_(u"Plugin Register Account initialization")) |
50 self.host = host | 49 self.host = host |
51 self._sessions = Sessions() | 50 self._sessions = Sessions() |
52 host.registerCallback(self.registerNewAccountCB, with_data=True, force_id="registerNewAccount") | 51 host.registerCallback(self.registerNewAccountCB, with_data=True, force_id="registerNewAccount") |
53 self.__register_account_id = host.registerCallback(self._registerConfirmation, with_data=True) | 52 self.__register_account_id = host.registerCallback(self._registerConfirmation, with_data=True) |
54 | 53 |
55 def registerNewAccountCB(self, data, profile): | 54 def registerNewAccountCB(self, data, profile): |
56 """Called when the use click on the "New account" button.""" | 55 """Called when the user click on the "New account" button.""" |
57 session_data = {} | 56 session_data = {} |
58 for param in ('JabberID', 'Password', C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM): | 57 |
58 # FIXME: following loop is overcomplicated, hard to read | |
59 # FIXME: while used with parameters, hashed password is used and overwrite clear one | |
60 for param in (u'JabberID', u'Password', C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM): | |
59 try: | 61 try: |
60 session_data[param] = data["%s%s%s%s" % (SAT_FORM_PREFIX, "Connection", SAT_PARAM_SEPARATOR, param)] | 62 session_data[param] = data[SAT_FORM_PREFIX + u"Connection" + SAT_PARAM_SEPARATOR + param] |
61 except KeyError: | 63 except KeyError: |
62 if param in (C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM): | 64 if param in (C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM): |
63 session_data[param] = '' | 65 session_data[param] = '' |
64 | 66 |
65 for param in ('JabberID', 'Password'): | 67 for param in (u'JabberID', u'Password'): |
66 if not session_data[param]: | 68 if not session_data[param]: |
67 form_ui = xml_tools.XMLUI("popup", title=D_("Missing values")) | 69 form_ui = xml_tools.XMLUI(u"popup", title=D_(u"Missing values")) |
68 form_ui.addText(D_("No user JID or password given: can't register new account.")) | 70 form_ui.addText(D_(u"No user JID or password given: can't register new account.")) |
69 return {'xmlui': form_ui.toXml()} | 71 return {u'xmlui': form_ui.toXml()} |
70 | 72 |
71 session_data['user'], host, resource = jid.parse(session_data['JabberID']) | 73 session_data['user'], host, resource = jid.parse(session_data['JabberID']) |
72 session_data['server'] = session_data[C.FORCE_SERVER_PARAM] or host | 74 session_data['server'] = session_data[C.FORCE_SERVER_PARAM] or host |
73 session_id, dummy = self._sessions.newSession(session_data, profile=profile) | 75 session_id, dummy = self._sessions.newSession(session_data, profile=profile) |
74 form_ui = xml_tools.XMLUI("form", title=D_("Register new account"), submit_id=self.__register_account_id, session_id=session_id) | 76 form_ui = xml_tools.XMLUI("form", title=D_("Register new account"), submit_id=self.__register_account_id, session_id=session_id) |
75 form_ui.addText(D_("Do you want to register a new XMPP account [%(user)s] on server %(server)s ?") % {'user': session_data['user'], 'server': session_data['server']}) | 77 form_ui.addText(D_(u"Do you want to register a new XMPP account {jid}?").format( |
78 jid = session_data['JabberID'])) | |
76 return {'xmlui': form_ui.toXml()} | 79 return {'xmlui': form_ui.toXml()} |
77 | 80 |
78 def _registerConfirmation(self, data, profile): | 81 def _registerConfirmation(self, data, profile): |
79 """Save the related parameters and proceed the registration.""" | 82 """Save the related parameters and proceed the registration.""" |
83 client = self.host.getClient(profile) | |
80 session_data = self._sessions.profileGet(data['session_id'], profile) | 84 session_data = self._sessions.profileGet(data['session_id'], profile) |
81 | 85 |
82 self.host.memory.setParam("JabberID", session_data["JabberID"], "Connection", profile_key=profile) | 86 self.host.memory.setParam("JabberID", session_data["JabberID"], "Connection", profile_key=profile) |
83 self.host.memory.setParam("Password", session_data["Password"], "Connection", profile_key=profile) | 87 self.host.memory.setParam("Password", session_data["Password"], "Connection", profile_key=profile) |
84 self.host.memory.setParam(C.FORCE_SERVER_PARAM, session_data[C.FORCE_SERVER_PARAM], "Connection", profile_key=profile) | 88 self.host.memory.setParam(C.FORCE_SERVER_PARAM, session_data[C.FORCE_SERVER_PARAM], "Connection", profile_key=profile) |
85 self.host.memory.setParam(C.FORCE_PORT_PARAM, session_data[C.FORCE_PORT_PARAM], "Connection", profile_key=profile) | 89 self.host.memory.setParam(C.FORCE_PORT_PARAM, session_data[C.FORCE_PORT_PARAM], "Connection", profile_key=profile) |
86 | 90 |
87 d = self._registerNewAccount(session_data['user'], session_data["Password"], None, session_data['server'], profile_key=profile) | 91 d = self._registerNewAccount(client, jid.JID(session_data['JabberID']), session_data["Password"], None, session_data['server']) |
88 del self._sessions[data['session_id']] | 92 del self._sessions[data['session_id']] |
89 return d | 93 return d |
90 | 94 |
91 def _registerNewAccount(self, user, password, email, host, port=C.XMPP_C2S_PORT, profile_key=C.PROF_KEY_NONE): | 95 def _registerNewAccount(self, client, jid_, password, email, server): |
92 """Connect to a server and create a new account using in-band registration. | 96 #Â FIXME: port is not set here |
93 @param user: login of the account | 97 def registeredCb(dummy): |
94 @param password: password of the account | 98 xmlui = xml_tools.XMLUI(u"popup", title=D_(u"Confirmation")) |
95 @param email: email of the account | |
96 @param host: host of the server to register to | |
97 @param port: port of the server to register to | |
98 @param profile_key: %(doc_profile_key)s | |
99 """ | |
100 profile = self.host.memory.getProfileName(profile_key) | |
101 | |
102 d = defer.Deferred() | |
103 serverRegistrer = xmlstream.XmlStreamFactory(xmpp.RegisteringAuthenticator(self, host, user, password, email, d, profile)) | |
104 connector = reactor.connectTCP(host, port or C.XMPP_C2S_PORT, serverRegistrer) | |
105 serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect() | |
106 | |
107 def cb(dummy): | |
108 xmlui = xml_tools.XMLUI("popup", title=D_("Confirmation")) | |
109 xmlui.addText(D_("Registration successful.")) | 99 xmlui.addText(D_("Registration successful.")) |
110 return ({'xmlui': xmlui.toXml()}) | 100 return ({'xmlui': xmlui.toXml()}) |
111 | 101 |
112 def eb(failure): | 102 def registeredEb(failure): |
113 xmlui = xml_tools.XMLUI("popup", title=D_("Failure")) | 103 xmlui = xml_tools.XMLUI("popup", title=D_("Failure")) |
114 xmlui.addText(D_("Registration failed: %s") % failure.getErrorMessage()) | 104 xmlui.addText(D_("Registration failed: %s") % failure.getErrorMessage()) |
115 try: | 105 try: |
116 if failure.value.condition == 'conflict': | 106 if failure.value.condition == 'conflict': |
117 xmlui.addText(D_("Username already exists, please choose an other one.")) | 107 xmlui.addText(D_("Username already exists, please choose an other one.")) |
118 except AttributeError: | 108 except AttributeError: |
119 pass | 109 pass |
120 return ({'xmlui': xmlui.toXml()}) | 110 return ({'xmlui': xmlui.toXml()}) |
121 | 111 |
122 d.addCallbacks(cb, eb) | 112 registered_d = self.host.plugins['XEP-0077'].registerNewAccount(client, jid_, password, email=email, host=server, port=C.XMPP_C2S_PORT) |
123 return d | 113 registered_d.addCallbacks(registeredCb, registeredEb) |
114 return registered_d |