Switched to dict for database access, Made progress on jwt revoking

This commit is contained in:
Thastertyn committed 2024-03-05 21:35:58 +01:00
1 parent b4ecbeaa37
commit 39d69ee0ca
33 files changed
+26 -28

No files matched your search

+1
View File
@@ -1 +1,2 @@
.env .env
**/__pycache__/
+4
View File
@@ -1,6 +1,10 @@
{ {
"cSpell.words": [ "cSpell.words": [
"blocklist",
"dotenv", "dotenv",
"gensalt",
"hashpw",
"checkpw",
"jsonify" "jsonify"
] ]
} }
+3 -3
View File
@@ -1,10 +1,10 @@
from flask import Flask from flask import Flask
from flask_jwt_extended import JWTManager from flask_jwt_extended import JWTManager
def create_app(): app = Flask(__name__)
app = Flask(__name__) jwt_manager = JWTManager(app)
jwt = JWTManager(app)
def create_app():
from app.api import bp, bp_errors, bp_product, bp_user from app.api import bp, bp_errors, bp_product, bp_user
app.register_blueprint(bp) app.register_blueprint(bp)
app.register_blueprint(bp_errors) app.register_blueprint(bp_errors)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -12,7 +12,7 @@ db_connection = mysql.connector.connect(
database=MySqlConfig.MYSQL_DATABASE, database=MySqlConfig.MYSQL_DATABASE,
) )
db_cursor = db_connection.cursor() db_cursor = db_connection.cursor(dictionary=True)
jwt_redis_blocklist = redis.StrictRedis( jwt_redis_blocklist = redis.StrictRedis(
host=RedisConfig.REDIS_HOST, host=RedisConfig.REDIS_HOST,
+1 -4
View File
@@ -1,9 +1,6 @@
from app.extensions import jwt_redis_blocklist from app.extensions import jwt_redis_blocklist
from flask_jwt_extended import create_access_token from . import jwt_manager
from flask_jwt_extended import get_jwt
from flask_jwt_extended import jwt_required
from flask_jwt_extended import JWTManager
@jwt.token_in_blocklist_loader @jwt.token_in_blocklist_loader
def check_if_token_is_revoked(jwt_header, jwt_payload: dict) -> bool: def check_if_token_is_revoked(jwt_header, jwt_payload: dict) -> bool:
+10 -14
View File
@@ -8,30 +8,30 @@ class ProductService:
def get_name(product_id: int): def get_name(product_id: int):
cursor.execute(f"select name from product where product.product_id = {product_id}") cursor.execute(f"select name from product where product.product_id = {product_id}")
result = cursor.fetchone() result = cursor.fetchone()
return result[0] return result['name']
@staticmethod @staticmethod
def get_manufacturer(product_id: int): def get_manufacturer(product_id: int):
cursor.execute(f"select manufacturer from product where product.product_id = {product_id}") cursor.execute(f"select manufacturer from product where product.product_id = {product_id}")
result = cursor.fetchone() result = cursor.fetchone()
return result[0] return result['manufacturer']
@staticmethod @staticmethod
def get_price(product_id: int): def get_price(product_id: int):
cursor.execute(f"select price_pc from product where product.product_id = {product_id}") cursor.execute(f"select price_pc from product where product.product_id = {product_id}")
result = cursor.fetchone() result = cursor['price_pc']
return result[0] return result[0]
@staticmethod @staticmethod
def get_image(product_id: int): def get_image(product_id: int):
cursor.execute(f"select image from product where product.product_id = {product_id}") cursor.execute(f"select image from product where product.product_id = {product_id}")
result = cursor.fetchone() result = cursor['image']
return base64.b64encode(result[0]).decode('utf-8') return base64.b64encode(result[0]).decode('utf-8')
@staticmethod @staticmethod
def get_image_name(product_id: int): def get_image_name(product_id: int):
cursor.execute(f"select image_name from product where product.product_id = {product_id}") cursor.execute(f"select image_name from product where product.product_id = {product_id}")
result = cursor.fetchone() result = cursor['image_name']
return result[0] return result[0]
@staticmethod @staticmethod
@@ -40,13 +40,9 @@ class ProductService:
result = cursor.fetchone() result = cursor.fetchone()
return { return {
"name": result[0], "name": result['name'],
"manufacturer": result[1], "manufacturer": result['manufacturer'],
"price": result[2], "price": result['price_pc'],
"image_name": result[3], "image_name": result['image_name'],
"image": base64.b64encode(result[4]).decode('utf-8') "image": base64.b64encode(result['image']).decode('utf-8')
} }
@staticmethod
def create_user(username: str, email: str, password: str):
print("asd")
+6 -6
View File
@@ -31,11 +31,11 @@ class UserService:
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
try: try:
db_cursor.execute("select max(user_id) from user") db_cursor.execute("select max(user_id) as max_id from user")
last_id = db_cursor.fetchone()[0] last_id = db_cursor.fetchone()['max_id']
if last_id < 23000: if last_id < 23000:
return {"Failed": "Error occured when fetching last user id"} return {"Failed": "Error occurred when fetching last user id"}
new_id = last_id + 1 new_id = last_id + 1
@@ -53,9 +53,9 @@ class UserService:
db_cursor.execute("select user_id, password, last_change from user where username = %s", (username,)) db_cursor.execute("select user_id, password, last_change from user where username = %s", (username,))
result = db_cursor.fetchone() result = db_cursor.fetchone()
user_id = result[0] user_id = result['user_id']
password_hash = result[1] password_hash = result['password']
last_change = result[2] last_change = result['last_change']
if user_id is None: if user_id is None:
return {"Failed": "Username not found"}, 400 return {"Failed": "Username not found"}, 400