Update modules/auth.py
Browse files- modules/auth.py +12 -6
modules/auth.py
CHANGED
|
@@ -4,6 +4,7 @@ from azure.cosmos import CosmosClient, exceptions
|
|
| 4 |
import bcrypt
|
| 5 |
import base64
|
| 6 |
|
|
|
|
| 7 |
def clean_and_validate_key(key):
|
| 8 |
key = key.strip()
|
| 9 |
while len(key) % 4 != 0:
|
|
@@ -34,16 +35,18 @@ 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 |
|
|
|
|
| 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 |
-
|
| 46 |
-
|
| 47 |
try:
|
| 48 |
query = f"SELECT * FROM c WHERE c.id = '{username}'"
|
| 49 |
existing_user = list(container.query_items(query=query, enable_cross_partition_query=True))
|
|
@@ -54,20 +57,19 @@ def register_user(username, password, role, additional_info=None):
|
|
| 54 |
new_user = {
|
| 55 |
'id': username,
|
| 56 |
'password': hash_password(password),
|
| 57 |
-
'role':
|
| 58 |
'additional_info': additional_info or {}
|
| 59 |
}
|
| 60 |
|
| 61 |
-
# Modificamos esta línea para incluir el partition_key en el cuerpo del documento
|
| 62 |
new_user['partitionKey'] = username
|
| 63 |
|
| 64 |
-
# Ahora creamos el item sin especificar partition_key como un argumento separado
|
| 65 |
container.create_item(body=new_user)
|
| 66 |
return True
|
| 67 |
except exceptions.CosmosHttpResponseError as e:
|
| 68 |
print(f"Error al registrar usuario: {str(e)}")
|
| 69 |
return False
|
| 70 |
|
|
|
|
| 71 |
def authenticate_user(username, password):
|
| 72 |
"""Authenticate a user."""
|
| 73 |
try:
|
|
@@ -83,6 +85,8 @@ def authenticate_user(username, password):
|
|
| 83 |
|
| 84 |
return False
|
| 85 |
|
|
|
|
|
|
|
| 86 |
def get_user_role(username):
|
| 87 |
"""Get the role of a user."""
|
| 88 |
try:
|
|
@@ -96,6 +100,7 @@ def get_user_role(username):
|
|
| 96 |
|
| 97 |
return None
|
| 98 |
|
|
|
|
| 99 |
def update_user_info(username, new_info):
|
| 100 |
"""Update user information."""
|
| 101 |
try:
|
|
@@ -112,6 +117,7 @@ def update_user_info(username, new_info):
|
|
| 112 |
|
| 113 |
return False
|
| 114 |
|
|
|
|
| 115 |
def delete_user(username):
|
| 116 |
"""Delete a user."""
|
| 117 |
try:
|
|
|
|
| 4 |
import bcrypt
|
| 5 |
import base64
|
| 6 |
|
| 7 |
+
################################################################################################################
|
| 8 |
def clean_and_validate_key(key):
|
| 9 |
key = key.strip()
|
| 10 |
while len(key) % 4 != 0:
|
|
|
|
| 35 |
print(f"Error al conectar con Cosmos DB: {str(e)}")
|
| 36 |
raise
|
| 37 |
|
| 38 |
+
#############################################################################################################3
|
| 39 |
def hash_password(password):
|
| 40 |
"""Hash a password for storing."""
|
| 41 |
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
| 42 |
|
| 43 |
+
################################################################################################################
|
| 44 |
def verify_password(stored_password, provided_password):
|
| 45 |
"""Verify a stored password against one provided by user"""
|
| 46 |
return bcrypt.checkpw(provided_password.encode('utf-8'), stored_password.encode('utf-8'))
|
| 47 |
+
|
| 48 |
+
################################################################################################################
|
| 49 |
+
def register_user(username, password, additional_info=None):
|
| 50 |
try:
|
| 51 |
query = f"SELECT * FROM c WHERE c.id = '{username}'"
|
| 52 |
existing_user = list(container.query_items(query=query, enable_cross_partition_query=True))
|
|
|
|
| 57 |
new_user = {
|
| 58 |
'id': username,
|
| 59 |
'password': hash_password(password),
|
| 60 |
+
'role': 'Estudiante',
|
| 61 |
'additional_info': additional_info or {}
|
| 62 |
}
|
| 63 |
|
|
|
|
| 64 |
new_user['partitionKey'] = username
|
| 65 |
|
|
|
|
| 66 |
container.create_item(body=new_user)
|
| 67 |
return True
|
| 68 |
except exceptions.CosmosHttpResponseError as e:
|
| 69 |
print(f"Error al registrar usuario: {str(e)}")
|
| 70 |
return False
|
| 71 |
|
| 72 |
+
################################################################################################################
|
| 73 |
def authenticate_user(username, password):
|
| 74 |
"""Authenticate a user."""
|
| 75 |
try:
|
|
|
|
| 85 |
|
| 86 |
return False
|
| 87 |
|
| 88 |
+
|
| 89 |
+
################################################################################################################
|
| 90 |
def get_user_role(username):
|
| 91 |
"""Get the role of a user."""
|
| 92 |
try:
|
|
|
|
| 100 |
|
| 101 |
return None
|
| 102 |
|
| 103 |
+
################################################################################################################
|
| 104 |
def update_user_info(username, new_info):
|
| 105 |
"""Update user information."""
|
| 106 |
try:
|
|
|
|
| 117 |
|
| 118 |
return False
|
| 119 |
|
| 120 |
+
################################################################################################################
|
| 121 |
def delete_user(username):
|
| 122 |
"""Delete a user."""
|
| 123 |
try:
|