Delete modules/auth.py
Browse files- modules/auth.py +0 -126
modules/auth.py
DELETED
@@ -1,126 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
from azure.cosmos import CosmosClient, exceptions
|
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:
|
10 |
-
key += '='
|
11 |
-
try:
|
12 |
-
base64.b64decode(key)
|
13 |
-
return key
|
14 |
-
except:
|
15 |
-
raise ValueError("La clave proporcionada no es v谩lida")
|
16 |
-
|
17 |
-
# Azure Cosmos DB configuration
|
18 |
-
endpoint = os.environ.get("COSMOS_ENDPOINT")
|
19 |
-
key = os.environ.get("COSMOS_KEY")
|
20 |
-
|
21 |
-
if not endpoint or not key:
|
22 |
-
raise ValueError("Las variables de entorno COSMOS_ENDPOINT y COSMOS_KEY deben estar configuradas")
|
23 |
-
|
24 |
-
key = clean_and_validate_key(key)
|
25 |
-
|
26 |
-
try:
|
27 |
-
client = CosmosClient(endpoint, key)
|
28 |
-
database = client.get_database_client("user_database")
|
29 |
-
container = database.get_container_client("users")
|
30 |
-
# Prueba de conexi贸n
|
31 |
-
database_list = list(client.list_databases())
|
32 |
-
print(f"Conexi贸n exitosa. Bases de datos encontradas: {len(database_list)}")
|
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}'"
|
48 |
-
existing_user = list(container.query_items(query=query, enable_cross_partition_query=True))
|
49 |
-
|
50 |
-
if existing_user:
|
51 |
-
return False # User already exists
|
52 |
-
|
53 |
-
new_user = {
|
54 |
-
'id': username,
|
55 |
-
'password': hash_password(password),
|
56 |
-
'role': 'Estudiante',
|
57 |
-
'additional_info': additional_info or {}
|
58 |
-
}
|
59 |
-
|
60 |
-
new_user['partitionKey'] = username
|
61 |
-
|
62 |
-
container.create_item(body=new_user)
|
63 |
-
return True
|
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 |
-
try:
|
70 |
-
query = f"SELECT * FROM c WHERE c.id = '{username}'"
|
71 |
-
results = list(container.query_items(query=query, partition_key=username))
|
72 |
-
|
73 |
-
if results:
|
74 |
-
stored_user = results[0]
|
75 |
-
if verify_password(stored_user['password'], password):
|
76 |
-
role = stored_user.get('role', 'Estudiante')
|
77 |
-
print(f"Usuario autenticado: {username}, Rol: {role}") # Log a帽adido
|
78 |
-
return True, role
|
79 |
-
print(f"Autenticaci贸n fallida para el usuario: {username}") # Log a帽adido
|
80 |
-
return False, None
|
81 |
-
except Exception as e:
|
82 |
-
print(f"Error durante la autenticaci贸n: {str(e)}") # Cambiado de logger.error a print
|
83 |
-
return False, None
|
84 |
-
|
85 |
-
########################################################################################################################
|
86 |
-
def verify_password(stored_password, provided_password):
|
87 |
-
"""Verify a stored password against one provided by user"""
|
88 |
-
return bcrypt.checkpw(provided_password.encode('utf-8'), stored_password.encode('utf-8'))
|
89 |
-
|
90 |
-
########################################################################################################################
|
91 |
-
#def get_user_role(username):
|
92 |
-
# """Get the role of a user."""
|
93 |
-
# return "Estudiante" # Siempre devuelve "Estudiante" ya que es el 煤nico perfil
|
94 |
-
|
95 |
-
########################################################################################################################
|
96 |
-
def update_user_info(username, new_info):
|
97 |
-
"""Update user information."""
|
98 |
-
try:
|
99 |
-
query = f"SELECT * FROM c WHERE c.id = '{username}'"
|
100 |
-
results = list(container.query_items(query=query, partition_key=username))
|
101 |
-
|
102 |
-
if results:
|
103 |
-
user = results[0]
|
104 |
-
user['additional_info'].update(new_info)
|
105 |
-
container.upsert_item(user, partition_key=username)
|
106 |
-
return True
|
107 |
-
except exceptions.CosmosHttpResponseError:
|
108 |
-
pass
|
109 |
-
|
110 |
-
return False
|
111 |
-
|
112 |
-
########################################################################################################################
|
113 |
-
def delete_user(username):
|
114 |
-
"""Delete a user."""
|
115 |
-
try:
|
116 |
-
query = f"SELECT * FROM c WHERE c.id = '{username}'"
|
117 |
-
results = list(container.query_items(query=query, partition_key=username))
|
118 |
-
|
119 |
-
if results:
|
120 |
-
user = results[0]
|
121 |
-
container.delete_item(item=user['id'], partition_key=username)
|
122 |
-
return True
|
123 |
-
except exceptions.CosmosHttpResponseError:
|
124 |
-
pass
|
125 |
-
|
126 |
-
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|