mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-27 13:34:28 +00:00
Added basic login support
This commit is contained in:
parent
a74a371579
commit
1564335792
2 changed files with 52 additions and 0 deletions
18
basicbot.py
18
basicbot.py
|
@ -7,6 +7,8 @@ import async_timeout
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import royalbotconfig
|
import royalbotconfig
|
||||||
import json
|
import json
|
||||||
|
import database
|
||||||
|
|
||||||
|
|
||||||
b = telegram.Bot(royalbotconfig.telegram_token)
|
b = telegram.Bot(royalbotconfig.telegram_token)
|
||||||
|
|
||||||
|
@ -107,9 +109,25 @@ Sintassi: `/discord <messaggio>`"""
|
||||||
await update.message.reply(bot, "Richiesta inviata.")
|
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 <username> <password>`"""
|
||||||
|
if len(arguments) != 2:
|
||||||
|
await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/login <username> <password>`")
|
||||||
|
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__":
|
if __name__ == "__main__":
|
||||||
b.commands["leggi"] = leggi
|
b.commands["leggi"] = leggi
|
||||||
b.commands["diario"] = diario
|
b.commands["diario"] = diario
|
||||||
b.commands["discord"] = discord
|
b.commands["discord"] = discord
|
||||||
|
b.commands["login"] = login
|
||||||
b.commands["help"] = help
|
b.commands["help"] = help
|
||||||
b.run()
|
b.run()
|
34
database.py
34
database.py
|
@ -1,6 +1,7 @@
|
||||||
from sqlalchemy import create_engine, Column, Integer, String, Boolean
|
from sqlalchemy import create_engine, Column, Integer, String, Boolean
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
import bcrypt
|
||||||
|
|
||||||
# Initialize the database
|
# Initialize the database
|
||||||
engine = create_engine("sqlite:///tempdb.sqlite")
|
engine = create_engine("sqlite:///tempdb.sqlite")
|
||||||
|
@ -15,4 +16,37 @@ class Member(Base):
|
||||||
password = Column(String, nullable=False)
|
password = Column(String, nullable=False)
|
||||||
royal = Column(Boolean, nullable=False)
|
royal = Column(Boolean, nullable=False)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"{self.id} - {self.username}"
|
||||||
|
|
||||||
Base.metadata.create_all(engine)
|
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
|
||||||
|
|
Loading…
Reference in a new issue