Start migration to postgresql and other minor fixes and improvements

This commit is contained in:
Lucas Mathews
2024-06-08 20:59:00 +02:00
parent 43ffea728c
commit c79f54eda7
16 changed files with 59 additions and 330 deletions

View File

@@ -20,7 +20,7 @@ from manager import log_event # Imports the log_event function from the manager
def create_app():
"""Creates the API using Connexion."""
app = connexion.FlaskApp(__name__)
app.add_api(CONFIG["api_file"]["name"])
app.add_api(CONFIG["server"]["api_file"])
flask_app = app.app
flask_app.config['SECRET_KEY'] = CONFIG["sessions"]["secret_key"]
@@ -34,7 +34,7 @@ def API():
app = create_app()
debug_value = CONFIG["server"]["debug"]
debug = False if debug_value.lower() == 'false' else True
app.run(host=CONFIG["server"]["listen_ip"], port=CONFIG["server"]["port"], debug=debug)
app.run(host=CONFIG["server"]["url"], debug=debug)
################
### Run Code ###

View File

@@ -1,26 +1,18 @@
# Lucas Mathews - Fontys Student ID: 5023572
# Banking System Manager File
import os.path
from config import CONFIG # Import Config
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from config import CONFIG # Import Config
db_url : str = f"{CONFIG['database']['type']}://{CONFIG['database']['user']}:{CONFIG['database']['password']}@{CONFIG['database']['ip']}:{CONFIG['database']['port']}/{CONFIG['database']['name']}"
print(f"Database URL set to: {db_url}")
if os.path.exists(CONFIG["database"]["name"]): # Check if the database exists
print(f"Database {CONFIG["database"]["name"]} already exists.")
else:
print(f"Database {CONFIG["database"]["name"]} does not exist. Creating it now.")
db_url : str = "sqlite:///" + CONFIG["database"]["name"] # Sets the database file to be used from the configuration file
print(f"Database file set to: {db_url}")
engine = create_engine(db_url, echo=True) # Creates the database engine (does not create the database file if it already exists)
engine = create_engine(db_url, echo=True) # Creates the database engine
from class_base import Base # Imports the base class required by SQLAlchemy
Base.metadata.create_all(bind=engine) # Creates the tables in the database from the classes
Session = sessionmaker(bind=engine) # Creates a session to interact with the database
session = Session() # Creates a session object
session = Session() # Creates a session object

View File

@@ -68,6 +68,8 @@ def get_current_client():
def verify_otp(client_id:str, otp:int):
"""Verifies a one time password for a client. Returns True if the OTP is correct and False otherwise."""
if CONFIG["smtp"]["true"] == "False":
return True
if client_id in otps:
stored_otp, creation_time = otps[client_id]
if stored_otp == otp and time.time() - creation_time <= 300: # Check if OTP is within 5 minutes
@@ -150,10 +152,11 @@ def admin_required(f):
@login_required
def generate_otp(client_id: str):
"""Generates a one-time password for a client and sends it to their email address. Returns a success message if the OTP is generated and an error message otherwise."""
if CONFIG["smtp"]["true"] == "False":
return format_response(True, "OTP generation disabled as SMTP is not enabled."), 200
current_client_id, is_admin = get_current_client()
if not is_admin and client_id != current_client_id:
return format_response(False, "You can only generate OTPs for your own client account."), 403
email = get_email(client_id)
if email:
password = int(random.randint(100000, 999999)) # Generate a 6-digit OTP

View File

@@ -1,15 +1,16 @@
[database]
name=bank.db
[api_file]
name=api.yml
[server]
url=http://0.0.0.0:80/
api_file=api.yml
debug=False
scheduler=True
[api]
url=http://0.0.0.0:81/
[database]
type=postgresql
ip=0.0.0.0
port=5432
name=bank
user=root
password=
[sessions]
secret_key=
@@ -17,8 +18,8 @@ secret_key=
[smtp]
enabled=True
host=
port=
port=465
username=
password=
sender_name=Luxbank
sender_email=
sender_email=bank@domain.com