Spaces:
Sleeping
Sleeping
Caner Giden commited on
Commit ·
ab3a8ab
1
Parent(s): bf0f173
Fix: Truncate API key to 72 bytes for bcrypt compatibility
Browse filesFiles changed (2)
- app/core/security.py+9-1
- main.py+4-2
5 unmodified lines67891011125 unmodified linesdef hash_api_key(api_key: str) -> str:"""Hash an API key using bcrypt."""return pwd_context.hash(api_key)5 unmodified lines678910111213141516171819205 unmodified linesdef hash_api_key(api_key: str) -> str:"""Hash an API key using bcrypt.Bcrypt has a 72 byte limit, so we truncate if necessary."""# Bcrypt limit is 72 bytes, truncate if longerapi_key_bytes = api_key.encode('utf-8')if len(api_key_bytes) > 72:api_key_bytes = api_key_bytes[:72]api_key = api_key_bytes.decode('utf-8', errors='ignore')return pwd_context.hash(api_key)
47 unmodified lines484950515253545547 unmodified linesdb.add(admin_user)db.flush() # Get the user ID# Generate API key (24 chars random + prefix = ~30 chars, well under 72 byte limit)api_key_plain = f"ff_{secrets.token_urlsafe(24)}"api_key_hash = hash_api_key(api_key_plain)# Create API key47 unmodified lines4849505152535455565747 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)# Create API key
app/core/security.py+9-1
main.py+4-2