Update modules/auth.py
Browse files- modules/auth.py +16 -8
modules/auth.py
CHANGED
|
@@ -3,6 +3,7 @@ from azure.cosmos import CosmosClient, exceptions
|
|
| 3 |
import bcrypt
|
| 4 |
import base64
|
| 5 |
|
|
|
|
| 6 |
def clean_and_validate_key(key):
|
| 7 |
key = key.strip()
|
| 8 |
while len(key) % 4 != 0:
|
|
@@ -32,15 +33,15 @@ try:
|
|
| 32 |
except Exception as e:
|
| 33 |
print(f"Error al conectar con Cosmos DB: {str(e)}")
|
| 34 |
raise
|
| 35 |
-
|
| 36 |
def hash_password(password):
|
| 37 |
"""Hash a password for storing."""
|
| 38 |
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
| 39 |
-
|
| 40 |
def verify_password(stored_password, provided_password):
|
| 41 |
"""Verify a stored password against one provided by user"""
|
| 42 |
return bcrypt.checkpw(provided_password.encode('utf-8'), stored_password.encode('utf-8'))
|
| 43 |
-
|
| 44 |
def register_user(username, password, additional_info=None):
|
| 45 |
try:
|
| 46 |
query = f"SELECT * FROM c WHERE c.id = '{username}'"
|
|
@@ -63,7 +64,7 @@ def register_user(username, password, additional_info=None):
|
|
| 63 |
except exceptions.CosmosHttpResponseError as e:
|
| 64 |
print(f"Error al registrar usuario: {str(e)}")
|
| 65 |
return False
|
| 66 |
-
|
| 67 |
def authenticate_user(username, password):
|
| 68 |
"""Authenticate a user."""
|
| 69 |
try:
|
|
@@ -73,16 +74,22 @@ def authenticate_user(username, password):
|
|
| 73 |
if results:
|
| 74 |
stored_user = results[0]
|
| 75 |
if verify_password(stored_user['password'], password):
|
| 76 |
-
return True
|
| 77 |
-
except exceptions.CosmosHttpResponseError:
|
| 78 |
-
|
| 79 |
|
| 80 |
-
return False
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
def get_user_role(username):
|
| 83 |
"""Get the role of a user."""
|
| 84 |
return "Estudiante" # Siempre devuelve "Estudiante" ya que es el único perfil
|
| 85 |
|
|
|
|
| 86 |
def update_user_info(username, new_info):
|
| 87 |
"""Update user information."""
|
| 88 |
try:
|
|
@@ -99,6 +106,7 @@ def update_user_info(username, new_info):
|
|
| 99 |
|
| 100 |
return False
|
| 101 |
|
|
|
|
| 102 |
def delete_user(username):
|
| 103 |
"""Delete a user."""
|
| 104 |
try:
|
|
|
|
| 3 |
import bcrypt
|
| 4 |
import base64
|
| 5 |
|
| 6 |
+
##################################################################################################
|
| 7 |
def clean_and_validate_key(key):
|
| 8 |
key = key.strip()
|
| 9 |
while len(key) % 4 != 0:
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
print(f"Error al conectar con Cosmos DB: {str(e)}")
|
| 35 |
raise
|
| 36 |
+
##############################################################################################################
|
| 37 |
def hash_password(password):
|
| 38 |
"""Hash a password for storing."""
|
| 39 |
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
| 40 |
+
##################################################################################33
|
| 41 |
def verify_password(stored_password, provided_password):
|
| 42 |
"""Verify a stored password against one provided by user"""
|
| 43 |
return bcrypt.checkpw(provided_password.encode('utf-8'), stored_password.encode('utf-8'))
|
| 44 |
+
######################################################################################################
|
| 45 |
def register_user(username, password, additional_info=None):
|
| 46 |
try:
|
| 47 |
query = f"SELECT * FROM c WHERE c.id = '{username}'"
|
|
|
|
| 64 |
except exceptions.CosmosHttpResponseError as e:
|
| 65 |
print(f"Error al registrar usuario: {str(e)}")
|
| 66 |
return False
|
| 67 |
+
#########################################################################################
|
| 68 |
def authenticate_user(username, password):
|
| 69 |
"""Authenticate a user."""
|
| 70 |
try:
|
|
|
|
| 74 |
if results:
|
| 75 |
stored_user = results[0]
|
| 76 |
if verify_password(stored_user['password'], password):
|
| 77 |
+
return True, stored_user.get('role', 'estudiante') # Retorna True y el rol (por defecto 'estudiante')
|
| 78 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 79 |
+
print(f"Error during authentication: {str(e)}")
|
| 80 |
|
| 81 |
+
return False, None
|
| 82 |
|
| 83 |
+
def verify_password(stored_password, provided_password):
|
| 84 |
+
"""Verify a stored password against one provided by user"""
|
| 85 |
+
return bcrypt.checkpw(provided_password.encode('utf-8'), stored_password.encode('utf-8'))
|
| 86 |
+
|
| 87 |
+
########################################################################################################################
|
| 88 |
def get_user_role(username):
|
| 89 |
"""Get the role of a user."""
|
| 90 |
return "Estudiante" # Siempre devuelve "Estudiante" ya que es el único perfil
|
| 91 |
|
| 92 |
+
########################################################################################################################
|
| 93 |
def update_user_info(username, new_info):
|
| 94 |
"""Update user information."""
|
| 95 |
try:
|
|
|
|
| 106 |
|
| 107 |
return False
|
| 108 |
|
| 109 |
+
########################################################################################################################
|
| 110 |
def delete_user(username):
|
| 111 |
"""Delete a user."""
|
| 112 |
try:
|