Emalawi19 commited on
Commit
aeeb938
Β·
verified Β·
1 Parent(s): d640ea7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os, io, json, subprocess, tempfile, secrets, shutil
 
2
  from datetime import datetime, timedelta
3
  from typing import Optional
4
  from pathlib import Path
@@ -7,7 +8,6 @@ from fastapi import FastAPI, UploadFile, File, HTTPException, Depends, Form, Req
7
  from fastapi.middleware.cors import CORSMiddleware
8
  from fastapi.responses import JSONResponse, HTMLResponse, PlainTextResponse, Response
9
  from pydantic import BaseModel
10
- from passlib.context import CryptContext
11
  from jose import JWTError, jwt
12
 
13
  # ── Config ────────────────────────────────────────────────────────────────────
@@ -40,21 +40,20 @@ def save_db(db: dict):
40
  DB_FILE.write_text(json.dumps(db, indent=2))
41
 
42
  # ── Security ──────────────────────────────────────────────────────────────────
43
- pwd_ctx = CryptContext(
44
- schemes=["bcrypt"],
45
- deprecated="auto",
46
- bcrypt__rounds=10
47
- )
48
-
49
  def hash_password(p: str) -> str:
50
  try:
51
- return pwd_ctx.hash(p[:72])
 
 
 
52
  except Exception as e:
53
  raise HTTPException(500, f"Password hashing error: {e}")
54
 
55
  def verify_password(plain: str, hashed: str) -> bool:
56
  try:
57
- return pwd_ctx.verify(plain[:72], hashed)
 
 
58
  except:
59
  return False
60
 
 
1
  import os, io, json, subprocess, tempfile, secrets, shutil
2
+ import bcrypt
3
  from datetime import datetime, timedelta
4
  from typing import Optional
5
  from pathlib import Path
 
8
  from fastapi.middleware.cors import CORSMiddleware
9
  from fastapi.responses import JSONResponse, HTMLResponse, PlainTextResponse, Response
10
  from pydantic import BaseModel
 
11
  from jose import JWTError, jwt
12
 
13
  # ── Config ────────────────────────────────────────────────────────────────────
 
40
  DB_FILE.write_text(json.dumps(db, indent=2))
41
 
42
  # ── Security ──────────────────────────────────────────────────────────────────
 
 
 
 
 
 
43
  def hash_password(p: str) -> str:
44
  try:
45
+ # Encode to bytes and safely truncate to 72 bytes (bcrypt's limit)
46
+ pwd_bytes = p.encode('utf-8')[:72]
47
+ salt = bcrypt.gensalt(rounds=10)
48
+ return bcrypt.hashpw(pwd_bytes, salt).decode('utf-8')
49
  except Exception as e:
50
  raise HTTPException(500, f"Password hashing error: {e}")
51
 
52
  def verify_password(plain: str, hashed: str) -> bool:
53
  try:
54
+ pwd_bytes = plain.encode('utf-8')[:72]
55
+ hashed_bytes = hashed.encode('utf-8')
56
+ return bcrypt.checkpw(pwd_bytes, hashed_bytes)
57
  except:
58
  return False
59