Spaces:
Sleeping
Sleeping
Caner Giden commited on
Commit ·
bafe296
1
Parent(s): ab3a8ab
Fix: Reduce API key length to 20 hex chars, add truncate to verify, fix logging
Browse filesFiles changed (2)
- app/core/security.py+9-1
- main.py+3-3
18 unmodified lines19202122232418 unmodified linesdef verify_api_key(plain_key: str, hashed_key: str) -> bool:"""Verify an API key against its hash."""return pwd_context.verify(plain_key, hashed_key)18 unmodified lines192021222324252627282930313218 unmodified linesdef verify_api_key(plain_key: str, hashed_key: str) -> bool:"""Verify an API key against its hash.Bcrypt has a 72 byte limit, so we truncate if necessary (same as hash)."""# Bcrypt limit is 72 bytes, truncate if longer (must match hash behavior)plain_key_bytes = plain_key.encode('utf-8')if len(plain_key_bytes) > 72:plain_key_bytes = plain_key_bytes[:72]plain_key = plain_key_bytes.decode('utf-8', errors='ignore')return pwd_context.verify(plain_key, hashed_key)
47 unmodified lines48495051525354555647 unmodified linesdb.add(admin_user)db.flush() # Get the user ID# Generate API key (16 chars hex = 32 hex chars + prefix = ~35 chars, well under 72 byte limit)# Using hex instead of token_urlsafe to ensure consistent byte lengthrandom_bytes = secrets.token_bytes(16) # 16 bytes = 32 hex charsapi_key_plain = f"ff_{random_bytes.hex()}"api_key_hash = hash_api_key(api_key_plain)47 unmodified lines48495051525354555647 unmodified linesdb.add(admin_user)db.flush() # Get the user ID# Generate API key (20 hex chars = 10 bytes + prefix = 23 chars, well under 72 byte limit)# Using hex to ensure consistent byte lengthrandom_bytes = secrets.token_bytes(10) # 10 bytes = 20 hex charsapi_key_plain = f"ff_{random_bytes.hex()}"api_key_hash = hash_api_key(api_key_plain)
app/core/security.py+9-1
main.py+3-3