Mercurial > libervia-backend
comparison src/stdui/ui_profile_manager.py @ 1043:066308706dc6
core, stdui (profile manager), quick_app: ask for another XMPP password when profile authentication succeed but XMPP connection fails
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 21 May 2014 23:15:01 +0200 |
parents | b262ae6d53af |
children | aa15453ec54d |
comparison
equal
deleted
inserted
replaced
1042:59de0c7a28ec | 1043:066308706dc6 |
---|---|
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.tools import xml_tools | 23 from sat.tools import xml_tools |
24 from sat.memory.crypto import PasswordHasher | 24 from sat.memory.crypto import PasswordHasher |
25 from sat.memory.memory import ProfileSessions | |
25 from twisted.internet import defer | 26 from twisted.internet import defer |
26 | 27 |
27 | 28 |
28 class ProfileManager(object): | 29 class ProfileManager(object): |
29 """Manage profiles.""" | 30 """Manage profiles.""" |
30 | 31 |
31 def __init__(self, host): | 32 def __init__(self, host): |
32 self.host = host | 33 self.host = host |
33 self.profile_ciphers = {} | 34 self.profile_ciphers = {} |
35 self._sessions = ProfileSessions() | |
34 host.registerCallback(self._authenticateProfile, force_id=C.AUTHENTICATE_PROFILE_ID, with_data=True) | 36 host.registerCallback(self._authenticateProfile, force_id=C.AUTHENTICATE_PROFILE_ID, with_data=True) |
37 host.registerCallback(self._changeXMPPPassword, force_id=C.CHANGE_XMPP_PASSWD_ID, with_data=True) | |
38 self.__new_xmpp_passwd_id = host.registerCallback(self._changeXMPPPasswordCb, with_data=True) | |
35 | 39 |
36 def _authenticateProfile(self, data, profile): | 40 def _authenticateProfile(self, data, profile): |
37 """Get the data/dialog for connecting a profile | 41 """Get the data/dialog for connecting a profile |
38 | 42 |
39 @param data (dict) | 43 @param data (dict) |
94 profile_password = data[xml_tools.formEscape('profile_password')] | 98 profile_password = data[xml_tools.formEscape('profile_password')] |
95 except KeyError: | 99 except KeyError: |
96 profile_password = data['profile_password'] # not received from a user input | 100 profile_password = data['profile_password'] # not received from a user input |
97 verified = yield PasswordHasher.verify(profile_password, self.profile_ciphers[profile]) | 101 verified = yield PasswordHasher.verify(profile_password, self.profile_ciphers[profile]) |
98 if not verified: | 102 if not verified: |
99 _dialog = xml_tools.XMLUI('popup', title=D_('Error')) | 103 _dialog = xml_tools.XMLUI('popup', title=D_('Connection error')) |
100 _dialog.addText(_("The provided profile password doesn't match.")) | 104 _dialog.addText(D_("The provided profile password doesn't match.")) |
101 defer.returnValue({'xmlui': _dialog.toXml()}) | 105 defer.returnValue({'xmlui': _dialog.toXml()}) |
102 | 106 |
103 yield self.host.memory.newAuthSession(profile_password, profile) | 107 yield self.host.memory.newAuthSession(profile_password, profile) |
104 defer.returnValue({'authenticated_profile': profile, 'caller': data['caller']}) | 108 defer.returnValue({'authenticated_profile': profile, 'caller': data['caller']}) |
109 | |
110 def _changeXMPPPassword(self, data, profile): | |
111 session_data = self._sessions.profileGetUnique(profile) | |
112 if not session_data: | |
113 server = self.host.memory.getParamA("Server", "Connection", profile_key=profile) | |
114 session_id, session_data = self._sessions.newSession({'count': 0, 'server': server}, profile) | |
115 if session_data['count'] > 2: # 3 attempts with a new password after the initial try | |
116 self._sessions.profileDelUnique(profile) | |
117 _dialog = xml_tools.XMLUI('popup', title=D_('Connection error')) | |
118 _dialog.addText(D_("Can't connect to %s. Please check your connection details.") % session_data['server']) | |
119 return {'xmlui': _dialog.toXml()} | |
120 session_data['count'] += 1 | |
121 counter = ' (%d)' % session_data['count'] if session_data['count'] > 1 else '' | |
122 title = D_('XMPP password for %(profile)s%(counter)s') % {'profile': profile, 'counter': counter} | |
123 form_ui = xml_tools.XMLUI("form", title=title, submit_id=self.__new_xmpp_passwd_id) | |
124 form_ui.addText(D_("Can't connect to %s. Please check your connection details or try with another password.") % session_data['server']) | |
125 form_ui.addPassword('xmpp_password', value='') | |
126 return {'xmlui': form_ui.toXml()} | |
127 | |
128 def _changeXMPPPasswordCb(self, data, profile): | |
129 xmpp_password = data[xml_tools.formEscape('xmpp_password')] | |
130 d = self.host.memory.setParam("Password", xmpp_password, "Connection", profile_key=profile) | |
131 d.addCallback(lambda dummy: self.host.asyncConnect(profile)) | |
132 return {} |