CBV01 commited on
Commit
017c650
·
1 Parent(s): de60c2e

Fix slicing indexing error in auth.py

Browse files
Files changed (1) hide show
  1. backend/auth.py +18 -10
backend/auth.py CHANGED
@@ -24,15 +24,23 @@ def hash_password(password: str) -> str:
24
 
25
  def verify_password(plain_password: str, stored_password: str) -> bool:
26
  """Verify a stored password against one provided by user."""
27
- salt = stored_password[:64].encode('ascii')
28
- stored_hash = stored_password[64:].encode('ascii')
29
- pwdhash = hashlib.pbkdf2_hmac('sha512',
30
- plain_password.encode('utf-8'),
31
- salt, 100000)
32
- pwdhash = binascii.hexlify(pwdhash)
33
- return pwdhash == stored_hash
 
 
 
 
 
 
 
 
34
 
35
- def get_password_hash(password):
36
  return hash_password(password)
37
 
38
  def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
@@ -53,10 +61,10 @@ async def get_current_user_id(token: str = Depends(oauth2_scheme)):
53
  )
54
  try:
55
  payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
56
- user_id: str = payload.get("sub")
57
  if user_id is None:
58
  raise credentials_exception
59
- return user_id
60
  except JWTError as e:
61
  print(f"DEBUG_AUTH: JWTError - {str(e)}")
62
  raise credentials_exception
 
24
 
25
  def verify_password(plain_password: str, stored_password: str) -> bool:
26
  """Verify a stored password against one provided by user."""
27
+ pwd_str = str(stored_password)
28
+ if len(pwd_str) < 66: # Minimal length for salt + hash
29
+ return False
30
+ try:
31
+ salt_str = pwd_str[0:64]
32
+ hash_str = pwd_str[64:]
33
+ salt_bytes = salt_str.encode('ascii')
34
+ stored_hash_bytes = hash_str.encode('ascii')
35
+ pwdhash = hashlib.pbkdf2_hmac('sha512',
36
+ plain_password.encode('utf-8'),
37
+ salt_bytes, 100000)
38
+ pwdhash = binascii.hexlify(pwdhash)
39
+ return pwdhash == stored_hash_bytes
40
+ except Exception:
41
+ return False
42
 
43
+ def get_password_hash(password: str) -> str:
44
  return hash_password(password)
45
 
46
  def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
 
61
  )
62
  try:
63
  payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
64
+ user_id = payload.get("sub")
65
  if user_id is None:
66
  raise credentials_exception
67
+ return str(user_id)
68
  except JWTError as e:
69
  print(f"DEBUG_AUTH: JWTError - {str(e)}")
70
  raise credentials_exception