From 15643357924f82590974bbefbd2aa0075275a143 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 9 Mar 2017 15:52:02 +0100 Subject: [PATCH] Added basic login support --- basicbot.py | 18 ++++++++++++++++++ database.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/basicbot.py b/basicbot.py index 8161e0f8..ae7429f5 100644 --- a/basicbot.py +++ b/basicbot.py @@ -7,6 +7,8 @@ import async_timeout import aiohttp import royalbotconfig import json +import database + b = telegram.Bot(royalbotconfig.telegram_token) @@ -107,9 +109,25 @@ Sintassi: `/discord `""" await update.message.reply(bot, "Richiesta inviata.") +# TODO: delete me +async def login(bot, update, arguments): + """Fai il login usando i dati del database Royal Games. + +Sintassi: `/login `""" + if len(arguments) != 2: + await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/login `") + return + logged_user = database.login(arguments[0], arguments[1]) + if logged_user is not None: + await update.message.chat.send_message(bot, f"Login riuscito: {logged_user}") + else: + await update.message.chat.send_message(bot, "⚠ Username o password non validi.") + + if __name__ == "__main__": b.commands["leggi"] = leggi b.commands["diario"] = diario b.commands["discord"] = discord + b.commands["login"] = login b.commands["help"] = help b.run() \ No newline at end of file diff --git a/database.py b/database.py index bc3b28be..1053901e 100644 --- a/database.py +++ b/database.py @@ -1,6 +1,7 @@ from sqlalchemy import create_engine, Column, Integer, String, Boolean from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base +import bcrypt # Initialize the database engine = create_engine("sqlite:///tempdb.sqlite") @@ -15,4 +16,37 @@ class Member(Base): password = Column(String, nullable=False) royal = Column(Boolean, nullable=False) + def __str__(self): + return f"{self.id} - {self.username}" + Base.metadata.create_all(engine) + +def create_member(username, password, royal=False): + """Create a new member and add it to the database.""" + # Create a new session + session = Session() + # Hash the password with bcrypt + hashed_password = bcrypt.hashpw(password.encode("utf8"), bcrypt.gensalt()) + # Create a new member + new_member = Member(username=username, password=hashed_password, royal=royal) + # Add the newly created member to the session + session.add(new_member) + # Commit the changes + session.commit() + +def login(username, password): + """Try to login using the database password.""" + # Create a new session + session = Session() + # Find the matching user + users = session.query(Member).filter(Member.username == username).all() + # No user with a matching username found + if len(users) == 0: + return None + else: + db_user = users[0] + # Test the password and return the user if successful + if bcrypt.hashpw(password.encode("utf8"), db_user.password) == db_user.password: + return db_user + else: + return None