diff --git a/api/main.py b/api/main.py index 148b219..d880841 100644 --- a/api/main.py +++ b/api/main.py @@ -22,6 +22,7 @@ DB_USER = "verimund_user" DB_PASS = os.getenv("DB_PASS", "Shlevison2k17") JWT_SECRET = os.getenv("JWT_SECRET", "") HMAC_SECRET = os.getenv("HMAC_SECRET", "") +ADMIN_KEY = os.getenv("ADMIN_KEY", "") JWT_ALGORITHM = "HS256" JWT_EXPIRE_HOURS = 24 @@ -54,6 +55,11 @@ def verify_jwt(authorization: Optional[str] = Header(None)): raise HTTPException(status_code=401, detail="Invalid token") +def verify_admin(authorization: Optional[str] = Header(None)): + if not authorization or authorization != f"Bearer {ADMIN_KEY}": + raise HTTPException(status_code=401, detail="Unauthorized") + + class AuthRequest(BaseModel): license_key: str hwid: str @@ -61,6 +67,12 @@ class AuthRequest(BaseModel): signature: str +class RegisterVersionRequest(BaseModel): + version: str + channel: str + release_notes: str = "" + + @app.post("/auth") def authenticate(req: AuthRequest, db=Depends(get_db)): try: @@ -127,18 +139,37 @@ def get_sector_stats(payload=Depends(verify_jwt), db=Depends(get_db)): def check_update(payload=Depends(verify_jwt), db=Depends(get_db)): cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) channel = payload.get("channel", "stable") - cur.execute("SELECT * FROM app_versions WHERE channel = %s ORDER BY created_at DESC LIMIT 1", (channel,)) + cur.execute("SELECT * FROM app_versions WHERE channel = %s ORDER BY released_at DESC LIMIT 1", (channel,)) version = cur.fetchone() if not version: raise HTTPException(status_code=404, detail="No version found") return {"version": version["version"], "channel": channel, "release_notes": version.get("release_notes", "")} +@app.get("/admin/versions") +def get_versions(_=Depends(verify_admin), db=Depends(get_db)): + cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) + cur.execute("SELECT * FROM app_versions ORDER BY released_at DESC LIMIT 1") + version = cur.fetchone() + return {"version": version["version"] if version else "1.0.0"} + + +@app.post("/admin/register-version") +def register_version(req: RegisterVersionRequest, _=Depends(verify_admin), db=Depends(get_db)): + cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) + cur.execute( + "INSERT INTO app_versions (version, channel, release_notes, released_at) VALUES (%s, %s, %s, %s)", + (req.version, req.channel, req.release_notes, datetime.now(timezone.utc)) + ) + db.commit() + return {"status": "ok", "version": req.version, "channel": req.channel} + + @app.post("/download") def download_exe(payload=Depends(verify_jwt), db=Depends(get_db)): cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) channel = payload.get("channel", "stable") - cur.execute("SELECT * FROM app_versions WHERE channel = %s ORDER BY created_at DESC LIMIT 1", (channel,)) + cur.execute("SELECT * FROM app_versions WHERE channel = %s ORDER BY released_at DESC LIMIT 1", (channel,)) version = cur.fetchone() if not version: raise HTTPException(status_code=404, detail="No version found")