Update main.py
Browse files
main.py
CHANGED
|
@@ -36,6 +36,27 @@ def now_utc() -> datetime:
|
|
| 36 |
def iso(dt: datetime) -> str:
|
| 37 |
return dt.astimezone(timezone.utc).isoformat()
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def create_access_token(sub: str, scopes=None, minutes=60) -> str:
|
| 40 |
now = now_utc()
|
| 41 |
payload = {"sub": sub, "scopes": scopes or [], "iat": int(now.timestamp()), "exp": int((now + timedelta(minutes=minutes)).timestamp())}
|
|
@@ -216,7 +237,7 @@ def check_otp(body: EmailOTP, payload=Depends(require_scopes(["verifyAccount"]))
|
|
| 216 |
rec = latest_valid_otp(user["id"], "verify")
|
| 217 |
if not rec: raise HTTPException(400, "OTP not found")
|
| 218 |
if rec.get("consumed_at"): raise HTTPException(400, "OTP already used")
|
| 219 |
-
if
|
| 220 |
raise HTTPException(400, "OTP expired")
|
| 221 |
if not pwd.verify(body.otp, rec["code_hash"]):
|
| 222 |
supa_update("user_otps", eq("id", rec["id"]), {"attempts": (rec.get("attempts") or 0) + 1})
|
|
@@ -248,7 +269,8 @@ def token_refresh(refresh_token: str = Form(...)):
|
|
| 248 |
for s in sessions:
|
| 249 |
try:
|
| 250 |
if s.get("revoked_at"): continue
|
| 251 |
-
if
|
|
|
|
| 252 |
if pwd.verify(refresh_token, s["refresh_token_hash"]):
|
| 253 |
matched = s; break
|
| 254 |
except Exception:
|
|
@@ -300,7 +322,7 @@ def check_forget_password(body: EmailOTP):
|
|
| 300 |
rec = latest_valid_otp(user["id"], "reset")
|
| 301 |
if not rec: raise HTTPException(400, "OTP not found")
|
| 302 |
if rec.get("consumed_at"): raise HTTPException(400, "OTP already used")
|
| 303 |
-
if
|
| 304 |
raise HTTPException(400, "OTP expired")
|
| 305 |
if not pwd.verify(body.otp, rec["code_hash"]):
|
| 306 |
supa_update("user_otps", eq("id", rec["id"]), {"attempts": (rec.get("attempts") or 0) + 1})
|
|
|
|
| 36 |
def iso(dt: datetime) -> str:
|
| 37 |
return dt.astimezone(timezone.utc).isoformat()
|
| 38 |
|
| 39 |
+
|
| 40 |
+
def parse_iso_utc(value: str) -> datetime:
|
| 41 |
+
"""
|
| 42 |
+
يحوّل أي نص ISO إلى datetime ب timezone=UTC.
|
| 43 |
+
لو السلسلة بدون timezone، نعتبرها UTC.
|
| 44 |
+
يقبل قيم زي: '2025-10-16T05:21:00', '...Z', '...+00:00'
|
| 45 |
+
"""
|
| 46 |
+
if not value:
|
| 47 |
+
return now_utc()
|
| 48 |
+
s = value.strip()
|
| 49 |
+
if s.endswith("Z"):
|
| 50 |
+
s = s[:-1] + "+00:00"
|
| 51 |
+
try:
|
| 52 |
+
dt = datetime.fromisoformat(s)
|
| 53 |
+
except Exception:
|
| 54 |
+
# لو فورمات مش متوقع: رجّع وقت قديم قوي علشان نعتبره منتهي
|
| 55 |
+
return now_utc() - timedelta(days=3650)
|
| 56 |
+
if dt.tzinfo is None:
|
| 57 |
+
dt = dt.replace(tzinfo=timezone.utc)
|
| 58 |
+
return dt.astimezone(timezone.utc)
|
| 59 |
+
|
| 60 |
def create_access_token(sub: str, scopes=None, minutes=60) -> str:
|
| 61 |
now = now_utc()
|
| 62 |
payload = {"sub": sub, "scopes": scopes or [], "iat": int(now.timestamp()), "exp": int((now + timedelta(minutes=minutes)).timestamp())}
|
|
|
|
| 237 |
rec = latest_valid_otp(user["id"], "verify")
|
| 238 |
if not rec: raise HTTPException(400, "OTP not found")
|
| 239 |
if rec.get("consumed_at"): raise HTTPException(400, "OTP already used")
|
| 240 |
+
if parse_iso_utc(rec["expires_at"]) < now_utc():
|
| 241 |
raise HTTPException(400, "OTP expired")
|
| 242 |
if not pwd.verify(body.otp, rec["code_hash"]):
|
| 243 |
supa_update("user_otps", eq("id", rec["id"]), {"attempts": (rec.get("attempts") or 0) + 1})
|
|
|
|
| 269 |
for s in sessions:
|
| 270 |
try:
|
| 271 |
if s.get("revoked_at"): continue
|
| 272 |
+
if parse_iso_utc(s["expires_at"]) < now_utc():
|
| 273 |
+
continue
|
| 274 |
if pwd.verify(refresh_token, s["refresh_token_hash"]):
|
| 275 |
matched = s; break
|
| 276 |
except Exception:
|
|
|
|
| 322 |
rec = latest_valid_otp(user["id"], "reset")
|
| 323 |
if not rec: raise HTTPException(400, "OTP not found")
|
| 324 |
if rec.get("consumed_at"): raise HTTPException(400, "OTP already used")
|
| 325 |
+
if parse_iso_utc(rec["expires_at"]) < now_utc():
|
| 326 |
raise HTTPException(400, "OTP expired")
|
| 327 |
if not pwd.verify(body.otp, rec["code_hash"]):
|
| 328 |
supa_update("user_otps", eq("id", rec["id"]), {"attempts": (rec.get("attempts") or 0) + 1})
|