diff --git a/learnapp.py b/learnapp.py index 6296332..8e17b6e 100644 --- a/learnapp.py +++ b/learnapp.py @@ -29,12 +29,16 @@ class LearnAppException(Exception): # ============================================================================ # helper functions # ============================================================================ -async def check_password(try_pw, pw): +async def _bcrypt_hash(a, b): loop = asyncio.get_running_loop() - return pw == await loop.run_in_executor(None, bcrypt.hashpw, try_pw.encode('utf-8'), pw) + return await loop.run_in_executor(None, bcrypt.hashpw, a.encode('utf-8'), b) + +async def check_password(try_pw, pw): + return pw == await _bcrypt_hash(try_pw, pw) + +async def bcrypt_hash_gen(new_pw): + return await _bcrypt_hash(new_pw, bcrypt.gensalt()) -def bcrypt_hash_gen(pw): - return bcrypt.hashpw(pw.encode('utf-8'), bcrypt.gensalt()) # ============================================================================ # LearnApp - application logic @@ -115,14 +119,13 @@ class LearnApp(object): logger.info(f'User "{uid}" logged out') # ------------------------------------------------------------------------ - # change_password + # change_password. returns True if password is successfully changed. # ------------------------------------------------------------------------ async def change_password(self, uid, pw): if not pw: return False - loop = asyncio.get_running_loop() - pw = await loop.run_in_executor(None, bcrypt_hash_gen, pw) + pw = await bcrypt_hash_gen(pw) with self.db_session() as s: u = s.query(Student).get(uid) diff --git a/serve.py b/serve.py index 43ca9ca..dcd93ed 100755 --- a/serve.py +++ b/serve.py @@ -16,7 +16,6 @@ import functools import tornado.ioloop import tornado.web import tornado.httpserver -# from tornado import iostream # this project from learnapp import LearnApp @@ -34,6 +33,7 @@ def admin_only(func): func(self, *args, **kwargs) return wrapper + # ============================================================================ # WebApplication - Tornado Web Server # ============================================================================ @@ -45,7 +45,6 @@ class WebApplication(tornado.web.Application): (r'/change_password', ChangePasswordHandler), (r'/question', QuestionHandler), # renders each question (r'/topic/(.+)', TopicHandler), # page for exercising a topic - # (r'/learn/(.+)', LearnHandler), # page for learning a topic (r'/file/(.+)', FileHandler), # serve files, images, etc (r'/', RootHandler), # show list of topics ] @@ -159,24 +158,6 @@ class TopicHandler(BaseHandler): name=self.learn.get_student_name(uid), ) -# class LearnHandler(BaseHandler): -# @tornado.web.authenticated -# def get(self, topic): -# uid = self.current_user - -# try: -# ok = self.learn.start_learning(uid, topic) -# except KeyError: -# self.redirect('/') -# else: -# if ok: -# self.render('learn.html', -# uid=uid, -# name=self.learn.get_student_name(uid), -# ) -# else: -# self.redirect('/') - # ---------------------------------------------------------------------------- # Serves files from the /public subdir of the topics. @@ -185,8 +166,6 @@ class TopicHandler(BaseHandler): class FileHandler(BaseHandler): SUPPORTED_METHODS = ['GET'] - # chunk_size = 4 * 1024 * 1024 # serve up to 4 MiB multiple times - @tornado.web.authenticated async def get(self, filename): uid = self.current_user @@ -209,18 +188,6 @@ class FileHandler(BaseHandler): self.write(f.read()) await self.flush() - # chunk = f.read(self.chunk_size) - # while chunk: - # try: - # self.write(chunk) # write the chunk to response - # await self.flush() # flush the current chunk to socket - # except iostream.StreamClosedError: - # break # client closed the connection - # finally: - # del chunk - # await asyncio.sleep(0) # 1 nanosecond (hack) - # chunk = f.read(self.chunk_size) - # ---------------------------------------------------------------------------- # respond to AJAX to get a JSON question @@ -326,6 +293,8 @@ def signal_handler(signal, frame): tornado.ioloop.IOLoop.current().stop() logging.critical('Webserver stopped.') sys.exit(0) + else: + logging.info('Abort canceled...') # ------------------------------------------------------------------------- @@ -336,8 +305,10 @@ def main(): argparser = argparse.ArgumentParser(description='Server for online learning. Enrolled students and topics have to be previously configured. Please read the documentation included with this software before running the server.') argparser.add_argument('conffile', type=str, nargs='+', help='Topics configuration file in YAML format.') # FIXME only one + argparser.add_argument('--port', type=int, default=8443, + help='Port to be used by the HTTPS server') argparser.add_argument('--debug', action='store_true', - help='Enable debug messages.') + help='Enable debug messages') arg = argparser.parse_args() # --- Setup logging @@ -380,7 +351,8 @@ def main(): logging.critical('Certificates cert.pem and privkey.pem not found') sys.exit(1) else: - http_server.listen(8443) + http_server.listen(arg.port) + logging.info(f'Listening on port {arg.port}.') # --- run webserver logging.info('Webserver running... (Ctrl-C to stop)') -- libgit2 0.21.2