Mercurial > libervia-web
changeset 160:6f913f5adca8
server side: registration refactoring first draft; main registration code is moved to SàT backend
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 07 Jan 2013 01:09:57 +0100 |
parents | 6947450a477b |
children | 03ac7f51a35b |
files | libervia.tac |
diffstat | 1 files changed, 33 insertions(+), 77 deletions(-) [+] |
line wrap: on
line diff
--- a/libervia.tac Mon Jan 07 01:04:35 2013 +0100 +++ b/libervia.tac Mon Jan 07 01:09:57 2013 +0100 @@ -19,14 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. """ -#You need do adapt the following consts to your server -_REG_EMAIL_FROM = "NOREPLY@libervia.org" -_REG_EMAIL_SERVER = "localhost" -_REG_ADMIN_EMAIL = "goffi@goffi.org" -_NEW_ACCOUNT_SERVER = "localhost" -_NEW_ACCOUNT_DOMAIN = "tazar.int" -_NEW_ACCOUNT_RESOURCE = "libervia" - from twisted.application import internet, service from twisted.internet import glib2reactor glib2reactor.install() @@ -388,6 +380,7 @@ - ALREADY WAITING: a request has already be made for this profile - server.NOT_DONE_YET: the profile is being processed, the return value will be given by self._logged or self._logginError """ + try: if request.args['submit_type'][0] == 'login': _login = request.args['login'][0] @@ -396,7 +389,7 @@ _pass = request.args['login_password'][0] elif request.args['submit_type'][0] == 'register': - return self._registerNewAccount(request.args) + return self._registerNewAccount(request) else: raise Exception('Unknown submit type') @@ -449,84 +442,47 @@ self.sat_host.bridge.asyncConnect(profile, lambda: d.callback(None), d.errback) d.addCallback(_connected) - def _registerNewAccount(self, args): + def _registerNewAccount(self, request): """Create a new account, or return error - @param args: dict of args as given by the form + @param request: initial login request @return: "REGISTRATION" in case of success""" #TODO: must be moved in SàT core + try: - profile = login = args['register_login'][0] - password = args['register_password'][0] #FIXME: password is ignored so far - email = args['email'][0] + profile = login = request.args['register_login'][0] + password = request.args['register_password'][0] #FIXME: password is ignored so far + email = request.args['email'][0] except KeyError: return "BAD REQUEST" if not re.match(r'^[a-z0-9_-]+$', login, re.IGNORECASE) or \ not re.match(r'^.+@.+\..+', email, re.IGNORECASE): return "BAD REQUEST" - #_charset = [chr(i) for i in range(0x21,0x7F)] #XXX: this charset seems to have some issues with openfire - _charset = [chr(i) for i in range(0x30,0x3A) + range(0x41,0x5B) + range (0x61,0x7B)] - import random - random.seed() - password = ''.join([random.choice(_charset) for i in range(15)]) - if login in self.sat_host.bridge.getProfilesList(): #FIXME: must use a deferred + create a new profile check method - return "ALREADY EXISTS" - - #we now create the profile - self.sat_host.bridge.createProfile(login) - #FIXME: values must be in a config file instead of hardcoded - self.sat_host.bridge.setParam("JabberID", "%s@%s/%s" % (login, _NEW_ACCOUNT_DOMAIN, _NEW_ACCOUNT_RESOURCE), "Connection", profile) - self.sat_host.bridge.setParam("Server", _NEW_ACCOUNT_SERVER, "Connection", profile) - self.sat_host.bridge.setParam("Password", password, "Connection", profile) - #and the account - action_id = self.sat_host.bridge.registerNewAccount(login, password, email, _NEW_ACCOUNT_DOMAIN, 5222) - self.sat_host.action_handler.waitForId(self._postAccountCreation, action_id, profile) - - #time to send the email - - _email_host = _REG_EMAIL_SERVER - _email_from = _REG_EMAIL_FROM - - def email_ok(ignore): - print ("Account creation email sent to %s" % email) - - def email_ko(ignore): - #TODO: return error code to user - error ("Failed to send email to %s" % email) - - body = (u"""Welcome to Libervia, a Salut à Toi project part - -/!\\ WARNING, THIS IS ONLY A TECHNICAL DEMO, DON'T USE THIS ACCOUNT FOR ANY SERIOUS PURPOSE /!\\ - -Here are your connection informations: -login: %(login)s -password: %(password)s - -Your Jabber ID (JID) is: %(jid)s - -Any feedback welcome - -Cheers -Goffi""" % { 'login': login, 'password': password, 'jid':"%s@%s" % (login, _NEW_ACCOUNT_DOMAIN) }).encode('utf-8') - msg = MIMEText(body, 'plain', 'UTF-8') - msg['Subject'] = 'Libervia account created' - msg['From'] = _email_from - msg['To'] = email - - d = sendmail(_email_host, _email_from, email, msg.as_string()) - d.addCallbacks(email_ok, email_ko) - - #email to the administrator - - body = (u"""New account created: %(login)s [%(email)s]""" % { 'login': login, 'email': email }).encode('utf-8') - msg = MIMEText(body, 'plain', 'UTF-8') - msg['Subject'] = 'Libervia new account created' - msg['From'] = _email_from - msg['To'] = _REG_ADMIN_EMAIL - - d = sendmail(_email_host, _email_from, _REG_ADMIN_EMAIL, msg.as_string()) - d.addCallbacks(email_ok, email_ko) - return "REGISTRATION" + def registered(result): + request.write('REGISTRATION') + request.finish() + #import pudb + #pudb.set_trace() + + def registeringError(failure): + reason = str(failure.value) + if reason == "CONFLICT": + request.write('ALREADY EXISTS') + elif reason == "INTERNAL": + request.write('INTERNAL') + else: + #import pdb + #pdb.set_trace() + + error('Unknown registering error: %s' % (reason,)) + request.write('Unknown error (%s)' % reason) + request.finish() + + d = defer.Deferred() + self.sat_host.bridge.registerSatAccount(email, password, profile, lambda: d.callback(None), d.errback) + d.addCallback(registered) + d.addErrback(registeringError) + return server.NOT_DONE_YET def __cleanWaiting(self, login): """Remove login from waiting queue"""