Update main.py
Browse files
main.py
CHANGED
|
@@ -161,6 +161,48 @@ def rotate_refresh(user_id: int, device: Optional[str], ua: Optional[str]):
|
|
| 161 |
})
|
| 162 |
return raw, rec
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
# ====== Schemas ======
|
| 165 |
class LoginIn(BaseModel):
|
| 166 |
email: EmailStr
|
|
@@ -262,6 +304,46 @@ def login(body: LoginIn):
|
|
| 262 |
refresh, _ = rotate_refresh(user["id"], device="mobile", ua=None)
|
| 263 |
return {"access_token": access, "refresh_token": refresh, "user": user}
|
| 264 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
@app.post("/api/auth/token/refresh")
|
| 266 |
def token_refresh(refresh_token: str = Form(...)):
|
| 267 |
sessions = supa_select("user_sessions", {"select":"id,user_id,refresh_token_hash,expires_at,revoked_at", "order":"created_at.desc"})
|
|
|
|
| 161 |
})
|
| 162 |
return raw, rec
|
| 163 |
|
| 164 |
+
# ====== Permanent Token helpers (testing-only) ======
|
| 165 |
+
def create_permanent_token_for_email(email: str, scopes=None, note: Optional[str] = None):
|
| 166 |
+
"""
|
| 167 |
+
ينشئ JWT بدون exp، ويخزّن هاشه في user_permanent_tokens.
|
| 168 |
+
يرجّع (token, rec) حيث rec هو الريكورد المُدخل.
|
| 169 |
+
"""
|
| 170 |
+
scopes = scopes or ["user"]
|
| 171 |
+
# هات المستخدم
|
| 172 |
+
rows = supa_select("users", {**eq("email", email), "select": "id", "limit": "1"})
|
| 173 |
+
if not rows:
|
| 174 |
+
raise HTTPException(404, "User not found")
|
| 175 |
+
uid = rows[0]["id"]
|
| 176 |
+
|
| 177 |
+
# أنشئ توكن بدون exp
|
| 178 |
+
now = now_utc()
|
| 179 |
+
payload = {
|
| 180 |
+
"sub": email,
|
| 181 |
+
"scopes": scopes,
|
| 182 |
+
"iat": int(now.timestamp()),
|
| 183 |
+
# بدون exp
|
| 184 |
+
}
|
| 185 |
+
token = jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALG)
|
| 186 |
+
|
| 187 |
+
# خزّن الهاش فقط
|
| 188 |
+
rec = supa_insert("user_permanent_tokens", {
|
| 189 |
+
"user_id": uid,
|
| 190 |
+
"token_hash": pwd.hash(token),
|
| 191 |
+
"scopes": scopes,
|
| 192 |
+
"note": note,
|
| 193 |
+
"issued_by": "api",
|
| 194 |
+
"created_at": iso(now),
|
| 195 |
+
})
|
| 196 |
+
return token, rec
|
| 197 |
+
|
| 198 |
+
def revoke_permanent_token_by_id(token_id: int):
|
| 199 |
+
"""
|
| 200 |
+
يعلّم التوكن كـ revoked.
|
| 201 |
+
"""
|
| 202 |
+
return supa_update("user_permanent_tokens", eq("id", token_id), {
|
| 203 |
+
"revoked_at": iso(now_utc())
|
| 204 |
+
})
|
| 205 |
+
|
| 206 |
# ====== Schemas ======
|
| 207 |
class LoginIn(BaseModel):
|
| 208 |
email: EmailStr
|
|
|
|
| 304 |
refresh, _ = rotate_refresh(user["id"], device="mobile", ua=None)
|
| 305 |
return {"access_token": access, "refresh_token": refresh, "user": user}
|
| 306 |
|
| 307 |
+
@app.post("/api/auth/never_expire")
|
| 308 |
+
def never_expire(payload=Depends(require_scopes(["user"])), note: Optional[str] = Form(None)):
|
| 309 |
+
"""
|
| 310 |
+
ينشئ access token دائم (بدون exp) ويُخزّن هاشه في user_permanent_tokens.
|
| 311 |
+
⚠️ للتجارب فقط.
|
| 312 |
+
"""
|
| 313 |
+
token, rec = create_permanent_token_for_email(payload["sub"], scopes=["user"], note=note)
|
| 314 |
+
return {
|
| 315 |
+
"access_token": token,
|
| 316 |
+
"token_type": "bearer",
|
| 317 |
+
"expires": "never",
|
| 318 |
+
"id": rec["id"],
|
| 319 |
+
"issued_at": rec.get("created_at"),
|
| 320 |
+
"note": rec.get("note"),
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
@app.post("/api/auth/permanent/revoke")
|
| 324 |
+
def permanent_revoke(token_id: int = Form(...), payload=Depends(require_scopes(["user"]))):
|
| 325 |
+
"""
|
| 326 |
+
إلغاء (revocation) توكن دائم محدّد بـ id.
|
| 327 |
+
"""
|
| 328 |
+
# (اختياري) تتأكد إن الـ token_id يخص نفس المستخدم:
|
| 329 |
+
row = supa_select("user_permanent_tokens", {**eq("id", token_id), "select": "id,user_id,revoked_at", "limit": "1"})
|
| 330 |
+
if not row:
|
| 331 |
+
raise HTTPException(404, "Token not found")
|
| 332 |
+
|
| 333 |
+
# جيب user_id الخاص بصاحب الطلب
|
| 334 |
+
me = supa_select("users", {**eq("email", payload["sub"]), "select": "id", "limit": "1"})
|
| 335 |
+
if not me:
|
| 336 |
+
raise HTTPException(404, "User not found")
|
| 337 |
+
|
| 338 |
+
if row[0]["user_id"] != me[0]["id"]:
|
| 339 |
+
raise HTTPException(403, "Not your token")
|
| 340 |
+
|
| 341 |
+
if row[0].get("revoked_at"):
|
| 342 |
+
return {"message": "Already revoked"}
|
| 343 |
+
|
| 344 |
+
revoke_permanent_token_by_id(token_id)
|
| 345 |
+
return {"message": "Revoked"}
|
| 346 |
+
|
| 347 |
@app.post("/api/auth/token/refresh")
|
| 348 |
def token_refresh(refresh_token: str = Form(...)):
|
| 349 |
sessions = supa_select("user_sessions", {"select":"id,user_id,refresh_token_hash,expires_at,revoked_at", "order":"created_at.desc"})
|