MetiMiester commited on
Commit
f4cb7b4
·
verified ·
1 Parent(s): 848eb2c

Update src/services/auth_service.py

Browse files
Files changed (1) hide show
  1. src/services/auth_service.py +4 -21
src/services/auth_service.py CHANGED
@@ -1,9 +1,6 @@
1
- import sqlite3
2
  from passlib.context import CryptContext
3
-
4
  from src.db.connection import get_conn
5
 
6
- # Password hashing context (bcrypt)
7
  PWD_CTX = CryptContext(
8
  schemes=["bcrypt"],
9
  deprecated="auto",
@@ -11,13 +8,6 @@ PWD_CTX = CryptContext(
11
 
12
 
13
  def authenticate_user(db_path: str, username: str, password: str):
14
- """
15
- Authenticate a user against the database.
16
-
17
- Returns:
18
- dict {username, role} on success
19
- None on failure
20
- """
21
  if not username or not password:
22
  return None
23
 
@@ -27,15 +17,12 @@ def authenticate_user(db_path: str, username: str, password: str):
27
  """
28
  SELECT username, role, password_hash, is_active
29
  FROM users
30
- WHERE username = ?
31
  """,
32
  (username,),
33
  ).fetchone()
34
 
35
- if not row:
36
- return None
37
-
38
- if not row["is_active"]:
39
  return None
40
 
41
  if not PWD_CTX.verify(password, row["password_hash"]):
@@ -45,15 +32,11 @@ def authenticate_user(db_path: str, username: str, password: str):
45
  "username": row["username"],
46
  "role": row["role"],
47
  }
48
-
49
  finally:
50
  conn.close()
 
 
51
  def is_admin(auth_state) -> bool:
52
- """
53
- Returns True if the current user is an admin.
54
- Expects auth_state to be a dict like:
55
- { "username": "...", "role": "admin" }
56
- """
57
  if not auth_state:
58
  return False
59
  return auth_state.get("role") == "admin"
 
 
1
  from passlib.context import CryptContext
 
2
  from src.db.connection import get_conn
3
 
 
4
  PWD_CTX = CryptContext(
5
  schemes=["bcrypt"],
6
  deprecated="auto",
 
8
 
9
 
10
  def authenticate_user(db_path: str, username: str, password: str):
 
 
 
 
 
 
 
11
  if not username or not password:
12
  return None
13
 
 
17
  """
18
  SELECT username, role, password_hash, is_active
19
  FROM users
20
+ WHERE username=?
21
  """,
22
  (username,),
23
  ).fetchone()
24
 
25
+ if not row or not row["is_active"]:
 
 
 
26
  return None
27
 
28
  if not PWD_CTX.verify(password, row["password_hash"]):
 
32
  "username": row["username"],
33
  "role": row["role"],
34
  }
 
35
  finally:
36
  conn.close()
37
+
38
+
39
  def is_admin(auth_state) -> bool:
 
 
 
 
 
40
  if not auth_state:
41
  return False
42
  return auth_state.get("role") == "admin"