comparison libervia.tac @ 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 46835a4e2551
children d3437b3c88bd
comparison
equal deleted inserted replaced
159:6947450a477b 160:6f913f5adca8
16 GNU Affero General Public License for more details. 16 GNU Affero General Public License for more details.
17 17
18 You should have received a copy of the GNU Affero General Public License 18 You should have received a copy of the GNU Affero General Public License
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
22 #You need do adapt the following consts to your server
23 _REG_EMAIL_FROM = "NOREPLY@libervia.org"
24 _REG_EMAIL_SERVER = "localhost"
25 _REG_ADMIN_EMAIL = "goffi@goffi.org"
26 _NEW_ACCOUNT_SERVER = "localhost"
27 _NEW_ACCOUNT_DOMAIN = "tazar.int"
28 _NEW_ACCOUNT_RESOURCE = "libervia"
29 21
30 from twisted.application import internet, service 22 from twisted.application import internet, service
31 from twisted.internet import glib2reactor 23 from twisted.internet import glib2reactor
32 glib2reactor.install() 24 glib2reactor.install()
33 from twisted.internet import reactor, defer 25 from twisted.internet import reactor, defer
386 - BAD REQUEST: something is wrong in the request (bad arguments, profile_key for login) 378 - BAD REQUEST: something is wrong in the request (bad arguments, profile_key for login)
387 - AUTH ERROR: either the profile or the password is wrong 379 - AUTH ERROR: either the profile or the password is wrong
388 - ALREADY WAITING: a request has already be made for this profile 380 - ALREADY WAITING: a request has already be made for this profile
389 - server.NOT_DONE_YET: the profile is being processed, the return value will be given by self._logged or self._logginError 381 - server.NOT_DONE_YET: the profile is being processed, the return value will be given by self._logged or self._logginError
390 """ 382 """
383
391 try: 384 try:
392 if request.args['submit_type'][0] == 'login': 385 if request.args['submit_type'][0] == 'login':
393 _login = request.args['login'][0] 386 _login = request.args['login'][0]
394 if _login.startswith('@'): 387 if _login.startswith('@'):
395 raise Exception('No profile_key allowed') 388 raise Exception('No profile_key allowed')
396 _pass = request.args['login_password'][0] 389 _pass = request.args['login_password'][0]
397 390
398 elif request.args['submit_type'][0] == 'register': 391 elif request.args['submit_type'][0] == 'register':
399 return self._registerNewAccount(request.args) 392 return self._registerNewAccount(request)
400 393
401 else: 394 else:
402 raise Exception('Unknown submit type') 395 raise Exception('Unknown submit type')
403 except KeyError: 396 except KeyError:
404 return "BAD REQUEST" 397 return "BAD REQUEST"
447 440
448 d = defer.Deferred() 441 d = defer.Deferred()
449 self.sat_host.bridge.asyncConnect(profile, lambda: d.callback(None), d.errback) 442 self.sat_host.bridge.asyncConnect(profile, lambda: d.callback(None), d.errback)
450 d.addCallback(_connected) 443 d.addCallback(_connected)
451 444
452 def _registerNewAccount(self, args): 445 def _registerNewAccount(self, request):
453 """Create a new account, or return error 446 """Create a new account, or return error
454 @param args: dict of args as given by the form 447 @param request: initial login request
455 @return: "REGISTRATION" in case of success""" 448 @return: "REGISTRATION" in case of success"""
456 #TODO: must be moved in SàT core 449 #TODO: must be moved in SàT core
450
457 try: 451 try:
458 profile = login = args['register_login'][0] 452 profile = login = request.args['register_login'][0]
459 password = args['register_password'][0] #FIXME: password is ignored so far 453 password = request.args['register_password'][0] #FIXME: password is ignored so far
460 email = args['email'][0] 454 email = request.args['email'][0]
461 except KeyError: 455 except KeyError:
462 return "BAD REQUEST" 456 return "BAD REQUEST"
463 if not re.match(r'^[a-z0-9_-]+$', login, re.IGNORECASE) or \ 457 if not re.match(r'^[a-z0-9_-]+$', login, re.IGNORECASE) or \
464 not re.match(r'^.+@.+\..+', email, re.IGNORECASE): 458 not re.match(r'^.+@.+\..+', email, re.IGNORECASE):
465 return "BAD REQUEST" 459 return "BAD REQUEST"
466 #_charset = [chr(i) for i in range(0x21,0x7F)] #XXX: this charset seems to have some issues with openfire 460
467 _charset = [chr(i) for i in range(0x30,0x3A) + range(0x41,0x5B) + range (0x61,0x7B)] 461 def registered(result):
468 import random 462 request.write('REGISTRATION')
469 random.seed() 463 request.finish()
470 password = ''.join([random.choice(_charset) for i in range(15)]) 464 #import pudb
471 465 #pudb.set_trace()
472 if login in self.sat_host.bridge.getProfilesList(): #FIXME: must use a deferred + create a new profile check method 466
473 return "ALREADY EXISTS" 467 def registeringError(failure):
474 468 reason = str(failure.value)
475 #we now create the profile 469 if reason == "CONFLICT":
476 self.sat_host.bridge.createProfile(login) 470 request.write('ALREADY EXISTS')
477 #FIXME: values must be in a config file instead of hardcoded 471 elif reason == "INTERNAL":
478 self.sat_host.bridge.setParam("JabberID", "%s@%s/%s" % (login, _NEW_ACCOUNT_DOMAIN, _NEW_ACCOUNT_RESOURCE), "Connection", profile) 472 request.write('INTERNAL')
479 self.sat_host.bridge.setParam("Server", _NEW_ACCOUNT_SERVER, "Connection", profile) 473 else:
480 self.sat_host.bridge.setParam("Password", password, "Connection", profile) 474 #import pdb
481 #and the account 475 #pdb.set_trace()
482 action_id = self.sat_host.bridge.registerNewAccount(login, password, email, _NEW_ACCOUNT_DOMAIN, 5222) 476
483 self.sat_host.action_handler.waitForId(self._postAccountCreation, action_id, profile) 477 error('Unknown registering error: %s' % (reason,))
484 478 request.write('Unknown error (%s)' % reason)
485 #time to send the email 479 request.finish()
486 480
487 _email_host = _REG_EMAIL_SERVER 481 d = defer.Deferred()
488 _email_from = _REG_EMAIL_FROM 482 self.sat_host.bridge.registerSatAccount(email, password, profile, lambda: d.callback(None), d.errback)
489 483 d.addCallback(registered)
490 def email_ok(ignore): 484 d.addErrback(registeringError)
491 print ("Account creation email sent to %s" % email) 485 return server.NOT_DONE_YET
492
493 def email_ko(ignore):
494 #TODO: return error code to user
495 error ("Failed to send email to %s" % email)
496
497 body = (u"""Welcome to Libervia, a Salut à Toi project part
498
499 /!\\ WARNING, THIS IS ONLY A TECHNICAL DEMO, DON'T USE THIS ACCOUNT FOR ANY SERIOUS PURPOSE /!\\
500
501 Here are your connection informations:
502 login: %(login)s
503 password: %(password)s
504
505 Your Jabber ID (JID) is: %(jid)s
506
507 Any feedback welcome
508
509 Cheers
510 Goffi""" % { 'login': login, 'password': password, 'jid':"%s@%s" % (login, _NEW_ACCOUNT_DOMAIN) }).encode('utf-8')
511 msg = MIMEText(body, 'plain', 'UTF-8')
512 msg['Subject'] = 'Libervia account created'
513 msg['From'] = _email_from
514 msg['To'] = email
515
516 d = sendmail(_email_host, _email_from, email, msg.as_string())
517 d.addCallbacks(email_ok, email_ko)
518
519 #email to the administrator
520
521 body = (u"""New account created: %(login)s [%(email)s]""" % { 'login': login, 'email': email }).encode('utf-8')
522 msg = MIMEText(body, 'plain', 'UTF-8')
523 msg['Subject'] = 'Libervia new account created'
524 msg['From'] = _email_from
525 msg['To'] = _REG_ADMIN_EMAIL
526
527 d = sendmail(_email_host, _email_from, _REG_ADMIN_EMAIL, msg.as_string())
528 d.addCallbacks(email_ok, email_ko)
529 return "REGISTRATION"
530 486
531 def __cleanWaiting(self, login): 487 def __cleanWaiting(self, login):
532 """Remove login from waiting queue""" 488 """Remove login from waiting queue"""
533 try: 489 try:
534 del self.profiles_waiting[login] 490 del self.profiles_waiting[login]