Mercurial > libervia-backend
comparison sat/stdui/ui_profile_manager.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 378188abe941 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 19 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | 20 |
21 from sat.core.i18n import D_ | 21 from sat.core.i18n import D_ |
22 from sat.core.constants import Const as C | 22 from sat.core.constants import Const as C |
23 from sat.core.log import getLogger | 23 from sat.core.log import getLogger |
24 | |
24 log = getLogger(__name__) | 25 log = getLogger(__name__) |
25 from sat.core import exceptions | 26 from sat.core import exceptions |
26 from sat.tools import xml_tools | 27 from sat.tools import xml_tools |
27 from sat.memory.memory import ProfileSessions | 28 from sat.memory.memory import ProfileSessions |
28 from twisted.words.protocols.jabber import jid | 29 from twisted.words.protocols.jabber import jid |
33 | 34 |
34 def __init__(self, host): | 35 def __init__(self, host): |
35 self.host = host | 36 self.host = host |
36 self.profile_ciphers = {} | 37 self.profile_ciphers = {} |
37 self._sessions = ProfileSessions() | 38 self._sessions = ProfileSessions() |
38 host.registerCallback(self._authenticateProfile, force_id=C.AUTHENTICATE_PROFILE_ID, with_data=True) | 39 host.registerCallback( |
39 host.registerCallback(self._changeXMPPPassword, force_id=C.CHANGE_XMPP_PASSWD_ID, with_data=True) | 40 self._authenticateProfile, force_id=C.AUTHENTICATE_PROFILE_ID, with_data=True |
40 self.__new_xmpp_passwd_id = host.registerCallback(self._changeXMPPPasswordCb, with_data=True) | 41 ) |
42 host.registerCallback( | |
43 self._changeXMPPPassword, force_id=C.CHANGE_XMPP_PASSWD_ID, with_data=True | |
44 ) | |
45 self.__new_xmpp_passwd_id = host.registerCallback( | |
46 self._changeXMPPPasswordCb, with_data=True | |
47 ) | |
41 | 48 |
42 def _startSessionEb(self, fail, first, profile): | 49 def _startSessionEb(self, fail, first, profile): |
43 """Errback method for startSession during profile authentication | 50 """Errback method for startSession during profile authentication |
44 | 51 |
45 @param first(bool): if True, this is the first try and we have tryied empty password | 52 @param first(bool): if True, this is the first try and we have tryied empty password |
47 @param profile(unicode, None): %(doc_profile)s | 54 @param profile(unicode, None): %(doc_profile)s |
48 must only be used if first is True | 55 must only be used if first is True |
49 """ | 56 """ |
50 if first: | 57 if first: |
51 # first call, we ask for the password | 58 # first call, we ask for the password |
52 form_ui = xml_tools.XMLUI("form", title=D_('Profile password for {}').format(profile), submit_id='') | 59 form_ui = xml_tools.XMLUI( |
53 form_ui.addPassword('profile_password', value='') | 60 "form", title=D_("Profile password for {}").format(profile), submit_id="" |
61 ) | |
62 form_ui.addPassword("profile_password", value="") | |
54 d = xml_tools.deferredUI(self.host, form_ui, chained=True) | 63 d = xml_tools.deferredUI(self.host, form_ui, chained=True) |
55 d.addCallback(self._authenticateProfile, profile) | 64 d.addCallback(self._authenticateProfile, profile) |
56 return {'xmlui': form_ui.toXml()} | 65 return {"xmlui": form_ui.toXml()} |
57 | 66 |
58 assert profile is None | 67 assert profile is None |
59 | 68 |
60 if fail.check(exceptions.PasswordError): | 69 if fail.check(exceptions.PasswordError): |
61 dialog = xml_tools.XMLUI('popup', title=D_('Connection error')) | 70 dialog = xml_tools.XMLUI("popup", title=D_("Connection error")) |
62 dialog.addText(D_("The provided profile password doesn't match.")) | 71 dialog.addText(D_("The provided profile password doesn't match.")) |
63 else: | 72 else: |
64 log.error(u"Unexpected exceptions: {}".format(fail)) | 73 log.error(u"Unexpected exceptions: {}".format(fail)) |
65 dialog = xml_tools.XMLUI('popup', title=D_('Internal error')) | 74 dialog = xml_tools.XMLUI("popup", title=D_("Internal error")) |
66 dialog.addText(D_(u"Internal error: {}".format(fail))) | 75 dialog.addText(D_(u"Internal error: {}".format(fail))) |
67 return {'xmlui': dialog.toXml(), 'validated': C.BOOL_FALSE} | 76 return {"xmlui": dialog.toXml(), "validated": C.BOOL_FALSE} |
68 | 77 |
69 def _authenticateProfile(self, data, profile): | 78 def _authenticateProfile(self, data, profile): |
70 if C.bool(data.get('cancelled', 'false')): | 79 if C.bool(data.get("cancelled", "false")): |
71 return {} | 80 return {} |
72 if self.host.memory.isSessionStarted(profile): | 81 if self.host.memory.isSessionStarted(profile): |
73 return {'validated': C.BOOL_TRUE} | 82 return {"validated": C.BOOL_TRUE} |
74 try: | 83 try: |
75 password = data[xml_tools.formEscape('profile_password')] | 84 password = data[xml_tools.formEscape("profile_password")] |
76 except KeyError: | 85 except KeyError: |
77 # first request, we try empty password | 86 # first request, we try empty password |
78 password = '' | 87 password = "" |
79 first = True | 88 first = True |
80 eb_profile = profile | 89 eb_profile = profile |
81 else: | 90 else: |
82 first = False | 91 first = False |
83 eb_profile = None | 92 eb_profile = None |
84 d = self.host.memory.startSession(password, profile) | 93 d = self.host.memory.startSession(password, profile) |
85 d.addCallback(lambda dummy: {'validated': C.BOOL_TRUE}) | 94 d.addCallback(lambda dummy: {"validated": C.BOOL_TRUE}) |
86 d.addErrback(self._startSessionEb, first, eb_profile) | 95 d.addErrback(self._startSessionEb, first, eb_profile) |
87 return d | 96 return d |
88 | 97 |
89 def _changeXMPPPassword(self, data, profile): | 98 def _changeXMPPPassword(self, data, profile): |
90 session_data = self._sessions.profileGetUnique(profile) | 99 session_data = self._sessions.profileGetUnique(profile) |
91 if not session_data: | 100 if not session_data: |
92 server = self.host.memory.getParamA(C.FORCE_SERVER_PARAM, "Connection", profile_key=profile) | 101 server = self.host.memory.getParamA( |
102 C.FORCE_SERVER_PARAM, "Connection", profile_key=profile | |
103 ) | |
93 if not server: | 104 if not server: |
94 server = jid.parse(self.host.memory.getParamA('JabberID', "Connection", profile_key=profile))[1] | 105 server = jid.parse( |
95 session_id, session_data = self._sessions.newSession({'count': 0, 'server': server}, profile=profile) | 106 self.host.memory.getParamA( |
96 if session_data['count'] > 2: # 3 attempts with a new password after the initial try | 107 "JabberID", "Connection", profile_key=profile |
108 ) | |
109 )[1] | |
110 session_id, session_data = self._sessions.newSession( | |
111 {"count": 0, "server": server}, profile=profile | |
112 ) | |
113 if ( | |
114 session_data["count"] > 2 | |
115 ): # 3 attempts with a new password after the initial try | |
97 self._sessions.profileDelUnique(profile) | 116 self._sessions.profileDelUnique(profile) |
98 _dialog = xml_tools.XMLUI('popup', title=D_('Connection error')) | 117 _dialog = xml_tools.XMLUI("popup", title=D_("Connection error")) |
99 _dialog.addText(D_("Can't connect to %s. Please check your connection details.") % session_data['server']) | 118 _dialog.addText( |
100 return {'xmlui': _dialog.toXml()} | 119 D_("Can't connect to %s. Please check your connection details.") |
101 session_data['count'] += 1 | 120 % session_data["server"] |
102 counter = ' (%d)' % session_data['count'] if session_data['count'] > 1 else '' | 121 ) |
103 title = D_('XMPP password for %(profile)s%(counter)s') % {'profile': profile, 'counter': counter} | 122 return {"xmlui": _dialog.toXml()} |
104 form_ui = xml_tools.XMLUI("form", title=title, submit_id=self.__new_xmpp_passwd_id) | 123 session_data["count"] += 1 |
105 form_ui.addText(D_("Can't connect to %s. Please check your connection details or try with another password.") % session_data['server']) | 124 counter = " (%d)" % session_data["count"] if session_data["count"] > 1 else "" |
106 form_ui.addPassword('xmpp_password', value='') | 125 title = D_("XMPP password for %(profile)s%(counter)s") % { |
107 return {'xmlui': form_ui.toXml()} | 126 "profile": profile, |
127 "counter": counter, | |
128 } | |
129 form_ui = xml_tools.XMLUI( | |
130 "form", title=title, submit_id=self.__new_xmpp_passwd_id | |
131 ) | |
132 form_ui.addText( | |
133 D_( | |
134 "Can't connect to %s. Please check your connection details or try with another password." | |
135 ) | |
136 % session_data["server"] | |
137 ) | |
138 form_ui.addPassword("xmpp_password", value="") | |
139 return {"xmlui": form_ui.toXml()} | |
108 | 140 |
109 def _changeXMPPPasswordCb(self, data, profile): | 141 def _changeXMPPPasswordCb(self, data, profile): |
110 xmpp_password = data[xml_tools.formEscape('xmpp_password')] | 142 xmpp_password = data[xml_tools.formEscape("xmpp_password")] |
111 d = self.host.memory.setParam("Password", xmpp_password, "Connection", profile_key=profile) | 143 d = self.host.memory.setParam( |
144 "Password", xmpp_password, "Connection", profile_key=profile | |
145 ) | |
112 d.addCallback(lambda dummy: self.host.connect(profile)) | 146 d.addCallback(lambda dummy: self.host.connect(profile)) |
113 d.addCallback(lambda dummy: {}) | 147 d.addCallback(lambda dummy: {}) |
114 d.addErrback(lambda dummy: self._changeXMPPPassword({}, profile)) | 148 d.addErrback(lambda dummy: self._changeXMPPPassword({}, profile)) |
115 return d | 149 return d |